diff options
Diffstat (limited to 'js/lib/inlines.js')
-rw-r--r-- | js/lib/inlines.js | 46 |
1 files changed, 32 insertions, 14 deletions
diff --git a/js/lib/inlines.js b/js/lib/inlines.js index 72c4448..4d49861 100644 --- a/js/lib/inlines.js +++ b/js/lib/inlines.js @@ -65,6 +65,8 @@ var reEntityHere = new RegExp('^' + ENTITY, 'i'); var reEntityOrEscapedChar = new RegExp('\\\\' + ESCAPABLE + '|' + ENTITY, 'gi'); +var reBackslashOrAmp = /[\\&]/; + var reTicks = new RegExp('`+'); var reTicksHere = new RegExp('^`+'); @@ -75,6 +77,18 @@ var reAutolink = /^<(?:coap|doi|javascript|aaa|aaas|about|acap|cap|cid|crid|data var reSpnl = /^ *(?:\n *)?/; +var reWhitespaceChar = /^\s/; + +var reWhitespace = /\s+/g; + +var reFinalSpace = / *$/; + +var reInitialSpace = /^ */; + +var reAsciiAlnum = /[a-z0-9]/i; + +var reLinkLabel = /^\[(?:[^\\\[\]]|\\[\[\]]){0,1000}\]/; + // Matches a string of non-special characters. var reMain = /^[^\n`\[\]\\!<&*_]+/m; @@ -90,7 +104,11 @@ var unescapeChar = function(s) { // Replace entities and backslash escapes with literal characters. var unescapeString = function(s) { "use strict"; - return s.replace(reEntityOrEscapedChar, unescapeChar); + if (reBackslashOrAmp.test(s)) { + return s.replace(reEntityOrEscapedChar, unescapeChar); + } else { + return s; + } }; // Normalize reference label: collapse internal whitespace @@ -167,8 +185,7 @@ var parseBackticks = function(block) { node = new Node('Code'); node.literal = this.subject.slice(afterOpenTicks, this.pos - ticks.length) - .replace(/[ \n]+/g, ' ') - .trim(); + .trim().replace(reWhitespace, ' '); block.appendChild(node); return true; } @@ -270,17 +287,17 @@ var scanDelims = function(cc) { char_after = fromCodePoint(cc_after); } - var can_open = numdelims > 0 && !(/\s/.test(char_after)) && + var can_open = numdelims > 0 && !(reWhitespaceChar.test(char_after)) && !(rePunctuation.test(char_after) && !(/\s/.test(char_before)) && !(rePunctuation.test(char_before))); - var can_close = numdelims > 0 && !(/\s/.test(char_before)) && + var can_close = numdelims > 0 && !(reWhitespaceChar.test(char_before)) && !(rePunctuation.test(char_before) && - !(/\s/.test(char_after)) && + !(reWhitespaceChar.test(char_after)) && !(rePunctuation.test(char_after))); if (cc === C_UNDERSCORE) { - can_open = can_open && !((/[a-z0-9]/i).test(char_before)); - can_close = can_close && !((/[a-z0-9]/i).test(char_after)); + can_open = can_open && !((reAsciiAlnum).test(char_before)); + can_close = can_close && !((reAsciiAlnum).test(char_after)); } this.pos = startpos; return { numdelims: numdelims, @@ -463,7 +480,7 @@ var parseLinkDestination = function() { // Attempt to parse a link label, returning number of characters parsed. var parseLinkLabel = function() { "use strict"; - var m = this.match(/^\[(?:[^\\\[\]]|\\[\[\]]){0,1000}\]/); + var m = this.match(reLinkLabel); return m === null ? 0 : m.length; }; @@ -581,10 +598,11 @@ var parseCloseBracket = function(block) { ((dest = this.parseLinkDestination()) !== null) && this.spnl() && // make sure there's a space before the title: - (/^\s/.test(this.subject.charAt(this.pos - 1)) && + (reWhitespaceChar.test(this.subject.charAt(this.pos - 1)) && (title = this.parseLinkTitle() || '') || true) && this.spnl() && - this.match(/^\)/)) { + this.subject.charAt(this.pos) === ')') { + this.pos += 1; matched = true; } } else { @@ -691,15 +709,15 @@ var parseNewline = function(block) { // check previous node for trailing spaces var lastc = block.lastChild; if (lastc && lastc.t === 'Text') { - var sps = / *$/.exec(lastc.literal)[0].length; + var sps = reFinalSpace.exec(lastc.literal)[0].length; if (sps > 0) { - lastc.literal = lastc.literal.replace(/ *$/, ''); + lastc.literal = lastc.literal.replace(reFinalSpace, ''); } block.appendChild(new Node(sps >= 2 ? 'Hardbreak' : 'Softbreak')); } else { block.appendChild(new Node('Softbreak')); } - this.match(/^ */); // gobble leading spaces in next line + this.match(reInitialSpace); // gobble leading spaces in next line return true; }; |