@node-projects/web-component-designer-stylesheetservice-css-parser 1.1.7 → 1.1.9

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.
package/README.md CHANGED
@@ -13,4 +13,52 @@ This is a Stylesheetparser using @node-projects/css-parser
13
13
  ## Usage
14
14
 
15
15
  import { CssParserStylesheetService } from '@node-projects/web-component-designer-stylesheetservice-css-parser';
16
- serviceContainer.registerDocumentService("stylesheetService", container => new CssParserStylesheetService(container));
16
+ serviceContainer.registerDocumentService("stylesheetService", container => new CssParserStylesheetService(container));
17
+
18
+ ## Conditional rule matching
19
+
20
+ `getAppliedRules(designItem)` evaluates `@media` using the element's own window,
21
+ `@supports` using that window's `CSS.supports()`, and `@container` using
22
+ `designItem.element.matchContainer(query).matches`. The complete container query,
23
+ including any container name, is passed to the browser. Nested conditions must all
24
+ match; nested selectors keep their parent selector context.
25
+
26
+ The container API is experimental. Chromium's prototype can be enabled with
27
+ `--enable-blink-features=ElementMatchContainer` (see the
28
+ [CSSWG discussion](https://github.com/w3c/csswg-drafts/issues/6205#issuecomment-6758924380)).
29
+ There is no addon flag: the service detects the method on the inspected element.
30
+ Without that API or a compatible polyfill, container rules are omitted from
31
+ applied rules. CSS can still render those rules even when the scripting API is
32
+ unavailable. `getRules(selector)` continues to include rules inside conditional
33
+ blocks regardless of their current match state, so lookup and editing work.
34
+
35
+ Results are evaluated when requested; this service does not subscribe to query
36
+ changes. After resizing a container, refresh the inspector (for example, reselect
37
+ the element).
38
+
39
+ Open [the HTML/CSS sample](examples/container-and-scope.html) in the designer:
40
+
41
+ - Select `#wide`: its matching container rule sets a green background.
42
+ - Select `#narrow`: its size query sets an orange background, and its style query
43
+ sets purple text and a purple border in browsers supporting that query.
44
+ - Change either parent's width across 400px and reselect its card. The matching
45
+ background rule switches. Above a 700px viewport, the wide card also matches
46
+ the nested media/container rule setting bold text.
47
+
48
+ ## Scope support
49
+
50
+ Scope rendering depends on the browser, but the addon's applied-rule inspection
51
+ has these limitations:
52
+
53
+ - `@scope` is parsed and preserved, but its rules are not traversed or reported.
54
+ This also applies to inline `<style>@scope { ... }</style>`, where the style
55
+ element's parent supplies the implicit scope root.
56
+ - `:scope` is passed to `Element.matches()`. That API uses the inspected element
57
+ as its scope, which does not establish the scope root used by stylesheet rules.
58
+ Consequently, stylesheet `:scope` matching is not correctly supported.
59
+ - Scope proximity is not included in declaration ordering.
60
+
61
+ The sample includes explicit scopes, scope limits, `:scope`, and an inline
62
+ implicit scope to demonstrate browser rendering separately from inspector support.
63
+ The old `<style scoped>` attribute is not an implementation of modern `@scope`;
64
+ use an inline `@scope` block instead.
@@ -16,6 +16,7 @@ export declare class CssParserStylesheetService extends AbstractStylesheetServic
16
16
  constructor(designerCanvas: IDesignerCanvas | InstanceServiceContainer, keepFormatting?: boolean);
17
17
  internalParse(style: string): Promise<CssStylesheetAST>;
18
18
  getAppliedRules(designItem: IDesignItem): IRuleWithAST[];
19
+ private static getRuleCssText;
19
20
  getRules(selector: string): IStyleRule[];
20
21
  addRule(stylesheet: IStylesheet, selector: string): Promise<IStyleRule>;
21
22
  private resolveNestedSelectors;
@@ -17,6 +17,7 @@ export class CssParserStylesheetService extends AbstractStylesheetService {
17
17
  let rs = Array.from(this.getRulesFromAst(item[1].ast?.stylesheet?.rules, item[1].stylesheet, designItem))
18
18
  .map(x => ({
19
19
  selector: x[2].join(', '),
20
+ get cssText() { return CssParserStylesheetService.getRuleCssText(x[0]); },
20
21
  declarations: x[0].declarations.filter(y => y.type == 'declaration').map(y => ({
21
22
  name: y.property,
22
23
  value: y.value.endsWith('!important') ? y.value.substring(0, y.value.length - 10).trimEnd() : y.value,
@@ -35,6 +36,20 @@ export class CssParserStylesheetService extends AbstractStylesheetService {
35
36
  }
36
37
  return rules;
37
38
  }
39
+ static getRuleCssText(rule) {
40
+ let ast = rule;
41
+ let parent = rule.parent;
42
+ while (parent) {
43
+ if (parent.type == 'rule')
44
+ ast = { ...parent, declarations: [ast] };
45
+ else if ('rules' in parent)
46
+ ast = { ...parent, rules: [ast] };
47
+ else
48
+ break;
49
+ parent = parent.parent;
50
+ }
51
+ return stringify({ type: 'stylesheet', stylesheet: { rules: [ast] } }, { indent: ' ', compress: false });
52
+ }
38
53
  getRules(selector) {
39
54
  const rules = this.getAppliedRules(null);
40
55
  return rules.filter(x => x.selector == selector);
@@ -62,10 +77,18 @@ export class CssParserStylesheetService extends AbstractStylesheetService {
62
77
  *getRulesFromAst(cssAtRuleAst, stylesheet, designItem, parentSelectors) {
63
78
  for (const atRule of cssAtRuleAst) {
64
79
  if (atRule.type == 'media') {
65
- yield* this.getRulesFromAst(atRule.rules, stylesheet, designItem, parentSelectors);
80
+ if (!designItem || designItem.element.ownerDocument.defaultView?.matchMedia(atRule.media).matches)
81
+ yield* this.getRulesFromAst(atRule.rules, stylesheet, designItem, parentSelectors);
66
82
  }
67
83
  else if (atRule.type == 'supports') {
68
- yield* this.getRulesFromAst(atRule.rules, stylesheet, designItem, parentSelectors);
84
+ if (!designItem || designItem.element.ownerDocument.defaultView?.CSS?.supports(atRule.supports))
85
+ yield* this.getRulesFromAst(atRule.rules, stylesheet, designItem, parentSelectors);
86
+ }
87
+ else if (atRule.type == 'container') {
88
+ // Use the inspected element: the browser selects its eligible ancestor container.
89
+ const element = designItem?.element;
90
+ if (!designItem || element.matchContainer?.(atRule.container).matches)
91
+ yield* this.getRulesFromAst(atRule.rules, stylesheet, designItem, parentSelectors);
69
92
  }
70
93
  else if (atRule.type == 'rule') {
71
94
  const resolvedSelectors = parentSelectors
@@ -0,0 +1,70 @@
1
+ <!doctype html>
2
+ <html lang="en">
3
+ <meta charset="utf-8">
4
+ <title>Container and scope rule matching</title>
5
+ <style>
6
+ body { font: 16px system-ui; margin: 24px; }
7
+ .container {
8
+ container: layout / inline-size;
9
+ resize: horizontal;
10
+ overflow: auto;
11
+ padding: 12px;
12
+ border: 2px solid #888;
13
+ margin-block: 16px;
14
+ min-width: 180px;
15
+ --theme: light;
16
+ }
17
+ .card { padding: 16px; background: #eee; color: #222; }
18
+ @container layout (width >= 400px) {
19
+ .card { background: #c8f7d1; }
20
+ }
21
+ @container layout (width < 400px) {
22
+ .card { background: #ffe3aa; }
23
+ }
24
+ @container layout style(--theme: dark) {
25
+ .card { color: #663399; border: 3px solid #663399; }
26
+ }
27
+ @media (min-width: 700px) {
28
+ @container layout (width >= 400px) {
29
+ .card { font-weight: 700; }
30
+ }
31
+ }
32
+
33
+ /* Scope rendering examples. The addon does not yet report these rules. */
34
+ @scope (.scoped) to (.excluded) {
35
+ :scope { border: 2px solid royalblue; padding: 12px; }
36
+ .scope-label { color: royalblue; }
37
+ }
38
+ </style>
39
+ <body>
40
+ <h1>Container query test</h1>
41
+ <p>Select each card in the designer. Resize its parent or change its width
42
+ across 400px, then reselect the card to refresh matching rules.</p>
43
+ <div class="container" style="width: 520px">
44
+ <div class="card" id="wide">Wide: green background; bold above a 700px viewport.</div>
45
+ </div>
46
+ <div class="container" style="width: 280px; --theme: dark">
47
+ <div class="card" id="narrow">Narrow: orange background, purple text and border.</div>
48
+ </div>
49
+ <p>The backgrounds render with ordinary CSS container support. Reporting the
50
+ container rules in the designer requires Element.matchContainer().</p>
51
+
52
+ <h2>Scope rendering tests (inspector support still missing)</h2>
53
+ <section class="scoped">
54
+ <p class="scope-label">Blue within the explicit scope.</p>
55
+ <div class="excluded">
56
+ <p class="scope-label">Default text color beyond the scope limit.</p>
57
+ </div>
58
+ </section>
59
+ <section>
60
+ <style>
61
+ @scope {
62
+ :scope { border: 2px dashed seagreen; padding: 12px; }
63
+ .inline-label { color: seagreen; }
64
+ }
65
+ </style>
66
+ <p class="inline-label">Green within an inline implicit scope.</p>
67
+ </section>
68
+ <p class="inline-label">Default text color outside the inline scope.</p>
69
+ </body>
70
+ </html>
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": "1.1.7",
4
+ "version": "1.1.9",
5
5
  "type": "module",
6
6
  "main": "./dist/service/stylesheetservice/CssParserStylesheetService.js",
7
7
  "author": "jochen.kuehner@gmx.de",
@@ -14,7 +14,7 @@
14
14
  },
15
15
  "dependencies": {
16
16
  "@node-projects/css-parser": "^5.0.0",
17
- "@node-projects/web-component-designer": "1.1.7"
17
+ "@node-projects/web-component-designer": "1.1.9"
18
18
  },
19
19
  "repository": {
20
20
  "type": "git",