From ecd010c020b33b8967f67288dfa14600effa7ad1 Mon Sep 17 00:00:00 2001 From: Alex Kocharin Date: Mon, 2 Mar 2015 13:16:41 +0300 Subject: [PATCH] Bring emphasis up to spec --- lib/rules_inline/emphasis.js | 23 ++++++++++------------- 1 file changed, 10 insertions(+), 13 deletions(-) diff --git a/lib/rules_inline/emphasis.js b/lib/rules_inline/emphasis.js index 0fbfcbbe2..71f3ab68a 100644 --- a/lib/rules_inline/emphasis.js +++ b/lib/rules_inline/emphasis.js @@ -8,12 +8,6 @@ var isPunctChar = require('../common/utils').isPunctChar; var isMdAsciiPunct = require('../common/utils').isMdAsciiPunct; -function isAlphaNum(code) { - return (code >= 0x30 /* 0 */ && code <= 0x39 /* 9 */) || - (code >= 0x41 /* A */ && code <= 0x5A /* Z */) || - (code >= 0x61 /* a */ && code <= 0x7A /* z */); -} - // parse sequence of emphasis markers, // "start" should point at a valid marker function scanDelims(state, start) { @@ -37,13 +31,15 @@ function scanDelims(state, start) { (isMdAsciiPunct(lastChar) || isPunctChar(String.fromCharCode(lastChar))); isNextPunctChar = nextChar >= 0 && (isMdAsciiPunct(nextChar) || isPunctChar(String.fromCharCode(nextChar))); - isLastWhiteSpace = lastChar >= 0 && isWhiteSpace(lastChar); - isNextWhiteSpace = nextChar >= 0 && isWhiteSpace(nextChar); + + // begin/end of the line counts as a whitespace too + isLastWhiteSpace = lastChar < 0 || isWhiteSpace(lastChar); + isNextWhiteSpace = nextChar < 0 || isWhiteSpace(nextChar); if (isNextWhiteSpace) { can_open = false; } else if (isNextPunctChar) { - if (!(isLastWhiteSpace || isLastPunctChar || lastChar === -1)) { + if (!(isLastWhiteSpace || isLastPunctChar)) { can_open = false; } } @@ -51,15 +47,16 @@ function scanDelims(state, start) { if (isLastWhiteSpace) { can_close = false; } else if (isLastPunctChar) { - if (!(isNextWhiteSpace || isNextPunctChar || nextChar === -1)) { + if (!(isNextWhiteSpace || isNextPunctChar)) { can_close = false; } } if (marker === 0x5F /* _ */) { - // check if we aren't inside the word - if (isAlphaNum(lastChar)) { can_open = false; } - if (isAlphaNum(nextChar)) { can_close = false; } + if (can_open && can_close) { + // "_" inside a word can neither open nor close an emphasis + can_open = can_close = false; + } } return {