ember-scoped-css 3.3.0 → 3.4.0

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/dist/cjs/all.cjs CHANGED
@@ -1,3 +1,3 @@
1
- const require_all = require('./all-Cl5IZZ-T.cjs');
1
+ const require_all = require('./all-DurjGbDW.cjs');
2
2
 
3
3
  exports.scopedCSS = require_all.scopedCSS;
@@ -1,4 +1,4 @@
1
- const require_all = require('./all-Cl5IZZ-T.cjs');
1
+ const require_all = require('./all-DurjGbDW.cjs');
2
2
 
3
3
  //#region src/build/public-exports/babel.js
4
4
  const scopedCSS = require_all.scopedCSS.babel;
@@ -1,4 +1,4 @@
1
- const require_all = require('./all-Cl5IZZ-T.cjs');
1
+ const require_all = require('./all-DurjGbDW.cjs');
2
2
 
3
3
  //#region src/build/public-exports/rollup.js
4
4
  const scopedCSS = require_all.scopedCSS.rollup;
package/dist/cjs/vite.cjs CHANGED
@@ -1,4 +1,4 @@
1
- const require_all = require('./all-Cl5IZZ-T.cjs');
1
+ const require_all = require('./all-DurjGbDW.cjs');
2
2
 
3
3
  //#region src/build/public-exports/vite.js
4
4
  const scopedCSS = require_all.scopedCSS.vite;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ember-scoped-css",
3
- "version": "3.3.0",
3
+ "version": "3.4.0",
4
4
  "description": "",
