fix mozImport: properly discern comments outside of @-moz-document

This commit is contained in:
tophf 2017-11-23 12:01:42 +03:00
parent 4fc54450b7
commit d97337ded7

View File

@ -18,18 +18,15 @@ var mozParser = (() => {
parser.addListener('startdocument', e => { parser.addListener('startdocument', e => {
const lastSection = sectionStack[sectionStack.length - 1]; const lastSection = sectionStack[sectionStack.length - 1];
let outerText = getRange(lastSection.start, {line: e.line, col: e.col - 1}); let outerText = getRange(lastSection.start, {line: e.line, col: e.col - 1});
const gapComment = outerText.match(/(\/\*[\s\S]*?\*\/)[\s\n]*$/); const lastCmt = getLastComment(outerText);
const section = { const {endLine: line, endCol: col} = parser._tokenStream._token;
code: '', const section = {code: '', start: {line, col}};
start: {
line: parser._tokenStream._token.endLine,
col: parser._tokenStream._token.endCol,
},
};
// move last comment before @-moz-document inside the section // move last comment before @-moz-document inside the section
if (gapComment && !gapComment[1].match(/\/\*\s*AGENT_SHEET\s*\*\//)) { if (!/\/\*[\s\n]*AGENT_SHEET[\s\n]*\*\//.test(lastCmt)) {
section.code = gapComment[1] + '\n'; section.code = lastCmt + '\n';
outerText = outerText.substring(0, gapComment.index).trim(); const indent = outerText.match(/^\s*/)[0];
outerText = outerText.slice(0, -lastCmt.length);
outerText = indent + outerText.trim();
} }
if (outerText.trim()) { if (outerText.trim()) {
lastSection.code = outerText; lastSection.code = outerText;
@ -118,6 +115,29 @@ var mozParser = (() => {
const first = s.charAt(0); const first = s.charAt(0);
return (first === '"' || first === "'") && s.endsWith(first) ? s.slice(1, -1) : s; 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);
}
}); });
} }