Skip to content

Commit 2974f5c

Browse files
authored
Merge pull request #20 from tbela99/version-0.2.0
- [x] cancellable parser promise using abortSignal - [x] node visitor (callback) : - [x] Declaration visitor - [x] selector visitor - [x] at-rule visitor - [x] support mixing units with calc() # shorthands - [x] transition - [x] list-style - [x] text-emphasis - [x] animation ### Minification - [x] parsing column combinator - [x] css selector level 4 attribute modifiers - [x] selector-4 parsing https://www.w3.org/TR/selectors-4/
2 parents 831fabb + c55e63b commit 2974f5c

File tree

144 files changed

+8852
-18319
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

144 files changed

+8852
-18319
lines changed

.gitattributes

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
/test/ export-ignore
22
/docs/ export-ignore
3+
/build.sh export-ignore
4+
/Writerside export-ignore
35
/benchmark
46
/tools/ export-ignore
57
/happydom.ts export-ignore

.github/workflows/node.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,9 @@ jobs:
1515
runs-on: ubuntu-latest
1616

1717
strategy:
18+
fail-fast: false
1819
matrix:
19-
node-version: [18.x, 20.x]
20+
node-version: [18.x, 20.x, 21.x]
2021
# See supported Node.js release schedule at https://nodejs.org/en/about/releases/
2122

2223
steps:
@@ -26,5 +27,6 @@ jobs:
2627
with:
2728
node-version: ${{ matrix.node-version }}
2829
- run: npm install
30+
- run: npx playwright install
2931
# - run: npm run build
3032
- run: npm test

.npmignore

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
/happydom.ts
22
/bunfig.toml
33
/test
4+
/build.sh
5+
/docs
46
/benchmark
57
/tools
68
/ROADMAP.md
@@ -13,4 +15,5 @@
1315
/node_modules
1416
/coverage
1517
/.github
16-
/.gitattributes
18+
/.gitattributes
19+
/Writerside

CHANGELOG.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,27 @@
11
# Changelog
22

3+
## v0.2.0
4+
5+
- [x] cancellable parser promise using abortSignal
6+
- [x] node visitor (callback) :
7+
- [x] Declaration visitor
8+
- [x] selector visitor
9+
- [x] at-rule visitor
10+
- [x] support mixing units with calc()
11+
12+
# shorthands
13+
14+
- [x] transition
15+
- [x] list-style
16+
- [x] text-emphasis
17+
- [x] animation
18+
19+
### Minification
20+
21+
- [x] parsing column combinator
22+
- [x] css selector level 4 attribute modifiers
23+
- [x] selector-4 parsing https://www.w3.org/TR/selectors-4/
24+
325
## v0.1.0
426

527
- [x] sourcemap generation

README.md

Lines changed: 201 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ $ npm install @tbela99/css-parser
1313
## Features
1414

