Skip to content

Commit

Permalink
Full CSSTree API export
Browse files Browse the repository at this point in the history
  • Loading branch information
scripthunter7 committed Feb 20, 2023
1 parent f0aabb0 commit b2a8559
Show file tree
Hide file tree
Showing 15 changed files with 74 additions and 38 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
14 changes: 8 additions & 6 deletions rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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`,
Expand All @@ -16,23 +17,24 @@ 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`,
format: "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({
Expand All @@ -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`,
Expand All @@ -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`,
Expand Down
40 changes: 40 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -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;
27 changes: 5 additions & 22 deletions src/syntax/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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("];
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand All @@ -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,
Expand All @@ -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;
3 changes: 3 additions & 0 deletions src/version.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import pkg from "../package.json";

export const { version } = pkg;
2 changes: 1 addition & 1 deletion test/syntax/-abp-has.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { parse, generate, toPlainObject } from "../../src/syntax";
import { parse, generate, toPlainObject } from "../../src/index.js";

const parserConfig = {
context: "selector",
Expand Down
2 changes: 1 addition & 1 deletion test/syntax/contains.test.js
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
2 changes: 1 addition & 1 deletion test/syntax/if-not.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { parse, generate, toPlainObject } from "../../src/syntax";
import { parse, generate, toPlainObject } from "../../src/index.js";

const parserConfig = {
context: "selector",
Expand Down
2 changes: 1 addition & 1 deletion test/syntax/matches-media.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { parse, generate, toPlainObject } from "../../src/syntax";
import { parse, generate, toPlainObject } from "../../src/index.js";

const parserConfig = {
context: "selector",
Expand Down
2 changes: 1 addition & 1 deletion test/syntax/min-text-length.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { parse, generate, toPlainObject } from "../../src/syntax";
import { parse, generate, toPlainObject } from "../../src/index.js";

const parserConfig = {
context: "selector",
Expand Down
2 changes: 1 addition & 1 deletion test/syntax/nth-ancestor.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { parse, generate, toPlainObject } from "../../src/syntax";
import { parse, generate, toPlainObject } from "../../src/index.js";

const parserConfig = {
context: "selector",
Expand Down
2 changes: 1 addition & 1 deletion test/syntax/style.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { parse, generate, toPlainObject } from "../../src/syntax";
import { parse, generate, toPlainObject } from "../../src/index.js";

const parserConfig = {
context: "selector",
Expand Down
2 changes: 1 addition & 1 deletion test/syntax/upward.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { parse, generate, toPlainObject } from "../../src/syntax";
import { parse, generate, toPlainObject } from "../../src/index.js";

const parserConfig = {
context: "selector",
Expand Down
2 changes: 1 addition & 1 deletion test/syntax/xpath.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { parse, generate, toPlainObject } from "../../src/syntax";
import { parse, generate, toPlainObject } from "../../src/index.js";

const parserConfig = {
context: "selector",
Expand Down
9 changes: 8 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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==
Expand Down

0 comments on commit b2a8559

Please sign in to comment.