Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -71,14 +71,18 @@ jobs:
run: |
git diff --name-only --exit-code

# Starting with Ubuntu 23+, a security feature prevents running Puppeteer
# Starting with Ubuntu 23+, a security feature prevents running Chromium
# by default. It needs to be disabled. Using the "easiest" option, see:
# https://chromium.googlesource.com/chromium/src/+/main/docs/security/apparmor-userns-restrictions.md
# https://github.com/puppeteer/puppeteer/pull/13196/files
- name: Disable AppArmor
run: echo 0 | sudo tee /proc/sys/kernel/apparmor_restrict_unprivileged_userns
if: matrix.is_base_node_version && matrix.is_base_os_version

- name: install Playwright browsers
run: npx playwright-core install --with-deps chromium webkit
if: matrix.is_base_node_version && matrix.is_base_os_version

# Basic tests
- name: test
run: npm run test --if-present $MODIFIED_WORKSPACES
Expand Down
19 changes: 17 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 15 additions & 3 deletions plugins/postcss-custom-functions/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,20 @@
# Changes to PostCSS Custom Functions

### Unreleased (patch)

- Support empty parameter default values
### Unreleased (minor)

- Support `@property` rules for local variables, including multi-name preludes and the shorthand form, and hoist registrations out of `@container`, `@scope` and `@starting-style`
- Support local variables and registrations inside conditional rules
- Resolve local variables, `inherit()` and CSS-wide keywords in function arguments, `if()` conditions and `style()` query values
- Support empty `@function` bodies and ignore `!important` declarations in them
- Treat `@function` definitions inside `@container`, `@scope` and `@starting-style` as unconditional, matching browsers
- Keep parameter registrations and values when a local variable shadows a typed parameter or a local `@property` uses the parameter name
- Keep empty `@layer` blocks when removing unused definitions and order `@layer` rules inside `@scope` as global layers
- Preserve cascade order when definitions of the same name are inlined and do not evaluate cycles in inactive conditional rules, unused fallbacks or losing declarations
- Leave calls untouched when a definition can not be transpiled, an argument has an invalid `<declaration-value>`, or a typed parameter in a font-relative property can not be resolved at build time
- Keep functions referenced from `@apply` and `@mixin` preludes
- Support trailing commas, whitespace and empty arguments in dashed-function calls
- Treat `--x:` as an empty default value, reject parameter defaults that mix `{}` blocks with other tokens, and suppress defaults when a CSS-wide keyword can be selected through a `var()` fallback
- Use `@csstools/generate-idents`, `@csstools/utilities-for-property-registrations` and `@csstools/custom-function-parser` for shared implementation

### 1.0.1

Expand Down
32 changes: 20 additions & 12 deletions plugins/postcss-custom-functions/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,27 +14,34 @@ This plugin is only a partial implementation to avoid conflicts with the final s
Unsupported:
- typed parameters without default values
- return types other than the default `type(*)`
- `@function` definitions inside `@container` or `@scope`
- `@function` definitions inside a `@layer` that is itself inside a conditional group rule
- `@function` definitions inside a style rule
- definitions whose body contains anything other than declarations and conditional group rules
- `result` descriptors that resolve to a CSS-wide keyword through a conditional rule or a `var()` fallback
- definitions whose body contains anything other than declarations, `@property` rules and conditional group rules
- typed parameters in `font-size`, `font` or `line-height` when arguments contain font-relative units or run-time values
- arguments that contain top-level `!` or `;`
- `result` descriptors that resolve to a CSS-wide keyword, unless the definition is unconditional and the winning `result` descriptor is a static keyword
- functions whose body uses `attr()` together with local variables or parameters
- validation that a typed default value matches its parameter type

Definitions inside `@layer`, `@media` and `@supports` are supported. When a name
has several active definitions, the browser cascade selects the strongest one.
Typed local variables that shadow a typed parameter are registered with the parameter syntax and no initial value. Browsers that do not support an omitted `initial-value` will ignore those registrations.