5
5
  "keywords": [
6
6
  "ember-addon"
@@ -171,43 +171,47 @@ export function createPlugin(config) {
171
171
  AttrNode(...args) {
172
172
  return visitors.AttrNode(...args);
173
173
  },
174
- ElementNode(node, walker) {
175
- // class attribute handling
176
- visitors.ElementNode(node, walker);
177
-
178
- if (hasScopedAttribute(node)) {
179
- if (walker.parent?.node.type !== 'Template') {
180
- throw new Error(
181
- '<style scoped> tags must be at the root of the template, they cannot be nested',
182
- );
174
+ ElementNode: {
175
+ enter(node, walker) {
176
+ visitors.ElementNode.enter(node, walker);
177
+
178
+ if (hasScopedAttribute(node)) {
179
+ if (walker.parent?.node.type !== 'Template') {
180
+ throw new Error(
181
+ '<style scoped> tags must be at the root of the template, they cannot be nested',
182
+ );
183
+ }
184
+
185
+ if (hasInlineAttributeWithoutLang(node)) {
186
+ let text = textContent(node);
187
+ let scopedText = rewriteCss(
188
+ text,
189
+ postfix,
190
+ localCssPath,
191
+ config.layerName,
192
+ );
193
+
194
+ /**
195
+ * Traverse this and allow interpolation
196
+ */
197
+ node.children = [env.syntax.builders.text(scopedText)];
198
+
199
+ return;
200
+ }
201
+
202
+ // Returning null removes the node
203
+ return null;
183
204
  }
184
205
 
185
206
  if (hasInlineAttributeWithoutLang(node)) {
186
- let text = textContent(node);
187
- let scopedText = rewriteCss(
188
- text,
189
- postfix,
190
- localCssPath,
191
- config.layerName,
207
+ throw new Error(
208
+ `<style inline> is not valid. Please add the scoped attribute: <style scoped inline>`,
192
209
  );
193
-
194
- /**
195
- * Traverse this and allow interpolation
196
- */
197
- node.children = [env.syntax.builders.text(scopedText)];
198
-
199
- return;
200
210
  }
201
-
202
- // Returning null removes the node
203
- return null;
204
- }
205
-
206
- if (hasInlineAttributeWithoutLang(node)) {
207
- throw new Error(
208
- `<style inline> is not valid. Please add the scoped attribute: <style scoped inline>`,
209
- );
210
- }
211
+ },
212
+ exit(...args) {
213
+ return visitors.ElementNode.exit(...args);
214
+ },
211
215
  },
212
216
  MustacheStatement(...args) {
213
217
  return visitors.MustacheStatement(...args);
@@ -167,6 +167,14 @@ export function rewriteCss(css, postfix, fileName, layerName) {
167
167
  }
168
168
  }
169
169
 
170
+ function updateDeclarationName(node) {
171
+ let replacement = referenceables.property[node.prop];
172
+
173
+ if (replacement) {
174
+ node.prop = replacement;
175
+ }
176
+ }
177
+
170
178
  function updateShorthandContents(node) {
171
179
  if (node.prop === 'animation') {
172
180
  let parts = node.value.split(' ');
@@ -218,6 +226,7 @@ export function rewriteCss(css, postfix, fileName, layerName) {
218
226
  // Step 2: postfix and update referenced referenceables
219
227
  ast.walk((node) => {
220
228
  if (isDeclaration(node)) {
229
+ updateDeclarationName(node);
221
230
  updateDirectReferences(node);
222
231
  updateShorthandContents(node);
223
232
 
@@ -565,7 +565,7 @@ describe('@property', () => {
565
565
  border: 1px dashed black;
566
566
 
567
567
  /* set custom property values on parent */
568
- --item-size: 20%;
568
+ --item-size__postfix: 20%;
569
569
  --item-color: orange;
570
570
  }
571
571
 
@@ -579,6 +579,40 @@ describe('@property', () => {
579
579
  "
580
580
  `);
581
581
  });
582
+
583
+ it('updates assignments inside keyframes', () => {
584
+ const css = `
585
+ @property --loading-angle {
586
+ syntax: "<angle>";
587
+ inherits: false;
588
+ initial-value: 0turn;
589
+ }
590
+
591
+ .button::after {
592
+ background: conic-gradient(from var(--loading-angle), blue, transparent);
593
+ animation: loading-ring 1s linear infinite;
594
+ }
595
+
596
+ @keyframes loading-ring {
597
+ to {
598
+ --loading-angle: 1turn;
599
+ }
600
+ }
601
+ `;
602
+
603
+ const postfix = 'postfix';
604
+ const fileName = 'foo.css';
605
+ const rewritten = rewriteCss(css, postfix, fileName, 'components');
606
+ const postfixedProperty = '--loading-angle__postfix';
607
+
608
+ expect(rewritten).toContain(`@property ${postfixedProperty}`);
609
+ expect(rewritten).toContain(`var(${postfixedProperty})`);
610
+ expect(rewritten).toContain(`${postfixedProperty}: 1turn;`);
611
+ expect(rewritten).toContain(
612
+ 'animation: loading-ring__postfix 1s linear infinite;',
613
+ );
614
+ expect(rewritten).toContain('@keyframes loading-ring__postfix');
615
+ });
582
616
  });
583
617
 
584
618
  describe('@supports', () => {
@@ -0,0 +1,156 @@
1
+ import { describe, expect, it } from 'vitest';
2
+
3
+ import rewriteHbs from './rewriteHbs.js';
4
+
5
+ const postfix = 'pfx';
6
+
7
+ /**
8
+ * `a` and `b` are in the co-located CSS; `global-thing` deliberately is not, so
9
+ * tests can distinguish "renamed" from "left alone for a global class".
10
+ */
11
+ const classes = new Set(['a', 'b']);
12
+
13
+ function rewrite(hbs) {
14
+ return rewriteHbs(hbs, classes, new Set(), postfix);
15
+ }
16
+
17
+ /**
18
+ * A `concat` sitting in the class attribute's value itself -- the whole
19
+ * unquoted value, a quoted value, or one part of a value the attribute
20
+ * already has (e.g. once a scoped element's postfix class is appended) --
21
+ * is spliced into the native parts it's equivalent to, so the call is gone
22
+ * from every case here regardless of whether anything ends up renamed. A
23
+ * `concat` nested inside that one's own params joins in the same way, since
24
+ * concatenation doesn't care how its inputs were grouped. Only a `concat`
25
+ * nested inside an `if`/`unless` branch keeps its call, because a branch has
26
+ * to stay one value; see the last test.
27
+ */
28
+ describe('concat in a class attribute', () => {
29
+ it('renames a lone param that is the whole class', () => {
30
+ expect(rewrite('<div class={{concat "a"}}></div>')).to.equal(
31
+ '<div class="a_pfx"></div>',
32
+ );
33
+ });
34
+
35
+ it('renames every param that is a whole class name', () => {
36
+ expect(rewrite('<div class={{concat "a" " " "b"}}></div>')).to.equal(
37
+ '<div class="a_pfx b_pfx"></div>',
38
+ );
39
+ });
40
+
41
+ it('leaves a param that is not in the CSS alone', () => {
42
+ expect(
43
+ rewrite('<div class={{concat "a" " " "global-thing"}}></div>'),
44
+ ).to.equal('<div class="a_pfx global-thing"></div>');
45
+ });
46
+
47
+ it('folds fused literal params into one class and renames it', () => {
48
+ // Renaming just "a" would emit a_pfx-suffix, which matches no selector
49
+ // the CSS rewrite produces -- concat's own job of joining "a" and
50
+ // "-suffix" into "a-suffix" makes this the same case as any other
51
+ // TextNode class value.
52
+ const rewriteWithSuffixClass = (hbs) =>
53
+ rewriteHbs(hbs, new Set(['a', 'b', 'a-suffix']), new Set(), postfix);
54
+
55
+ expect(
56
+ rewriteWithSuffixClass('<div class={{concat "a" "-suffix"}}></div>'),
57
+ ).to.equal('<div class="a-suffix_pfx"></div>');
58
+ });
59
+
60
+ it('renames a literal fused with a runtime value the same as a hand-written attribute would', () => {
61
+ // Once concat is spliced away, "a" and this.suffix are just adjacent
62
+ // parts of a native attribute value -- the same shape `class="a{{this.suffix}}"`
63
+ // already has -- so "a" is renamed on its own, with no cross-part fusion
64
+ // detection. A boundary of its own still bounds it either way.
65
+ expect(rewrite('<div class={{concat "a" this.suffix}}></div>')).to.equal(
66
+ '<div class="a_pfx{{this.suffix}}"></div>',
67
+ );
68
+ expect(rewrite('<div class={{concat "a " this.suffix}}></div>')).to.equal(
69
+ '<div class="a_pfx {{this.suffix}}"></div>',
70
+ );
71
+ });
72
+
73
+ it('gives an arbitrary helper call among its params its own mustache', () => {
74
+ expect(
75
+ rewrite('<div class={{concat "a " (someHelper "x") " b"}}></div>'),
76
+ ).to.equal('<div class="a_pfx {{someHelper "x"}} b_pfx"></div>');
77
+ });
78
+
79
+ it('replaces the call with the native parts it is equivalent to', () => {
80
+ expect(rewrite('<div class={{concat x y z}}></div>')).to.equal(
81
+ '<div class="{{x}}{{y}}{{z}}"></div>',
82
+ );
83
+ });
84
+
85
+ it('gives a nested class-building helper its own mustache too', () => {
86
+ expect(
87
+ rewrite('<div class={{concat (if x y z) " " (if a b c)}}></div>'),
88
+ ).to.equal('<div class="{{if x y z}} {{if a b c}}"></div>');
89
+ });
90
+
91
+ it('splices into the value a scoped element has already been given', () => {
92
+ // A scoped tag gets its postfix class appended to the class attribute,
93
+ // which the concat now arrives as one part of rather than as the whole
94
+ // of it.
95
+ const rewriteScopingDiv = (hbs) =>
96
+ rewriteHbs(hbs, classes, new Set(['div']), postfix);
97
+
98
+ expect(
99
+ rewriteScopingDiv('<div class={{concat "a" " " "b"}}></div>'),
100
+ ).to.equal('<div class="a_pfx b_pfx pfx"></div>');
101
+ });
102
+
103
+ it('joins no params at all into an empty value', () => {
104
+ expect(rewrite('<div class={{concat ""}}></div>')).to.equal(
105
+ '<div class=""></div>',
106
+ );
107
+ });
108
+
109
+ it('renames the same params in a quoted attribute value', () => {
110
+ expect(rewrite(`<div class="{{concat 'a' ' ' 'b'}}"></div>`)).to.equal(
111
+ `<div class="a_pfx b_pfx"></div>`,
112
+ );
113
+ });
114
+
115
+ it('skips concat when it is a block param', () => {
116
+ expect(
117
+ rewrite(
118
+ '{{#let x as |concat|}}<div class={{concat "a" " " "b"}}></div>{{/let}}',
119
+ ),
120
+ ).to.equal(
121
+ '{{#let x as |concat|}}<div class={{concat "a" " " "b"}}></div>{{/let}}',
122
+ );
123
+ });
124
+
125
+ it("joins a nested concat's params in directly, folding fused literals", () => {
126
+ // concat(concat("a", " ", "b"), " b") joins to the same string as
127
+ // concat("a", " ", "b", " b") -- the inner call's own params flatten
128
+ // into the outer one rather than surviving as a nested call.
129
+ expect(
130
+ rewrite('<div class={{concat (concat "a" " " "b") " b"}}></div>'),
131
+ ).to.equal('<div class="a_pfx b_pfx b_pfx"></div>');
132
+ });
133
+
134
+ it("gives a class-building call among a nested concat's params its own mustache", () => {
135
+ expect(
136
+ rewrite('<div class={{concat (concat "a " (if x "a" "b")) " c"}}></div>'),
137
+ ).to.equal('<div class="a_pfx {{if x "a_pfx" "b_pfx"}} c"></div>');
138
+ });
139
+
140
+ it("gives an opaque helper among a nested concat's params its own mustache too", () => {
141
+ expect(
142
+ rewrite(
143
+ '<div class={{concat (concat "a " (someHelper "z") " b") " c"}}></div>',
144
+ ),
145
+ ).to.equal('<div class="a_pfx {{someHelper "z"}} b_pfx c"></div>');
146
+ });
147
+
148
+ it('keeps a concat nested inside an if branch, renaming its whole-class-name params', () => {
149
+ // The branch's result has to stay one value for if to choose between, so
150
+ // the concat inside it survives as a call -- and gets the boundary-aware
151
+ // rename a concat that must stay one value needs, not the splice above.
152
+ expect(
153
+ rewrite('<div class={{if x (concat "a" " " "b") "c"}}></div>'),
154
+ ).to.equal('<div class={{if x (concat "a_pfx" " " "b_pfx") "c"}}></div>');
155
+ });
156
+ });
@@ -0,0 +1,28 @@
1
+ import { describe, expect, it } from 'vitest';
2
+
3
+ import rewriteHbs from './rewriteHbs.js';
4
+
5
+ const postfix = 'pfx';
6
+
7
+ /**
8
+ * `a` and `b` are in the co-located CSS; `global-thing` deliberately is not, so
9
+ * tests can distinguish "renamed" from "left alone for a global class".
10
+ */
11
+ const classes = new Set(['a', 'b']);
12
+
13
+ function rewrite(hbs) {
14
+ return rewriteHbs(hbs, classes, new Set(), postfix);
15
+ }
16
+
17
+ /**
18
+ * Cross-cutting behavior of the class-attribute dispatch that isn't specific
19
+ * to any one helper; see rewriteHbs.if.test.ts, rewriteHbs.scopedClass.test.ts,
20
+ * and rewriteHbs.classes.concat.test.ts for per-helper coverage.
21
+ */
22
+ describe('class attribute dispatch', () => {
23
+ it('only touches the class attribute', () => {
24
+ expect(rewrite('<div data-x={{if x "a" "b"}}></div>')).to.equal(
25
+ '<div data-x={{if x "a" "b"}}></div>',
26
+ );
27
+ });
28
+ });
@@ -0,0 +1,168 @@
1
+ import { describe, expect, it } from 'vitest';
2
+
3
+ import rewriteHbs from './rewriteHbs.js';
4
+
5
+ const postfix = 'pfx';
6
+
7
+ /**
8
+ * `a` and `b` are in the co-located CSS; `global-thing` deliberately is not, so
9
+ * tests can distinguish "renamed" from "left alone for a global class".
10
+ */
11
+ const classes = new Set(['a', 'b']);
12
+
13
+ function rewrite(hbs) {
14
+ return rewriteHbs(hbs, classes, new Set(), postfix);
15
+ }
16
+
17
+ /** As `rewrite`, but with `div` also matched by a tag selector in the CSS. */
18
+ function rewriteWithScopedDiv(hbs) {
19
+ return rewriteHbs(hbs, classes, new Set(['div']), postfix);
20
+ }
21
+
22
+ describe('if in a class attribute', () => {
23
+ it('renames both branches, unquoted', () => {
24
+ expect(rewrite('<div class={{if x "a" "b"}}></div>')).to.equal(
25
+ '<div class={{if x "a_pfx" "b_pfx"}}></div>',
26
+ );
27
+ });
28
+
29
+ it('renames both branches, quoted', () => {
30
+ expect(rewrite('<div class="{{if x "a" "b"}}"></div>')).to.equal(
31
+ '<div class="{{if x "a_pfx" "b_pfx"}}"></div>',
32
+ );
33
+ });
34
+
35
+ it('leaves a branch that is not in the CSS alone', () => {
36
+ expect(rewrite('<div class={{if x "global-thing"}}></div>')).to.equal(
37
+ '<div class={{if x "global-thing"}}></div>',
38
+ );
39
+ });
40
+
41
+ it('leaves the condition alone, even when it is a helper call', () => {
42
+ // checkAlphabet's own arguments are data, not class names -- only the
43
+ // branches (params 1+) reach the class attribute.
44
+ expect(
45
+ rewrite('<div class={{if (checkAlphabet "a" "b") "a" "b"}}></div>'),
46
+ ).to.equal(
47
+ '<div class={{if (checkAlphabet "a" "b") "a_pfx" "b_pfx"}}></div>',
48
+ );
49
+ });
50
+
51
+ it('leaves the condition alone, even when it is a helper call, quoted', () => {
52
+ expect(
53
+ rewrite(`<div class="{{if (checkAlphabet 'a' 'b') 'a' 'b'}}"></div>`),
54
+ ).to.equal(
55
+ `<div class="{{if (checkAlphabet 'a' 'b') 'a_pfx' 'b_pfx'}}"></div>`,
56
+ );
57
+ });
58
+
59
+ it('skips if when it is a block param', () => {
60
+ expect(
61
+ rewrite(
62
+ '{{#each xs as |if|}}<div class={{if y "a" "b"}}></div>{{/each}}',
63
+ ),
64
+ ).to.equal(
65
+ '{{#each xs as |if|}}<div class={{if y "a" "b"}}></div>{{/each}}',
66
+ );
67
+ });
68
+
69
+ it('skips if when an element introduces it, and resumes after that element', () => {
70
+ expect(
71
+ rewrite(
72
+ '<Foo as |if|><div class={{if y "a"}}></div></Foo><div class={{if y "b"}}></div>',
73
+ ),
74
+ ).to.equal(
75
+ '<Foo as |if|><div class={{if y "a"}}></div></Foo><div class={{if y "b_pfx"}}></div>',
76
+ );
77
+ });
78
+
79
+ it('matches a branch against the CSS whole, not by prefix', () => {
80
+ expect(rewrite('<div class={{if x "ab"}}></div>')).to.equal(
81
+ '<div class={{if x "ab"}}></div>',
82
+ );
83
+ });
84
+
85
+ it('still leaves the condition alone when the element is itself scoped', () => {
86
+ // A scoped element gets the postfix appended to its class attribute,
87
+ // which turns an unquoted value into a concat.
88
+ expect(
89
+ rewriteWithScopedDiv('<div class={{if (eq this.m "a") "a" "b"}}></div>'),
90
+ ).to.equal(
91
+ '<div class="{{if (eq this.m "a") "a_pfx" "b_pfx"}} pfx"></div>',
92
+ );
93
+ });
94
+
95
+ it('leaves if alone outside an attribute', () => {
96
+ expect(rewrite('<div>{{if x "a" "b"}}</div>')).to.equal(
97
+ '<div>{{if x "a" "b"}}</div>',
98
+ );
99
+ });
100
+
101
+ it('leaves a helper that is not if alone', () => {
102
+ expect(rewrite('<div class={{someHelper "a" "b"}}></div>')).to.equal(
103
+ '<div class={{someHelper "a" "b"}}></div>',
104
+ );
105
+ });
106
+ });
107
+
108
+ /**
109
+ * A branch of `if` may itself be a class-building subexpression, in which
110
+ * case its own literals are reached the same way a top-level `concat` call
111
+ * would be.
112
+ */
113
+ describe('deep if', () => {
114
+ it('reaches whole-class-name params of a concat branch', () => {
115
+ expect(
116
+ rewrite('<div class={{if x (concat "a" " " "b") "b"}}></div>'),
117
+ ).to.equal(
118
+ '<div class={{if x (concat "a_pfx" " " "b_pfx") "b_pfx"}}></div>',
119
+ );
120
+ });
121
+
122
+ it('leaves a param not in the CSS alone inside a concat branch', () => {
123
+ expect(
124
+ rewrite('<div class={{if x (concat "a" " " "global-thing") "b"}}></div>'),
125
+ ).to.equal(
126
+ '<div class={{if x (concat "a_pfx" " " "global-thing") "b_pfx"}}></div>',
127
+ );
128
+ });
129
+
130
+ it('recurses into a branch that is itself an if', () => {
131
+ expect(rewrite('<div class={{if x (if y "a" "b") "b"}}></div>')).to.equal(
132
+ '<div class={{if x (if y "a_pfx" "b_pfx") "b_pfx"}}></div>',
133
+ );
134
+ });
135
+
136
+ it('leaves an opaque helper call in a branch alone, params and all', () => {
137
+ // someHelper is not a class-building helper, so what it does with the
138
+ // concat's result is unknown -- the concat is an argument to someHelper
139
+ // rather than a value reaching the class attribute.
140
+ expect(
141
+ rewrite(
142
+ '<div class={{if x (someHelper (concat "a" " " "b")) "b"}}></div>',
143
+ ),
144
+ ).to.equal(
145
+ '<div class={{if x (someHelper (concat "a" " " "b")) "b_pfx"}}></div>',
146
+ );
147
+ });
148
+
149
+ it('skips a concat shadowed by a block param at any depth', () => {
150
+ expect(
151
+ rewrite(
152
+ '{{#let q as |concat|}}<div class={{if x (if y (concat "a" " " "b") "b") "b"}}></div>{{/let}}',
153
+ ),
154
+ ).to.equal(
155
+ '{{#let q as |concat|}}<div class={{if x (if y (concat "a" " " "b") "b_pfx") "b_pfx"}}></div>{{/let}}',
156
+ );
157
+ });
158
+
159
+ it('skips the whole branch subtree when if itself is a block param', () => {
160
+ expect(
161
+ rewrite(
162
+ '{{#let q as |if|}}<div class={{if y (concat "a" " " "b") "b"}}></div>{{/let}}',
163
+ ),
164
+ ).to.equal(
165
+ '{{#let q as |if|}}<div class={{if y (concat "a" " " "b") "b"}}></div>{{/let}}',
166
+ );
167
+ });
168
+ });