This commit is contained in:
Iliyan Angelov
2025-09-14 23:24:25 +03:00
commit c67067a2a4
71311 changed files with 6800714 additions and 0 deletions

View File

@@ -0,0 +1,39 @@
# Changes to PostCSS Cascade Layers
### 1.1.1 (September 17, 2022)
- Fix pre-defined layer order in nested `@layer` rules.
### 1.1.0 (September 14, 2022)
- Add support for `@scope` and `@container` as parent rules for `@layer`
### 1.0.6 (September 7, 2022)
- Fix broken `@keyframes` in `@layer`.
### 1.0.5 (July 8, 2022)
- Fix case insensitive `@layer` matching (`@LaYeR`).
- Updated `@csstools/selector-specificity` to `2.0.2` (patch)
### 1.0.4 (June 23, 2022)
- Fix selector order with any pseudo element. This plugin will no longer re-order selectors.
### 1.0.3 (June 4, 2022)
- Update `@csstools/selector-specificity` (major)
### 1.0.2 (May 20, 2022)
- Use only simple `:not(#\#)` selectors to adjust specificity.
### 1.0.1 (May 17, 2022)
- Process CSS after most other plugins to ensure correct analysis and transformation of sugary CSS.
- Fix selector order with `:before` and other pseudo elements.
### 1.0.0 (May 12, 2022)
- Initial version

View File

@@ -0,0 +1,108 @@
# CC0 1.0 Universal
## Statement of Purpose
The laws of most jurisdictions throughout the world automatically confer
exclusive Copyright and Related Rights (defined below) upon the creator and
subsequent owner(s) (each and all, an “owner”) of an original work of
authorship and/or a database (each, a “Work”).
Certain owners wish to permanently relinquish those rights to a Work for the
purpose of contributing to a commons of creative, cultural and scientific works
(“Commons”) that the public can reliably and without fear of later claims of
infringement build upon, modify, incorporate in other works, reuse and
redistribute as freely as possible in any form whatsoever and for any purposes,
including without limitation commercial purposes. These owners may contribute
to the Commons to promote the ideal of a free culture and the further
production of creative, cultural and scientific works, or to gain reputation or
greater distribution for their Work in part through the use and efforts of
others.
For these and/or other purposes and motivations, and without any expectation of
additional consideration or compensation, the person associating CC0 with a
Work (the “Affirmer”), to the extent that he or she is an owner of Copyright
and Related Rights in the Work, voluntarily elects to apply CC0 to the Work and
publicly distribute the Work under its terms, with knowledge of his or her
Copyright and Related Rights in the Work and the meaning and intended legal
effect of CC0 on those rights.
1. Copyright and Related Rights. A Work made available under CC0 may be
protected by copyright and related or neighboring rights (“Copyright and
Related Rights”). Copyright and Related Rights include, but are not limited
to, the following:
1. the right to reproduce, adapt, distribute, perform, display, communicate,
and translate a Work;
2. moral rights retained by the original author(s) and/or performer(s);
3. publicity and privacy rights pertaining to a persons image or likeness
depicted in a Work;
4. rights protecting against unfair competition in regards to a Work,
subject to the limitations in paragraph 4(i), below;
5. rights protecting the extraction, dissemination, use and reuse of data in
a Work;
6. database rights (such as those arising under Directive 96/9/EC of the
European Parliament and of the Council of 11 March 1996 on the legal
protection of databases, and under any national implementation thereof,
including any amended or successor version of such directive); and
7. other similar, equivalent or corresponding rights throughout the world
based on applicable law or treaty, and any national implementations
thereof.
2. Waiver. To the greatest extent permitted by, but not in contravention of,
applicable law, Affirmer hereby overtly, fully, permanently, irrevocably and
unconditionally waives, abandons, and surrenders all of Affirmers Copyright
and Related Rights and associated claims and causes of action, whether now
known or unknown (including existing as well as future claims and causes of
action), in the Work (i) in all territories worldwide, (ii) for the maximum
duration provided by applicable law or treaty (including future time
extensions), (iii) in any current or future medium and for any number of
copies, and (iv) for any purpose whatsoever, including without limitation
commercial, advertising or promotional purposes (the “Waiver”). Affirmer
makes the Waiver for the benefit of each member of the public at large and
to the detriment of Affirmers heirs and successors, fully intending that
such Waiver shall not be subject to revocation, rescission, cancellation,
termination, or any other legal or equitable action to disrupt the quiet
enjoyment of the Work by the public as contemplated by Affirmers express
Statement of Purpose.
3. Public License Fallback. Should any part of the Waiver for any reason be
judged legally invalid or ineffective under applicable law, then the Waiver
shall be preserved to the maximum extent permitted taking into account
Affirmers express Statement of Purpose. In addition, to the extent the
Waiver is so judged Affirmer hereby grants to each affected person a
royalty-free, non transferable, non sublicensable, non exclusive,
irrevocable and unconditional license to exercise Affirmers Copyright and
Related Rights in the Work (i) in all territories worldwide, (ii) for the
maximum duration provided by applicable law or treaty (including future time
extensions), (iii) in any current or future medium and for any number of
copies, and (iv) for any purpose whatsoever, including without limitation
commercial, advertising or promotional purposes (the “License”). The License
shall be deemed effective as of the date CC0 was applied by Affirmer to the
Work. Should any part of the License for any reason be judged legally
invalid or ineffective under applicable law, such partial invalidity or
ineffectiveness shall not invalidate the remainder of the License, and in
such case Affirmer hereby affirms that he or she will not (i) exercise any
of his or her remaining Copyright and Related Rights in the Work or (ii)
assert any associated claims and causes of action with respect to the Work,
in either case contrary to Affirmers express Statement of Purpose.
4. Limitations and Disclaimers.
1. No trademark or patent rights held by Affirmer are waived, abandoned,
surrendered, licensed or otherwise affected by this document.
2. Affirmer offers the Work as-is and makes no representations or warranties
of any kind concerning the Work, express, implied, statutory or
otherwise, including without limitation warranties of title,
merchantability, fitness for a particular purpose, non infringement, or
the absence of latent or other defects, accuracy, or the present or
absence of errors, whether or not discoverable, all to the greatest
extent permissible under applicable law.
3. Affirmer disclaims responsibility for clearing rights of other persons
that may apply to the Work or any use thereof, including without
limitation any persons Copyright and Related Rights in the Work.
Further, Affirmer disclaims responsibility for obtaining any necessary
consents, permissions or other rights required for any use of the Work.
4. Affirmer understands and acknowledges that Creative Commons is not a
party to this document and has no duty or obligation with respect to this
CC0 or use of the Work.
For more information, please see
http://creativecommons.org/publicdomain/zero/1.0/.

