From 20368194426c49a1ca723460b1daa0588bfcb151 Mon Sep 17 00:00:00 2001 From: tophf Date: Sat, 6 Jan 2018 09:48:56 +0300 Subject: [PATCH] reuse parserCache on subsequent saving of usercss --- js/moz-parser.js | 16 ++++++++++++++-- js/usercss.js | 6 +++++- vendor-overwrites/csslint/csslint-loader.js | 8 ++++++-- vendor-overwrites/csslint/parserlib.js | 9 +++++++-- 4 files changed, 32 insertions(+), 7 deletions(-) diff --git a/js/moz-parser.js b/js/moz-parser.js index 28b07819..7d52d495 100644 --- a/js/moz-parser.js +++ b/js/moz-parser.js @@ -1,7 +1,15 @@ /* global parserlib */ 'use strict'; -function parseMozFormat(mozStyle) { +/** + * Extracts @-moz-document blocks into sections and the code between them into global sections. + * Puts the global comments into the following section to minimize the amount of global sections. + * Doesn't move the comment with ==UserStyle== inside. + * @param {string} code + * @param {number} styleId - used to preserve parserCache on subsequent runs over the same style + * @returns {{sections: Array, errors: Array}} + */ +function parseMozFormat({code, styleId}) { const CssToProperty = { 'url': 'urls', 'url-prefix': 'urlPrefixes', @@ -12,6 +20,7 @@ function parseMozFormat(mozStyle) { const sectionStack = [{code: '', start: 0}]; const errors = []; const sections = []; + const mozStyle = code; parser.addListener('startdocument', e => { const lastSection = sectionStack[sectionStack.length - 1]; @@ -62,7 +71,10 @@ function parseMozFormat(mozStyle) { errors.push(`${e.line}:${e.col} ${e.message.replace(/ at line \d.+$/, '')}`); }); - parser.parse(mozStyle); + parser.parse(mozStyle, { + reuseCache: !parseMozFormat.styleId || styleId === parseMozFormat.styleId, + }); + parseMozFormat.styleId = styleId; return {sections, errors}; function doAddSection(section) { diff --git a/js/usercss.js b/js/usercss.js index 340136e1..59233dcc 100644 --- a/js/usercss.js +++ b/js/usercss.js @@ -489,7 +489,11 @@ var usercss = (() => { Promise.resolve( builder.preprocess && builder.preprocess(sourceCode, sVars) || sourceCode) - .then(mozStyle => invokeWorker({action: 'parse', code: mozStyle})) + .then(mozStyle => invokeWorker({ + action: 'parse', + styleId: style.id, + code: mozStyle, + })) .then(({sections, errors}) => sections.length && sections || Promise.reject(errors)) .then(sections => { style.sections = sections; diff --git a/vendor-overwrites/csslint/csslint-loader.js b/vendor-overwrites/csslint/csslint-loader.js index 89788180..54776d57 100644 --- a/vendor-overwrites/csslint/csslint-loader.js +++ b/vendor-overwrites/csslint/csslint-loader.js @@ -4,13 +4,16 @@ self.importScripts('./parserlib.js'); parserlib.css.Tokens[parserlib.css.Tokens.COMMENT].hide = false; -self.onmessage = ({data: {action = 'run', code, config}}) => { +self.onmessage = ({data}) => { + + const {action = 'run'} = data; if (action === 'parse') { if (!self.parseMozFormat) self.importScripts('/js/moz-parser.js'); - self.postMessage(parseMozFormat(code)); + self.postMessage(parseMozFormat(data)); return; } + if (!self.CSSLint) self.importScripts('./csslint.js'); switch (action) { @@ -25,6 +28,7 @@ self.onmessage = ({data: {action = 'run', code, config}}) => { return; case 'run': { + const {code, config} = data; const results = CSSLint.verify(code, config).messages //.filter(m => !m.message.includes('/*[[') && !m.message.includes(']]*/')) .map(m => Object.assign(m, {rule: {id: m.rule.id}})); diff --git a/vendor-overwrites/csslint/parserlib.js b/vendor-overwrites/csslint/parserlib.js index aa722c1f..51d1370e 100644 --- a/vendor-overwrites/csslint/parserlib.js +++ b/vendor-overwrites/csslint/parserlib.js @@ -1467,10 +1467,15 @@ self.parserlib = (() => { */ function start(newParser) { parser = newParser; - if (!parser) return; + if (!parser) { + data.clear(); + stack.length = 0; + generationBase = performance.now(); + return; + } if (firstRun) firstRun = false; stream = parser._tokenStream; - generation = generationBase = performance.now(); + generation = performance.now(); trim(); }