@node-projects/web-component-designer-stylesheetservice-css-parser 0.1.3 → 0.1.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -12,11 +12,13 @@ interface IDeclarationWithAST extends IStyleDeclaration {
|
|
|
12
12
|
ast: CssDeclarationAST;
|
|
13
13
|
}
|
|
14
14
|
export declare class CssParserStylesheetService extends AbstractStylesheetService {
|
|
15
|
-
|
|
15
|
+
keepFormatting: boolean;
|
|
16
|
+
constructor(designerCanvas: IDesignerCanvas, keepFormatting?: boolean);
|
|
16
17
|
internalParse(style: string): Promise<CssStylesheetAST>;
|
|
17
18
|
getAppliedRules(designItem: IDesignItem): IRuleWithAST[];
|
|
18
19
|
getRules(selector: string): IStyleRule[];
|
|
19
20
|
addRule(stylesheet: IStylesheet, selector: string): Promise<IStyleRule>;
|
|
21
|
+
private resolveNestedSelectors;
|
|
20
22
|
private getRulesFromAst;
|
|
21
23
|
getDeclarations(designItem: IDesignItem, styleName: string): IStyleDeclaration[];
|
|
22
24
|
updateDeclarationValueWithoutUndo(declaration: IDeclarationWithAST, value: string, important: boolean): void;
|
|
@@ -1,11 +1,13 @@
|
|
|
1
1
|
import { parse, stringify } from "@node-projects/css-parser";
|
|
2
2
|
import { AbstractStylesheetService } from "@node-projects/web-component-designer";
|
|
3
3
|
export class CssParserStylesheetService extends AbstractStylesheetService {
|
|
4
|
-
|
|
4
|
+
keepFormatting;
|
|
5
|
+
constructor(designerCanvas, keepFormatting = true) {
|
|
5
6
|
super(designerCanvas);
|
|
7
|
+
this.keepFormatting = keepFormatting;
|
|
6
8
|
}
|
|
7
9
|
async internalParse(style) {
|
|
8
|
-
return parse(style);
|
|
10
|
+
return parse(style, this.keepFormatting ? { preserveFormatting: true } : undefined);
|
|
9
11
|
}
|
|
10
12
|
getAppliedRules(designItem) {
|
|
11
13
|
let rules = [];
|
|
@@ -14,7 +16,7 @@ export class CssParserStylesheetService extends AbstractStylesheetService {
|
|
|
14
16
|
continue;
|
|
15
17
|
let rs = Array.from(this.getRulesFromAst(item[1].ast?.stylesheet?.rules, item[1].stylesheet, designItem))
|
|
16
18
|
.map(x => ({
|
|
17
|
-
selector: x[
|
|
19
|
+
selector: x[2].join(', '),
|
|
18
20
|
declarations: x[0].declarations.filter(y => y.type == 'declaration').map(y => ({
|
|
19
21
|
name: y.property,
|
|
20
22
|
value: y.value.endsWith('!important') ? y.value.substring(0, y.value.length - 10).trimEnd() : y.value,
|
|
@@ -42,18 +44,37 @@ export class CssParserStylesheetService extends AbstractStylesheetService {
|
|
|
42
44
|
const rules = this.getRules(selector).filter(x => x.stylesheetName == stylesheet.name);
|
|
43
45
|
return rules[rules.length - 1];
|
|
44
46
|
}
|
|
45
|
-
|
|
47
|
+
resolveNestedSelectors(parentSelectors, nestedSelectors) {
|
|
48
|
+
const parentRef = parentSelectors.length === 1
|
|
49
|
+
? parentSelectors[0]
|
|
50
|
+
: `:is(${parentSelectors.join(', ')})`;
|
|
51
|
+
const resolved = [];
|
|
52
|
+
for (const nested of nestedSelectors) {
|
|
53
|
+
if (nested.includes('&')) {
|
|
54
|
+
resolved.push(nested.replace(/&/g, parentRef));
|
|
55
|
+
}
|
|
56
|
+
else {
|
|
57
|
+
resolved.push(parentRef + ' ' + nested);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
return resolved;
|
|
61
|
+
}
|
|
62
|
+
*getRulesFromAst(cssAtRuleAst, stylesheet, designItem, parentSelectors) {
|
|
46
63
|
for (const atRule of cssAtRuleAst) {
|
|
47
64
|
if (atRule.type == 'media') {
|
|
48
|
-
yield* this.getRulesFromAst(atRule.rules, stylesheet, designItem);
|
|
65
|
+
yield* this.getRulesFromAst(atRule.rules, stylesheet, designItem, parentSelectors);
|
|
49
66
|
}
|
|
50
67
|
else if (atRule.type == 'supports') {
|
|
51
|
-
yield* this.getRulesFromAst(atRule.rules, stylesheet, designItem);
|
|
68
|
+
yield* this.getRulesFromAst(atRule.rules, stylesheet, designItem, parentSelectors);
|
|
52
69
|
}
|
|
53
70
|
else if (atRule.type == 'rule') {
|
|
54
|
-
|
|
71
|
+
const resolvedSelectors = parentSelectors
|
|
72
|
+
? this.resolveNestedSelectors(parentSelectors, atRule.selectors)
|
|
73
|
+
: atRule.selectors;
|
|
74
|
+
let spec = this.elementMatchesASelector(designItem, resolvedSelectors);
|
|
55
75
|
if (spec)
|
|
56
|
-
yield [atRule, spec];
|
|
76
|
+
yield [atRule, spec, resolvedSelectors];
|
|
77
|
+
yield* this.getRulesFromAst(atRule.declarations, stylesheet, designItem, resolvedSelectors);
|
|
57
78
|
}
|
|
58
79
|
}
|
|
59
80
|
return null;
|
|
@@ -63,17 +84,10 @@ export class CssParserStylesheetService extends AbstractStylesheetService {
|
|
|
63
84
|
}
|
|
64
85
|
updateDeclarationValueWithoutUndo(declaration, value, important) {
|
|
65
86
|
declaration.ast.value = important ? value + ' !important' : value;
|
|
87
|
+
if (declaration.ast.rawValue)
|
|
88
|
+
delete declaration.ast.rawValue;
|
|
66
89
|
let ss = this._allStylesheets.get(declaration.parent.stylesheetName);
|
|
67
90
|
this.updateStylesheet(ss);
|
|
68
|
-
/*
|
|
69
|
-
declaration.ast.value = important ? value + ' !important' : value;
|
|
70
|
-
let ss = declaration.ast;
|
|
71
|
-
while (ss?.parent)
|
|
72
|
-
ss = ss?.parent;
|
|
73
|
-
let obj = { ast: ss, stylesheet: declaration.stylesheet };
|
|
74
|
-
this._allStylesheets.set(declaration.parent.stylesheetName, obj)
|
|
75
|
-
this.updateStylesheet(obj);
|
|
76
|
-
*/
|
|
77
91
|
}
|
|
78
92
|
insertDeclarationIntoRule(rule, property, value, important) {
|
|
79
93
|
rule.ast.declarations.push({
|
|
@@ -94,7 +108,9 @@ export class CssParserStylesheetService extends AbstractStylesheetService {
|
|
|
94
108
|
}
|
|
95
109
|
updateStylesheet(ss) {
|
|
96
110
|
const old = ss.stylesheet.content;
|
|
97
|
-
ss.stylesheet.content =
|
|
111
|
+
ss.stylesheet.content = this.keepFormatting
|
|
112
|
+
? stringify(ss.ast, { identity: true })
|
|
113
|
+
: stringify(ss.ast, { indent: ' ', compress: false });
|
|
98
114
|
if (ss.stylesheet.designItem) {
|
|
99
115
|
ss.stylesheet.designItem.content = ss.stylesheet.content;
|
|
100
116
|
}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"description": "web-component-designer addon: Stylesheet Parser Service using @node-projects/css-parser",
|
|
3
3
|
"name": "@node-projects/web-component-designer-stylesheetservice-css-parser",
|
|
4
|
-
"version": "0.1.
|
|
4
|
+
"version": "0.1.5",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/service/stylesheetservice/CssParserStylesheetService.js",
|
|
7
7
|
"author": "jochen.kuehner@gmx.de",
|