Skip to content

Commit 512b00a

Browse files
committed
Fixed bug RF#29521: HTML math output not always XHTML compatible
The characters < and & are not allowed in a script tag in XHTML. So since the HTML converter uses script tags for math elements, whenever these characters appear in a value the value is wrapped in a CDATA section to make the output XHTML compatible.
1 parent 8f11194 commit 512b00a

File tree

3 files changed

+5
-4
lines changed

3 files changed

+5
-4
lines changed

lib/kramdown/converter/html.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,8 @@ def convert_smart_quote(el, indent)
315315

316316
def convert_math(el, indent)
317317
block = (el.options[:category] == :block)
318-
"<script type=\"math/tex#{block ? '; mode=display' : ''}\">#{el.value}</script>#{block ? "\n" : ''}"
318+
value = (el.value =~ /<|&/ ? "<![CDATA[#{el.value}]]>" : el.value)
319+
"<script type=\"math/tex#{block ? '; mode=display' : ''}\">#{value}</script>#{block ? "\n" : ''}"
319320
end
320321

321322
def convert_abbreviation(el, indent)

lib/kramdown/parser/html.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -529,7 +529,7 @@ def is_math_tag?(el)
529529

530530
def handle_math_tag(el)
531531
set_basics(el, :math, :category => (el.attr['type'] =~ /mode=display/ ? :block : :span))
532-
el.value = el.children.shift.value
532+
el.value = el.children.shift.value.sub(/\A<!\[CDATA\[(.*)\]\]>\z/m, '\1')
533533
el.attr.delete('type')
534534
end
535535

test/testcases/block/15_math/normal.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@
66
<p><script type="math/tex">\lambda_\alpha > 5</script>
77
This is a para.</p>
88

9-
<script type="math/tex; mode=display">\begin{align*}
9+
<script type="math/tex; mode=display"><![CDATA[\begin{align*}
1010
&=5 \\
1111
&=6 \\
12-
\end{align*}</script>
12+
\end{align*}]]></script>
1313

1414
<script type="math/tex; mode=display">5+5</script>
1515

0 commit comments

Comments
 (0)