Unsupported definitions are left as-is. A definition is only removed when every
call site is transformed by this plugin, so call sites that are left as-is (for
example inside `@keyframes` or a nested conditional declaration) keep their
definitions.
Definitions inside `@layer`, `@media`, `@supports`, `@container`, `@scope` and
`@starting-style` are supported. Container queries, `@scope` and
`@starting-style` do not constrain a name definition, so definitions inside them
are unconditional. When a name has several active definitions, the browser
cascade selects the strongest one.

Unsupported definitions are left as-is. When any definition of a name can not be
transpiled, every call to that name is left as-is as well.

Run `@csstools/postcss-private-rule` before this plugin so that function bodies do not capture private properties from the calling style rule.

Untyped parameters with default values are supported and may use any value,
including relative units and `var()` references.

Typed parameters with default values are checked by the browser through a
generated `@property` registration. A default value that is not
computationally independent (for example `1em` or `var(--x)`) can not be used
computationally independent (for example `1em`, `var(--x)` or `--fn()`) can not be used
as an `initial-value` and makes the function unsupported.

```css
Expand Down Expand Up @@ -63,9 +70,10 @@ html {
--_csstools-cf-1gice1e-2-result: calc(2 * var(--_csstools-cf-1gice1e-3-arg-0));
margin: var(--_csstools-cf-1gice1e-2-result);
}

@property --_csstools-cf-1gice1e-3-arg-0 {
syntax: "<length>";
inherits: false;
inherits: true;
initial-value: 1px;
}
```
Expand Down Expand Up @@ -150,7 +158,7 @@ html {

@property --_csstools-cf-1gice1e-3-arg-0 {
syntax: "<length>";
inherits: false;
inherits: true;
initial-value: 1px;
}
```
Expand Down
2 changes: 1 addition & 1 deletion plugins/postcss-custom-functions/dist/index.mjs

Large diffs are not rendered by default.

27 changes: 17 additions & 10 deletions plugins/postcss-custom-functions/docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,27 +25,34 @@ This plugin is only a partial implementation to avoid conflicts with the final s
Unsupported:
- typed parameters without default values
- return types other than the default `type(*)`
- `@function` definitions inside `@container` or `@scope`
- `@function` definitions inside a `@layer` that is itself inside a conditional group rule
- `@function` definitions inside a style rule
- definitions whose body contains anything other than declarations and conditional group rules
- `result` descriptors that resolve to a CSS-wide keyword through a conditional rule or a `var()` fallback
- definitions whose body contains anything other than declarations, `@property` rules and conditional group rules
- typed parameters in `font-size`, `font` or `line-height` when arguments contain font-relative units or run-time values
- arguments that contain top-level `!` or `;`
- `result` descriptors that resolve to a CSS-wide keyword, unless the definition is unconditional and the winning `result` descriptor is a static keyword
- functions whose body uses `attr()` together with local variables or parameters
- validation that a typed default value matches its parameter type

Definitions inside `@layer`, `@media` and `@supports` are supported. When a name
has several active definitions, the browser cascade selects the strongest one.
Typed local variables that shadow a typed parameter are registered with the parameter syntax and no initial value. Browsers that do not support an omitted `initial-value` will ignore those registrations.

Unsupported definitions are left as-is. A definition is only removed when every
call site is transformed by this plugin, so call sites that are left as-is (for
example inside `@keyframes` or a nested conditional declaration) keep their
definitions.
Definitions inside `@layer`, `@media`, `@supports`, `@container`, `@scope` and
`@starting-style` are supported. Container queries, `@scope` and
`@starting-style` do not constrain a name definition, so definitions inside them
are unconditional. When a name has several active definitions, the browser
cascade selects the strongest one.

Unsupported definitions are left as-is. When any definition of a name can not be
transpiled, every call to that name is left as-is as well.

Run `@csstools/postcss-private-rule` before this plugin so that function bodies do not capture private properties from the calling style rule.

Untyped parameters with default values are supported and may use any value,
including relative units and `var()` references.

Typed parameters with default values are checked by the browser through a
generated `@property` registration. A default value that is not
computationally independent (for example `1em` or `var(--x)`) can not be used
computationally independent (for example `1em`, `var(--x)` or `--fn()`) can not be used
as an `initial-value` and makes the function unsupported.

```css
Expand Down
6 changes: 4 additions & 2 deletions plugins/postcss-custom-functions/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,16 @@
"@csstools/cascade-layer-name-parser": "^3.0.2",
"@csstools/css-parser-algorithms": "^4.0.2",
"@csstools/css-tokenizer": "^4.0.2",
"@csstools/custom-function-parser": "^1.0.0"
"@csstools/custom-function-parser": "^1.0.0",
"@csstools/generate-idents": "^0.0.0",
"@csstools/utilities-for-property-registrations": "^0.0.0"
},
"peerDependencies": {
"postcss": "^8.4"
},
"devDependencies": {
"@csstools/postcss-tape": "*",
"puppeteer": "^25.14.0"
"playwright-core": "^1.65.0"
},
"scripts": {
"build": "rollup -c ../../rollup/default.mjs",
Expand Down
43 changes: 39 additions & 4 deletions plugins/postcss-custom-functions/src/cascade-layers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ import { parse as parseCascadeLayerNames, addLayerToModel } from '@csstools/casc

const implicitLayerNameForCloning = parseCascadeLayerNames('csstools-implicit-layer')[0];

// These at-rules do not constrain name-defining at-rules, so a layer inside
// them is a global layer, exactly as if it were declared outside of them.
const TRANSPARENT_LAYER_ANCESTORS = new Set(['container', 'scope', 'starting-style']);

export function collectCascadeLayerOrder(root: Root): WeakMap<Node, number> {
const references: Map<Node, LayerName> = new Map();
const referencesForLayerNames: Map<Node, LayerName> = new Map();
Expand All @@ -17,17 +21,27 @@ export function collectCascadeLayerOrder(root: Root): WeakMap<Node, number> {
}

{
// We do not want to process anything except for `@layer` rules
// and maybe `@layer` rules inside other `@later` rules.
// We do not want to process anything except for `@layer` rules,
// `@layer` rules inside other `@layer` rules and `@layer` rules
// inside element-sensitive group rules, which do not constrain them.
//
// Traverse up the tree and abort when we find something unexpected
let parent: Container | Document | undefined = node.parent;
while (parent) {
if (parent.type === 'atrule' && (parent as AtRule).name.toLowerCase() === 'layer') {
if (parent.type === 'rule') {
// A `@layer` nested in a style rule is a global layer.
parent = parent.parent;
continue;
}

if (parent.type === 'atrule') {
const name = (parent as AtRule).name.toLowerCase();
if (name === 'layer' || TRANSPARENT_LAYER_ANCESTORS.has(name)) {
parent = parent.parent;
continue;
}
}

if (parent === node.root()) {
break;
}
Expand All @@ -54,8 +68,29 @@ export function collectCascadeLayerOrder(root: Root): WeakMap<Node, number> {
// Stitch the layer names of the current node together with those of ancestors.
// @layer foo { @layer bar { .any {} } }
// -> "foo.bar"
//
// Element-sensitive group rules and style rules are transparent.
let parent: Container | Document | undefined = node.parent;
while (parent && parent.type === 'atrule' && (parent as AtRule).name.toLowerCase() === 'layer') {
while (parent) {
if (parent.type === 'rule') {
parent = parent.parent;
continue;
}

if (parent.type !== 'atrule') {
break;
}

const name = (parent as AtRule).name.toLowerCase();
if (TRANSPARENT_LAYER_ANCESTORS.has(name)) {
parent = parent.parent;
continue;
}

if (name !== 'layer') {
break;
}

const parentLayerName = referencesForLayerNames.get(parent);
if (!parentLayerName) {
parent = parent.parent;
Expand Down
Loading
Loading