소스 검색

fix(hashCodeTags): escape code tags

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
Estevao Soares dos Santos 8 년 전
부모
커밋
41cb3f6b7f

+ 20 - 13
dist/showdown.js

@@ -1755,6 +1755,25 @@ showdown.subParser('hashBlock', function (text, options, globals) {
   return text;
 });
 
+/**
+ * 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;
+});
+
 showdown.subParser('hashElement', function (text, options, globals) {
   'use strict';
 
@@ -1911,7 +1930,7 @@ showdown.subParser('unhashHTMLSpans', function (text, options, globals) {
 });
 
 /**
- * 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';
@@ -1929,18 +1948,6 @@ 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;
-});
 
 showdown.subParser('headers', function (text, options, globals) {
   'use strict';

파일 크기가 너무 크기때문에 변경 상태를 표시하지 않습니다.
+ 0 - 0
dist/showdown.js.map


파일 크기가 너무 크기때문에 변경 상태를 표시하지 않습니다.
+ 0 - 0
dist/showdown.min.js


파일 크기가 너무 크기때문에 변경 상태를 표시하지 않습니다.
+ 0 - 0
dist/showdown.min.js.map


+ 18 - 0
src/subParsers/hashCodeTags.js

@@ -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;
+});

+ 1 - 13
src/subParsers/hashPreCodeTags.js

@@ -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';
@@ -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 - 0
test/cases/encodeHTMLCodeTags.html

@@ -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 - 0
test/cases/encodeHTMLCodeTags.md

@@ -0,0 +1,5 @@
+this is code <code>some <span>text</span></code> yeah!
+
+<pre><code>
+<div>foo</div>
+</code></pre>

+ 1 - 1
test/cases/literal-html-tags.html

@@ -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>

이 변경점에서 너무 많은 파일들이 변경되어 몇몇 파일들은 표시되지 않았습니다.