Handle style element updates in StylesElement

This commit is contained in:
Nathan Sobo 2014-10-14 12:51:58 -06:00
parent be51ccf786
commit a8b9e1b790
2 changed files with 22 additions and 1 deletions

View File

@ -2,15 +2,17 @@ StylesElement = require '../src/styles-element'
StyleManager = require '../src/style-manager'
describe "StylesElement", ->
[element, addedStyleElements, removedStyleElements] = []
[element, addedStyleElements, removedStyleElements, updatedStyleElements] = []
beforeEach ->
element = new StylesElement
document.querySelector('#jasmine-content').appendChild(element)
addedStyleElements = []
removedStyleElements = []
updatedStyleElements = []
element.onDidAddStyleElement (element) -> addedStyleElements.push(element)
element.onDidRemoveStyleElement (element) -> removedStyleElements.push(element)
element.onDidUpdateStyleElement (element) -> updatedStyleElements.push(element)
it "renders a style tag for all currently active stylesheets in the style manager", ->
initialChildCount = element.children.length
@ -40,3 +42,13 @@ describe "StylesElement", ->
expect(element.children[initialChildCount].textContent).toBe "a {color: red}"
expect(element.children[initialChildCount + 1].textContent).toBe "a {color: green}"
expect(element.children[initialChildCount + 2].textContent).toBe "a {color: blue}"
it "updates existing style nodes when style elements are updated", ->
initialChildCount = element.children.length
atom.styles.addStyleSheet("a {color: red;}", sourcePath: '/foo/bar')
atom.styles.addStyleSheet("a {color: blue;}", sourcePath: '/foo/bar')
expect(element.children.length).toBe initialChildCount + 1
expect(element.children[initialChildCount].textContent).toBe "a {color: blue;}"
expect(updatedStyleElements).toEqual [element.children[initialChildCount]]

View File

@ -10,11 +10,15 @@ class StylesElement extends HTMLElement
onDidRemoveStyleElement: (callback) ->
@emitter.on 'did-remove-style-element', callback
onDidUpdateStyleElement: (callback) ->
@emitter.on 'did-update-style-element', callback
attachedCallback: ->
@subscriptions = new CompositeDisposable
@styleElementClonesByOriginalElement = new WeakMap
@subscriptions.add atom.styles.observeStyleElements(@styleElementAdded.bind(this))
@subscriptions.add atom.styles.onDidRemoveStyleElement(@styleElementRemoved.bind(this))
@subscriptions.add atom.styles.onDidUpdateStyleElement(@styleElementUpdated.bind(this))
styleElementAdded: (styleElement) ->
styleElementClone = styleElement.cloneNode(true)
@ -35,6 +39,11 @@ class StylesElement extends HTMLElement
styleElementClone.remove()
@emitter.emit 'did-remove-style-element', styleElementClone
styleElementUpdated: (styleElement) ->
styleElementClone = @styleElementClonesByOriginalElement.get(styleElement)
styleElementClone.textContent = styleElement.textContent
@emitter.emit 'did-update-style-element', styleElementClone
detachedCallback: ->
@subscriptions.dispose()