first commit

This commit is contained in:
monjack
2025-06-20 18:01:48 +08:00
commit 6daa6d65c1
24611 changed files with 2512443 additions and 0 deletions

View File

@ -0,0 +1,22 @@
Copyright (c) Ben Briggs <beneb.info@gmail.com> (http://beneb.info)
Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.

View File

@ -0,0 +1,42 @@
# [postcss][postcss]-minify-selectors
> Minify selectors with PostCSS.
## Install
With [npm](https://www.npmjs.com/package/postcss-minify-selectors) do:
```
npm install postcss-minify-selectors --save
```
## Example
### Input
```css
h1 + p, h2, h3, h2{color:blue}
```
### Output
```css
h1+p,h2,h3{color:blue}
```
For more examples see the [tests](test.js).
## Usage
See the [PostCSS documentation](https://github.com/postcss/postcss#usage) for
examples for your environment.
## Contributors
See [CONTRIBUTORS.md](https://github.com/cssnano/cssnano/blob/master/CONTRIBUTORS.md).
## License
MIT © [Ben Briggs](http://beneb.info)
[postcss]: https://github.com/postcss/postcss

View File

@ -0,0 +1,43 @@
{
"name": "postcss-minify-selectors",
"version": "5.2.1",
"description": "Minify selectors with PostCSS.",
"main": "src/index.js",
"types": "types/index.d.ts",
"files": [
"src",
"LICENSE-MIT",
"types"
],
"keywords": [
"css",
"minify",
"optimise",
"postcss",
"postcss-plugin",
"selectors"
],
"license": "MIT",
"homepage": "https://github.com/cssnano/cssnano",
"author": {
"name": "Ben Briggs",
"email": "beneb.info@gmail.com",
"url": "http://beneb.info"
},
"repository": "cssnano/cssnano",
"dependencies": {
"postcss-selector-parser": "^6.0.5"
},
"bugs": {
"url": "https://github.com/cssnano/cssnano/issues"
},
"engines": {
"node": "^10 || ^12 || >=14.0"
},
"devDependencies": {
"postcss": "^8.2.15"
},
"peerDependencies": {
"postcss": "^8.2.15"
}
}

View File

@ -0,0 +1,260 @@
'use strict';
const parser = require('postcss-selector-parser');
const canUnquote = require('./lib/canUnquote.js');
const pseudoElements = new Set([
'::before',
'::after',
'::first-letter',
'::first-line',
]);
/**
* @param {parser.Attribute} selector
* @return {void}
*/
function attribute(selector) {
if (selector.value) {
if (selector.raws.value) {
// Join selectors that are split over new lines
selector.raws.value = selector.raws.value.replace(/\\\n/g, '').trim();
}
if (canUnquote(selector.value)) {
selector.quoteMark = null;
}
if (selector.operator) {
selector.operator = /** @type {parser.AttributeOperator} */ (
selector.operator.trim()
);
}
}
selector.rawSpaceBefore = '';
selector.rawSpaceAfter = '';
selector.spaces.attribute = { before: '', after: '' };
selector.spaces.operator = { before: '', after: '' };
selector.spaces.value = {
before: '',
after: selector.insensitive ? ' ' : '',
};
if (selector.raws.spaces) {
selector.raws.spaces.attribute = {
before: '',
after: '',
};
selector.raws.spaces.operator = {
before: '',
after: '',
};
selector.raws.spaces.value = {
before: '',
after: selector.insensitive ? ' ' : '',
};
if (selector.insensitive) {
selector.raws.spaces.insensitive = {
before: '',
after: '',
};
}
}
selector.attribute = selector.attribute.trim();
}
/**
* @param {parser.Combinator} selector
* @return {void}
*/
function combinator(selector) {
const value = selector.value.trim();
selector.spaces.before = '';
selector.spaces.after = '';
selector.rawSpaceBefore = '';
selector.rawSpaceAfter = '';
selector.value = value.length ? value : ' ';
}
const pseudoReplacements = new Map([
[':nth-child', ':first-child'],
[':nth-of-type', ':first-of-type'],
[':nth-last-child', ':last-child'],
[':nth-last-of-type', ':last-of-type'],
]);
/**
* @param {parser.Pseudo} selector
* @return {void}
*/
function pseudo(selector) {
const value = selector.value.toLowerCase();
if (selector.nodes.length === 1 && pseudoReplacements.has(value)) {
const first = selector.at(0);
const one = first.at(0);
if (first.length === 1) {
if (one.value === '1') {
selector.replaceWith(
parser.pseudo({
value: /** @type {string} */ (pseudoReplacements.get(value)),
})
);
}
if (one.value && one.value.toLowerCase() === 'even') {
one.value = '2n';
}
}
if (first.length === 3) {
const two = first.at(1);
const three = first.at(2);
if (
one.value &&
one.value.toLowerCase() === '2n' &&
two.value === '+' &&
three.value === '1'
) {
one.value = 'odd';
two.remove();
three.remove();
}
}
return;
}
selector.walk((child) => {
if (child.type === 'selector' && child.parent) {
const uniques = new Set();
child.parent.each((sibling) => {
const siblingStr = String(sibling);
if (!uniques.has(siblingStr)) {
uniques.add(siblingStr);
} else {
sibling.remove();
}
});
}
});
if (pseudoElements.has(value)) {
selector.value = selector.value.slice(1);
}
}
const tagReplacements = new Map([
['from', '0%'],
['100%', 'to'],
]);
/**
* @param {parser.Tag} selector
* @return {void}
*/
function tag(selector) {
const value = selector.value.toLowerCase();
if (tagReplacements.has(value)) {
selector.value = /** @type {string} */ (tagReplacements.get(value));
}
}
/**
* @param {parser.Universal} selector
* @return {void}
*/
function universal(selector) {
const next = selector.next();
if (next && next.type !== 'combinator') {
selector.remove();
}
}
const reducers = new Map(
/** @type {[string, ((selector: parser.Node) => void)][]}*/ ([
['attribute', attribute],
['combinator', combinator],
['pseudo', pseudo],
['tag', tag],
['universal', universal],
])
);
/**
* @type {import('postcss').PluginCreator<void>}
* @return {import('postcss').Plugin}
*/
function pluginCreator() {
return {
postcssPlugin: 'postcss-minify-selectors',
OnceExit(css) {
const cache = new Map();
const processor = parser((selectors) => {
const uniqueSelectors = new Set();
selectors.walk((sel) => {
// Trim whitespace around the value
sel.spaces.before = sel.spaces.after = '';
const reducer = reducers.get(sel.type);
if (reducer !== undefined) {
reducer(sel);
return;
}
const toString = String(sel);
if (
sel.type === 'selector' &&
sel.parent &&
sel.parent.type !== 'pseudo'
) {
if (!uniqueSelectors.has(toString)) {
uniqueSelectors.add(toString);
} else {
sel.remove();
}
}
});
selectors.nodes.sort();
});
css.walkRules((rule) => {
const selector =
rule.raws.selector && rule.raws.selector.value === rule.selector
? rule.raws.selector.raw
: rule.selector;
// If the selector ends with a ':' it is likely a part of a custom mixin,
// so just pass through.
if (selector[selector.length - 1] === ':') {
return;
}
if (cache.has(selector)) {
rule.selector = cache.get(selector);
return;
}
const optimizedSelector = processor.processSync(selector);
rule.selector = optimizedSelector;
cache.set(selector, optimizedSelector);
});
},
};
}
pluginCreator.postcss = true;
module.exports = pluginCreator;

View File

@ -0,0 +1,24 @@
'use strict';
/**
* Can unquote attribute detection from mothereff.in
* Copyright Mathias Bynens <https://mathiasbynens.be/>
* https://github.com/mathiasbynens/mothereff.in
*/
const escapes = /\\([0-9A-Fa-f]{1,6})[ \t\n\f\r]?/g;
const range =
// eslint-disable-next-line no-control-regex
/[\u0000-\u002c\u002e\u002f\u003A-\u0040\u005B-\u005E\u0060\u007B-\u009f]/;
/**
* @param {string} value
* @return {boolean}
*/
module.exports = function canUnquote(value) {
if (value === '-' || value === '') {
return false;
}
value = value.replace(escapes, 'a').replace(/\\./g, 'a');
return !(range.test(value) || /^(?:-?\d|--)/.test(value));
};

View File

@ -0,0 +1,9 @@
export = pluginCreator;
/**
* @type {import('postcss').PluginCreator<void>}
* @return {import('postcss').Plugin}
*/
declare function pluginCreator(): import('postcss').Plugin;
declare namespace pluginCreator {
const postcss: true;
}

View File

@ -0,0 +1,2 @@
declare function _exports(value: string): boolean;
export = _exports;