@node-projects/web-component-designer-stylesheetservice-css-parser 0.1.3 → 0.1.4

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
- constructor(designerCanvas: IDesignerCanvas);
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
- constructor(designerCanvas) {
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[0].selectors.join(', '),
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
- *getRulesFromAst(cssAtRuleAst, stylesheet, designItem) {
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
- let spec = this.elementMatchesASelector(designItem, atRule.selectors);
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;
@@ -94,7 +115,9 @@ export class CssParserStylesheetService extends AbstractStylesheetService {
94
115
  }
95
116
  updateStylesheet(ss) {
96
117
  const old = ss.stylesheet.content;
97
- ss.stylesheet.content = stringify(ss.ast, { indent: ' ', compress: false });
118
+ ss.stylesheet.content = this.keepFormatting
119
+ ? stringify(ss.ast, { identity: true })
120
+ : stringify(ss.ast, { indent: ' ', compress: false });
98
121
  if (ss.stylesheet.designItem) {
99
122
  ss.stylesheet.designItem.content = ss.stylesheet.content;
100
123
  }
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.3",
4
+ "version": "0.1.4",
5
5
  "type": "module",
6
6
  "main": "./dist/service/stylesheetservice/CssParserStylesheetService.js",
7
7
  "author": "jochen.kuehner@gmx.de",