From 4c74b07b22d4b3e88e6b4ed5c5da8b9b8fa58094 Mon Sep 17 00:00:00 2001 From: Nathan Sobo Date: Tue, 6 Jan 2015 19:14:54 -0700 Subject: [PATCH 1/5] Order style elements by priority --- spec/styles-element-spec.coffee | 13 +++++++++++++ src/style-manager.coffee | 5 +++++ src/styles-element.coffee | 8 ++++++++ 3 files changed, 26 insertions(+) diff --git a/spec/styles-element-spec.coffee b/spec/styles-element-spec.coffee index 9224c2883..282cc616a 100644 --- a/spec/styles-element-spec.coffee +++ b/spec/styles-element-spec.coffee @@ -32,6 +32,19 @@ describe "StylesElement", -> expect(element.children[initialChildCount].textContent).toBe "a {color: blue;}" expect(removedStyleElements).toEqual [addedStyleElements[0]] + it "orders style elements by priority", -> + initialChildCount = element.children.length + + atom.styles.addStyleSheet("a {color: red}", priority: 1) + atom.styles.addStyleSheet("a {color: blue}", priority: 0) + atom.styles.addStyleSheet("a {color: green}", priority: 2) + atom.styles.addStyleSheet("a {color: yellow}", priority: 1) + + expect(element.children[initialChildCount].textContent).toBe "a {color: blue}" + expect(element.children[initialChildCount + 1].textContent).toBe "a {color: red}" + expect(element.children[initialChildCount + 2].textContent).toBe "a {color: yellow}" + expect(element.children[initialChildCount + 3].textContent).toBe "a {color: green}" + it "orders style elements by group", -> initialChildCount = element.children.length diff --git a/src/style-manager.coffee b/src/style-manager.coffee index f9d6aa373..04ac6534a 100644 --- a/src/style-manager.coffee +++ b/src/style-manager.coffee @@ -93,6 +93,7 @@ class StyleManager sourcePath = params?.sourcePath context = params?.context group = params?.group + priority = params?.priority if sourcePath? and styleElement = @styleElementsBySourcePath[sourcePath] updated = true @@ -110,6 +111,10 @@ class StyleManager styleElement.group = group styleElement.setAttribute('group', group) + if priority? + styleElement.priority = priority + styleElement.setAttribute('priority', priority) + styleElement.textContent = source if updated diff --git a/src/styles-element.coffee b/src/styles-element.coffee index c4fe264f4..89673fb63 100644 --- a/src/styles-element.coffee +++ b/src/styles-element.coffee @@ -53,6 +53,7 @@ class StylesElement extends HTMLElement styleElementClone = styleElement.cloneNode(true) styleElementClone.sourcePath = styleElement.sourcePath styleElementClone.context = styleElement.context + styleElementClone.priority = styleElement.priority @styleElementClonesByOriginalElement.set(styleElement, styleElementClone) group = styleElement.getAttribute('group') @@ -62,6 +63,13 @@ class StylesElement extends HTMLElement insertBefore = child.nextSibling break + priority = styleElement.priority + if priority? + for child in @children + if child.priority > priority + insertBefore = child + break + @insertBefore(styleElementClone, insertBefore) if @context is 'atom-text-editor' From 94e12ee8862c48f36cdd50e20531bb6ab35068aa Mon Sep 17 00:00:00 2001 From: Nathan Sobo Date: Tue, 6 Jan 2015 19:33:14 -0700 Subject: [PATCH 2/5] Handle priority in when inserting style elements in style manager --- spec/style-manager-spec.coffee | 14 ++++++++++++++ src/style-manager.coffee | 9 ++++++++- 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/spec/style-manager-spec.coffee b/spec/style-manager-spec.coffee index 1bf4770b3..d36637dc6 100644 --- a/spec/style-manager-spec.coffee +++ b/spec/style-manager-spec.coffee @@ -64,3 +64,17 @@ describe "StyleManager", -> "a {color: green}" "a {color: blue}" ] + + describe "when a priority parameter is specified", -> + it "inserts the style sheet based on the priority", -> + manager.addStyleSheet("a {color: red}", priority: 1) + manager.addStyleSheet("a {color: blue}", priority: 0) + manager.addStyleSheet("a {color: green}", priority: 2) + manager.addStyleSheet("a {color: yellow}", priority: 1) + + expect(manager.getStyleElements().map (elt) -> elt.textContent).toEqual [ + "a {color: blue}" + "a {color: red}" + "a {color: yellow}" + "a {color: green}" + ] diff --git a/src/style-manager.coffee b/src/style-manager.coffee index 04ac6534a..7e4124359 100644 --- a/src/style-manager.coffee +++ b/src/style-manager.coffee @@ -125,7 +125,7 @@ class StyleManager new Disposable => @removeStyleElement(styleElement) addStyleElement: (styleElement) -> - {sourcePath, group} = styleElement + {sourcePath, group, priority} = styleElement if group? for existingElement, index in @styleElements @@ -133,6 +133,13 @@ class StyleManager insertIndex = index + 1 else break if insertIndex? + + if priority? + for existingElement, index in @styleElements + if existingElement.priority > priority + insertIndex = index + break + insertIndex ?= @styleElements.length @styleElements.splice(insertIndex, 0, styleElement) From 62adc98b1749cba3028f79d5541098338d4ed7f4 Mon Sep 17 00:00:00 2001 From: Nathan Sobo Date: Tue, 6 Jan 2015 19:33:39 -0700 Subject: [PATCH 3/5] =?UTF-8?q?Use=20=E2=80=98user=E2=80=99=20group=20for?= =?UTF-8?q?=20user=20style=20sheet=20instead=20of=20=E2=80=98userTheme?= =?UTF-8?q?=E2=80=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/theme-manager.coffee | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/theme-manager.coffee b/src/theme-manager.coffee index dd60b99e6..70db13a14 100644 --- a/src/theme-manager.coffee +++ b/src/theme-manager.coffee @@ -240,7 +240,7 @@ class ThemeManager @userStylesheetFile = new File(userStylesheetPath) @userStylesheetFile.on 'contents-changed moved removed', => @loadUserStylesheet() userStylesheetContents = @loadStylesheet(userStylesheetPath, true) - @applyStylesheet(userStylesheetPath, userStylesheetContents, 'userTheme') + @applyStylesheet(userStylesheetPath, userStylesheetContents, 'user') loadBaseStylesheets: -> @requireStylesheet('../static/bootstrap') From 2c1f8ce73384878c69b3c50d9d6ec5af7c7960fe Mon Sep 17 00:00:00 2001 From: Nathan Sobo Date: Tue, 6 Jan 2015 19:41:57 -0700 Subject: [PATCH 4/5] Set the style element priority based on the group This ensures that elements are always in the desired order regardless of the order the groups are added. --- spec/style-manager-spec.coffee | 11 +++++++++++ src/style-manager.coffee | 6 +++++- 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/spec/style-manager-spec.coffee b/spec/style-manager-spec.coffee index d36637dc6..61ddd6624 100644 --- a/spec/style-manager-spec.coffee +++ b/spec/style-manager-spec.coffee @@ -65,6 +65,17 @@ describe "StyleManager", -> "a {color: blue}" ] + it "assigns priorities to 'bundled', 'theme', and 'user' groups that place them in the proper order", -> + manager.addStyleSheet("a {color: red}", group: 'user') + manager.addStyleSheet("a {color: blue}", group: 'bundled') + manager.addStyleSheet("a {color: green}", group: 'theme') + + expect(manager.getStyleElements().map (elt) -> elt.textContent).toEqual [ + "a {color: blue}" + "a {color: green}" + "a {color: red}" + ] + describe "when a priority parameter is specified", -> it "inserts the style sheet based on the priority", -> manager.addStyleSheet("a {color: red}", priority: 1) diff --git a/src/style-manager.coffee b/src/style-manager.coffee index 7e4124359..b18188521 100644 --- a/src/style-manager.coffee +++ b/src/style-manager.coffee @@ -93,7 +93,11 @@ class StyleManager sourcePath = params?.sourcePath context = params?.context group = params?.group - priority = params?.priority + priority = params?.priority ? + switch group + when 'bundled' then 0 + when 'theme' then 1 + when 'user' then 2 if sourcePath? and styleElement = @styleElementsBySourcePath[sourcePath] updated = true From 7dd67caf57d5b0c8e24eb47db5bb7cb9b8a56b64 Mon Sep 17 00:00:00 2001 From: Nathan Sobo Date: Tue, 6 Jan 2015 20:08:45 -0700 Subject: [PATCH 5/5] =?UTF-8?q?Remove=20=E2=80=98group=E2=80=99=20paramete?= =?UTF-8?q?r=20to=20addStyleSheet=20in=20favor=20of=20=E2=80=98priority?= =?UTF-8?q?=E2=80=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- spec/style-manager-spec.coffee | 23 ----------------------- spec/styles-element-spec.coffee | 11 ----------- spec/theme-manager-spec.coffee | 14 +++++++------- src/package.coffee | 7 ++++--- src/style-manager.coffee | 20 ++------------------ src/styles-element.coffee | 7 ------- src/theme-manager.coffee | 16 +++++++++------- src/theme-package.coffee | 2 +- 8 files changed, 23 insertions(+), 77 deletions(-) diff --git a/spec/style-manager-spec.coffee b/spec/style-manager-spec.coffee index 61ddd6624..7e601bcc1 100644 --- a/spec/style-manager-spec.coffee +++ b/spec/style-manager-spec.coffee @@ -53,29 +53,6 @@ describe "StyleManager", -> expect(addEvents[0].getAttribute('source-path')).toBe '/foo/bar' expect(addEvents[0].textContent).toBe "a {color: yellow;}" - describe "when a group parameter is specified", -> - it "inserts the stylesheet at the end of any existing stylesheets for the same group", -> - manager.addStyleSheet("a {color: red}", group: 'a') - manager.addStyleSheet("a {color: blue}", group: 'b') - manager.addStyleSheet("a {color: green}", group: 'a') - - expect(manager.getStyleElements().map (elt) -> elt.textContent).toEqual [ - "a {color: red}" - "a {color: green}" - "a {color: blue}" - ] - - it "assigns priorities to 'bundled', 'theme', and 'user' groups that place them in the proper order", -> - manager.addStyleSheet("a {color: red}", group: 'user') - manager.addStyleSheet("a {color: blue}", group: 'bundled') - manager.addStyleSheet("a {color: green}", group: 'theme') - - expect(manager.getStyleElements().map (elt) -> elt.textContent).toEqual [ - "a {color: blue}" - "a {color: green}" - "a {color: red}" - ] - describe "when a priority parameter is specified", -> it "inserts the style sheet based on the priority", -> manager.addStyleSheet("a {color: red}", priority: 1) diff --git a/spec/styles-element-spec.coffee b/spec/styles-element-spec.coffee index 282cc616a..7d2fe722a 100644 --- a/spec/styles-element-spec.coffee +++ b/spec/styles-element-spec.coffee @@ -45,17 +45,6 @@ describe "StylesElement", -> expect(element.children[initialChildCount + 2].textContent).toBe "a {color: yellow}" expect(element.children[initialChildCount + 3].textContent).toBe "a {color: green}" - it "orders style elements by group", -> - initialChildCount = element.children.length - - atom.styles.addStyleSheet("a {color: red}", group: 'a') - atom.styles.addStyleSheet("a {color: blue}", group: 'b') - atom.styles.addStyleSheet("a {color: green}", group: 'a') - - 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 diff --git a/spec/theme-manager-spec.coffee b/spec/theme-manager-spec.coffee index 1d322f7d8..fc9624f93 100644 --- a/spec/theme-manager-spec.coffee +++ b/spec/theme-manager-spec.coffee @@ -96,8 +96,8 @@ describe "ThemeManager", -> runs -> reloadHandler.reset() - expect($('style[group=theme]')).toHaveLength 2 - expect($('style[group=theme]:eq(0)').attr('source-path')).toMatch /atom-dark-ui/ + expect($('style[priority=1]')).toHaveLength 2 + expect($('style[priority=1]:eq(0)').attr('source-path')).toMatch /atom-dark-ui/ atom.config.set('core.themes', ['atom-light-ui', 'atom-dark-ui']) waitsFor -> @@ -105,9 +105,9 @@ describe "ThemeManager", -> runs -> reloadHandler.reset() - expect($('style[group=theme]')).toHaveLength 2 - expect($('style[group=theme]:eq(0)').attr('source-path')).toMatch /atom-dark-ui/ - expect($('style[group=theme]:eq(1)').attr('source-path')).toMatch /atom-light-ui/ + expect($('style[priority=1]')).toHaveLength 2 + expect($('style[priority=1]:eq(0)').attr('source-path')).toMatch /atom-dark-ui/ + expect($('style[priority=1]:eq(1)').attr('source-path')).toMatch /atom-light-ui/ atom.config.set('core.themes', []) waitsFor -> @@ -115,7 +115,7 @@ describe "ThemeManager", -> runs -> reloadHandler.reset() - expect($('style[group=theme]')).toHaveLength 2 + expect($('style[priority=1]')).toHaveLength 2 # atom-dark-ui has an directory path, the syntax one doesn't atom.config.set('core.themes', ['theme-with-index-less', 'atom-dark-ui']) @@ -123,7 +123,7 @@ describe "ThemeManager", -> reloadHandler.callCount == 1 runs -> - expect($('style[group=theme]')).toHaveLength 2 + expect($('style[priority=1]')).toHaveLength 2 importPaths = themeManager.getImportPaths() expect(importPaths.length).toBe 1 expect(importPaths[0]).toContain 'atom-dark-ui' diff --git a/src/package.coffee b/src/package.coffee index 7bb81d481..8ef160972 100644 --- a/src/package.coffee +++ b/src/package.coffee @@ -109,7 +109,7 @@ class Package getType: -> 'atom' - getStylesheetType: -> 'bundled' + getStyleSheetPriority: -> 0 load: -> @measure 'loadTime', => @@ -175,8 +175,9 @@ class Package activateStylesheets: -> return if @stylesheetsActivated - group = @getStylesheetType() @stylesheetDisposables = new CompositeDisposable + + priority = @getStyleSheetPriority() for [sourcePath, source] in @stylesheets if match = path.basename(sourcePath).match(/[^.]*\.([^.]*)\./) context = match[1] @@ -185,7 +186,7 @@ class Package else context = undefined - @stylesheetDisposables.add(atom.styles.addStyleSheet(source, {sourcePath, group, context})) + @stylesheetDisposables.add(atom.styles.addStyleSheet(source, {sourcePath, priority, context})) @stylesheetsActivated = true activateResources: -> diff --git a/src/style-manager.coffee b/src/style-manager.coffee index b18188521..c891223b9 100644 --- a/src/style-manager.coffee +++ b/src/style-manager.coffee @@ -92,12 +92,7 @@ class StyleManager addStyleSheet: (source, params) -> sourcePath = params?.sourcePath context = params?.context - group = params?.group - priority = params?.priority ? - switch group - when 'bundled' then 0 - when 'theme' then 1 - when 'user' then 2 + priority = params?.priority if sourcePath? and styleElement = @styleElementsBySourcePath[sourcePath] updated = true @@ -111,10 +106,6 @@ class StyleManager styleElement.context = context styleElement.setAttribute('context', context) - if group? - styleElement.group = group - styleElement.setAttribute('group', group) - if priority? styleElement.priority = priority styleElement.setAttribute('priority', priority) @@ -129,14 +120,7 @@ class StyleManager new Disposable => @removeStyleElement(styleElement) addStyleElement: (styleElement) -> - {sourcePath, group, priority} = styleElement - - if group? - for existingElement, index in @styleElements - if existingElement.group is group - insertIndex = index + 1 - else - break if insertIndex? + {sourcePath, priority} = styleElement if priority? for existingElement, index in @styleElements diff --git a/src/styles-element.coffee b/src/styles-element.coffee index 89673fb63..d333a2e45 100644 --- a/src/styles-element.coffee +++ b/src/styles-element.coffee @@ -56,13 +56,6 @@ class StylesElement extends HTMLElement styleElementClone.priority = styleElement.priority @styleElementClonesByOriginalElement.set(styleElement, styleElementClone) - group = styleElement.getAttribute('group') - if group? - for child in @children - if child.getAttribute('group') is group and child.nextSibling?.getAttribute('group') isnt group - insertBefore = child.nextSibling - break - priority = styleElement.priority if priority? for child in @children diff --git a/src/theme-manager.coffee b/src/theme-manager.coffee index 70db13a14..0379066ca 100644 --- a/src/theme-manager.coffee +++ b/src/theme-manager.coffee @@ -219,28 +219,30 @@ class ThemeManager # # Returns a {Disposable} on which `.dispose()` can be called to remove the # required stylesheet. - requireStylesheet: (stylesheetPath, type='bundled') -> + requireStylesheet: (stylesheetPath) -> if fullPath = @resolveStylesheet(stylesheetPath) content = @loadStylesheet(fullPath) - @applyStylesheet(fullPath, content, type) + @applyStylesheet(fullPath, content) else throw new Error("Could not find a file at path '#{stylesheetPath}'") unwatchUserStylesheet: -> @userStylesheetFile?.off() @userStylesheetFile = null - @removeStylesheet(@userStylesheetPath) if @userStylesheetPath? + @userStyleSheetDisposable?.dispose() + @userStyleSheetDisposable = null loadUserStylesheet: -> @unwatchUserStylesheet() + userStylesheetPath = atom.styles.getUserStyleSheetPath() return unless fs.isFileSync(userStylesheetPath) - @userStylesheetPath = userStylesheetPath @userStylesheetFile = new File(userStylesheetPath) @userStylesheetFile.on 'contents-changed moved removed', => @loadUserStylesheet() userStylesheetContents = @loadStylesheet(userStylesheetPath, true) - @applyStylesheet(userStylesheetPath, userStylesheetContents, 'user') + + @userStyleSheetDisposable = atom.styles.addStyleSheet(userStylesheetContents, sourcePath: userStylesheetPath, priority: 2) loadBaseStylesheets: -> @requireStylesheet('../static/bootstrap') @@ -291,8 +293,8 @@ class ThemeManager removeStylesheet: (stylesheetPath) -> @styleSheetDisposablesBySourcePath[stylesheetPath]?.dispose() - applyStylesheet: (path, text, type='bundled') -> - @styleSheetDisposablesBySourcePath[path] = atom.styles.addStyleSheet(text, sourcePath: path, group: type) + applyStylesheet: (path, text) -> + @styleSheetDisposablesBySourcePath[path] = atom.styles.addStyleSheet(text, sourcePath: path) stringToId: (string) -> string.replace(/\\/g, '/') diff --git a/src/theme-package.coffee b/src/theme-package.coffee index 3c03d818f..e1b87e783 100644 --- a/src/theme-package.coffee +++ b/src/theme-package.coffee @@ -5,7 +5,7 @@ module.exports = class ThemePackage extends Package getType: -> 'theme' - getStylesheetType: -> 'theme' + getStyleSheetPriority: -> 1 enable: -> atom.config.unshiftAtKeyPath('core.themes', @name)