ignore empty documents produced by stylus-lang bug

This commit is contained in:
tophf 2020-10-09 21:05:09 +03:00
parent 5501efb1be
commit d405bc64ae
4 changed files with 19 additions and 4 deletions

View File

@ -37,7 +37,7 @@ function compileUsercss(preprocessor, code, vars) {
const builder = getUsercssCompiler(preprocessor); const builder = getUsercssCompiler(preprocessor);
vars = simpleVars(vars); vars = simpleVars(vars);
return Promise.resolve(builder.preprocess ? builder.preprocess(code, vars) : code) return Promise.resolve(builder.preprocess ? builder.preprocess(code, vars) : code)
.then(code => parseMozFormat({code})) .then(code => parseMozFormat({code, emptyDocument: preprocessor === 'stylus'}))
.then(({sections, errors}) => { .then(({sections, errors}) => {
if (builder.postprocess) { if (builder.postprocess) {
builder.postprocess(sections, vars); builder.postprocess(sections, vars);

View File

@ -7,10 +7,12 @@
* Puts the global comments into the following section to minimize the amount of 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. * Doesn't move the comment with ==UserStyle== inside.
* @param {string} code * @param {string} code
* @param {boolean} emptyDocument - https://github.com/stylus/stylus/issues/2415,
* TODO: update stylus-lang and remove emptyDocument everywhere
* @param {number} styleId - used to preserve parserCache on subsequent runs over the same style * @param {number} styleId - used to preserve parserCache on subsequent runs over the same style
* @returns {{sections: Array, errors: Array}} * @returns {{sections: Array, errors: Array}}
*/ */
function parseMozFormat({code, styleId}) { function parseMozFormat({code, emptyDocument, styleId}) {
const CssToProperty = { const CssToProperty = {
'url': 'urls', 'url': 'urls',
'url-prefix': 'urlPrefixes', 'url-prefix': 'urlPrefixes',
@ -18,7 +20,7 @@ function parseMozFormat({code, styleId}) {
'regexp': 'regexps', 'regexp': 'regexps',
}; };
const hasSingleEscapes = /([^\\]|^)\\([^\\]|$)/; const hasSingleEscapes = /([^\\]|^)\\([^\\]|$)/;
const parser = new parserlib.css.Parser({starHack: true}); const parser = new parserlib.css.Parser({starHack: true, emptyDocument});
const sectionStack = [{code: '', start: 0}]; const sectionStack = [{code: '', start: 0}];
const errors = []; const errors = [];
const sections = []; const sections = [];
@ -70,6 +72,13 @@ function parseMozFormat({code, styleId}) {
doAddSection(section); doAddSection(section);
}); });
parser.addListener('emptydocument', e => {
const token = parser._tokenStream._token;
const section = sectionStack[sectionStack.length - 1];
section.code += mozStyle.slice(section.start, e.offset);
section.start = token.offset + token.value.length;
});
parser.addListener('endstylesheet', () => { parser.addListener('endstylesheet', () => {
// add nonclosed outer sections (either broken or the last global one) // add nonclosed outer sections (either broken or the last global one)
const lastSection = sectionStack[sectionStack.length - 1]; const lastSection = sectionStack[sectionStack.length - 1];

View File

@ -71,7 +71,7 @@ const usercss = (() => {
.then(({sections, errors}) => { .then(({sections, errors}) => {
if (!errors.length) errors = false; if (!errors.length) errors = false;
if (!sections.length || errors && !allowErrors) { if (!sections.length || errors && !allowErrors) {
throw errors; throw errors || 'Style does not contain any actual CSS to apply.';
} }
style.sections = sections; style.sections = sections;
return allowErrors ? {style, errors} : style; return allowErrors ? {style, errors} : style;

View File

@ -3865,6 +3865,7 @@ self.parserlib = (() => {
* @param {Boolean} [options.starHack] - allows IE6 star hack * @param {Boolean} [options.starHack] - allows IE6 star hack
* @param {Boolean} [options.underscoreHack] - interprets leading underscores as IE6-7 for known properties * @param {Boolean} [options.underscoreHack] - interprets leading underscores as IE6-7 for known properties
* @param {Boolean} [options.ieFilters] - accepts IE < 8 filters instead of throwing syntax errors * @param {Boolean} [options.ieFilters] - accepts IE < 8 filters instead of throwing syntax errors
* @param {Boolean} [options.emptyDocument] - accepts @document without {} block produced by stylus-lang
*/ */
constructor(options) { constructor(options) {
super(); super();
@ -4333,6 +4334,11 @@ self.parserlib = (() => {
functions.push(this._documentFunction()); functions.push(this._documentFunction());
} while (stream.match(Tokens.COMMA)); } while (stream.match(Tokens.COMMA));
this._ws();
if (this.options.emptyDocument && stream.peek() !== Tokens.LBRACE) {
this.fire({type: 'emptydocument', functions, prefix}, start);
return;
}
stream.mustMatch(Tokens.LBRACE); stream.mustMatch(Tokens.LBRACE);
this.fire({ this.fire({