Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure <pre> nodes are not removed after syntax highlighting #295

Merged
merged 1 commit into from
May 4, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ group :development do
end

group :test do
gem 'commonmarker', '~> 0.14', require: false
gem 'commonmarker', '~> 0.16', require: false
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bumped because :GITHUB_PRE_LANG was not introduced until 0.16.1

gem 'email_reply_parser', '~> 0.5', require: false
gem 'gemoji', '~> 2.0', require: false
gem 'minitest'
Expand Down
1 change: 1 addition & 0 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ Rake::TestTask.new do |t|
t.libs << 'test'
t.test_files = FileList['test/**/*_test.rb']
t.verbose = true
t.warning = false
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rouge is spitting out a ton of warnings which made the test output a bit annoying, so I added this flag.

end

task default: :test
5 changes: 3 additions & 2 deletions lib/html/pipeline/syntax_highlight_filter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@ def call
html = highlight_with_timeout_handling(text, lang)
next if html.nil?

next unless (node = node.replace(html).first)
node.inner_html = html
klass = node['class']
klass = [klass, "highlight-#{lang}"].compact.join ' '
scope = context[:scope] || "highlight-#{lang}"
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would like to be able to scope my css, so I added this option.

For more info, https://github.com/jneen/rouge#scope-highlight

klass = [klass, scope].compact.join ' '

node['class'] = klass
end
Expand Down
40 changes: 40 additions & 0 deletions test/html/pipeline/syntax_highlight_filter_test.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require 'test_helper'
require 'escape_utils'

SyntaxHighlightFilter = HTML::Pipeline::SyntaxHighlightFilter

Expand All @@ -19,4 +20,43 @@ def test_highlight_default_will_not_override
assert doc.css('.highlight-coffeescript').empty?
assert !doc.css('.highlight-c').empty?
end

def test_highlight_does_not_remove_pre_tag
filter = SyntaxHighlightFilter.new \
"<pre lang='c'>hello</pre>", highlight: 'coffeescript'

doc = filter.call

assert !doc.css('pre').empty?
end

def test_highlight_allows_optional_scope
filter = SyntaxHighlightFilter.new \
"<pre lang='c'>hello</pre>", highlight: 'coffeescript', scope: 'test-scope'

doc = filter.call

assert !doc.css('pre.test-scope').empty?
end

def test_highlight_keeps_the_pre_tags_lang
filter = SyntaxHighlightFilter.new \
"<pre lang='c'>hello</pre>", highlight: 'coffeescript'

doc = filter.call

assert !doc.css('pre[lang=c]').empty?
end

def test_highlight_handles_nested_pre_tags
inner_code = "<pre>console.log('i am nested!')</pre>"
escaped = EscapeUtils.escape_html(inner_code)
html = "<pre lang='html'>#{escaped}</pre>"
filter = SyntaxHighlightFilter.new html, highlight: 'html'

doc = filter.call

assert_equal 2, doc.css('span[class=nt]').length
assert_equal EscapeUtils.unescape_html(escaped), doc.inner_text
end
end