diff --git a/changelog_unreleased/css/19516.md b/changelog_unreleased/css/19516.md new file mode 100644 index 000000000..ef249cf2e --- /dev/null +++ b/changelog_unreleased/css/19516.md @@ -0,0 +1,21 @@ +#### Prevent addition space in `type()` with `+` (#19516 by @bigandy) + +This fixes the addition space before `+` in CSS `type()` declaration. For example `type(+)` was being converted into `type( +)` which is invalid CSS and does not work. + + +```css +/* Input */ +div { + border-radius: attr(br type(+)); +} + +/* Prettier stable */ +div { + border-radius: attr(br type( +)); +} + +/* Prettier main */ +div { + border-radius: attr(br type(+)); +} +``` diff --git a/src/language-css/print/comma-separated-value-group.js b/src/language-css/print/comma-separated-value-group.js index 6447c7607..0b60240aa 100644 --- a/src/language-css/print/comma-separated-value-group.js +++ b/src/language-css/print/comma-separated-value-group.js @@ -311,6 +311,17 @@ function printCommaSeparatedValueGroup(path, options, print) { continue; } + // Prevent the addition of a space inside type() declarations that include an + operator + // due to the fact that it is not valid syntax + // (i.e. `type( +)`, `type( +)`) + if ( + isAdditionNode(iNextNode) && + insideValueFunctionNode(path, "type") && + hasEmptyRawBefore(iNextNode) + ) { + continue; + } + // absolute paths are only parsed as one token if they are part of url(/abs/path) call // but if you have custom -fb-url(/abs/path/) then it is parsed as "division /" and rest // of the path. We don't want to put a space after that first division in this case. diff --git a/tests/format/css/type-function/__snapshots__/format.test.js.snap b/tests/format/css/type-function/__snapshots__/format.test.js.snap new file mode 100644 index 000000000..7db5ab652 --- /dev/null +++ b/tests/format/css/type-function/__snapshots__/format.test.js.snap @@ -0,0 +1,58 @@ +// Jest Snapshot v1, https://jestjs.io/docs/snapshot-testing + +exports[`type.css format 1`] = ` +====================================options===================================== +parsers: ["css"] + printWidth: 80 (default) | +=====================================input====================================== +div { + border-radius: attr(br type(+)); +} + +div { + background: attr(bg type(+)); +} + +div { + background: attr(bg type(#)); +} + +div { + margin: attr(data-margin type( | | auto)); +} + +div { + margin: attr(data-margin type(+ | + | auto)); +} + +div { + aspect-ratio: attr(data-ar type(*)); +} + +=====================================output===================================== +div { + border-radius: attr(br type(+)); +} + +div { + background: attr(bg type(+)); +} + +div { + background: attr(bg type(#)); +} + +div { + margin: attr(data-margin type( | | auto)); +} + +div { + margin: attr(data-margin type(+ | + | auto)); +} + +div { + aspect-ratio: attr(data-ar type(*)); +} + +================================================================================ +`; diff --git a/tests/format/css/type-function/format.test.js b/tests/format/css/type-function/format.test.js new file mode 100644 index 000000000..96617399e --- /dev/null +++ b/tests/format/css/type-function/format.test.js @@ -0,0 +1 @@ +runFormatTest(import.meta, ["css"]); diff --git a/tests/format/css/type-function/type.css b/tests/format/css/type-function/type.css new file mode 100644 index 000000000..bdb6b90ad --- /dev/null +++ b/tests/format/css/type-function/type.css @@ -0,0 +1,23 @@ +div { + border-radius: attr(br type(+)); +} + +div { + background: attr(bg type(+)); +} + +div { + background: attr(bg type(#)); +} + +div { + margin: attr(data-margin type( | | auto)); +} + +div { + margin: attr(data-margin type(+ | + | auto)); +} + +div { + aspect-ratio: attr(data-ar type(*)); +}