2021-06-27 23:21:27 +00:00
|
|
|
/*
|
2022-02-03 01:43:10 +00:00
|
|
|
* Copyright (C) 2021-2022 Yomichan Authors
|
2021-06-27 23:21:27 +00:00
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This class is used to apply CSS styles to elements using a consistent method
|
|
|
|
* that is the same across different browsers.
|
|
|
|
*/
|
|
|
|
class CssStyleApplier {
|
2022-05-14 22:13:04 +00:00
|
|
|
/**
|
2022-05-18 23:40:47 +00:00
|
|
|
* A CSS rule.
|
2022-05-14 22:13:04 +00:00
|
|
|
* @typedef {object} CssRule
|
|
|
|
* @property {string} selectors A CSS selector string representing one or more selectors.
|
2022-05-18 23:40:47 +00:00
|
|
|
* @property {CssDeclaration[]} styles A list of CSS property and value pairs.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A single CSS property declaration.
|
|
|
|
* @typedef {object} CssDeclaration
|
|
|
|
* @property {string} property A CSS property's name, using kebab-case.
|
|
|
|
* @property {string} value The property's value.
|
2022-05-14 22:13:04 +00:00
|
|
|
*/
|
|
|
|
|
2021-06-27 23:21:27 +00:00
|
|
|
/**
|
|
|
|
* Creates a new instance of the class.
|
2022-05-20 14:28:38 +00:00
|
|
|
* @param {string} styleDataUrl The local URL to the JSON file continaing the style rules.
|
2021-06-27 23:21:27 +00:00
|
|
|
* The style rules should be of the format:
|
2022-05-20 14:28:38 +00:00
|
|
|
* ```
|
2021-06-27 23:21:27 +00:00
|
|
|
* [
|
|
|
|
* {
|
|
|
|
* selectors: [(selector:string)...],
|
|
|
|
* styles: [
|
|
|
|
* [(property:string), (value:string)]...
|
|
|
|
* ]
|
|
|
|
* }...
|
|
|
|
* ]
|
2022-05-20 14:28:38 +00:00
|
|
|
* ```
|
2021-06-27 23:21:27 +00:00
|
|
|
*/
|
|
|
|
constructor(styleDataUrl) {
|
|
|
|
this._styleDataUrl = styleDataUrl;
|
|
|
|
this._styleData = [];
|
|
|
|
this._cachedRules = new Map();
|
2022-05-14 22:13:04 +00:00
|
|
|
// eslint-disable-next-line no-control-regex
|
|
|
|
this._patternHtmlWhitespace = /[\t\r\n\x0C ]+/g;
|
|
|
|
this._patternClassNameCharacter = /[0-9a-zA-Z-_]/;
|
2021-06-27 23:21:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Loads the data file for use.
|
|
|
|
*/
|
|
|
|
async prepare() {
|
2022-05-18 23:40:47 +00:00
|
|
|
let rawData;
|
2021-08-12 00:14:19 +00:00
|
|
|
try {
|
2022-05-18 23:40:47 +00:00
|
|
|
rawData = await this._fetchJsonAsset(this._styleDataUrl);
|
2021-08-12 00:14:19 +00:00
|
|
|
} catch (e) {
|
|
|
|
console.error(e);
|
|
|
|
}
|
2022-05-18 23:40:47 +00:00
|
|
|
const styleData = this._styleData;
|
|
|
|
styleData.length = 0;
|
|
|
|
for (const {selectors, styles} of rawData) {
|
|
|
|
const selectors2 = selectors.join(',');
|
|
|
|
const styles2 = [];
|
|
|
|
for (const [property, value] of styles) {
|
|
|
|
styles2.push({property, value});
|
|
|
|
}
|
|
|
|
styleData.push({
|
|
|
|
selectors: selectors2,
|
|
|
|
styles: styles2
|
|
|
|
});
|
2021-08-12 00:14:19 +00:00
|
|
|
}
|
2021-06-27 23:21:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Applies CSS styles directly to the "style" attribute using the "class" attribute.
|
|
|
|
* This only works for elements with a single class.
|
2022-05-20 14:28:38 +00:00
|
|
|
* @param {Iterable<HTMLElement>} elements An iterable collection of HTMLElement objects.
|
2021-06-27 23:21:27 +00:00
|
|
|
*/
|
|
|
|
applyClassStyles(elements) {
|
|
|
|
const elementStyles = [];
|
|
|
|
for (const element of elements) {
|
2021-07-18 17:43:11 +00:00
|
|
|
const className = element.getAttribute('class');
|
2021-06-27 23:21:27 +00:00
|
|
|
if (className.length === 0) { continue; }
|
|
|
|
let cssTextNew = '';
|
2022-05-18 23:40:47 +00:00
|
|
|
for (const {selectors, styles} of this._getCandidateCssRulesForClass(className)) {
|
|
|
|
if (!element.matches(selectors)) { continue; }
|
2021-06-27 23:21:27 +00:00
|
|
|
cssTextNew += this._getCssText(styles);
|
|
|
|
}
|
|
|
|
cssTextNew += element.style.cssText;
|
|
|
|
elementStyles.push({element, style: cssTextNew});
|
|
|
|
}
|
|
|
|
for (const {element, style} of elementStyles) {
|
|
|
|
element.removeAttribute('class');
|
2021-07-02 22:28:29 +00:00
|
|
|
if (style.length > 0) {
|
|
|
|
element.setAttribute('style', style);
|
|
|
|
} else {
|
|
|
|
element.removeAttribute('style');
|
|
|
|
}
|
|
|
|
}
|
2021-06-27 23:21:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Private
|
|
|
|
|
2022-05-18 23:40:47 +00:00
|
|
|
/**
|
|
|
|
* Fetches and parses a JSON file.
|
|
|
|
* @param {string} url The URL to the file.
|
|
|
|
* @returns {Promise<*>} A JSON object.
|
|
|
|
* @throws {Error} An error is thrown if the fetch fails.
|
|
|
|
*/
|
2021-06-27 23:21:27 +00:00
|
|
|
async _fetchJsonAsset(url) {
|
2021-07-03 02:12:02 +00:00
|
|
|
const response = await fetch(url, {
|
2021-06-27 23:21:27 +00:00
|
|
|
method: 'GET',
|
|
|
|
mode: 'no-cors',
|
|
|
|
cache: 'default',
|
|
|
|
credentials: 'omit',
|
|
|
|
redirect: 'follow',
|
|
|
|
referrerPolicy: 'no-referrer'
|
|
|
|
});
|
|
|
|
if (!response.ok) {
|
|
|
|
throw new Error(`Failed to fetch ${url}: ${response.status}`);
|
|
|
|
}
|
|
|
|
return await response.json();
|
|
|
|
}
|
|
|
|
|
2022-05-14 22:13:04 +00:00
|
|
|
/**
|
|
|
|
* Gets an array of candidate CSS rules which might match a specific class.
|
|
|
|
* @param {string} className A whitespace-separated list of classes.
|
|
|
|
* @returns {CssRule[]} An array of candidate CSS rules.
|
|
|
|
*/
|
|
|
|
_getCandidateCssRulesForClass(className) {
|
2021-06-27 23:21:27 +00:00
|
|
|
let rules = this._cachedRules.get(className);
|
|
|
|
if (typeof rules !== 'undefined') { return rules; }
|
|
|
|
|
|
|
|
rules = [];
|
|
|
|
this._cachedRules.set(className, rules);
|
|
|
|
|
2022-05-14 22:13:04 +00:00
|
|
|
const classList = this._getTokens(className);
|
2021-06-27 23:21:27 +00:00
|
|
|
for (const {selectors, styles} of this._styleData) {
|
2022-05-18 23:40:47 +00:00
|
|
|
if (!this._selectorMightMatch(selectors, classList)) { continue; }
|
|
|
|
rules.push({selectors, styles});
|
2021-06-27 23:21:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return rules;
|
|
|
|
}
|
|
|
|
|
2022-05-18 23:40:47 +00:00
|
|
|
/**
|
|
|
|
* Converts an array of CSS rules to a CSS string.
|
|
|
|
* @param {CssRule[]} styles An array of CSS rules.
|
|
|
|
* @returns {string} The CSS string.
|
|
|
|
*/
|
2021-06-27 23:21:27 +00:00
|
|
|
_getCssText(styles) {
|
|
|
|
let cssText = '';
|
2022-05-18 23:40:47 +00:00
|
|
|
for (const {property, value} of styles) {
|
2021-06-27 23:21:27 +00:00
|
|
|
cssText += `${property}:${value};`;
|
|
|
|
}
|
|
|
|
return cssText;
|
|
|
|
}
|
2022-05-14 22:13:04 +00:00
|
|
|
|
2022-05-18 23:40:47 +00:00
|
|
|
/**
|
|
|
|
* Checks whether or not a CSS string might match at least one class in a list.
|
|
|
|
* @param {string} selectors A CSS selector string.
|
|
|
|
* @param {string[]} classList An array of CSS classes.
|
|
|
|
* @returns {boolean} `true` if the selector string might match one of the classes in `classList`, false otherwise.
|
|
|
|
*/
|
|
|
|
_selectorMightMatch(selectors, classList) {
|
2022-05-14 22:13:04 +00:00
|
|
|
const pattern = this._patternClassNameCharacter;
|
|
|
|
for (const item of classList) {
|
|
|
|
const prefixedItem = `.${item}`;
|
|
|
|
let start = 0;
|
|
|
|
while (true) {
|
2022-05-18 23:40:47 +00:00
|
|
|
const index = selectors.indexOf(prefixedItem, start);
|
2022-05-14 22:13:04 +00:00
|
|
|
if (index < 0) { break; }
|
|
|
|
start = index + prefixedItem.length;
|
2022-05-18 23:40:47 +00:00
|
|
|
if (start >= selectors.length || !pattern.test(selectors[start])) { return true; }
|
2022-05-14 22:13:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-05-18 23:40:47 +00:00
|
|
|
/**
|
|
|
|
* Gets the whitespace-delimited tokens from a string.
|
|
|
|
* @param {string} tokenListString The string to parse.
|
|
|
|
* @returns {string[]} An array of tokens.
|
|
|
|
*/
|
2022-05-14 22:13:04 +00:00
|
|
|
_getTokens(tokenListString) {
|
|
|
|
let start = 0;
|
|
|
|
const pattern = this._patternHtmlWhitespace;
|
|
|
|
pattern.lastIndex = 0;
|
|
|
|
const result = [];
|
|
|
|
while (true) {
|
|
|
|
const match = pattern.exec(tokenListString);
|
|
|
|
const end = match === null ? tokenListString.length : match.index;
|
|
|
|
if (end > start) { result.push(tokenListString.substring(start, end)); }
|
|
|
|
if (match === null) { return result; }
|
|
|
|
start = end + match[0].length;
|
|
|
|
}
|
|
|
|
}
|
2021-06-27 23:21:27 +00:00
|
|
|
}
|