From 986e8bf85fd67c03a587f8bd225ea69e073735a5 Mon Sep 17 00:00:00 2001 From: Nathan Sobo Date: Wed, 25 Feb 2015 14:35:24 -0700 Subject: [PATCH] Allow multiple space-delimited classes for highlight decorations Fixes #5747 --- spec/text-editor-component-spec.coffee | 8 ++++++++ src/highlights-component.coffee | 14 ++++++++++++-- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/spec/text-editor-component-spec.coffee b/spec/text-editor-component-spec.coffee index 5d6e7ed85..d7b9ed2ff 100644 --- a/spec/text-editor-component-spec.coffee +++ b/spec/text-editor-component-spec.coffee @@ -1171,6 +1171,14 @@ describe "TextEditorComponent", -> regions = componentNode.querySelectorAll('.test-highlight .region') expect(regions.length).toBe 2 + it "allows multiple space-delimited decoration classes", -> + decoration.setProperties(type: 'highlight', class: 'foo bar') + nextAnimationFrame() + expect(componentNode.querySelectorAll('.foo.bar').length).toBe 1 + decoration.setProperties(type: 'highlight', class: 'bar baz') + nextAnimationFrame() + expect(componentNode.querySelectorAll('.bar.baz').length).toBe 1 + it "renders classes on the regions directly if 'deprecatedRegionClass' option is defined", -> decoration = editor.decorateMarker(marker, type: 'highlight', class: 'test-highlight', deprecatedRegionClass: 'test-highlight-region') nextAnimationFrame() diff --git a/src/highlights-component.coffee b/src/highlights-component.coffee index 488a2236f..48d2cc70e 100644 --- a/src/highlights-component.coffee +++ b/src/highlights-component.coffee @@ -1,4 +1,5 @@ RegionStyleProperties = ['top', 'left', 'right', 'width', 'height'] +SpaceRegex = /\s+/ module.exports = class HighlightsComponent @@ -44,8 +45,17 @@ class HighlightsComponent # update class if newHighlightState.class isnt oldHighlightState.class - highlightNode.classList.remove(oldHighlightState.class) if oldHighlightState.class? - highlightNode.classList.add(newHighlightState.class) + if oldHighlightState.class? + if SpaceRegex.test(oldHighlightState.class) + highlightNode.classList.remove(oldHighlightState.class.split(SpaceRegex)...) + else + highlightNode.classList.remove(oldHighlightState.class) + + if SpaceRegex.test(newHighlightState.class) + highlightNode.classList.add(newHighlightState.class.split(SpaceRegex)...) + else + highlightNode.classList.add(newHighlightState.class) + oldHighlightState.class = newHighlightState.class @updateHighlightRegions(id, newHighlightState)