From 1c97dcd195a3eeb1f2f741eee1c118ad5338aec0 Mon Sep 17 00:00:00 2001 From: Nathan Sobo Date: Mon, 13 Oct 2014 12:09:33 -0600 Subject: [PATCH] Add sourcePath parameter to StyleManager::addStyleSheet --- spec/style-manager-spec.coffee | 31 ++++++++++++++++++++++++++++++- src/style-manager.coffee | 27 ++++++++++++++++++++++----- 2 files changed, 52 insertions(+), 6 deletions(-) diff --git a/spec/style-manager-spec.coffee b/spec/style-manager-spec.coffee index 496e70419..d7f0b0e97 100644 --- a/spec/style-manager-spec.coffee +++ b/spec/style-manager-spec.coffee @@ -7,12 +7,18 @@ describe "StyleManager", -> manager = new StyleManager describe "::addStyleSheet(source, params)", -> - it "adds a stylesheet based on the given source and returns a disposable allowing it to be removed", -> + [addEvents, removeEvents, updateEvents] = [] + + beforeEach -> addEvents = [] removeEvents = [] + updateEvents = [] + manager.onDidAddStyleSheet (event) -> addEvents.push(event) manager.onDidRemoveStyleSheet (event) -> removeEvents.push(event) + manager.onDidUpdateStyleSheet (event) -> updateEvents.push(event) + it "adds a stylesheet based on the given source and returns a disposable allowing it to be removed", -> disposable = manager.addStyleSheet("a {color: red;}") expect(addEvents.length).toBe 1 @@ -27,3 +33,26 @@ describe "StyleManager", -> expect(removeEvents.length).toBe 1 expect(removeEvents[0].styleElement.textContent).toBe "a {color: red;}" expect(manager.getStyleElements().length).toBe 0 + + describe "when a sourcePath parameter is specified", -> + it "ensures a maximum of one style element for the given source path, updating a previous if it exists", -> + disposable1 = manager.addStyleSheet("a {color: red;}", sourcePath: '/foo/bar') + + expect(addEvents.length).toBe 1 + expect(addEvents[0].sourcePath).toBe '/foo/bar' + + disposable2 = manager.addStyleSheet("a {color: blue;}", sourcePath: '/foo/bar') + + expect(addEvents.length).toBe 1 + expect(updateEvents.length).toBe 1 + expect(updateEvents[0].sourcePath).toBe '/foo/bar' + expect(updateEvents[0].styleElement.textContent).toBe "a {color: blue;}" + + disposable2.dispose() + addEvents = [] + + manager.addStyleSheet("a {color: yellow;}", sourcePath: '/foo/bar') + + expect(addEvents.length).toBe 1 + expect(addEvents[0].sourcePath).toBe '/foo/bar' + expect(addEvents[0].styleElement.textContent).toBe "a {color: yellow;}" diff --git a/src/style-manager.coffee b/src/style-manager.coffee index 6d67bfce0..cd4604f8e 100644 --- a/src/style-manager.coffee +++ b/src/style-manager.coffee @@ -5,6 +5,7 @@ class StyleManager constructor: -> @emitter = new Emitter @styleElements = [] + @styleElementsBySourcePath = {} onDidAddStyleSheet: (callback) -> @emitter.on 'did-add-style-sheet', callback @@ -12,19 +13,35 @@ class StyleManager onDidRemoveStyleSheet: (callback) -> @emitter.on 'did-remove-style-sheet', callback + onDidUpdateStyleSheet: (callback) -> + @emitter.on 'did-update-style-sheet', callback + getStyleElements: -> @styleElements.slice() - addStyleSheet: (source) -> - styleElement = document.createElement('style') + addStyleSheet: (source, params) -> + sourcePath = params?.sourcePath + if sourcePath? and styleElement = @styleElementsBySourcePath[sourcePath] + updated = true + else + styleElement = document.createElement('style') + styleElement.textContent = source + @styleElements.push(styleElement) - @emitter.emit 'did-add-style-sheet', {styleElement} + @styleElementsBySourcePath[sourcePath] ?= styleElement if sourcePath? - new Disposable => @removeStyleElement(styleElement) + if updated + @emitter.emit 'did-update-style-sheet', {styleElement, sourcePath} + else + @emitter.emit 'did-add-style-sheet', {styleElement, sourcePath} - removeStyleElement: (styleElement) -> + new Disposable => @removeStyleElement(styleElement, params) + + removeStyleElement: (styleElement, params) -> index = @styleElements.indexOf(styleElement) unless index is -1 @styleElements.splice(index, 1) + sourcePath = params?.sourcePath + delete @styleElementsBySourcePath[sourcePath] if sourcePath? @emitter.emit 'did-remove-style-sheet', {styleElement}