2017-08-05 16:49:25 +00:00
|
|
|
'use strict';
|
|
|
|
|
2021-01-01 14:27:58 +00:00
|
|
|
require([
|
|
|
|
'/js/csslint/parserlib', /* global parserlib */
|
|
|
|
'/js/sections-util', /* global MozDocMapper */
|
|
|
|
]);
|
|
|
|
|
|
|
|
/* exported extractSections */
|
2018-01-06 06:48:56 +00:00
|
|
|
/**
|
|
|
|
* 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.
|
2021-01-01 14:27:58 +00:00
|
|
|
* @param {Object} _
|
|
|
|
* @param {string} _.code
|
|
|
|
* @param {boolean} [_.fast] - uses topDocOnly option to extract sections as text
|
|
|
|
* @param {number} [_.styleId] - used to preserve parserCache on subsequent runs over the same style
|
2018-01-06 06:48:56 +00:00
|
|
|
* @returns {{sections: Array, errors: Array}}
|
2021-01-01 14:27:58 +00:00
|
|
|
* @property {?number} lastStyleId
|
2018-01-06 06:48:56 +00:00
|
|
|
*/
|
2021-01-01 14:27:58 +00:00
|
|
|
function extractSections({code, styleId, fast = true}) {
|
2018-01-11 12:05:38 +00:00
|
|
|
const hasSingleEscapes = /([^\\]|^)\\([^\\]|$)/;
|
2021-01-01 14:27:58 +00:00
|
|
|
const parser = new parserlib.css.Parser({
|
|
|
|
starHack: true,
|
|
|
|
skipValidation: true,
|
|
|
|
topDocOnly: fast,
|
|
|
|
});
|
2017-12-26 20:39:52 +00:00
|
|
|
const sectionStack = [{code: '', start: 0}];
|
|
|
|
const errors = [];
|
|
|
|
const sections = [];
|
2020-11-24 17:53:11 +00:00
|
|
|
const mozStyle = code.replace(/\r\n?/g, '\n'); // same as parserlib.StringReader
|
2017-08-05 16:49:25 +00:00
|
|
|
|
2017-12-26 20:39:52 +00:00
|
|
|
parser.addListener('startdocument', e => {
|
|
|
|
const lastSection = sectionStack[sectionStack.length - 1];
|
|
|
|
let outerText = mozStyle.slice(lastSection.start, e.offset);
|
|
|
|
const lastCmt = getLastComment(outerText);
|
|
|
|
const section = {
|
|
|
|
code: '',
|
|
|
|
start: parser._tokenStream._token.offset + 1,
|
|
|
|
};
|
|
|
|
// move last comment before @-moz-document inside the section
|
|
|
|
if (!lastCmt.includes('AGENT_SHEET') &&
|
2018-02-28 02:15:28 +00:00
|
|
|
!/==userstyle==/i.test(lastCmt)) {
|
2017-12-26 20:39:52 +00:00
|
|
|
if (lastCmt) {
|
|
|
|
section.code = lastCmt + '\n';
|
|
|
|
outerText = outerText.slice(0, -lastCmt.length);
|
|
|
|
}
|
|
|
|
outerText = outerText.match(/^\s*/)[0] + outerText.trim();
|
|
|
|
}
|
|
|
|
if (outerText.trim()) {
|
|
|
|
lastSection.code = outerText;
|
|
|
|
doAddSection(lastSection);
|
|
|
|
lastSection.code = '';
|
|
|
|
}
|
|
|
|
for (const {name, expr, uri} of e.functions) {
|
2021-01-01 14:27:58 +00:00
|
|
|
const aType = MozDocMapper.FROM_CSS[name.toLowerCase()];
|
2018-01-11 12:05:38 +00:00
|
|
|
const p0 = expr && expr.parts[0];
|
|
|
|
if (p0 && aType === 'regexps') {
|
|
|
|
const s = p0.text;
|
|
|
|
if (hasSingleEscapes.test(p0.text)) {
|
2021-01-01 14:27:58 +00:00
|
|
|
const isQuoted = /^['"]/.test(s) && s.endsWith(s[0]);
|
2018-01-11 12:05:38 +00:00
|
|
|
p0.value = isQuoted ? s.slice(1, -1) : s;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
(section[aType] = section[aType] || []).push(uri || p0 && p0.value || '');
|
2017-12-26 20:39:52 +00:00
|
|
|
}
|
|
|
|
sectionStack.push(section);
|
|
|
|
});
|
2017-08-05 16:49:25 +00:00
|
|
|
|
2017-12-26 20:39:52 +00:00
|
|
|
parser.addListener('enddocument', e => {
|
|
|
|
const section = sectionStack.pop();
|
|
|
|
const lastSection = sectionStack[sectionStack.length - 1];
|
|
|
|
section.code += mozStyle.slice(section.start, e.offset);
|
|
|
|
lastSection.start = e.offset + 1;
|
|
|
|
doAddSection(section);
|
|
|
|
});
|
2017-08-05 16:49:25 +00:00
|
|
|
|
2017-12-26 20:39:52 +00:00
|
|
|
parser.addListener('endstylesheet', () => {
|
|
|
|
// add nonclosed outer sections (either broken or the last global one)
|
|
|
|
const lastSection = sectionStack[sectionStack.length - 1];
|
|
|
|
lastSection.code += mozStyle.slice(lastSection.start);
|
|
|
|
sectionStack.forEach(doAddSection);
|
|
|
|
});
|
2017-08-05 16:49:25 +00:00
|
|
|
|
2017-12-26 20:39:52 +00:00
|
|
|
parser.addListener('error', e => {
|
2021-01-01 14:27:58 +00:00
|
|
|
errors.push(e);
|
2017-12-26 20:39:52 +00:00
|
|
|
});
|
2017-08-05 16:49:25 +00:00
|
|
|
|
2018-01-30 15:52:38 +00:00
|
|
|
try {
|
|
|
|
parser.parse(mozStyle, {
|
2021-01-01 14:27:58 +00:00
|
|
|
reuseCache: !extractSections.lastStyleId || styleId === extractSections.lastStyleId,
|
2018-01-30 15:52:38 +00:00
|
|
|
});
|
|
|
|
} catch (e) {
|
2021-01-01 14:27:58 +00:00
|
|
|
errors.push(e);
|
2018-01-30 15:52:38 +00:00
|
|
|
}
|
2021-01-01 14:27:58 +00:00
|
|
|
for (const err of errors) {
|
|
|
|
for (const [k, v] of Object.entries(err)) {
|
|
|
|
if (typeof v === 'object') delete err[k];
|
|
|
|
}
|
|
|
|
err.message = `${err.line}:${err.col} ${err.message}`;
|
|
|
|
}
|
|
|
|
extractSections.lastStyleId = styleId;
|
|
|
|
|
2017-12-26 20:39:52 +00:00
|
|
|
return {sections, errors};
|
2017-08-05 16:49:25 +00:00
|
|
|
|
2017-12-26 20:39:52 +00:00
|
|
|
function doAddSection(section) {
|
|
|
|
section.code = section.code.trim();
|
|
|
|
// don't add empty sections
|
|
|
|
if (
|
|
|
|
!section.code &&
|
|
|
|
!section.urls &&
|
|
|
|
!section.urlPrefixes &&
|
|
|
|
!section.domains &&
|
|
|
|
!section.regexps
|
|
|
|
) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
/* ignore boilerplate NS */
|
|
|
|
if (section.code === '@namespace url(http://www.w3.org/1999/xhtml);') {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
sections.push(Object.assign({}, section));
|
|
|
|
}
|
2017-11-14 05:55:01 +00:00
|
|
|
|
2017-12-26 20:39:52 +00:00
|
|
|
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;
|
2017-11-14 05:55:01 +00:00
|
|
|
}
|
2017-12-26 20:39:52 +00:00
|
|
|
// stop if a non-whitespace precedes and return what we currently have
|
|
|
|
const tailEmpty = !text.substring(close + 2, open).trim();
|
|
|
|
if (!tailEmpty) {
|
|
|
|
break;
|
2017-11-23 09:01:42 +00:00
|
|
|
}
|
2017-12-26 20:39:52 +00:00
|
|
|
// find a closed preceding comment
|
|
|
|
const prevClose = text.lastIndexOf('*/', close - 2);
|
|
|
|
// then find the real start of current comment
|
|
|
|
// e.g. /* preceding */ /* current /* current /* current */
|
|
|
|
open = text.indexOf('/*', prevClose < 0 ? 0 : prevClose + 2);
|
2017-08-05 16:49:25 +00:00
|
|
|
}
|
2017-12-26 20:39:52 +00:00
|
|
|
return open ? text.slice(open) : text;
|
|
|
|
}
|
|
|
|
}
|