@adguard/aglint
Version:
Universal adblock filter list linter.
51 lines (48 loc) • 1.39 kB
JavaScript
/*
* AGLint v3.0.0 (build date: Wed, 21 May 2025 13:24:14 GMT)
* (c) 2025 AdGuard Software Ltd.
* Released under the MIT license
* https://github.com/AdguardTeam/AGLint#readme
*/
import { CssTreeParsingContext } from './css-tree-types.js';
/**
* Cache for CSS tree nodes. We share the cache between all rules to avoid parsing the same CSS multiple times.
* For errors, we cache the relative error position, so the error position can be calculated for any AGTree node.
*/
class CssCache {
/**
* Internal cache maps.
*/
maps;
/**
* Creates a new cache instance for CSS tree nodes.
*/
constructor() {
this.maps = Object.values(CssTreeParsingContext).reduce((acc, context) => {
acc[context] = new Map();
return acc;
}, {});
}
/**
* Gets entry from the cache.
*
* @param context The parsing context.
* @param css The CSS string.
*
* @returns The CSS tree node / error or undefined if CSS string is not cached.
*/
get(context, css) {
return this.maps[context].get(css);
}
/**
* Adds entry to the cache.
*
* @param context The parsing context.
* @param css The CSS string.
* @param node The CSS tree node / error.
*/
set(context, css, node) {
this.maps[context].set(css, node);
}
}
export { CssCache };