Commit 7a6fb02d authored by Brett Walker's avatar Brett Walker

Ignore multiple <code> when syntax highlighting

A <pre> should have only ony <code> node when
being syntax highlighted.

Changelog: fixed
parent bff05c9a
...@@ -14,7 +14,7 @@ module Banzai ...@@ -14,7 +14,7 @@ module Banzai
LANG_PARAMS_DELIMITER = ':' LANG_PARAMS_DELIMITER = ':'
LANG_PARAMS_ATTR = 'data-lang-params' LANG_PARAMS_ATTR = 'data-lang-params'
CSS = 'pre:not([data-math-style]):not([data-mermaid-style]):not([data-kroki-style]) > code' CSS = 'pre:not([data-math-style]):not([data-mermaid-style]):not([data-kroki-style]) > code:only-child'
XPATH = Gitlab::Utils::Nokogiri.css_to_xpath(CSS).freeze XPATH = Gitlab::Utils::Nokogiri.css_to_xpath(CSS).freeze
def call def call
...@@ -106,8 +106,8 @@ module Banzai ...@@ -106,8 +106,8 @@ module Banzai
(Rouge::Lexer.find(language) || Rouge::Lexers::PlainText).new (Rouge::Lexer.find(language) || Rouge::Lexers::PlainText).new
end end
# Replace the parent `pre` element with the entire highlighted block
def replace_parent_pre_element(node, highlighted) def replace_parent_pre_element(node, highlighted)
# Replace the parent `pre` element with the entire highlighted block
node.parent.replace(highlighted) node.parent.replace(highlighted)
end end
......
...@@ -37,6 +37,14 @@ RSpec.describe Banzai::Filter::SyntaxHighlightFilter do ...@@ -37,6 +37,14 @@ RSpec.describe Banzai::Filter::SyntaxHighlightFilter do
end end
end end
context "when <pre> contains multiple <code> tags" do
it "ignores the block" do
result = filter('<pre><code>one</code> and <code>two</code></pre>')
expect(result.to_html).to eq('<pre><code>one</code> and <code>two</code></pre>')
end
end
context "when a valid language is specified" do context "when a valid language is specified" do
it "highlights as that language" do it "highlights as that language" do
result = filter('<pre lang="ruby"><code>def fun end</code></pre>') result = filter('<pre lang="ruby"><code>def fun end</code></pre>')
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment