Browse Source

Fix: "\*" isn't a part of a emphasis marker sequence

pull/14/head
Alex Kocharin 10 years ago
parent
commit
3afaf55344
  1. 34
      lib/lexer_inline/emphasis.js

34
lib/lexer_inline/emphasis.js

@ -9,8 +9,11 @@ function isAlphaNum(code) {
(code >= 0x61 /* a */ && code <= 0x7A /* z */); (code >= 0x61 /* a */ && code <= 0x7A /* z */);
} }
// returns the amount of markers (1, 2, 3), or -1 on failure; // returns the amount of markers (1, 2, 3, 4+), or -1 on failure;
// "start" should point at a valid marker // "start" should point at a valid marker
//
// note: in case if 4+ markers it is still not a valid emphasis,
// should be treated as a special case
function parseStart(state, start) { function parseStart(state, start) {
var pos = start, lastChar, count, var pos = start, lastChar, count,
max = Math.min(state.posMax, pos + 4), max = Math.min(state.posMax, pos + 4),
@ -35,7 +38,7 @@ function parseStart(state, start) {
if (count >= 4) { if (count >= 4) {
// check condition 1 // check condition 1
// sequence of four or more unescaped markers can't start an emphasis // sequence of four or more unescaped markers can't start an emphasis
return -1; return count;
} }
// check condition 2, marker followed by whitespace // check condition 2, marker followed by whitespace
@ -50,8 +53,11 @@ function parseStart(state, start) {
return count; return count;
} }
// returns the amount of markers (1, 2, 3), or -1 on failure; // returns the amount of markers (1, 2, 3, 4+), or -1 on failure;
// "start" should point at a valid marker // "start" should point at a valid marker
//
// note: in case if 4+ markers it is still not a valid emphasis,
// should be treated as a special case
function parseEnd(state, start) { function parseEnd(state, start) {
var pos = start, lastChar, count, var pos = start, lastChar, count,
max = Math.min(state.posMax, pos + 4), max = Math.min(state.posMax, pos + 4),
@ -59,8 +65,6 @@ function parseEnd(state, start) {
lastChar = state.pending.length !== 0 ? state.pending.charCodeAt(state.pending.length - 1) : -1; lastChar = state.pending.length !== 0 ? state.pending.charCodeAt(state.pending.length - 1) : -1;
if (lastChar === marker) { return -1; }
while (pos < max && state.src.charCodeAt(pos) === marker) { pos++; } while (pos < max && state.src.charCodeAt(pos) === marker) { pos++; }
count = pos - start; count = pos - start;
@ -74,7 +78,7 @@ function parseEnd(state, start) {
if (count >= 4) { if (count >= 4) {
// check condition 1 // check condition 1
// sequence of four or more unescaped markers can't start an emphasis // sequence of four or more unescaped markers can't start an emphasis
return -1; return count;
} }
// check condition 2, marker preceded by whitespace // check condition 2, marker preceded by whitespace
@ -104,12 +108,18 @@ module.exports = function emphasis(state/*, silent*/) {
breakOutOfOuterLoop, breakOutOfOuterLoop,
max = state.posMax, max = state.posMax,
start = state.pos, start = state.pos,
haveLiteralAsterisk,
marker = state.src.charCodeAt(start); marker = state.src.charCodeAt(start);
if (marker !== 0x5F/* _ */ && marker !== 0x2A /* * */) { return false; } if (marker !== 0x5F/* _ */ && marker !== 0x2A /* * */) { return false; }
startCount = parseStart(state, start); startCount = parseStart(state, start);
if (startCount < 0) { return false; } if (startCount < 0) { return false; }
if (startCount >= 4) {
state.pos += startCount;
state.pending += state.src.slice(start, startCount);
return true;
}
oldLength = state.tokens.length; oldLength = state.tokens.length;
oldPending = state.pending; oldPending = state.pending;
@ -120,9 +130,9 @@ module.exports = function emphasis(state/*, silent*/) {
len = rules.length; len = rules.length;
while (state.pos < max) { while (state.pos < max) {
if (state.src.charCodeAt(state.pos) === marker) { if (state.src.charCodeAt(state.pos) === marker && !haveLiteralAsterisk) {
count = parseEnd(state, state.pos); count = parseEnd(state, state.pos);
if (count >= 1) { if (count >= 1 && count < 4) {
oldCount = stack.pop(); oldCount = stack.pop();
newCount = count; newCount = count;
@ -176,7 +186,13 @@ module.exports = function emphasis(state/*, silent*/) {
if (ok) { break; } if (ok) { break; }
} }
if (!ok) { state.pending += state.src[state.pos++]; } if (ok) {
haveLiteralAsterisk = false;
} else {
haveLiteralAsterisk = state.src.charCodeAt(state.pos) === marker;
state.pending += state.src[state.pos];
state.pos++;
}
} }
// restore old state // restore old state

Loading…
Cancel
Save