From d97337ded7c061bd023ad2438dc347c52985b78a Mon Sep 17 00:00:00 2001 From: tophf Date: Thu, 23 Nov 2017 12:01:42 +0300 Subject: [PATCH] fix mozImport: properly discern comments outside of @-moz-document --- js/moz-parser.js | 42 +++++++++++++++++++++++++++++++----------- 1 file changed, 31 insertions(+), 11 deletions(-) diff --git a/js/moz-parser.js b/js/moz-parser.js index 7d8151bc..f47ef844 100644 --- a/js/moz-parser.js +++ b/js/moz-parser.js @@ -18,18 +18,15 @@ var mozParser = (() => { parser.addListener('startdocument', e => { const lastSection = sectionStack[sectionStack.length - 1]; let outerText = getRange(lastSection.start, {line: e.line, col: e.col - 1}); - const gapComment = outerText.match(/(\/\*[\s\S]*?\*\/)[\s\n]*$/); - const section = { - code: '', - start: { - line: parser._tokenStream._token.endLine, - col: parser._tokenStream._token.endCol, - }, - }; + const lastCmt = getLastComment(outerText); + const {endLine: line, endCol: col} = parser._tokenStream._token; + const section = {code: '', start: {line, col}}; // move last comment before @-moz-document inside the section - if (gapComment && !gapComment[1].match(/\/\*\s*AGENT_SHEET\s*\*\//)) { - section.code = gapComment[1] + '\n'; - outerText = outerText.substring(0, gapComment.index).trim(); + if (!/\/\*[\s\n]*AGENT_SHEET[\s\n]*\*\//.test(lastCmt)) { + section.code = lastCmt + '\n'; + const indent = outerText.match(/^\s*/)[0]; + outerText = outerText.slice(0, -lastCmt.length); + outerText = indent + outerText.trim(); } if (outerText.trim()) { lastSection.code = outerText; @@ -118,6 +115,29 @@ var mozParser = (() => { const first = s.charAt(0); return (first === '"' || first === "'") && s.endsWith(first) ? s.slice(1, -1) : s; } + + function getLastComment(text) { + let open = text.length; + let close; + while (open) { + // at this point we're guaranteed to be outside of a comment + close = text.lastIndexOf('*/', open - 2); + if (close < 0) { + break; + } + // stop if a non-whitespace precedes and return what we currently have + const tailEmpty = !text.substring(close + 2, open).trim(); + if (!tailEmpty) { + break; + } + // find a closed preceding comment + const prevClose = text.lastIndexOf('*/', close); + // then find the real start of current comment + // e.g. /* preceding */ /* current /* current /* current */ + open = text.indexOf('/*', prevClose < 0 ? 0 : prevClose + 2); + } + return text.substr(open); + } }); }