From 2532a9cd591450ffd32df3c0239c4bb3abf24d58 Mon Sep 17 00:00:00 2001 From: Antonio Scandurra Date: Tue, 6 Oct 2015 14:17:47 +0200 Subject: [PATCH] Don't use atom globals in StylesElement --- spec/jasmine-test-runner.coffee | 6 +++++- spec/styles-element-spec.coffee | 3 ++- src/atom-environment.coffee | 5 ++++- src/styles-element.coffee | 21 ++++++++++----------- src/text-editor-element.coffee | 9 ++++++--- 5 files changed, 27 insertions(+), 17 deletions(-) diff --git a/spec/jasmine-test-runner.coffee b/spec/jasmine-test-runner.coffee index 35f08e2ac..7aa264b29 100644 --- a/spec/jasmine-test-runner.coffee +++ b/spec/jasmine-test-runner.coffee @@ -25,7 +25,11 @@ module.exports = ({logFile, headless, testPaths, buildAtomEnvironment}) -> jasmineContent = document.createElement('div') jasmineContent.setAttribute('id', 'jasmine-content') - document.head.appendChild(new StylesElement) + + stylesElement = new StylesElement + stylesElement.initialize(atom) + + document.head.appendChild(stylesElement) document.body.appendChild(jasmineContent) jasmineEnv.execute() diff --git a/spec/styles-element-spec.coffee b/spec/styles-element-spec.coffee index 7d2fe722a..ae2648644 100644 --- a/spec/styles-element-spec.coffee +++ b/spec/styles-element-spec.coffee @@ -6,6 +6,7 @@ describe "StylesElement", -> beforeEach -> element = new StylesElement + element.initialize(atom) document.querySelector('#jasmine-content').appendChild(element) addedStyleElements = [] removedStyleElements = [] @@ -99,8 +100,8 @@ describe "StylesElement", -> it "defers selector upgrade until the element is attached", -> element = new StylesElement + element.initialize(atom) element.setAttribute('context', 'atom-text-editor') - element.initialize() atom.styles.addStyleSheet ".editor {background: black;}", context: 'atom-text-editor' expect(element.firstChild.sheet).toBeNull() diff --git a/src/atom-environment.coffee b/src/atom-environment.coffee index 48145b8ce..ca059d2f8 100644 --- a/src/atom-environment.coffee +++ b/src/atom-environment.coffee @@ -547,7 +547,10 @@ class AtomEnvironment extends Model @config.load() @setBodyPlatformClass() - document.head.appendChild(new StylesElement) + + stylesElement = new StylesElement + stylesElement.initialize(this) + document.head.appendChild(stylesElement) @windowEventHandler = new WindowEventHandler(this) @packages.loadPackages() diff --git a/src/styles-element.coffee b/src/styles-element.coffee index fc3b888cf..e5ab9884e 100644 --- a/src/styles-element.coffee +++ b/src/styles-element.coffee @@ -14,6 +14,7 @@ class StylesElement extends HTMLElement @emitter.on 'did-update-style-element', callback createdCallback: -> + @subscriptions = new CompositeDisposable @emitter = new Emitter @styleElementClonesByOriginalElement = new WeakMap @@ -21,31 +22,29 @@ class StylesElement extends HTMLElement if @context is 'atom-text-editor' for styleElement in @children @upgradeDeprecatedSelectors(styleElement) - @initialize() + + @context = @getAttribute('context') ? undefined detachedCallback: -> @subscriptions.dispose() - @subscriptions = null + @subscriptions = new CompositeDisposable attributeChangedCallback: (attrName, oldVal, newVal) -> @contextChanged() if attrName is 'context' - initialize: -> - return if @subscriptions? + initialize: ({@styles}) -> + throw new Error("Must pass a styles parameter when initializing TextEditorElements") unless @styles? - @subscriptions = new CompositeDisposable - @context = @getAttribute('context') ? undefined - - @subscriptions.add atom.styles.observeStyleElements(@styleElementAdded.bind(this)) - @subscriptions.add atom.styles.onDidRemoveStyleElement(@styleElementRemoved.bind(this)) - @subscriptions.add atom.styles.onDidUpdateStyleElement(@styleElementUpdated.bind(this)) + @subscriptions.add @styles.observeStyleElements(@styleElementAdded.bind(this)) + @subscriptions.add @styles.onDidRemoveStyleElement(@styleElementRemoved.bind(this)) + @subscriptions.add @styles.onDidUpdateStyleElement(@styleElementUpdated.bind(this)) contextChanged: -> return unless @subscriptions? @styleElementRemoved(child) for child in Array::slice.call(@children) @context = @getAttribute('context') - @styleElementAdded(styleElement) for styleElement in atom.styles.getStyleElements() + @styleElementAdded(styleElement) for styleElement in @styles.getStyleElements() return styleElementAdded: (styleElement) -> diff --git a/src/text-editor-element.coffee b/src/text-editor-element.coffee index 68d84a560..71219fe6a 100644 --- a/src/text-editor-element.coffee +++ b/src/text-editor-element.coffee @@ -4,6 +4,7 @@ Path = require 'path' TextBuffer = require 'text-buffer' TextEditor = require './text-editor' TextEditorComponent = require './text-editor-component' +StylesElement = require './styles-element' ShadowStyleSheet = null @@ -25,6 +26,7 @@ class TextEditorElement extends HTMLElement @workspace = atom.workspace @assert = atom.assert @views = atom.views + @styles = atom.styles @emitter = new Emitter @subscriptions = new CompositeDisposable @@ -46,9 +48,9 @@ class TextEditorElement extends HTMLElement @createShadowRoot() @shadowRoot.appendChild(ShadowStyleSheet.cloneNode(true)) - @stylesElement = document.createElement('atom-styles') + @stylesElement = new StylesElement + @stylesElement.initialize({@styles}) @stylesElement.setAttribute('context', 'atom-text-editor') - @stylesElement.initialize() @rootElement = document.createElement('div') @rootElement.classList.add('editor--private') @@ -84,12 +86,13 @@ class TextEditorElement extends HTMLElement @subscriptions.add @component.onDidChangeScrollLeft => @emitter.emit("did-change-scroll-left", arguments...) - initialize: (model, {@views, @config, @themes, @workspace, @assert}) -> + initialize: (model, {@views, @config, @themes, @workspace, @assert, @styles}) -> throw new Error("Must pass a config parameter when initializing TextEditorElements") unless @views? throw new Error("Must pass a config parameter when initializing TextEditorElements") unless @config? throw new Error("Must pass a themes parameter when initializing TextEditorElements") unless @themes? throw new Error("Must pass a workspace parameter when initializing TextEditorElements") unless @workspace? throw new Error("Must pass a assert parameter when initializing TextEditorElements") unless @assert? + throw new Error("Must pass a styles parameter when initializing TextEditorElements") unless @styles? @setModel(model) this