Skip to content

Commit

Permalink
fix(hashCodeTags): escape code tags
Browse files Browse the repository at this point in the history
Previously, `<code>` tags were not escaped. This was counter intuitive since ´<pre><code>` tags
were being escaped. Now both pre code and code are escaped.

Closes #339
  • Loading branch information
tivie committed Feb 6, 2017
1 parent 7f43b79 commit 41cb3f6
Show file tree
Hide file tree
Showing 9 changed files with 52 additions and 30 deletions.
33 changes: 20 additions & 13 deletions dist/showdown.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/showdown.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/showdown.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/showdown.min.js.map

Large diffs are not rendered by default.

18 changes: 18 additions & 0 deletions src/subParsers/hashCodeTags.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/**
* Hash and escape <code> elements that should not be parsed as markdown
*/
showdown.subParser('hashCodeTags', function (text, options, globals) {
'use strict';
text = globals.converter._dispatch('hashCodeTags.before', text, options, globals);

var repFunc = function (wholeMatch, match, left, right) {
var codeblock = left + showdown.subParser('encodeCode')(match, options, globals) + right;
return '¨C' + (globals.gHtmlSpans.push(codeblock) - 1) + 'C';
};

// Hash naked <code>
text = showdown.helper.replaceRecursiveRegExp(text, repFunc, '<code\\b[^>]*>', '</code>', 'gim');

text = globals.converter._dispatch('hashCodeTags.after', text, options, globals);
return text;
});
14 changes: 1 addition & 13 deletions src/subParsers/hashPreCodeTags.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Hash span elements that should not be parsed as markdown
* Hash and escape <pre><code> elements that should not be parsed as markdown
*/
showdown.subParser('hashPreCodeTags', function (text, options, globals) {
'use strict';
Expand All @@ -17,15 +17,3 @@ showdown.subParser('hashPreCodeTags', function (text, options, globals) {
text = globals.converter._dispatch('hashPreCodeTags.after', text, options, globals);
return text;
});

showdown.subParser('hashCodeTags', function (text, options, globals) {
'use strict';
text = globals.converter._dispatch('hashCodeTags.before', text, options, globals);
// Hash naked <code>
var matches = showdown.helper.matchRecursiveRegExp(text, '<code\\b[^>]*>', '</code>', 'gi');
for (var i = 0; i < matches.length; ++i) {
text = text.replace(matches[i][0], '¨C' + (globals.gHtmlSpans.push(matches[i][0]) - 1) + 'C');
}
text = globals.converter._dispatch('hashCodeTags.after', text, options, globals);
return text;
});
4 changes: 4 additions & 0 deletions test/cases/encodeHTMLCodeTags.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<p>this is code <code>some &lt;span&gt;text&lt;/span&gt;</code> yeah!</p>
<pre><code>
&lt;div&gt;foo&lt;/div&gt;
</code></pre>
5 changes: 5 additions & 0 deletions test/cases/encodeHTMLCodeTags.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
this is code <code>some <span>text</span></code> yeah!

<pre><code>
<div>foo</div>
</code></pre>
2 changes: 1 addition & 1 deletion test/cases/literal-html-tags.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<p><code>some **code** yeah</code></p>
<p>some <code>inline **code** block</code></p>
<p><code>some inline **code**</code> block</p>
<p>yo dawg <code start="true">some <code start="false">code</code> inception</code></p>
<p>yo dawg <code start="true">some &lt;code start="false"&gt;code&lt;/code&gt; inception</code></p>
<div>some **div** yeah</div>

0 comments on commit 41cb3f6

Please sign in to comment.