Visitar URL original
Prevent the addition of space in `type()` with `+` (#19516) · prettier/prettier@a4e6f7a · GitHub
Skip to content

Commit a4e6f7a

Browse files
bigandyfisker
andauthored
Prevent the addition of space in type() with + (#19516)
Co-authored-by: fisker Cheung <lionkay@gmail.com>
1 parent 3d063b5 commit a4e6f7a

5 files changed

Lines changed: 114 additions & 0 deletions

File tree

‎changelog_unreleased/css/19516.md‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#### Prevent addition space in `type()` with `+` (#19516 by @bigandy)
2+
3+
This fixes the addition space before `+` in CSS `type()` declaration. For example `type(<number>+)` was being converted into `type(<number> +)` which is invalid CSS and does not work.
4+
5+
<!-- prettier-ignore -->
6+
```css
7+
/* Input */
8+
div {
9+
border-radius: attr(br type(<length>+));
10+
}
11+
12+
/* Prettier stable */
13+
div {
14+
border-radius: attr(br type(<length> +));
15+
}
16+
17+
/* Prettier main */
18+
div {
19+
border-radius: attr(br type(<length>+));
20+
}
21+
```

‎src/language-css/print/comma-separated-value-group.js‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,17 @@ function printCommaSeparatedValueGroup(path, options, print) {
311311
continue;
312312
}
313313

314+
// Prevent the addition of a space inside type() declarations that include an + operator
315+
// due to the fact that it is not valid syntax
316+
// (i.e. `type(<number> +)`, `type(<color> +)`)
317+
if (
318+
isAdditionNode(iNextNode) &&
319+
insideValueFunctionNode(path, "type") &&
320+
hasEmptyRawBefore(iNextNode)
321+
) {
322+
continue;
323+
}
324+
314325
// absolute paths are only parsed as one token if they are part of url(/abs/path) call
315326
// but if you have custom -fb-url(/abs/path/) then it is parsed as "division /" and rest
316327
// of the path. We don't want to put a space after that first division in this case.
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
// Jest Snapshot v1, https://jestjs.io/docs/snapshot-testing
2+
3+
exports[`type.css format 1`] = `
4+
====================================options=====================================
5+
parsers: ["css"]
6+
printWidth: 80 (default) |
7+
=====================================input======================================
8+
div {
9+
border-radius: attr(br type(<length>+));
10+
}
11+
12+
div {
13+
background: attr(bg type(<color>+));
14+
}
15+
16+
div {
17+
background: attr(bg type(<color>#));
18+
}
19+
20+
div {
21+
margin: attr(data-margin type(<percentage> | <number> | auto));
22+
}
23+
24+
div {
25+
margin: attr(data-margin type(<percentage>+ | <number>+ | auto));
26+
}
27+
28+
div {
29+
aspect-ratio: attr(data-ar type(*));
30+
}
31+
32+
=====================================output=====================================
33+
div {
34+
border-radius: attr(br type(<length>+));
35+
}
36+
37+
div {
38+
background: attr(bg type(<color>+));
39+
}
40+
41+
div {
42+
background: attr(bg type(<color>#));
43+
}
44+
45+
div {
46+
margin: attr(data-margin type(<percentage> | <number> | auto));
47+
}
48+
49+
div {
50+
margin: attr(data-margin type(<percentage>+ | <number>+ | auto));
51+
}
52+
53+
div {
54+
aspect-ratio: attr(data-ar type(*));
55+
}
56+
57+
================================================================================
58+
`;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
runFormatTest(import.meta, ["css"]);
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
div {
2+
border-radius: attr(br type(<length>+));
3+
}
4+
5+
div {
6+
background: attr(bg type(<color>+));
7+
}
8+
9+
div {
10+
background: attr(bg type(<color>#));
11+
}
12+
13+
div {
14+
margin: attr(data-margin type(<percentage> | <number> | auto));
15+
}
16+
17+
div {
18+
margin: attr(data-margin type(<percentage>+ | <number>+ | auto));
19+
}
20+
21+
div {
22+
aspect-ratio: attr(data-ar type(*));
23+
}

0 commit comments

Comments
 (0)