Welcome to mirror list, hosted at ThFree Co, Russian Federation.

gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRobert Speicher <robert@gitlab.com>2016-12-22 17:23:37 +0300
committerDouglas Barbosa Alexandre <dbalexandre@gmail.com>2016-12-22 23:53:19 +0300
commit5e1080c5e07e3ea5a79b2ac1f91df5d3a0001a5c (patch)
tree5f22e299417f49f09475e8fee93b6c7286975846
parent60ed1a25980598b6ebc3401e6cf0abe54e17c577 (diff)
Merge branch 'inline-math-dollar' into 'master'
Don't render inline math when dollar signs are inside markup See merge request !8259
-rw-r--r--lib/banzai/filter/math_filter.rb11
-rw-r--r--spec/lib/banzai/filter/math_filter_spec.rb7
2 files changed, 10 insertions, 8 deletions
diff --git a/lib/banzai/filter/math_filter.rb b/lib/banzai/filter/math_filter.rb
index cb037f89337..b6e784c886b 100644
--- a/lib/banzai/filter/math_filter.rb
+++ b/lib/banzai/filter/math_filter.rb
@@ -5,12 +5,6 @@ module Banzai
# HTML filter that adds class="code math" and removes the dollar sign in $`2+2`$.
#
class MathFilter < HTML::Pipeline::Filter
- # This picks out <code>...</code>.
- INLINE_MATH = 'descendant-or-self::code'.freeze
-
- # Pick out a code block which is declared math
- DISPLAY_MATH = "descendant-or-self::pre[contains(@class, 'math') and contains(@class, 'code')]".freeze
-
# Attribute indicating inline or display math.
STYLE_ATTRIBUTE = 'data-math-style'.freeze
@@ -22,13 +16,14 @@ module Banzai
DOLLAR_SIGN = '$'.freeze
def call
- doc.xpath(INLINE_MATH).each do |code|
+ doc.css('code').each do |code|
closing = code.next
opening = code.previous
# We need a sibling before and after.
# They should end and start with $ respectively.
if closing && opening &&
+ closing.text? && opening.text? &&
closing.content.first == DOLLAR_SIGN &&
opening.content.last == DOLLAR_SIGN
@@ -39,7 +34,7 @@ module Banzai
end
end
- doc.xpath(DISPLAY_MATH).each do |el|
+ doc.css('pre.code.math').each do |el|
el[STYLE_ATTRIBUTE] = 'display'
el[:class] += " #{TAG_CLASS}"
end
diff --git a/spec/lib/banzai/filter/math_filter_spec.rb b/spec/lib/banzai/filter/math_filter_spec.rb
index 3fe2c7f5d5d..51883782e19 100644
--- a/spec/lib/banzai/filter/math_filter_spec.rb
+++ b/spec/lib/banzai/filter/math_filter_spec.rb
@@ -79,6 +79,13 @@ describe Banzai::Filter::MathFilter, lib: true do
expect(doc.to_s).to eq input
end
+ it 'ignores dollar signs if they are inside another element' do
+ input = '<p>We check strictly <em>$</em><code>2+2</code><em>$</em></p>'
+ doc = filter(input)
+
+ expect(doc.to_s).to eq input
+ end
+
# Display math
it 'adds data-math-style display attribute to display math' do