From 7cbf5e4d18f6ba0bf1afbddcb090fa39837e9529 Mon Sep 17 00:00:00 2001 From: Robert Speicher Date: Sat, 5 Sep 2015 17:50:47 -0400 Subject: Prevent result of SyntaxHighlightFilter being sanitized --- lib/gitlab/markdown/sanitization_filter.rb | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/lib/gitlab/markdown/sanitization_filter.rb b/lib/gitlab/markdown/sanitization_filter.rb index 68ed57f6257..54faf981b7d 100644 --- a/lib/gitlab/markdown/sanitization_filter.rb +++ b/lib/gitlab/markdown/sanitization_filter.rb @@ -67,12 +67,16 @@ module Gitlab def clean_spans lambda do |env| - return unless env[:node_name] == 'span' - return unless env[:node].has_attribute?('class') + node = env[:node] - unless has_ancestor?(env[:node], 'pre') - env[:node].remove_attribute('class') + return unless node.name == 'span' + return unless node.has_attribute?('class') + + unless has_ancestor?(node, 'pre') + node.remove_attribute('class') end + + {node_whitelist: [node]} end end end -- cgit v1.2.1 From 404a01ab148417f6314c94d213769dd72fd0589e Mon Sep 17 00:00:00 2001 From: Robert Speicher Date: Thu, 10 Sep 2015 15:28:42 -0400 Subject: Add specs for syntax_highlight JS Also makes it work when given a parent element containing a `.js-syntax-highlight` element so for dynamically-added things like notes or Markdown previews, we can more accurately target just the element we care about. --- app/assets/javascripts/syntax_highlight.coffee | 10 +++++- spec/javascripts/syntax_highlight_spec.js.coffee | 42 ++++++++++++++++++++++++ 2 files changed, 51 insertions(+), 1 deletion(-) create mode 100644 spec/javascripts/syntax_highlight_spec.js.coffee diff --git a/app/assets/javascripts/syntax_highlight.coffee b/app/assets/javascripts/syntax_highlight.coffee index 71295cd4b08..980f0232d10 100644 --- a/app/assets/javascripts/syntax_highlight.coffee +++ b/app/assets/javascripts/syntax_highlight.coffee @@ -1,3 +1,5 @@ +# Syntax Highlighter +# # Applies a syntax highlighting color scheme CSS class to any element with the # `js-syntax-highlight` class # @@ -6,7 +8,13 @@ #
# $.fn.syntaxHighlight = -> - $(this).addClass(gon.user_color_scheme) + if $(this).hasClass('js-syntax-highlight') + # Given the element itself, apply highlighting + $(this).addClass(gon.user_color_scheme) + else + # Given a parent element, recurse to any of its applicable children + $children = $(this).find('.js-syntax-highlight') + $children.syntaxHighlight() if $children.length $(document).on 'ready page:load', -> $('.js-syntax-highlight').syntaxHighlight() diff --git a/spec/javascripts/syntax_highlight_spec.js.coffee b/spec/javascripts/syntax_highlight_spec.js.coffee new file mode 100644 index 00000000000..6a73b6bf32c --- /dev/null +++ b/spec/javascripts/syntax_highlight_spec.js.coffee @@ -0,0 +1,42 @@ +#= require syntax_highlight + +describe 'Syntax Highlighter', -> + stubUserColorScheme = (value) -> + window.gon ?= {} + window.gon.user_color_scheme = value + + describe 'on a js-syntax-highlight element', -> + beforeEach -> + fixture.set('
') + + it 'applies syntax highlighting', -> + stubUserColorScheme('monokai') + + $('.js-syntax-highlight').syntaxHighlight() + + expect($('.js-syntax-highlight')).toHaveClass('monokai') + + describe 'on a parent element', -> + beforeEach -> + fixture.set """ +
+
+
+
+
+ """ + + it 'applies highlighting to all applicable children', -> + stubUserColorScheme('monokai') + + $('.parent').syntaxHighlight() + + expect($('.parent, .foo')).not.toHaveClass('monokai') + expect($('.monokai').length).toBe(2) + + it 'prevents an infinite loop when no matches exist', -> + fixture.set('
') + + highlight = -> $('div').syntaxHighlight() + + expect(highlight).not.toThrow() -- cgit v1.2.1 From 4e54b82326e0a24c3b5196956f1e957b3f7c02e9 Mon Sep 17 00:00:00 2001 From: Robert Speicher Date: Thu, 10 Sep 2015 15:29:56 -0400 Subject: Apply syntax highlighting to Markdown previews --- app/assets/javascripts/dropzone_input.js.coffee | 1 + 1 file changed, 1 insertion(+) diff --git a/app/assets/javascripts/dropzone_input.js.coffee b/app/assets/javascripts/dropzone_input.js.coffee index a0dcaa8c27a..6f789e668af 100644 --- a/app/assets/javascripts/dropzone_input.js.coffee +++ b/app/assets/javascripts/dropzone_input.js.coffee @@ -167,6 +167,7 @@ class @DropzoneInput dataType: "json" ).success (data) -> preview.html data.body + preview.syntaxHighlight() renderReferencedUsers data.references.users -- cgit v1.2.1 From 19c0bf2723bb0895159e4e4e0c6f47bc73157449 Mon Sep 17 00:00:00 2001 From: Robert Speicher Date: Thu, 10 Sep 2015 15:30:06 -0400 Subject: Simplify syntax highlighting of new notes --- app/assets/javascripts/notes.js.coffee | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/app/assets/javascripts/notes.js.coffee b/app/assets/javascripts/notes.js.coffee index b7f2c63c5a7..ce638c2641b 100644 --- a/app/assets/javascripts/notes.js.coffee +++ b/app/assets/javascripts/notes.js.coffee @@ -122,8 +122,9 @@ class @Notes # or skip if rendered if @isNewNote(note) @note_ids.push(note.id) - $('ul.main-notes-list').append(note.html) - $('.js-syntax-highlight').syntaxHighlight() + $('ul.main-notes-list'). + append(note.html). + syntaxHighlight() @initTaskList() ### -- cgit v1.2.1 From e3c97ede9663fe7905fcb350875c46526cb4f832 Mon Sep 17 00:00:00 2001 From: Robert Speicher Date: Thu, 10 Sep 2015 16:06:24 -0400 Subject: RU-BO-COOOOOOOOP --- lib/gitlab/markdown/sanitization_filter.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/gitlab/markdown/sanitization_filter.rb b/lib/gitlab/markdown/sanitization_filter.rb index 54faf981b7d..e368de7d848 100644 --- a/lib/gitlab/markdown/sanitization_filter.rb +++ b/lib/gitlab/markdown/sanitization_filter.rb @@ -76,7 +76,7 @@ module Gitlab node.remove_attribute('class') end - {node_whitelist: [node]} + { node_whitelist: [node] } end end end -- cgit v1.2.1