Skip to content

Commit

Permalink
Merge pull request #209 from Elberet/fix-bbcode-refs
Browse files Browse the repository at this point in the history
FIX: edge cases with bbcodes looking like reference definitions and empty links
  • Loading branch information
ashb committed Sep 16, 2014
2 parents 48d435e + 528c80f commit 9782714
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/dialects/gruber.js
Original file line number Diff line number Diff line change
Expand Up @@ -457,7 +457,7 @@ define(['../markdown_helpers', './dialect_helpers', '../parser'], function (Mark
},

referenceDefn: function referenceDefn( block, next) {
var re = /^\s*\[(.*?)\]:\s*(\S+)(?:\s+(?:(['"])(.*)\3|\((.*?)\)))?\n?/;
var re = /^\s*\[([^\[\]]+)\]:\s*(\S+)(?:\s+(?:(['"])(.*)\3|\((.*?)\)))?\n?/;
// interesting matches are [ , ref_id, url, , title, title ]

if ( !block.match(re) )
Expand Down Expand Up @@ -612,6 +612,9 @@ define(['../markdown_helpers', './dialect_helpers', '../parser'], function (Mark
return [ res[0] + 1, text.charAt(0) ].concat(res[2]);
}

// empty link
if ( res[0] === 1 ) { return [ 2, "[]" ]; }

var consumed = 1 + res[ 0 ],
children = res[ 1 ],
link,
Expand Down
9 changes: 9 additions & 0 deletions test/regressions.t.js
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,11 @@ test( "referenceDefn", function(t, md) {
t.equivalent(md.tree[ 1 ].references, { "id": { href: "example.com" } }, "reference extracted");
t.equivalent(next, [ mk_block("para") ], "paragraph put back into blocks");

// Check empty tags and non-tags are ignored
md.tree = ["markdown"];
t.type(rd.call( md, mk_block("[]: http://example.com"), []), "undefined", "no empty ref name");
t.type(rd.call( md, mk_block("[b]label[/b]: description"), []), "undefined", "no brackets in ref name");

});

test( "inline_br", function(t, md) {
Expand Down Expand Up @@ -533,6 +538,10 @@ test( "inline_link", function(t, md) {
],
"ref link II" );
/* jshint indent: 2 */

t.equivalent( md.processInline( "[]: text" ),
[ "[]: text" ],
"no empty links" );
});

test( "inline_autolink", function(t, md) {
Expand Down

0 comments on commit 9782714

Please sign in to comment.