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-DurjGbDW.cjs +1711 -0
- package/dist/cjs/all.cjs +1 -1
- package/dist/cjs/babel.cjs +1 -1
- package/dist/cjs/rollup.cjs +1 -1
- package/dist/cjs/vite.cjs +1 -1
- package/package.json +1 -1
- package/src/build/template-plugin.js +36 -32
- package/src/lib/css/rewrite.js +9 -0
- package/src/lib/css/rewrite.test.ts +35 -1
- package/src/lib/rewriteHbs.classes.concat.test.ts +156 -0
- package/src/lib/rewriteHbs.classes.test.ts +28 -0
- package/src/lib/rewriteHbs.if.test.ts +168 -0
- package/src/lib/rewriteHbs.js +277 -38
- package/src/lib/rewriteHbs.scopedClass.test.ts +19 -0
- package/src/lib/rewriteHbs.unless.test.ts +63 -0
- package/dist/cjs/all-Cl5IZZ-T.cjs +0 -1539
package/src/lib/rewriteHbs.js
CHANGED
|
@@ -30,6 +30,69 @@ function elementHasScopedAttribute(node, attributes) {
|
|
|
30
30
|
);
|
|
31
31
|
}
|
|
32
32
|
|
|
33
|
+
/**
|
|
34
|
+
* Helpers whose result is assembled from their own params, so a literal
|
|
35
|
+
* param can be a class name in its own right. The condition at index 0 of
|
|
36
|
+
* `if` and `unless` is excluded -- it decides which branch wins rather than
|
|
37
|
+
* contributing to the class string, so postfixing it (e.g. a comparand:
|
|
38
|
+
* `{{if (eq this.mode "a") "a" "b"}}`) would stop the comparison ever
|
|
39
|
+
* matching. `concat` has no such condition, but fuses its params together --
|
|
40
|
+
* see isWholeClassName.
|
|
41
|
+
*
|
|
42
|
+
* Any other helper is opaque: it may return a class name, but what it does
|
|
43
|
+
* with its arguments is unknown, so those arguments are left alone.
|
|
44
|
+
* `{{scopedClass "..."}}` is the way to rename a literal such a helper
|
|
45
|
+
* receives.
|
|
46
|
+
*
|
|
47
|
+
* Matching is by name, so a block param that shadows one of these is checked
|
|
48
|
+
* for -- see isShadowed.
|
|
49
|
+
*/
|
|
50
|
+
const CLASS_BUILDING_HELPERS = new Set(['if', 'unless', 'concat']);
|
|
51
|
+
|
|
52
|
+
function endsAtClassBoundary(value) {
|
|
53
|
+
return /\s$/.test(value);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
function startsAtClassBoundary(value) {
|
|
57
|
+
return /^\s/.test(value);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Whether the param at `index` sits in a whole-class-name position rather
|
|
62
|
+
* than fused onto a neighbour -- true whether that param is itself a literal
|
|
63
|
+
* or a class-building call, since a call is renamed by looking inside it
|
|
64
|
+
* (see renameConcatParams), not by postfixing its result. Postfixing a
|
|
65
|
+
* fused fragment would bury the postfix in the middle of the class the
|
|
66
|
+
* fragments build. A neighbour whose value is only known at runtime bounds
|
|
67
|
+
* nothing of its own, since it could begin or end with anything.
|
|
68
|
+
*
|
|
69
|
+
* {{concat "a" " " "b"}} -> "a" and "b", the space separates them
|
|
70
|
+
* {{concat "a" "-suffix"}} -> neither, the result is the one class a-suffix
|
|
71
|
+
* {{concat "a" this.x}} -> not "a", it fuses with an unknown value
|
|
72
|
+
* {{concat "a " this.x}} -> "a ", its own trailing space ends it
|
|
73
|
+
*/
|
|
74
|
+
function isWholeClassName(params, index) {
|
|
75
|
+
// Only a StringLiteral has a value of its own to test for a boundary; a
|
|
76
|
+
// call or a path contributes no boundary and is bounded by its neighbours
|
|
77
|
+
// alone.
|
|
78
|
+
const param = params[index];
|
|
79
|
+
const value = param.type === 'StringLiteral' ? param.value : '';
|
|
80
|
+
const previous = params[index - 1];
|
|
81
|
+
const next = params[index + 1];
|
|
82
|
+
|
|
83
|
+
const boundedLeft =
|
|
84
|
+
index === 0 ||
|
|
85
|
+
startsAtClassBoundary(value) ||
|
|
86
|
+
(previous.type === 'StringLiteral' && endsAtClassBoundary(previous.value));
|
|
87
|
+
|
|
88
|
+
const boundedRight =
|
|
89
|
+
index === params.length - 1 ||
|
|
90
|
+
endsAtClassBoundary(value) ||
|
|
91
|
+
(next.type === 'StringLiteral' && startsAtClassBoundary(next.value));
|
|
92
|
+
|
|
93
|
+
return boundedLeft && boundedRight;
|
|
94
|
+
}
|
|
95
|
+
|
|
33
96
|
export function templatePlugin({ classes, tags, attributes, postfix }) {
|
|
34
97
|
let stack = [];
|
|
35
98
|
// scoped-class is a global we allow in hbs
|
|
@@ -42,9 +105,174 @@ export function templatePlugin({ classes, tags, attributes, postfix }) {
|
|
|
42
105
|
return scopedClassCandidates.some((candidate) => candidate === str);
|
|
43
106
|
}
|
|
44
107
|
|
|
108
|
+
/**
|
|
109
|
+
* Stack of ElementNode ancestors that introduce a block param, e.g.
|
|
110
|
+
* `<Foo as |if|>` -- pushed in ElementNode's own `enter` and popped in its
|
|
111
|
+
* own `exit` below. A `{{#each xs as |if|}}` block has no dedicated
|
|
112
|
+
* visitor, so it reaches `stack` through the generic `All` visitor
|
|
113
|
+
* instead; ElementNode has its own visitor, so `All` never runs for it,
|
|
114
|
+
* and this tracks it separately.
|
|
115
|
+
*/
|
|
116
|
+
let elementScope = [];
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* Whether `name` refers to the helper it appears to. A block param wins
|
|
120
|
+
* over both a helper and a keyword, so `{{#each xs as |if|}}` puts
|
|
121
|
+
* something else entirely behind the name for the length of the block.
|
|
122
|
+
*/
|
|
123
|
+
function isShadowed(name) {
|
|
124
|
+
return (
|
|
125
|
+
stack.some((ancestor) => ancestor.blockParams?.includes(name)) ||
|
|
126
|
+
elementScope.some((element) => element.blockParams.includes(name))
|
|
127
|
+
);
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
function isClassBuilding(name) {
|
|
131
|
+
return CLASS_BUILDING_HELPERS.has(name) && !isShadowed(name);
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
function renameConcatParams(node) {
|
|
135
|
+
const params = node.params ?? [];
|
|
136
|
+
|
|
137
|
+
for (const [index, param] of params.entries()) {
|
|
138
|
+
if (!isWholeClassName(params, index)) continue;
|
|
139
|
+
|
|
140
|
+
if (param.type === 'StringLiteral') {
|
|
141
|
+
param.value = renameClass(param.value, postfix, classes);
|
|
142
|
+
} else {
|
|
143
|
+
renameLiteralClasses(param);
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
/**
|
|
149
|
+
* Whether `node` is a `concat` call sitting in the class attribute's value
|
|
150
|
+
* itself, where the attribute joins the call's result into its own string.
|
|
151
|
+
* A `concat` nested any deeper -- an `if` branch, another `concat`'s
|
|
152
|
+
* params -- has to stay one value, so it keeps its call.
|
|
153
|
+
*/
|
|
154
|
+
function isAttributeLevelConcat(node) {
|
|
155
|
+
return (
|
|
156
|
+
node.type === 'MustacheStatement' &&
|
|
157
|
+
getValue(node.path) === 'concat' &&
|
|
158
|
+
isClassBuilding('concat')
|
|
159
|
+
);
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
/**
|
|
163
|
+
* `concat(concat(x, y), z)` joins to the same string as `concat(x, y, z)`
|
|
164
|
+
* -- string concatenation doesn't care how its inputs were grouped -- so a
|
|
165
|
+
* `concat` param that is itself an unshadowed `concat` call contributes
|
|
166
|
+
* its own params in its place rather than surviving as a nested call.
|
|
167
|
+
*/
|
|
168
|
+
function flattenConcatParams(params) {
|
|
169
|
+
return params.flatMap((param) =>
|
|
170
|
+
param.type === 'SubExpression' &&
|
|
171
|
+
getValue(param.path) === 'concat' &&
|
|
172
|
+
isClassBuilding('concat')
|
|
173
|
+
? flattenConcatParams(param.params ?? [])
|
|
174
|
+
: [param],
|
|
175
|
+
);
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
/**
|
|
179
|
+
* `concat`'s own job -- joining its params into one string -- is exactly
|
|
180
|
+
* what an attribute value already does natively: adjacent literal params
|
|
181
|
+
* join into one `TextNode`, and each other param becomes its own part. So
|
|
182
|
+
* a `concat` the attribute would join anyway can be spliced into the parts
|
|
183
|
+
* it's equivalent to, leaving the existing per-part handling (below) to
|
|
184
|
+
* treat it as it would a hand-written attribute string.
|
|
185
|
+
*
|
|
186
|
+
* {{concat x y z}} -> {{x}}{{y}}{{z}}
|
|
187
|
+
* {{concat (if x y z) " " (if a b c)}} -> {{if x y z}} {{if a b c}}
|
|
188
|
+
* {{concat "a" "-suffix"}} -> the single TextNode a-suffix
|
|
189
|
+
* {{concat (concat "a" " " "b") "c"}} -> {{concat "a" " " "b"}}'s own
|
|
190
|
+
* params joined in directly, not
|
|
191
|
+
* wrapped as a surviving call
|
|
192
|
+
*/
|
|
193
|
+
function concatParts(node) {
|
|
194
|
+
const parts = [];
|
|
195
|
+
let pendingText = '';
|
|
196
|
+
|
|
197
|
+
for (const param of flattenConcatParams(node.params ?? [])) {
|
|
198
|
+
if (param.type === 'StringLiteral') {
|
|
199
|
+
pendingText += param.value;
|
|
200
|
+
|
|
201
|
+
continue;
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
if (pendingText) {
|
|
205
|
+
parts.push(recast.builders.text(pendingText));
|
|
206
|
+
pendingText = '';
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
parts.push(
|
|
210
|
+
param.type === 'SubExpression'
|
|
211
|
+
? recast.builders.mustache(param.path, param.params, param.hash)
|
|
212
|
+
: recast.builders.mustache(param),
|
|
213
|
+
);
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
// A ConcatStatement needs at least one part, and joining no params -- or
|
|
217
|
+
// only empty ones -- still gives the empty string to carry.
|
|
218
|
+
if (pendingText || parts.length === 0) {
|
|
219
|
+
parts.push(recast.builders.text(pendingText));
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
return parts;
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
/**
|
|
226
|
+
* Rename the string literals whose value reaches the class attribute.
|
|
227
|
+
*
|
|
228
|
+
* {{if x "a" "b"}} -> both branches
|
|
229
|
+
* {{if (checkAlphabet "a" "b") "a" "b"}} -> the branches, not the condition
|
|
230
|
+
* {{if x (concat "a" " " "b") "c"}} -> concat's own whole-class-name params too
|
|
231
|
+
* {{if x (someHelper "a") "b"}} -> someHelper is opaque, left alone
|
|
232
|
+
* {{concat "a" " " "b"}} -> every param that's a whole class name
|
|
233
|
+
* {{this.fooClass}} -> resolved at runtime, nothing to rename
|
|
234
|
+
*
|
|
235
|
+
* The shadowing checked at each depth is the one in effect at the element
|
|
236
|
+
* carrying the attribute: only a block or an element can introduce a block
|
|
237
|
+
* param, and neither can appear inside a subexpression, so no name can be
|
|
238
|
+
* rebound part-way down a class attribute's value.
|
|
239
|
+
*/
|
|
240
|
+
function renameLiteralClasses(node) {
|
|
241
|
+
const name = getValue(node.path);
|
|
242
|
+
|
|
243
|
+
if (!isClassBuilding(name)) return;
|
|
244
|
+
|
|
245
|
+
if (name === 'concat') {
|
|
246
|
+
renameConcatParams(node);
|
|
247
|
+
|
|
248
|
+
return;
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
for (let param of node.params.slice(1)) {
|
|
252
|
+
if (param.type === 'StringLiteral') {
|
|
253
|
+
param.value = renameClass(param.value, postfix, classes);
|
|
254
|
+
} else {
|
|
255
|
+
renameLiteralClasses(param);
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
|
|
45
260
|
return {
|
|
46
261
|
AttrNode(node) {
|
|
47
262
|
if (node.name === 'class') {
|
|
263
|
+
if (isAttributeLevelConcat(node.value)) {
|
|
264
|
+
node.value = recast.builders.concat(concatParts(node.value));
|
|
265
|
+
node.quoteType = '"';
|
|
266
|
+
} else if (node.value.type === 'ConcatStatement') {
|
|
267
|
+
// Quoting always produces a ConcatStatement, even for a value
|
|
268
|
+
// that's nothing but a single concat call (`class="{{concat
|
|
269
|
+
// ...}}"`), so the concat to splice away can be any one of the
|
|
270
|
+
// parts rather than the whole value.
|
|
271
|
+
node.value.parts = node.value.parts.flatMap((part) =>
|
|
272
|
+
isAttributeLevelConcat(part) ? concatParts(part) : part,
|
|
273
|
+
);
|
|
274
|
+
}
|
|
275
|
+
|
|
48
276
|
if (node.value.type === 'TextNode' && node.value.chars) {
|
|
49
277
|
const renamedClass = renameClass(node.value.chars, postfix, classes);
|
|
50
278
|
|
|
@@ -56,53 +284,64 @@ export function templatePlugin({ classes, tags, attributes, postfix }) {
|
|
|
56
284
|
|
|
57
285
|
part.chars = renamedClass;
|
|
58
286
|
} else if (part.type === 'MustacheStatement') {
|
|
59
|
-
|
|
60
|
-
StringLiteral(node) {
|
|
61
|
-
const renamedClass = renameClass(
|
|
62
|
-
node.value,
|
|
63
|
-
postfix,
|
|
64
|
-
classes,
|
|
65
|
-
);
|
|
66
|
-
|
|
67
|
-
node.value = renamedClass;
|
|
68
|
-
},
|
|
69
|
-
});
|
|
287
|
+
renameLiteralClasses(part);
|
|
70
288
|
}
|
|
71
289
|
}
|
|
290
|
+
} else if (node.value.type === 'MustacheStatement') {
|
|
291
|
+
// Glimmer parses a quoted attribute value as a ConcatStatement and
|
|
292
|
+
// an unquoted one as a bare MustacheStatement, so `class={{if x
|
|
293
|
+
// "a" "b"}}` lands here rather than in the branch above.
|
|
294
|
+
renameLiteralClasses(node.value);
|
|
72
295
|
}
|
|
73
296
|
}
|
|
74
297
|
},
|
|
75
298
|
|
|
76
|
-
ElementNode
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
299
|
+
ElementNode: {
|
|
300
|
+
enter(node) {
|
|
301
|
+
// Only elements that introduce a name need tracking, which also
|
|
302
|
+
// keeps the stack balanced when a visitor removes a node and skips
|
|
303
|
+
// its exit.
|
|
304
|
+
if (node.blockParams?.length) elementScope.push(node);
|
|
305
|
+
},
|
|
82
306
|
|
|
83
|
-
|
|
307
|
+
// Appending runs on the way out so that AttrNode has already seen the
|
|
308
|
+
// class attribute in its authored shape. Wrapping `class={{if ...}}` in
|
|
309
|
+
// a concat first would hand AttrNode a ConcatStatement, whose every
|
|
310
|
+
// string literal is a class name by assumption -- including an `if`
|
|
311
|
+
// condition's, which must not be renamed.
|
|
312
|
+
exit(node) {
|
|
313
|
+
if (elementScope.at(-1) === node) elementScope.pop();
|
|
84
314
|
|
|
85
|
-
|
|
86
|
-
|
|
315
|
+
// An element is in scope if its tag matches a tag selector, or if it
|
|
316
|
+
// carries an attribute named in a scoped attribute selector. We add the
|
|
317
|
+
// postfix class at most once regardless of how many things matched.
|
|
318
|
+
const shouldScope =
|
|
319
|
+
tags.has(node.tag) || elementHasScopedAttribute(node, attributes);
|
|
87
320
|
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
classAttr.value
|
|
101
|
-
|
|
102
|
-
recast.builders.text(' ' + postfix)
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
321
|
+
if (!shouldScope) return;
|
|
322
|
+
|
|
323
|
+
// check if class attribute already exists
|
|
324
|
+
const classAttr = node.attributes.find((attr) => attr.name === 'class');
|
|
325
|
+
|
|
326
|
+
if (!classAttr) {
|
|
327
|
+
// push class attribute
|
|
328
|
+
node.attributes.push(
|
|
329
|
+
recast.builders.attr('class', recast.builders.text(postfix)),
|
|
330
|
+
);
|
|
331
|
+
} else if (classAttr.value.type === 'TextNode') {
|
|
332
|
+
classAttr.value.chars += ' ' + postfix;
|
|
333
|
+
} else if (classAttr.value.type === 'ConcatStatement') {
|
|
334
|
+
// class="foo {{bar}}"
|
|
335
|
+
classAttr.value.parts.push(recast.builders.text(' ' + postfix));
|
|
336
|
+
} else {
|
|
337
|
+
// class={{this.foo}} — wrap in a concat so we can append the text part
|
|
338
|
+
classAttr.value = recast.builders.concat([
|
|
339
|
+
classAttr.value,
|
|
340
|
+
recast.builders.text(' ' + postfix),
|
|
341
|
+
]);
|
|
342
|
+
classAttr.quoteType = '"';
|
|
343
|
+
}
|
|
344
|
+
},
|
|
106
345
|
},
|
|
107
346
|
|
|
108
347
|
All: {
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest';
|
|
2
|
+
|
|
3
|
+
import rewriteHbs from './rewriteHbs.js';
|
|
4
|
+
|
|
5
|
+
const postfix = 'pfx';
|
|
6
|
+
|
|
7
|
+
const classes = new Set(['a', 'b']);
|
|
8
|
+
|
|
9
|
+
function rewrite(hbs) {
|
|
10
|
+
return rewriteHbs(hbs, classes, new Set(), postfix);
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
describe('scopedClass in a class attribute', () => {
|
|
14
|
+
it('collapses to a quoted literal unconditionally, without a CSS lookup', () => {
|
|
15
|
+
expect(
|
|
16
|
+
rewrite('<div class={{scopedClass "global-thing"}}></div>'),
|
|
17
|
+
).to.equal('<div class="global-thing_pfx"></div>');
|
|
18
|
+
});
|
|
19
|
+
});
|
|
@@ -0,0 +1,63 @@
|
|
|
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
|
+
describe('unless in a class attribute', () => {
|
|
18
|
+
it('renames both branches', () => {
|
|
19
|
+
expect(rewrite('<div class={{unless x "a" "b"}}></div>')).to.equal(
|
|
20
|
+
'<div class={{unless x "a_pfx" "b_pfx"}}></div>',
|
|
21
|
+
);
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
it('leaves a branch that is not in the CSS alone', () => {
|
|
25
|
+
expect(rewrite('<div class={{unless x "global-thing"}}></div>')).to.equal(
|
|
26
|
+
'<div class={{unless x "global-thing"}}></div>',
|
|
27
|
+
);
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
it('leaves the condition alone, even when it is a helper call', () => {
|
|
31
|
+
expect(
|
|
32
|
+
rewrite('<div class={{unless (checkAlphabet "a" "b") "a" "b"}}></div>'),
|
|
33
|
+
).to.equal(
|
|
34
|
+
'<div class={{unless (checkAlphabet "a" "b") "a_pfx" "b_pfx"}}></div>',
|
|
35
|
+
);
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
it('skips unless when it is an element block param', () => {
|
|
39
|
+
expect(
|
|
40
|
+
rewrite('<Foo as |unless|><div class={{unless y "a" "b"}}></div></Foo>'),
|
|
41
|
+
).to.equal('<Foo as |unless|><div class={{unless y "a" "b"}}></div></Foo>');
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
it('is shadowed independently of if', () => {
|
|
45
|
+
// if and unless share one set of class-building helper names, and the
|
|
46
|
+
// shadow check keys on the name rather than the set.
|
|
47
|
+
expect(
|
|
48
|
+
rewrite(
|
|
49
|
+
'{{#let q as |if|}}<div class={{if a "a" "b"}}></div><div class={{unless a "a" "b"}}></div>{{/let}}',
|
|
50
|
+
),
|
|
51
|
+
).to.equal(
|
|
52
|
+
'{{#let q as |if|}}<div class={{if a "a" "b"}}></div><div class={{unless a "a_pfx" "b_pfx"}}></div>{{/let}}',
|
|
53
|
+
);
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
it('reaches whole-class-name params of a concat branch, same as if', () => {
|
|
57
|
+
expect(
|
|
58
|
+
rewrite('<div class={{unless x (concat "a" " " "b") "c"}}></div>'),
|
|
59
|
+
).to.equal(
|
|
60
|
+
'<div class={{unless x (concat "a_pfx" " " "b_pfx") "c"}}></div>',
|
|
61
|
+
);
|
|
62
|
+
});
|
|
63
|
+
});
|