Skip to content

Commit

Permalink
Improve code coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
rlidwka committed Dec 31, 2015
1 parent 94396e6 commit 351544a
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 24 deletions.
46 changes: 23 additions & 23 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,27 +67,26 @@ module.exports = function sub_plugin(md) {
blockTokens = state.tokens;

if (!state.env.abbreviations) { return; }
if (!state.env.abbrRegExp) {
state.env.abbrRegExpSimple = new RegExp('(?:' +
Object.keys(state.env.abbreviations).map(function (x) {
return x.substr(1);
}).sort(function (a, b) {
return b.length - a.length;
}).map(escapeRE).join('|') + ')');

regText = '(^|' + UNICODE_PUNCT_RE + '|' + UNICODE_SPACE_RE +
'|[' + OTHER_CHARS.split('').map(escapeRE).join('') + '])'
+ '(' + Object.keys(state.env.abbreviations).map(function (x) {
return x.substr(1);
}).sort(function (a, b) {
return b.length - a.length;
}).map(escapeRE).join('|') + ')'
+ '($|' + UNICODE_PUNCT_RE + '|' + UNICODE_SPACE_RE +
'|[' + OTHER_CHARS.split('').map(escapeRE).join('') + '])';
state.env.abbrRegExp = new RegExp(regText, 'g');
}
regSimple = state.env.abbrRegExpSimple;
reg = state.env.abbrRegExp;

regSimple = new RegExp('(?:' +
Object.keys(state.env.abbreviations).map(function (x) {
return x.substr(1);
}).sort(function (a, b) {
return b.length - a.length;
}).map(escapeRE).join('|') +
')');

regText = '(^|' + UNICODE_PUNCT_RE + '|' + UNICODE_SPACE_RE +
'|[' + OTHER_CHARS.split('').map(escapeRE).join('') + '])'
+ '(' + Object.keys(state.env.abbreviations).map(function (x) {
return x.substr(1);
}).sort(function (a, b) {
return b.length - a.length;
}).map(escapeRE).join('|') + ')'
+ '($|' + UNICODE_PUNCT_RE + '|' + UNICODE_SPACE_RE +
'|[' + OTHER_CHARS.split('').map(escapeRE).join('') + '])';

reg = new RegExp(regText, 'g');

for (j = 0, l = blockTokens.length; j < l; j++) {
if (blockTokens[j].type !== 'inline') { continue; }
Expand All @@ -108,7 +107,7 @@ module.exports = function sub_plugin(md) {
if (!regSimple.test(text)) { continue; }

while ((m = reg.exec(text))) {
if (reg.lastIndex > pos) {
if (m.index > 0 || m[1].length > 0) {
token = new state.Token('text', '', 0);
token.content = text.slice(pos, m.index + m[1].length);
nodes.push(token);
Expand All @@ -125,7 +124,8 @@ module.exports = function sub_plugin(md) {
token = new state.Token('abbr_close', 'abbr', -1);
nodes.push(token);

pos = reg.lastIndex - m[3].length;
reg.lastIndex -= m[3].length;
pos = reg.lastIndex;
}

if (!nodes.length) { continue; }
Expand Down
49 changes: 48 additions & 1 deletion test/fixtures/abbr.txt
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,17 @@ foobar
.


Prefer earlier abbr definitions
.
foo

*[foo]: bar
*[foo]: baz
.
<p><abbr title="bar">foo</abbr></p>
.


Interaction with linkifier:
.
http:https://example.com/foo/
Expand All @@ -89,6 +100,17 @@ http:https://example.com/foo/
.


Punctuation as a part of abbr
.
"foo" "bar"

*["foo"]: 123
*["bar"]: 456
.
<p><abbr title="123">&quot;foo&quot;</abbr> <abbr title="456">&quot;bar&quot;</abbr></p>
.


Security 1
.
*[__proto__]: blah
Expand All @@ -99,11 +121,36 @@ __proto__ \_\_proto\_\_
.


Security 1
Security 2
.
*[hasOwnProperty]: blah

hasOwnProperty
.
<p><abbr title="blah">hasOwnProperty</abbr></p>
.


Coverage 1
.
*[

*[test

*<test>
.
<p>*[</p>
<p>*[test</p>
<p>*&lt;test&gt;</p>
.


Coverage 2
.
[] \[\]

*[[]]: test
.
<p>[] []</p>
<p>*[[]]: test</p>
.

0 comments on commit 351544a

Please sign in to comment.