Browse Source

Improve replaceEntities handling

1. Use unescapeAll instead of replaceEntities(unescapeMd(...))
2. Move replaceEntities(title) out of the renderer.
pull/82/head
Alex Kocharin 10 years ago
parent
commit
4f6e8d4830
  1. 4
      lib/helpers/parse_link_title.js
  2. 9
      lib/renderer.js
  3. 24
      test/fixtures/markdown-it/commonmark_extras.txt

4
lib/helpers/parse_link_title.js

@ -3,7 +3,7 @@
'use strict'; 'use strict';
var unescapeMd = require('../common/utils').unescapeMd; var unescapeAll = require('../common/utils').unescapeAll;
module.exports = function parseLinkTitle(str, pos, max) { module.exports = function parseLinkTitle(str, pos, max) {
@ -34,7 +34,7 @@ module.exports = function parseLinkTitle(str, pos, max) {
if (code === marker) { if (code === marker) {
result.pos = pos + 1; result.pos = pos + 1;
result.lines = lines; result.lines = lines;
result.str = unescapeMd(str.slice(start + 1, pos)); result.str = unescapeAll(str.slice(start + 1, pos));
result.ok = true; result.ok = true;
return result; return result;
} else if (code === 0x0A) { } else if (code === 0x0A) {

9
lib/renderer.js

@ -9,8 +9,7 @@
var assign = require('./common/utils').assign; var assign = require('./common/utils').assign;
var unescapeMd = require('./common/utils').unescapeMd; var unescapeAll = require('./common/utils').unescapeAll;
var replaceEntities = require('./common/utils').replaceEntities;
var escapeHtml = require('./common/utils').escapeHtml; var escapeHtml = require('./common/utils').escapeHtml;
@ -39,7 +38,7 @@ rules.fence = function (tokens, idx, options /*, env, self*/) {
var highlighted; var highlighted;
if (token.params) { if (token.params) {
langName = escapeHtml(replaceEntities(unescapeMd(token.params.split(/\s+/g)[0]))); langName = escapeHtml(unescapeAll(token.params.split(/\s+/g)[0]));
langClass = ' class="' + langPrefix + langName + '"'; langClass = ' class="' + langPrefix + langName + '"';
} }
@ -101,7 +100,7 @@ rules.paragraph_close = function (tokens, idx /*, options, env */) {
rules.link_open = function (tokens, idx /*, options, env */) { rules.link_open = function (tokens, idx /*, options, env */) {
var title = tokens[idx].title ? (' title="' + escapeHtml(replaceEntities(tokens[idx].title)) + '"') : ''; var title = tokens[idx].title ? (' title="' + escapeHtml(tokens[idx].title) + '"') : '';
var target = tokens[idx].target ? (' target="' + escapeHtml(tokens[idx].target) + '"') : ''; var target = tokens[idx].target ? (' target="' + escapeHtml(tokens[idx].target) + '"') : '';
return '<a href="' + escapeHtml(tokens[idx].href) + '"' + title + target + '>'; return '<a href="' + escapeHtml(tokens[idx].href) + '"' + title + target + '>';
}; };
@ -112,7 +111,7 @@ rules.link_close = function (/* tokens, idx, options, env */) {
rules.image = function (tokens, idx, options, env, self) { rules.image = function (tokens, idx, options, env, self) {
var src = ' src="' + escapeHtml(tokens[idx].src) + '"'; var src = ' src="' + escapeHtml(tokens[idx].src) + '"';
var title = tokens[idx].title ? (' title="' + escapeHtml(replaceEntities(tokens[idx].title)) + '"') : ''; var title = tokens[idx].title ? (' title="' + escapeHtml(tokens[idx].title) + '"') : '';
var alt = ' alt="' + self.renderInlineAsText(tokens[idx].tokens, options, env) + '"'; var alt = ' alt="' + self.renderInlineAsText(tokens[idx].tokens, options, env) + '"';
var suffix = options.xhtmlOut ? ' /' : ''; var suffix = options.xhtmlOut ? ' /' : '';
return '<img' + src + alt + title + suffix + '>'; return '<img' + src + alt + title + suffix + '>';

24
test/fixtures/markdown-it/commonmark_extras.txt

@ -88,15 +88,27 @@ Autolinks do not allow escaping:
Escaping entities in links: Escaping entities in links:
. .
[](<&quot;>) [](<&quot;> "&amp;&ouml;")
[](<\&quot;>) [](<\&quot;> "\&amp;\&ouml;")
[](<\\&quot;>) [](<\\&quot;> "\\&quot;\\&ouml;")
. .
<p><a href="%22"></a></p> <p><a href="%22" title="&amp;ö"></a></p>
<p><a href="&amp;quot;"></a></p> <p><a href="&amp;quot;" title="&amp;amp;&amp;ouml;"></a></p>
<p><a href="%5C%22"></a></p> <p><a href="%5C%22" title="\&quot;\ö"></a></p>
.
Checking combination of replaceEntities and unescapeMd:
.
~~~ &amp;\&amp;\\&amp;
just a funny little fence
~~~
.
<pre><code class="&amp;&amp;amp;\&amp;">just a funny little fence
</code></pre>
. .

Loading…
Cancel
Save