Browse Source

Fix "too much recursion" error in links

pull/204/merge
Alex Kocharin 9 years ago
parent
commit
de4abb573c
  1. 18
      lib/parser_inline.js
  2. 16
      test/misc.js

18
lib/parser_inline.js

@ -69,7 +69,7 @@ function ParserInline() {
// returns `true` if any rule reported success // returns `true` if any rule reported success
// //
ParserInline.prototype.skipToken = function (state) { ParserInline.prototype.skipToken = function (state) {
var i, pos = state.pos, var ok, i, pos = state.pos,
rules = this.ruler.getRules(''), rules = this.ruler.getRules(''),
len = rules.length, len = rules.length,
maxNesting = state.md.options.maxNesting, maxNesting = state.md.options.maxNesting,
@ -81,17 +81,21 @@ ParserInline.prototype.skipToken = function (state) {
return; return;
} }
/*istanbul ignore else*/
if (state.level < maxNesting) { if (state.level < maxNesting) {
for (i = 0; i < len; i++) { for (i = 0; i < len; i++) {
if (rules[i](state, true)) { // Increment state.level and decrement it later to limit recursion.
cache[pos] = state.pos; // It's harmless to do here, because no tokens are created. But ideally,
return; // we'd need a separate private state variable for this purpose.
} //
state.level++;
ok = rules[i](state, true);
state.level--;
if (ok) { break; }
} }
} }
state.pos++; if (!ok) { state.pos++; }
cache[pos] = state.pos; cache[pos] = state.pos;
}; };

16
test/misc.js

@ -269,6 +269,22 @@ describe('maxNesting', function () {
); );
}); });
it('Inline parser should not nest above limit', function () {
var md = markdownit({ maxNesting: 1 });
assert.strictEqual(
md.render('[`foo`]()'),
'<p><a href="">`foo`</a></p>\n'
);
});
it('Inline nesting coverage', function () {
var md = markdownit({ maxNesting: 2 });
assert.strictEqual(
md.render('[[[[[[[[[[[[[[[[[[foo]()'),
'<p>[[[[[[[[[[[[[[[[[<a href="">foo</a></p>\n'
);
});
}); });

Loading…
Cancel
Save