diff --git a/js/moz-parser.js b/js/moz-parser.js index ca6d6f9b..7d8151bc 100644 --- a/js/moz-parser.js +++ b/js/moz-parser.js @@ -7,19 +7,6 @@ var mozParser = (() => { const propertyToCss = {urls: 'url', urlPrefixes: 'url-prefix', domains: 'domain', regexps: 'regexp'}; const CssToProperty = {'url': 'urls', 'url-prefix': 'urlPrefixes', 'domain': 'domains', 'regexp': 'regexps'}; - function backtrackTo(parser, tokenType, startEnd) { - const tokens = parser._tokenStream._lt; - for (let i = parser._tokenStream._ltIndex - 1; i >= 0; --i) { - if (tokens[i].type === tokenType) { - return {line: tokens[i][startEnd + 'Line'], col: tokens[i][startEnd + 'Col']}; - } - } - } - - function trimNewLines(s) { - return s.replace(/^[\s\n]+/, '').replace(/[\s\n]+$/, ''); - } - function parseMozFormat(mozStyle) { return new Promise((resolve, reject) => { const parser = new parserlib.css.Parser(); @@ -28,15 +15,21 @@ var mozParser = (() => { const errors = []; const sections = []; - parser.addListener('startdocument', function (e) { + parser.addListener('startdocument', e => { const lastSection = sectionStack[sectionStack.length - 1]; - let outerText = getRange(lastSection.start, (--e.col, e)); + let outerText = getRange(lastSection.start, {line: e.line, col: e.col - 1}); const gapComment = outerText.match(/(\/\*[\s\S]*?\*\/)[\s\n]*$/); - const section = {code: '', start: backtrackTo(this, parserlib.css.Tokens.LBRACE, 'end')}; + const section = { + code: '', + start: { + line: parser._tokenStream._token.endLine, + col: parser._tokenStream._token.endCol, + }, + }; // move last comment before @-moz-document inside the section if (gapComment && !gapComment[1].match(/\/\*\s*AGENT_SHEET\s*\*\//)) { section.code = gapComment[1] + '\n'; - outerText = trimNewLines(outerText.substring(0, gapComment.index)); + outerText = outerText.substring(0, gapComment.index).trim(); } if (outerText.trim()) { lastSection.code = outerText;