1515
- fault-tolerant parser, will try to fix invalid tokens according to the CSS syntax module 3 recommendations.
16-
- efficient minification, see [benchmark](https://tbela99.github.io/css-parser/benchmark/index.html), see [benchmark](https://tbela99.github.io/css-parser/benchmark/index.html)
16+
- efficient minification, see [benchmark](https://tbela99.github.io/css-parser/benchmark/index.html)
1717
- automatically generate nested css rules
1818
- generate sourcemap
1919
- compute css shorthands. see the list below
@@ -64,6 +64,8 @@ Include ParseOptions and RenderOptions
6464
- inlineCssVariables: boolean, optional. replace css variables with their current value.
6565
- computeCalcExpression: boolean, optional. evaluate calc() expression
6666
- inlineCssVariables: boolean, optional. replace some css variables with their actual value. they must be declared once in the :root {} rule.
67+
- visitor: VisitorNodeMap, optional. node visitor used to transform the ast.
68+
- signal: AbortSignal, optional. abort parsing.
6769

6870
#### RenderOptions
6971

@@ -100,7 +102,7 @@ const {ast, errors, stats} = await parse(css);
100102
### Usage
101103

102104
```javascript
103-
doRender(ast, RenderOptions = {});
105+
render(ast, RenderOptions = {});
104106
```
105107

106108
### Example
@@ -416,6 +418,203 @@ result
416418

417419
- [x] flatten @import
418420

421+
## Node Transformation
422+
423+
Ast can be transformed using node visitors
424+
425+
### Exemple 1: Declaration
426+
427+
```typescript
428+
429+
import {AstDeclaration, ParserOptions} from "../src/@types";
430+
431+
const options: ParserOptions = {
432+
433+
visitor: {
434+
435+
Declaration: (node: AstDeclaration) => {
436+
437+
if (node.nam == '-webkit-transform') {
438+
439+
node.nam = 'transform'
440+
}
441+
}
442+
}
443+
}
444+
445+
const css = `
446+
447+
.foo {
448+
-webkit-transform: scale(calc(100 * 2/ 15));
449+
}
450+
`;
451+
452+
console.debug(await transform(css, options));
453+
454+
// .foo{transform:scale(calc(40/3))}
455+
```
456+
457+
### Exemple 2: Declaration
458+
459+
```typescript
460+
461+
import {AstDeclaration, LengthToken, ParserOptions} from "../src/@types";
462+
import {EnumToken, NodeType} from "../src/lib";
463+
import {transform} from "../src/node";
464+
465+
const options: ParserOptions = {
466+
467+
visitor: {
468+
469+
Declaration: {
470+
471+
// called only for height declaration
472+
height: (node: AstDeclaration): AstDeclaration[] => {
473+
474+
475+
return [
476+
node,
477+
{
478+
479+
typ: NodeType.DeclarationNodeType,
480+
nam: 'width',
481+
val: [
482+
<LengthToken>{
483+
typ: EnumToken.Length,
484+
val: '3',
485+
unit: 'px'
486+
}
487+
]
488+
}
489+
];
490+
}
491+
}
492+
}
493+
};
494+
495+
const css = `
496+
497+
.foo {
498+
height: calc(100px * 2/ 15);
499+
}
500+
`;
501+
502+
console.debug(await transform(css, options));
503+
504+
// .foo{height:calc(40px/3);width:3px}
505+
506+
```
507+
508+
### Exemple 3: At-Rule
509+
510+
```typescript
511+
512+
import {AstAtRule, ParserOptions} from "../src/@types";
513+
import {transform} from "../src/node";
514+
515+
516+
const options: ParserOptions = {
517+
518+
visitor: {
519+
520+
AtRule: (node: AstAtRule): AstAtRule => {
521+
522+
if (node.nam == 'media') {
523+
524+
return {...node, val: 'all'}
525+
}
526+
}
527+
}
528+
};
529+
530+
const css = `
531+
532+
@media screen {
533+
534+
.foo {
535+
536+
height: calc(100px * 2/ 15);
537+
}
538+
}
539+
`;
540+
541+
console.debug(await transform(css, options));
542+
543+
// .foo{height:calc(40px/3)}
544+
545+
```
546+
547+
### Exemple 4: At-Rule
548+
549+
```typescript
550+
551+
import {AstAtRule, ParserOptions} from "../src/@types";
552+
import {transform} from "../src/node";
553+
554+
const options: ParserOptions = {
555+
556+
visitor: {
557+
558+
AtRule: {
559+
560+
media: (node: AstAtRule): AstAtRule => {
561+
562+
return {...node, val: 'all'}
563+
}
564+
}
565+
}
566+
};
567+
568+
const css = `
569+
570+
@media screen {
571+
572+
.foo {
573+
574+
height: calc(100px * 2/ 15);
575+
}
576+
}
577+
`;
578+
579+
console.debug(await transform(css, options));
580+
581+
// .foo{height:calc(40px/3)}
582+
583+
```
584+
585+
### Exemple 5: Rule
586+
587+
```typescript
588+
589+
import {AstAtRule, ParserOptions} from "../src/@types";
590+
import {transform} from "../src/node";
591+
592+
const options: ParserOptions = {
593+
594+
visitor: {
595+
596+
597+
Rule (node: AstRule): AstRule {
598+
599+
return {...node, sel: '.foo,.bar,.fubar'};
600+
}
601+
}
602+
};
603+
604+
const css = `
605+
606+
.foo {
607+
608+
height: calc(100px * 2/ 15);
609+
}
610+
`;
611+
612+
console.debug(await transform(css, options));
613+
614+
// .foo,.bar,.fubar{height:calc(40px/3)}
615+
616+
```
617+
419618
---
420619

421620
Thanks to [Jetbrains](https://jetbrains.com) for sponsoring this project with a free license

Writerside/c.list

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE categories
3+
SYSTEM "https://resources.jetbrains.com/writerside/1.0/categories.dtd">
4+
<categories>
5+
<category id="wrs" name="Writerside documentation" order="1"/>
6+
</categories>

Writerside/cfg/buildprofiles.xml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<buildprofiles xsi:noNamespaceSchemaLocation="https://resources.jetbrains.com/writerside/1.0/build-profiles.xsd"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
4+
5+
<variables></variables>
6+
<build-profile instance="in">
7+
<variables>
8+
<noindex-content>false</noindex-content>
9+
</variables>
10+
</build-profile>
11+
12+
</buildprofiles>

Writerside/in.tree

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE instance-profile
3+
SYSTEM "https://resources.jetbrains.com/writerside/1.0/product-profile.dtd">
4+
5+
<instance-profile id="in"
6+
name="Instance Name" start-page="index.md">
7+
8+
<toc-element topic="index.md"/>
9+
<toc-element topic="Reference.topic"/>
10+
<toc-element topic="rendering.md"/>
11+
<toc-element topic="traversing.md"/>
12+
<toc-element topic="parsing.md"/>
13+
<toc-element topic="ast.md"/>
14+
</instance-profile>

Writerside/topics/Reference.topic

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE topic
3+
SYSTEM "https://resources.jetbrains.com/writerside/1.0/xhtml-entities.dtd">
4+
<topic xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5+
xsi:noNamespaceSchemaLocation="https://resources.jetbrains.com/writerside/1.0/topic.v2.xsd"
6+
title="Reference" id="Reference">
7+
8+
<p>
9+
A reference article is information-oriented.
10+
It provides a structured description of a product:
11+
its APIs, classes, functions, configuration options, actions, and so on.
12+
Start with a summary of what this reference article is about, and what the items you are describing are used for.
13+
</p>
14+
15+
<chapter title="Command" id="command">
16+
<p>Syntax:</p>
17+
18+
<code-block lang="shell">
19+
cmd [OPTIONS]
20+
</code-block>
21+
</chapter>
22+
23+
<chapter title="Options" id="options">
24+
<p>Describe what each option is used for:</p>
25+
26+
<deflist type="medium">
27+
<def title="-o, --open">
28+
Opens a file.
29+
</def>
30+
<def title="-c, --close">
31+
Closes a file.
32+
</def>
33+
<def title="-v --version">
34+
Displays version information.
35+
</def>
36+
<def title="-h, --help">
37+
Displays help.
38+
</def>
39+
</deflist>
40+
</chapter>
41+
42+
<seealso>
43+
<!--Provide links to related how-to guides, overviews, and tutorials.-->
44+
</seealso>
45+
</topic>

Writerside/v.list

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE vars SYSTEM "https://resources.jetbrains.com/writerside/1.0/vars.dtd">
3+
<vars>
4+
<var name="product" value="Writerside"/>
5+
</vars>

0 commit comments

Comments
 (0)