Skip to content

Commit

Permalink
Fix and tests for unclosed tag handling being an infinite loop. Fixes #7
Browse files Browse the repository at this point in the history
.
  • Loading branch information
developit committed Feb 27, 2017
1 parent f5f9941 commit 91028d3
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 4 deletions.
14 changes: 10 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,14 @@ export default function parse(md) {
chunk, prev, token, inner, t;

md = trim(md).replace(/^\[(.+?)\]:\s*(.+)$/gm, (s, name, url) => {
function flush() {
let str = '';
for (let i=context.length; i--; ) {
str += tag(context[i]);
}
return str;
}

links[name.toLowerCase()] = url;
return '';
});
Expand Down Expand Up @@ -89,7 +97,7 @@ export default function parse(md) {
// Links:
else if (token[9]) {
out = out.replace('<a>', `<a href="${encodeAttr(token[10] || links[prev.toLowerCase()])}">`);
chunk = '</a>';
chunk = flush() + '</a>';
}
else if (token[8]) {
chunk = '<a>';
Expand All @@ -113,9 +121,7 @@ export default function parse(md) {

out += md.substring(last);

while ((token=context.pop())) {
out += tag(context, token);
}
out += flush();

return trim(out);
}
17 changes: 17 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,4 +113,21 @@ describe('snarkdown()', () => {
expect(snarkdown('> To be or not to be')).to.equal('<blockquote>\nTo be or not to be\n</blockquote>');
});
});

describe('edge cases', () => {
it('should close unclosed tags', () => {
expect(snarkdown('*foo')).to.equal('<em>foo</em>');
expect(snarkdown('foo**')).to.equal('foo<strong></strong>');
expect(snarkdown('[some **bold text](#winning)')).to.equal('<a href="#winning">some <strong>bold text</strong></a>');
expect(snarkdown('`foo')).to.equal('`foo');
});

it('should not choke on single characters', () => {
expect(snarkdown('*')).to.equal('<em></em>');
expect(snarkdown('_')).to.equal('<em></em>');
expect(snarkdown('**')).to.equal('<strong></strong>');
expect(snarkdown('>')).to.equal('>');
expect(snarkdown('`')).to.equal('`');
});
});
});

0 comments on commit 91028d3

Please sign in to comment.