diff --git a/spec/styles-element-spec.coffee b/spec/styles-element-spec.coffee index ba98633e5..2182594f0 100644 --- a/spec/styles-element-spec.coffee +++ b/spec/styles-element-spec.coffee @@ -94,3 +94,14 @@ describe "StylesElement", -> expect(element.firstChild.sheet.cssRules[0].selectorText).toBe ':host' expect(element.firstChild.sheet.cssRules[1].selectorText).toBe ':host(.mini)' expect(element.firstChild.sheet.cssRules[2].selectorText).toBe ':host(:focus)' + + it "defers selector upgrade until the element is attached", -> + element = new StylesElement + element.setAttribute('context', 'atom-text-editor') + element.initialize() + + atom.styles.addStyleSheet ".editor {background: black;}", context: 'atom-text-editor' + expect(element.firstChild.sheet).toBeNull() + + document.querySelector('#jasmine-content').appendChild(element) + expect(element.firstChild.sheet.cssRules[0].selectorText).toBe ':host' diff --git a/src/styles-element.coffee b/src/styles-element.coffee index 5bb856209..b011113fc 100644 --- a/src/styles-element.coffee +++ b/src/styles-element.coffee @@ -18,6 +18,9 @@ class StylesElement extends HTMLElement @styleElementClonesByOriginalElement = new WeakMap attachedCallback: -> + if @context is 'atom-text-editor' + for styleElement in @children + @upgradeDeprecatedSelectors(styleElement) @initialize() detachedCallback: -> @@ -48,6 +51,7 @@ class StylesElement extends HTMLElement return unless @styleElementMatchesContext(styleElement) styleElementClone = styleElement.cloneNode(true) + styleElementClone.sourcePath = styleElement.sourcePath styleElementClone.context = styleElement.context @styleElementClonesByOriginalElement.set(styleElement, styleElementClone) @@ -61,7 +65,7 @@ class StylesElement extends HTMLElement @insertBefore(styleElementClone, insertBefore) if @context is 'atom-text-editor' - @upgradeDeprecatedSelectors(styleElementClone, styleElement.sourcePath) + @upgradeDeprecatedSelectors(styleElementClone) @emitter.emit 'did-add-style-element', styleElementClone @@ -82,7 +86,9 @@ class StylesElement extends HTMLElement styleElementMatchesContext: (styleElement) -> not @context? or styleElement.context is @context - upgradeDeprecatedSelectors: (styleElement, sourcePath) -> + upgradeDeprecatedSelectors: (styleElement) -> + return unless styleElement.sheet? + upgradedSelectors = [] for rule in styleElement.sheet.cssRules @@ -99,7 +105,7 @@ class StylesElement extends HTMLElement upgradedSelectors.push({inputSelector, outputSelector}) if upgradedSelectors.length > 0 - warning = "Upgraded the following syntax theme selectors in `#{sourcePath}` for shadow DOM compatibility:\n\n" + warning = "Upgraded the following syntax theme selectors in `#{styleElement.sourcePath}` for shadow DOM compatibility:\n\n" for {inputSelector, outputSelector} in upgradedSelectors warning += "`#{inputSelector}` => `#{outputSelector}`\n"