Skip to content

Commit 2052c44

Browse files
committed
do not merge rules with vendor prefixed pseudo selector #35
1 parent 07afe19 commit 2052c44

File tree

6 files changed

+107
-51
lines changed

6 files changed

+107
-51
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
[![npm](https://img.shields.io/badge/dynamic/json?url=https%3A%2F%2Fraw.githubusercontent.com%2Ftbela99%2Fcss-parser%2Fmaster%2Fpackage.json&query=version&logo=npm&label=npm&link=https%3A%2F%2Fwww.npmjs.com%2Fpackage%2F%40tbela99%2Fcss-parser)](https://www.npmjs.com/package/@tbela99/css-parser) [![cov](https://tbela99.github.io/css-parser/badges/coverage.svg)](https://github.com/tbela99/css-parser/actions) [![NPM Downloads](https://img.shields.io/npm/dy/%40tbela99%2Fcss-parser)](https://www.npmjs.com/package/@tbela99/css-parser)
1+
[![npm](https://img.shields.io/badge/dynamic/json?url=https%3A%2F%2Fraw.githubusercontent.com%2Ftbela99%2Fcss-parser%2Fmaster%2Fpackage.json&query=version&logo=npm&label=npm&link=https%3A%2F%2Fwww.npmjs.com%2Fpackage%2F%40tbela99%2Fcss-parser)](https://www.npmjs.com/package/@tbela99/css-parser) [![cov](https://tbela99.github.io/css-parser/badges/coverage.svg)](https://github.com/tbela99/css-parser/actions) [![NPM Downloads](https://img.shields.io/npm/dm/%40tbela99%2Fcss-parser)](https://www.npmjs.com/package/@tbela99/css-parser)
22

33
# css-parser
44

dist/index-umd-web.js

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8418,7 +8418,6 @@
84188418
// @ts-ignore
84198419
const features = Object.values(allFeatures).sort((a, b) => a.ordering - b.ordering);
84208420
function minify(ast, options = {}, recursive = false, errors, nestingContent, context = {}) {
8421-
// console.debug(JSON.stringify({ast}, null, 1));
84228421
if (!('nodes' in context)) {
84238422
context.nodes = new Set;
84248423
}
@@ -8449,7 +8448,7 @@
84498448
curr.splice(0, 2);
84508449
}
84518450
else if (combinators.includes(curr[1])) {
8452-
curr.splice(0, 1);
8451+
curr.shift();
84538452
}
84548453
}
84558454
else if (ast.typ == exports.EnumToken.RuleNodeType && (isIdent(curr[0]) || isFunction(curr[0]))) {
@@ -8826,19 +8825,24 @@
88268825
// combinator
88278826
if (combinators.includes(optimized.at(-1))) {
88288827
const combinator = optimized.pop();
8829-
selector.forEach(selector => selector.unshift(combinator));
8828+
selector.forEach((selector) => selector.unshift(combinator));
88308829
}
88318830
let reducible = optimized.length == 1;
8832-
if (optimized[0] == '&' && optimized[1] == ' ') {
8833-
optimized.splice(0, 2);
8831+
if (optimized[0] == '&') {
8832+
if (optimized[1] == ' ') {
8833+
optimized.splice(0, 2);
8834+
}
8835+
// else if (combinators.includes(optimized[1])) {
8836+
//
8837+
// }
88348838
}
88358839
if (optimized.length == 0 ||
88368840
(optimized[0].charAt(0) == '&' ||
88378841
selector.length == 1)) {
88388842
return {
88398843
match: false,
88408844
optimized,
8841-
selector: selector.map(selector => selector[0] == '&' && selector[1] == ' ' ? selector.slice(2) : selector),
8845+
selector: selector.map((selector) => selector[0] == '&' && selector[1] == ' ' ? selector.slice(2) : (selector)),
88428846
reducible: selector.length > 1 && selector.every((selector) => !combinators.includes(selector[0]))
88438847
};
88448848
}
@@ -8848,7 +8852,7 @@
88488852
selector: selector.reduce((acc, curr) => {
88498853
let hasCompound = true;
88508854
if (hasCompound && curr.length > 0) {
8851-
hasCompound = !['&'].concat(combinators).includes(curr[0].charAt(0));
8855+
hasCompound = ':' != curr[0] || !['&'].concat(combinators).includes(curr[0].charAt(0));
88528856
}
88538857
// @ts-ignore
88548858
if (hasCompound && curr[0] == ' ') {
@@ -9337,6 +9341,11 @@
93379341
Object.defineProperty(node, 'optimized', { ...definedPropertySettings, value: optimized });
93389342
}
93399343
if (optimized != null && optimized.match && optimized.reducible && optimized.selector.length > 1) {
9344+
for (const selector of optimized.selector) {
9345+
if (selector.length > 1 && selector[0] == '&' && combinators.includes(selector[1])) {
9346+
selector.shift();
9347+
}
9348+
}
93409349
const raw = [
93419350
[
93429351
optimized.optimized[0], ':is('

dist/index.cjs

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8416,7 +8416,6 @@ const notEndingWith = ['(', '['].concat(combinators);
84168416
// @ts-ignore
84178417
const features = Object.values(allFeatures).sort((a, b) => a.ordering - b.ordering);
84188418
function minify(ast, options = {}, recursive = false, errors, nestingContent, context = {}) {
8419-
// console.debug(JSON.stringify({ast}, null, 1));
84208419
if (!('nodes' in context)) {
84218420
context.nodes = new Set;
84228421
}
@@ -8447,7 +8446,7 @@ function minify(ast, options = {}, recursive = false, errors, nestingContent, co
84478446
curr.splice(0, 2);
84488447
}
84498448
else if (combinators.includes(curr[1])) {
8450-
curr.splice(0, 1);
8449+
curr.shift();
84518450
}
84528451
}
84538452
else if (ast.typ == exports.EnumToken.RuleNodeType && (isIdent(curr[0]) || isFunction(curr[0]))) {
@@ -8824,19 +8823,24 @@ function reduceSelector(selector) {
88248823
// combinator
88258824
if (combinators.includes(optimized.at(-1))) {
88268825
const combinator = optimized.pop();
8827-
selector.forEach(selector => selector.unshift(combinator));
8826+
selector.forEach((selector) => selector.unshift(combinator));
88288827
}
88298828
let reducible = optimized.length == 1;
8830-
if (optimized[0] == '&' && optimized[1] == ' ') {
8831-
optimized.splice(0, 2);
8829+
if (optimized[0] == '&') {
8830+
if (optimized[1] == ' ') {
8831+
optimized.splice(0, 2);
8832+
}
8833+
// else if (combinators.includes(optimized[1])) {
8834+
//
8835+
// }
88328836
}
88338837
if (optimized.length == 0 ||
88348838
(optimized[0].charAt(0) == '&' ||
88358839
selector.length == 1)) {
88368840
return {
88378841
match: false,
88388842
optimized,
8839-
selector: selector.map(selector => selector[0] == '&' && selector[1] == ' ' ? selector.slice(2) : selector),
8843+
selector: selector.map((selector) => selector[0] == '&' && selector[1] == ' ' ? selector.slice(2) : (selector)),
88408844
reducible: selector.length > 1 && selector.every((selector) => !combinators.includes(selector[0]))
88418845
};
88428846
}
@@ -8846,7 +8850,7 @@ function reduceSelector(selector) {
88468850
selector: selector.reduce((acc, curr) => {
88478851
let hasCompound = true;
88488852
if (hasCompound && curr.length > 0) {
8849-
hasCompound = !['&'].concat(combinators).includes(curr[0].charAt(0));
8853+
hasCompound = ':' != curr[0] || !['&'].concat(combinators).includes(curr[0].charAt(0));
88508854
}
88518855
// @ts-ignore
88528856
if (hasCompound && curr[0] == ' ') {
@@ -9335,6 +9339,11 @@ function reduceRuleSelector(node) {
93359339
Object.defineProperty(node, 'optimized', { ...definedPropertySettings, value: optimized });
93369340
}
93379341
if (optimized != null && optimized.match && optimized.reducible && optimized.selector.length > 1) {
9342+
for (const selector of optimized.selector) {
9343+
if (selector.length > 1 && selector[0] == '&' && combinators.includes(selector[1])) {
9344+
selector.shift();
9345+
}
9346+
}
93389347
const raw = [
93399348
[
93409349
optimized.optimized[0], ':is('

dist/lib/ast/minify.js

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ const notEndingWith = ['(', '['].concat(combinators);
1313
// @ts-ignore
1414
const features = Object.values(index).sort((a, b) => a.ordering - b.ordering);
1515
function minify(ast, options = {}, recursive = false, errors, nestingContent, context = {}) {
16-
// console.debug(JSON.stringify({ast}, null, 1));
1716
if (!('nodes' in context)) {
1817
context.nodes = new Set;
1918
}
@@ -44,7 +43,7 @@ function minify(ast, options = {}, recursive = false, errors, nestingContent, co
4443
curr.splice(0, 2);
4544
}
4645
else if (combinators.includes(curr[1])) {
47-
curr.splice(0, 1);
46+
curr.shift();
4847
}
4948
}
5049
else if (ast.typ == EnumToken.RuleNodeType && (isIdent(curr[0]) || isFunction(curr[0]))) {
@@ -421,19 +420,24 @@ function reduceSelector(selector) {
421420
// combinator
422421
if (combinators.includes(optimized.at(-1))) {
423422
const combinator = optimized.pop();
424-
selector.forEach(selector => selector.unshift(combinator));
423+
selector.forEach((selector) => selector.unshift(combinator));
425424
}
426425
let reducible = optimized.length == 1;
427-
if (optimized[0] == '&' && optimized[1] == ' ') {
428-
optimized.splice(0, 2);
426+
if (optimized[0] == '&') {
427+
if (optimized[1] == ' ') {
428+
optimized.splice(0, 2);
429+
}
430+
// else if (combinators.includes(optimized[1])) {
431+
//
432+
// }
429433
}
430434
if (optimized.length == 0 ||
431435
(optimized[0].charAt(0) == '&' ||
432436
selector.length == 1)) {
433437
return {
434438
match: false,
435439
optimized,
436-
selector: selector.map(selector => selector[0] == '&' && selector[1] == ' ' ? selector.slice(2) : selector),
440+
selector: selector.map((selector) => selector[0] == '&' && selector[1] == ' ' ? selector.slice(2) : (selector)),
437441
reducible: selector.length > 1 && selector.every((selector) => !combinators.includes(selector[0]))
438442
};
439443
}
@@ -443,7 +447,7 @@ function reduceSelector(selector) {
443447
selector: selector.reduce((acc, curr) => {
444448
let hasCompound = true;
445449
if (hasCompound && curr.length > 0) {
446-
hasCompound = !['&'].concat(combinators).includes(curr[0].charAt(0));
450+
hasCompound = ':' != curr[0] || !['&'].concat(combinators).includes(curr[0].charAt(0));
447451
}
448452
// @ts-ignore
449453
if (hasCompound && curr[0] == ' ') {
@@ -932,6 +936,11 @@ function reduceRuleSelector(node) {
932936
Object.defineProperty(node, 'optimized', { ...definedPropertySettings, value: optimized });
933937
}
934938
if (optimized != null && optimized.match && optimized.reducible && optimized.selector.length > 1) {
939+
for (const selector of optimized.selector) {
940+
if (selector.length > 1 && selector[0] == '&' && combinators.includes(selector[1])) {
941+
selector.shift();
942+
}
943+
}
935944
const raw = [
936945
[
937946
optimized.optimized[0], ':is('

0 commit comments

Comments
 (0)