From b2a8559c95e979ad460dce23b17a2910a281745d Mon Sep 17 00:00:00 2001 From: scripthunter7 <57285466+scripthunter7@users.noreply.github.com> Date: Mon, 20 Feb 2023 09:02:53 +0100 Subject: [PATCH] Full CSSTree API export --- package.json | 1 + rollup.config.js | 14 +++++----- src/index.js | 40 +++++++++++++++++++++++++++++ src/syntax/index.js | 27 ++++--------------- src/version.js | 3 +++ test/syntax/-abp-has.test.js | 2 +- test/syntax/contains.test.js | 2 +- test/syntax/if-not.test.js | 2 +- test/syntax/matches-media.test.js | 2 +- test/syntax/min-text-length.test.js | 2 +- test/syntax/nth-ancestor.test.js | 2 +- test/syntax/style.test.js | 2 +- test/syntax/upward.test.js | 2 +- test/syntax/xpath.test.js | 2 +- yarn.lock | 9 ++++++- 15 files changed, 74 insertions(+), 38 deletions(-) create mode 100644 src/index.js create mode 100644 src/version.js diff --git a/package.json b/package.json index ca54521..4ac8212 100644 --- a/package.json +++ b/package.json @@ -42,6 +42,7 @@ "@rollup/plugin-alias": "^4.0.2", "@rollup/plugin-babel": "^6.0.3", "@rollup/plugin-commonjs": "^24.0.0", + "@rollup/plugin-json": "^6.0.0", "@rollup/plugin-node-resolve": "^15.0.1", "@rollup/plugin-terser": "^0.2.1", "husky": "^8.0.0", diff --git a/rollup.config.js b/rollup.config.js index 13667f6..635e107 100644 --- a/rollup.config.js +++ b/rollup.config.js @@ -4,10 +4,11 @@ import externals from "rollup-plugin-node-externals"; import alias from "@rollup/plugin-alias"; import { getBabelOutputPlugin } from "@rollup/plugin-babel"; import terser from "@rollup/plugin-terser"; +import json from "@rollup/plugin-json"; // CommonJS build const cjs = { - input: "./src/syntax/index.js", + input: "./src/index.js", output: [ { file: `./dist/ecsstree.cjs`, @@ -16,12 +17,12 @@ const cjs = { sourcemap: false, }, ], - plugins: [externals(), commonjs({ sourceMap: false }), resolve({ preferBuiltins: false })], + plugins: [json(), externals(), commonjs({ sourceMap: false }), resolve({ preferBuiltins: false })], }; // ECMAScript build const esm = { - input: "./src/syntax/index.js", + input: "./src/index.js", output: [ { file: `./dist/ecsstree.esm.js`, @@ -29,10 +30,11 @@ const esm = { sourcemap: false, }, ], - plugins: [externals(), commonjs({ sourceMap: false }), resolve({ preferBuiltins: false })], + plugins: [json(), externals(), commonjs({ sourceMap: false }), resolve({ preferBuiltins: false })], }; const browserPlugins = [ + json(), commonjs({ sourceMap: false }), resolve({ preferBuiltins: false, browser: true }), alias({ @@ -59,7 +61,7 @@ const browserPlugins = [ // Browser-friendly UMD build const umd = { - input: "./src/syntax/index.js", + input: "./src/index.js", output: [ { file: `./dist/ecsstree.umd.min.js`, @@ -73,7 +75,7 @@ const umd = { // Browser-friendly IIFE build const iife = { - input: "./src/syntax/index.js", + input: "./src/index.js", output: [ { file: `./dist/ecsstree.iife.min.js`, diff --git a/src/index.js b/src/index.js new file mode 100644 index 0000000..da1d05e --- /dev/null +++ b/src/index.js @@ -0,0 +1,40 @@ +import syntax from "./syntax/index.js"; + +// Fork API doesn't export everything, so we need to export the rest of +// the API manually. See the original source code: +// https://github.com/csstree/csstree/blob/master/lib/index.js + +export * from "./version.js"; + +export { + createSyntax, + List, + Lexer, + tokenTypes, + tokenNames, + TokenStream, + definitionSyntax, + clone, + ident, + string, + url, +} from "css-tree"; + +// Export the forked syntax (comes from the Fork API) +export const { + tokenize, + parse, + generate, + lexer, + createLexer, + + walk, + find, + findLast, + findAll, + + toPlainObject, + fromPlainObject, + + fork, +} = syntax; diff --git a/src/syntax/index.js b/src/syntax/index.js index ffa362e..9baeb53 100644 --- a/src/syntax/index.js +++ b/src/syntax/index.js @@ -7,7 +7,7 @@ * @see {@link https://github.com/gorhill/uBlock/wiki/Procedural-cosmetic-filters} */ -import { fork as originalFork, tokenize as originalTokenize, tokenTypes } from "css-tree"; +import { fork, tokenize, tokenTypes } from "css-tree"; import { CLOSING_PARENTHESIS, DOUBLE_QUOTE, ESCAPE, OPENING_PARENTHESIS, SPACE } from "../utils/constants.js"; const CONTAINS_PSEUDO_CLASSES = ["contains(", "-abp-contains(", "has-text("]; @@ -207,7 +207,7 @@ const extCssContains = { // Theoretically this "trick" doesn't cause problems, because we parsed the argument of the // contains() function as a Raw node, so we don't need to parse it again, but the parser will // continue its work from the correct position. - this.setSource(newSourceCode, originalTokenize); + this.setSource(newSourceCode, tokenize); // Restore the position within the token stream. while (this.tokenIndex < prevTokenIndex) { @@ -305,7 +305,7 @@ const xpath = { // Theoretically this "trick" doesn't cause problems, because we parsed the argument of the // xpath() function as a Raw node, so we don't need to parse it again, but the parser will // continue its work from the correct position. - this.setSource(newSourceCode, originalTokenize); + this.setSource(newSourceCode, tokenize); // Restore the position within the token stream. while (this.tokenIndex < prevTokenIndex) { @@ -322,7 +322,7 @@ const xpath = { /** * Extended CSS syntax for css-tree (forked from css-tree) */ -const extendedCss = originalFork({ +const extendedCssSyntax = fork({ pseudo: { "-abp-has": selectorList, "if-not": selector, @@ -338,21 +338,4 @@ const extendedCss = originalFork({ }, }); -// Export regular css-tree functions -export const { - tokenize, - parse, - generate, - lexer, - createLexer, - - walk, - find, - findLast, - findAll, - - toPlainObject, - fromPlainObject, - - fork, -} = extendedCss; +export default extendedCssSyntax; diff --git a/src/version.js b/src/version.js new file mode 100644 index 0000000..72e3692 --- /dev/null +++ b/src/version.js @@ -0,0 +1,3 @@ +import pkg from "../package.json"; + +export const { version } = pkg; diff --git a/test/syntax/-abp-has.test.js b/test/syntax/-abp-has.test.js index 4d1bd54..d985196 100644 --- a/test/syntax/-abp-has.test.js +++ b/test/syntax/-abp-has.test.js @@ -1,4 +1,4 @@ -import { parse, generate, toPlainObject } from "../../src/syntax"; +import { parse, generate, toPlainObject } from "../../src/index.js"; const parserConfig = { context: "selector", diff --git a/test/syntax/contains.test.js b/test/syntax/contains.test.js index 7a52cf0..956fcd8 100644 --- a/test/syntax/contains.test.js +++ b/test/syntax/contains.test.js @@ -1,6 +1,6 @@ // Tests for :contains(), :-abp-contains() and :has-text() pseudo-classes -import { parse, generate, toPlainObject } from "../../src/syntax"; +import { parse, generate, toPlainObject } from "../../src/index.js"; const parserConfig = { context: "selector", diff --git a/test/syntax/if-not.test.js b/test/syntax/if-not.test.js index ce52e5c..bdb3ded 100644 --- a/test/syntax/if-not.test.js +++ b/test/syntax/if-not.test.js @@ -1,4 +1,4 @@ -import { parse, generate, toPlainObject } from "../../src/syntax"; +import { parse, generate, toPlainObject } from "../../src/index.js"; const parserConfig = { context: "selector", diff --git a/test/syntax/matches-media.test.js b/test/syntax/matches-media.test.js index e833887..da35067 100644 --- a/test/syntax/matches-media.test.js +++ b/test/syntax/matches-media.test.js @@ -1,4 +1,4 @@ -import { parse, generate, toPlainObject } from "../../src/syntax"; +import { parse, generate, toPlainObject } from "../../src/index.js"; const parserConfig = { context: "selector", diff --git a/test/syntax/min-text-length.test.js b/test/syntax/min-text-length.test.js index 53fe326..b9bf8e2 100644 --- a/test/syntax/min-text-length.test.js +++ b/test/syntax/min-text-length.test.js @@ -1,4 +1,4 @@ -import { parse, generate, toPlainObject } from "../../src/syntax"; +import { parse, generate, toPlainObject } from "../../src/index.js"; const parserConfig = { context: "selector", diff --git a/test/syntax/nth-ancestor.test.js b/test/syntax/nth-ancestor.test.js index 25781d9..f34dbfe 100644 --- a/test/syntax/nth-ancestor.test.js +++ b/test/syntax/nth-ancestor.test.js @@ -1,4 +1,4 @@ -import { parse, generate, toPlainObject } from "../../src/syntax"; +import { parse, generate, toPlainObject } from "../../src/index.js"; const parserConfig = { context: "selector", diff --git a/test/syntax/style.test.js b/test/syntax/style.test.js index 9cfe564..2d3b245 100644 --- a/test/syntax/style.test.js +++ b/test/syntax/style.test.js @@ -1,4 +1,4 @@ -import { parse, generate, toPlainObject } from "../../src/syntax"; +import { parse, generate, toPlainObject } from "../../src/index.js"; const parserConfig = { context: "selector", diff --git a/test/syntax/upward.test.js b/test/syntax/upward.test.js index 904e58a..4988157 100644 --- a/test/syntax/upward.test.js +++ b/test/syntax/upward.test.js @@ -1,4 +1,4 @@ -import { parse, generate, toPlainObject } from "../../src/syntax"; +import { parse, generate, toPlainObject } from "../../src/index.js"; const parserConfig = { context: "selector", diff --git a/test/syntax/xpath.test.js b/test/syntax/xpath.test.js index 45c09eb..54ce2a2 100644 --- a/test/syntax/xpath.test.js +++ b/test/syntax/xpath.test.js @@ -1,4 +1,4 @@ -import { parse, generate, toPlainObject } from "../../src/syntax"; +import { parse, generate, toPlainObject } from "../../src/index.js"; const parserConfig = { context: "selector", diff --git a/yarn.lock b/yarn.lock index ba821bd..6ebbcd4 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1219,6 +1219,13 @@ is-reference "1.2.1" magic-string "^0.27.0" +"@rollup/plugin-json@^6.0.0": + version "6.0.0" + resolved "https://registry.yarnpkg.com/@rollup/plugin-json/-/plugin-json-6.0.0.tgz#199fea6670fd4dfb1f4932250569b14719db234a" + integrity sha512-i/4C5Jrdr1XUarRhVu27EEwjt4GObltD7c+MkCIpO2QIbojw8MUs+CCTqOphQi3Qtg1FLmYt+l+6YeoIf51J7w== + dependencies: + "@rollup/pluginutils" "^5.0.1" + "@rollup/plugin-node-resolve@^15.0.1": version "15.0.1" resolved "https://registry.yarnpkg.com/@rollup/plugin-node-resolve/-/plugin-node-resolve-15.0.1.tgz#72be449b8e06f6367168d5b3cd5e2802e0248971" @@ -1929,7 +1936,7 @@ human-signals@^2.1.0: resolved "https://registry.yarnpkg.com/human-signals/-/human-signals-2.1.0.tgz#dc91fcba42e4d06e4abaed33b3e7a3c02f514ea0" integrity sha512-B4FFZ6q/T2jhhksgkbEW3HBvWIfDW85snkQgawt07S7J5QXTk6BkNV+0yAeZrM5QpMAdYlocGoljn0sJ/WQkFw== -husky@^8.0.3: +husky@^8.0.0: version "8.0.3" resolved "https://registry.yarnpkg.com/husky/-/husky-8.0.3.tgz#4936d7212e46d1dea28fef29bb3a108872cd9184" integrity sha512-+dQSyqPh4x1hlO1swXBiNb2HzTDN1I2IGLQx1GrBuiqFJfoMrnZWwVmatvSiO+Iz8fBUnf+lekwNo4c2LlXItg==