View File

@@ -0,0 +1,200 @@
# PostCSS Cascade Layers [<img src="https://postcss.github.io/postcss/logo.svg" alt="PostCSS Logo" width="90" height="90" align="right">][postcss]
[<img alt="npm version" src="https://img.shields.io/npm/v/@csstools/postcss-cascade-layers.svg" height="20">][npm-url] [<img alt="CSS Standard Status" src="https://cssdb.org/images/badges/cascade-layers.svg" height="20">][css-url] [<img alt="Build Status" src="https://github.com/csstools/postcss-plugins/workflows/test/badge.svg" height="20">][cli-url] [<img alt="Discord" src="https://shields.io/badge/Discord-5865F2?logo=discord&logoColor=white">][discord]
[PostCSS Cascade Layers] lets you use `@layer` following the [Cascade Layers Specification]. For more information on layers, checkout [A Complete Guide to CSS Cascade Layers] by Miriam Suzanne.
```pcss
target {
color: purple;
}
@layer {
target {
color: green;
}
}
/* becomes */
target:not(#\#) {
color: purple;
}
target {
color: green;
}
```
## How it works
[PostCSS Cascade Layers] creates "layers" of specificity.
It applies extra specificity on all your styles based on :
- the most specific selector found
- the order in which layers are defined
```css
@layer A, B;
@layer B {
.a-less-specific-selector {
/* styles */
}
}
@layer A {
#something #very-specific {
/* styles */
}
}
@layer C {
.a-less-specific-selector {
/* styles */
}
}
```
most specific selector :
- `#something #very-specific`
- `[2, 0, 0]`
- `2 + 1` -> `3` to ensure there is no overlap
the order in which layers are defined :
- `A`
- `B`
- `C`
| layer | previous adjustment | specificity adjustment | selector |
| ------ | ------ | ----------- | --- |
| `A` | `0` | `0 + 0 = 0` | N/A |
| `B` | `0` | `0 + 3 = 3` | `:not(#/#):not(#/#):not(#/#)` |
| `C` | `3` | `3 + 3 = 6` | `:not(#/#):not(#/#):not(#/#):not(#/#):not(#/#):not(#/#)` |
This approach lets more important (later) layers always override less important (earlier) layers.<br>
And layers have enough room internally so that each selector works and overrides as expected.
More layers with more specificity will cause longer `:not(...)` selectors to be generated.
⚠️ For this to work the plugin needs to analyze your entire stylesheet at once.<br>
If you have different assets that are unaware of each other it will not work correctly as the analysis will be incorrect.
## Usage
Add [PostCSS Cascade Layers] to your project:
```bash
npm install postcss @csstools/postcss-cascade-layers --save-dev
```
Use it as a [PostCSS] plugin:
```js
const postcss = require('postcss');
const postcssCascadeLayers = require('@csstools/postcss-cascade-layers');
postcss([
postcssCascadeLayers(/* pluginOptions */)
]).process(YOUR_CSS /*, processOptions */);
```
[PostCSS Cascade Layers] runs in all Node environments, with special
instructions for:
| [Node](INSTALL.md#node) | [PostCSS CLI](INSTALL.md#postcss-cli) | [Webpack](INSTALL.md#webpack) | [Create React App](INSTALL.md#create-react-app) | [Gulp](INSTALL.md#gulp) | [Grunt](INSTALL.md#grunt) |
| --- | --- | --- | --- | --- | --- |
## Options
### onRevertLayerKeyword
The `onRevertLayerKeyword` option enables warnings if `revert-layer` is used.
Transforming `revert-layer` for older browsers is not possible in this plugin.
Defaults to `warn`
```js
postcssCascadeLayers({ onRevertLayerKeyword: 'warn' }) // 'warn' | false
```
```pcss
/* [postcss-cascade-layers]: handling "revert-layer" is unsupported by this plugin and will cause style differences between browser versions. */
@layer {
.foo {
color: revert-layer;
}
}
```
### onConditionalRulesChangingLayerOrder
The `onConditionalRulesChangingLayerOrder` option enables warnings if layers are declared in multiple different orders in conditional rules.
Transforming these layers correctly for older browsers is not possible in this plugin.
Defaults to `warn`
```js
postcssCascadeLayers({ onConditionalRulesChangingLayerOrder: 'warn' }) // 'warn' | false
```
```pcss
/* [postcss-cascade-layers]: handling different layer orders in conditional rules is unsupported by this plugin and will cause style differences between browser versions. */
@media (min-width: 10px) {
@layer B {
.foo {
color: red;
}
}
}
@layer A {
.foo {
color: pink;
}
}
@layer B {
.foo {
color: red;
}
}
```
### onImportLayerRule
The `@import` at-rule can also be used with cascade layers, specifically to create a new layer like so:
```css
@import 'theme.css' layer(utilities);
```
If your CSS uses `@import` with layers, you will also need the [postcss-import] plugin. This plugin alone will not handle the `@import` at-rule.
This plugin will warn you when it detects that [postcss-import] did not transform`@import` at-rules.
```js
postcssCascadeLayers({ onImportLayerRule: 'warn' }) // 'warn' | false
```
### Contributors
The contributors to this plugin were [Olu Niyi-Awosusi] and [Sana Javed] from [Oddbird] and Romain Menke.
[cli-url]: https://github.com/csstools/postcss-plugins/actions/workflows/test.yml?query=workflow/test
[css-url]: https://cssdb.org/#cascade-layers
[discord]: https://discord.gg/bUadyRwkJS
[npm-url]: https://www.npmjs.com/package/@csstools/postcss-cascade-layers
[Gulp PostCSS]: https://github.com/postcss/gulp-postcss
[Grunt PostCSS]: https://github.com/nDmitry/grunt-postcss
[PostCSS]: https://github.com/postcss/postcss
[PostCSS Loader]: https://github.com/postcss/postcss-loader
[PostCSS Cascade Layers]: https://github.com/csstools/postcss-plugins/tree/main/plugins/postcss-cascade-layers
[Cascade Layers Specification]: https://www.w3.org/TR/css-cascade-5/#layering
[A Complete Guide to CSS Cascade Layers]: https://css-tricks.com/css-cascade-layers/
[Olu Niyi-Awosusi]: https://github.com/oluoluoxenfree
[Sana Javed]: https://github.com/sanajaved7
[Oddbird]: https://github.com/oddbird
[postcss-import]: https://github.com/postcss/postcss-import

View File

@@ -0,0 +1 @@
export declare function adjustSelectorSpecificity(selector: string, amount: number): string;

View File

@@ -0,0 +1,3 @@
import type { Container } from 'postcss';
export declare function removeEmptyDescendantBlocks(block: Container): void;
export declare function removeEmptyAncestorBlocks(block: Container): void;

View File

@@ -0,0 +1,8 @@
/** @constant {string} INVALID_LAYER_NAME Used to replace "layer" temporarily when an invalid layer is detected. This allows us to ignore this rule in further processing. */
export declare const INVALID_LAYER_NAME = "csstools-invalid-layer";
/** @constant {string} WITH_SELECTORS_LAYER_NAME Used to replace "layer" temporarily for any layer at rules that contain selectors. This allows us to sort these differently from other layer at rules. */
export declare const WITH_SELECTORS_LAYER_NAME = "csstools-layer-with-selector-rules";
export declare const ANONYMOUS_LAYER_SUFFIX = "6efdb677-bb05-44e5-840f-29d2175862fd";
export declare const IMPLICIT_LAYER_SUFFIX = "b147acf6-11a6-4338-a4d0-80aef4cd1a2f";
export declare const CONDITIONAL_ATRULES: string[];
export declare const ATRULES_WITH_NON_SELECTOR_BLOCK_LISTS: string[];

View File

@@ -0,0 +1,3 @@
import type { Container } from 'postcss';
import type { Model } from './model';
export declare function desugarAndParseLayerNames(root: Container, model: Model): void;

View File

@@ -0,0 +1,3 @@
import type { Container, ChildNode } from 'postcss';
import type { Model } from './model';
export declare function desugarNestedLayers(root: Container<ChildNode>, model: Model): void;

View File

@@ -0,0 +1,2 @@
import type { AtRule } from 'postcss';
export declare function getConditionalAtRuleAncestor(layerRule: AtRule): AtRule | null;

View File

@@ -0,0 +1,2 @@
import type { AtRule, Node } from 'postcss';
export declare function getLayerAtRuleAncestor(node: Node): AtRule | null;

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,4 @@
import type { PluginCreator } from 'postcss';
import { pluginOptions } from './options';
declare const creator: PluginCreator<pluginOptions>;
export default creator;

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,2 @@
import type { AtRule } from 'postcss';
export declare function isProcessableLayerRule(atRule: AtRule): boolean;

View File

@@ -0,0 +1,18 @@
import type { AtRule } from 'postcss';
export declare class Model {
anonymousLayerCount: number;
layerCount: number;
layerOrder: Map<string, number>;
layerParamsParsed: Map<string, Array<string>>;
layerNameParts: Map<string, Array<string>>;
constructor();
createAnonymousLayerName(): string;
createImplicitLayerName(layerName: string): string;
addLayerParams(key: string, parts?: string): void;
addLayerParams(key: string, parts: Array<string>): void;
addLayerNameParts(parts: string): void;
addLayerNameParts(parts: Array<string>): void;
getLayerParams(layer: AtRule): Array<string>;
getLayerNameList(layerName: string): Array<string>;
sortLayerNames(): void;
}

View File

@@ -0,0 +1,5 @@
export declare type pluginOptions = {
onRevertLayerKeyword: 'warn' | false;
onConditionalRulesChangingLayerOrder: 'warn' | false;
onImportLayerRule: 'warn' | false;
};

View File

@@ -0,0 +1,7 @@
import type { Container, Result } from 'postcss';
import type { Model } from './model';
import { pluginOptions } from './options';
export declare function recordLayerOrder(root: Container, model: Model, { result, options }: {
result: Result;
options: pluginOptions;
}): void;

View File

@@ -0,0 +1,3 @@
import type { Container, ChildNode, AtRule } from 'postcss';
export declare function someInTree(container: Container, predicate: (node: ChildNode) => boolean): boolean;
export declare function someAtRuleInTree(container: Container, predicate: (node: AtRule) => boolean): boolean;

View File

@@ -0,0 +1,3 @@
import type { Container } from 'postcss';
import type { Model } from './model';
export declare function sortRootNodes(root: Container, model: Model): void;

View File

@@ -0,0 +1,2 @@
import type { Container } from 'postcss';
export declare function splitImportantStyles(root: Container): void;

View File

@@ -0,0 +1,97 @@
{
"name": "@csstools/postcss-cascade-layers",
"description": "Use cascade layers in CSS",
"version": "1.1.1",
"contributors": [
{
"name": "Olu Niyi-Awosusi",
"email": "o.niyiawosusi@gmail.com"
},
{
"name": "Sana Javed",
"email": "sanajaved7@gmail.com"
},
{
"name": "Antonio Laguna",
"email": "antonio@laguna.es",
"url": "https://antonio.laguna.es"
},
{
"name": "Romain Menke",
"email": "romainmenke@gmail.com"
}
],
"license": "CC0-1.0",
"funding": {
"type": "opencollective",
"url": "https://opencollective.com/csstools"
},
"engines": {
"node": "^12 || ^14 || >=16"
},
"main": "dist/index.cjs",
"module": "dist/index.mjs",
"types": "dist/index.d.ts",
"exports": {
".": {
"import": "./dist/index.mjs",
"require": "./dist/index.cjs",
"default": "./dist/index.mjs"
}
},
"files": [
"CHANGELOG.md",
"LICENSE.md",
"README.md",
"dist"
],
"dependencies": {
"@csstools/selector-specificity": "^2.0.2",
"postcss-selector-parser": "^6.0.10"
},
"peerDependencies": {
"postcss": "^8.2"
},
"devDependencies": {
"postcss-import": "^15.0.0",
"puppeteer": "^17.1.3"
},
"scripts": {
"build": "rollup -c ../../rollup/default.js",
"clean": "node -e \"fs.rmSync('./dist', { recursive: true, force: true });\"",
"docs": "node ../../.github/bin/generate-docs/install.mjs && node ../../.github/bin/generate-docs/readme.mjs",
"lint": "npm run lint:eslint && npm run lint:package-json",
"lint:eslint": "eslint ./src --ext .js --ext .ts --ext .mjs --no-error-on-unmatched-pattern",
"lint:package-json": "node ../../.github/bin/format-package-json.mjs",
"prepublishOnly": "npm run clean && npm run build && npm run test",
"test": "node .tape.mjs && npm run test:exports",
"test:browser": "node ./test/_browser.mjs",
"test:exports": "node ./test/_import.mjs && node ./test/_require.cjs",
"test:rewrite-expects": "REWRITE_EXPECTS=true node .tape.mjs"
},
"homepage": "https://github.com/csstools/postcss-plugins/tree/main/plugins/postcss-cascade-layers#readme",
"repository": {
"type": "git",
"url": "https://github.com/csstools/postcss-plugins.git",
"directory": "plugins/postcss-cascade-layers"
},
"bugs": "https://github.com/csstools/postcss-plugins/issues",
"keywords": [
"cascade",
"css",
"layers",
"postcss",
"postcss-plugin",
"selectors",
"specificity"
],
"csstools": {
"cssdbId": "cascade-layers",
"exportName": "postcssCascadeLayers",
"humanReadableName": "PostCSS Cascade Layers",
"specUrl": "https://www.w3.org/TR/css-cascade-5/#layering"
},
"volta": {
"extends": "../../package.json"
}
}