reuse parserCache on subsequent saving of usercss

This commit is contained in:
tophf 2018-01-06 09:48:56 +03:00
parent d2cba96e10
commit 2036819442
4 changed files with 32 additions and 7 deletions

View File

@ -1,7 +1,15 @@
/* global parserlib */ /* global parserlib */
'use strict'; '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 = { const CssToProperty = {
'url': 'urls', 'url': 'urls',
'url-prefix': 'urlPrefixes', 'url-prefix': 'urlPrefixes',
@ -12,6 +20,7 @@ function parseMozFormat(mozStyle) {
const sectionStack = [{code: '', start: 0}]; const sectionStack = [{code: '', start: 0}];
const errors = []; const errors = [];
const sections = []; const sections = [];
const mozStyle = code;
parser.addListener('startdocument', e => { parser.addListener('startdocument', e => {
const lastSection = sectionStack[sectionStack.length - 1]; 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.+$/, '')}`); 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}; return {sections, errors};
function doAddSection(section) { function doAddSection(section) {

View File

@ -489,7 +489,11 @@ var usercss = (() => {
Promise.resolve( Promise.resolve(
builder.preprocess && builder.preprocess(sourceCode, sVars) || builder.preprocess && builder.preprocess(sourceCode, sVars) ||
sourceCode) 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, errors}) => sections.length && sections || Promise.reject(errors))
.then(sections => { .then(sections => {
style.sections = sections; style.sections = sections;

View File

@ -4,13 +4,16 @@
self.importScripts('./parserlib.js'); self.importScripts('./parserlib.js');
parserlib.css.Tokens[parserlib.css.Tokens.COMMENT].hide = false; 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 (action === 'parse') {
if (!self.parseMozFormat) self.importScripts('/js/moz-parser.js'); if (!self.parseMozFormat) self.importScripts('/js/moz-parser.js');
self.postMessage(parseMozFormat(code)); self.postMessage(parseMozFormat(data));
return; return;
} }
if (!self.CSSLint) self.importScripts('./csslint.js'); if (!self.CSSLint) self.importScripts('./csslint.js');
switch (action) { switch (action) {
@ -25,6 +28,7 @@ self.onmessage = ({data: {action = 'run', code, config}}) => {
return; return;
case 'run': { case 'run': {
const {code, config} = data;
const results = CSSLint.verify(code, config).messages const results = CSSLint.verify(code, config).messages
//.filter(m => !m.message.includes('/*[[') && !m.message.includes(']]*/')) //.filter(m => !m.message.includes('/*[[') && !m.message.includes(']]*/'))
.map(m => Object.assign(m, {rule: {id: m.rule.id}})); .map(m => Object.assign(m, {rule: {id: m.rule.id}}));

View File

@ -1467,10 +1467,15 @@ self.parserlib = (() => {
*/ */
function start(newParser) { function start(newParser) {
parser = newParser; parser = newParser;
if (!parser) return; if (!parser) {
data.clear();
stack.length = 0;
generationBase = performance.now();
return;
}
if (firstRun) firstRun = false; if (firstRun) firstRun = false;
stream = parser._tokenStream; stream = parser._tokenStream;
generation = generationBase = performance.now(); generation = performance.now();
trim(); trim();
} }