Skip to content

Commit 07afe19

Browse files
committed
do not merge rules when the selector contains vendor specific pseudo selector #35
1 parent 3886cb4 commit 07afe19

File tree

21 files changed

+710
-292
lines changed

21 files changed

+710
-292
lines changed

CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
# Changelog
22

33
## V0.5.1
4+
45
- [x] failed to flatten @import when using url() syntax
5-
-
6+
67
## V0.5.0
8+
79
- [x] render node with parents
810
- [x] fix relative color from xyz
911
- [x] fix bug when inlineCss is true bug no css variable exists

README.md

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -497,30 +497,36 @@ for (const {node, parent, root} of walk(ast)) {
497497

498498
### Comment
499499

500-
- typ: string 'Comment'
500+
- typ: number
501501
- val: string, the comment
502502

503503
### Declaration
504504

505-
- typ: string 'Declaration'
505+
- typ: number
506506
- nam: string, declaration name
507507
- val: array of tokens
508508

509509
### Rule
510510

511-
- typ: string 'Rule'
511+
- typ: number
512512
- sel: string, css selector
513513
- chi: array of children
514514

515515
### AtRule
516516

517-
- typ: string 'AtRule'
517+
- typ: number
518518
- nam: string. AtRule name
519519
- val: rule prelude
520520

521521
### AtRuleStyleSheet
522522

523-
- typ: string 'Stylesheet'
523+
- typ: number
524+
- chi: array of children
525+
526+
### KeyFrameRule
527+
528+
- typ: number
529+
- sel: string, css selector
524530
- chi: array of children
525531

526532
## Sourcemap

dist/index-umd-web.js

Lines changed: 94 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@
8181
EnumToken[EnumToken["FractionTokenType"] = 69] = "FractionTokenType";
8282
EnumToken[EnumToken["IdenListTokenType"] = 70] = "IdenListTokenType";
8383
EnumToken[EnumToken["GridTemplateFuncTokenType"] = 71] = "GridTemplateFuncTokenType";
84+
EnumToken[EnumToken["KeyFrameRuleNodeType"] = 72] = "KeyFrameRuleNodeType";
8485
/* aliases */
8586
EnumToken[EnumToken["Time"] = 25] = "Time";
8687
EnumToken[EnumToken["Iden"] = 7] = "Iden";
@@ -2510,7 +2511,15 @@
25102511
* @param tokens
25112512
*/
25122513
function evaluate(tokens) {
2513-
const nodes = inlineExpression(evaluateExpression(buildExpression(tokens)));
2514+
let nodes;
2515+
try {
2516+
nodes = inlineExpression(evaluateExpression(buildExpression(tokens)));
2517+
}
2518+
catch (e) {
2519+
// console.error({tokens});
2520+
// console.error(e);
2521+
return tokens;
2522+
}
25142523
if (nodes.length <= 1) {
25152524
return nodes;
25162525
}
@@ -3036,7 +3045,7 @@
30363045
return result;
30373046
}
30383047
function updateSourceMap(node, options, cache, sourcemap, position, str) {
3039-
if ([exports.EnumToken.RuleNodeType, exports.EnumToken.AtRuleNodeType].includes(node.typ)) {
3048+
if ([exports.EnumToken.RuleNodeType, exports.EnumToken.AtRuleNodeType, exports.EnumToken.KeyFrameRuleNodeType].includes(node.typ)) {
30403049
let src = node.loc?.src ?? '';
30413050
let output = options.output ?? '';
30423051
if (!(src in cache)) {
@@ -3096,6 +3105,7 @@
30963105
}, '');
30973106
case exports.EnumToken.AtRuleNodeType:
30983107
case exports.EnumToken.RuleNodeType:
3108+
case exports.EnumToken.KeyFrameRuleNodeType:
30993109
if (data.typ == exports.EnumToken.AtRuleNodeType && !('chi' in data)) {
31003110
return `${indent}@${data.nam}${data.val === '' ? '' : options.indent || ' '}${data.val};`;
31013111
}
@@ -6446,6 +6456,9 @@
64466456
const position = map.get(tokens[0]);
64476457
const uniq = new Map;
64486458
parseTokens(tokens, { minify: true }).reduce((acc, curr, index, array) => {
6459+
if (curr.typ == exports.EnumToken.CommentTokenType) {
6460+
return acc;
6461+
}
64496462
if (curr.typ == exports.EnumToken.WhitespaceTokenType) {
64506463
if (trimWhiteSpace.includes(array[index - 1]?.typ) ||
64516464
trimWhiteSpace.includes(array[index + 1]?.typ) ||
@@ -6467,7 +6480,7 @@
64676480
return acc;
64686481
}, uniq);
64696482
const node = {
6470-
typ: exports.EnumToken.RuleNodeType,
6483+
typ: context.typ == exports.EnumToken.AtRuleNodeType && context.nam == 'keyframes' ? exports.EnumToken.KeyFrameRuleNodeType : exports.EnumToken.RuleNodeType,
64716484
// @ts-ignore
64726485
sel: [...uniq.keys()].join(','),
64736486
chi: []
@@ -8405,8 +8418,9 @@
84058418
// @ts-ignore
84068419
const features = Object.values(allFeatures).sort((a, b) => a.ordering - b.ordering);
84078420
function minify(ast, options = {}, recursive = false, errors, nestingContent, context = {}) {
8421+
// console.debug(JSON.stringify({ast}, null, 1));
84088422
if (!('nodes' in context)) {
8409-
context.nodes = new WeakSet;
8423+
context.nodes = new Set;
84108424
}
84118425
if (context.nodes.has(ast)) {
84128426
return ast;
@@ -8637,7 +8651,7 @@
86378651
}
86388652
if (shouldMerge) {
86398653
// @ts-ignore
8640-
if ((node.typ == exports.EnumToken.RuleNodeType && node.sel == previous.sel) ||
8654+
if (((node.typ == exports.EnumToken.RuleNodeType || node.typ == exports.EnumToken.KeyFrameRuleNodeType) && node.sel == previous.sel) ||
86418655
// @ts-ignore
86428656
(node.typ == exports.EnumToken.AtRuleNodeType) && node.val != 'font-face' && node.val == previous.val) {
86438657
// @ts-ignore
@@ -8656,32 +8670,33 @@
86568670
nodeIndex = i;
86578671
continue;
86588672
}
8659-
else if (node.typ == exports.EnumToken.RuleNodeType && previous?.typ == exports.EnumToken.RuleNodeType) {
8673+
else if (node.typ == previous?.typ && [exports.EnumToken.KeyFrameRuleNodeType, exports.EnumToken.RuleNodeType].includes(node.typ)) {
86608674
const intersect = diff(previous, node, reducer, options);
86618675
if (intersect != null) {
86628676
if (intersect.node1.chi.length == 0) {
86638677
// @ts-ignore
86648678
ast.chi.splice(i--, 1);
86658679
// @ts-ignore
8666-
node = ast.chi[i];
8680+
// node = ast.chi[i];
86678681
}
86688682
else {
86698683
// @ts-ignore
86708684
ast.chi.splice(i, 1, intersect.node1);
8671-
node = intersect.node1;
8685+
// node = ast.chi intersect.node1;
86728686
}
86738687
if (intersect.node2.chi.length == 0) {
86748688
// @ts-ignore
86758689
ast.chi.splice(nodeIndex, 1, intersect.result);
8676-
previous = intersect.result;
86778690
}
86788691
else {
86798692
// @ts-ignore
86808693
ast.chi.splice(nodeIndex, 1, intersect.result, intersect.node2);
8681-
previous = intersect.result;
86828694
// @ts-ignore
8683-
i = nodeIndex;
8695+
i = (nodeIndex ?? 0) + 1;
86848696
}
8697+
reduceRuleSelector(intersect.result);
8698+
previous = intersect.result;
8699+
nodeIndex = i;
86858700
}
86868701
}
86878702
}
@@ -8903,6 +8918,18 @@
89038918
result.push([]);
89048919
continue;
89058920
}
8921+
if (chr == ':') {
8922+
if (str !== '') {
8923+
// @ts-ignore
8924+
result.at(-1).push(str);
8925+
str = '';
8926+
}
8927+
if (buffer.charAt(i + 1) == ':') {
8928+
chr += buffer.charAt(++i);
8929+
}
8930+
str += chr;
8931+
continue;
8932+
}
89068933
str += chr;
89078934
if (chr == '\\') {
89088935
str += buffer.charAt(++i);
@@ -9204,6 +9231,52 @@
92049231
const raw1 = node1.raw;
92059232
// @ts-ignore
92069233
const raw2 = node2.raw;
9234+
if (raw1 != null && raw2 != null) {
9235+
const prefixes1 = new Set;
9236+
const prefixes2 = new Set;
9237+
for (const token1 of raw1) {
9238+
for (const t of token1) {
9239+
if (t[0] == ':') {
9240+
const matches = t.match(/::?-([a-z]+)-/);
9241+
if (matches == null) {
9242+
continue;
9243+
}
9244+
prefixes1.add(matches[1]);
9245+
if (prefixes1.size > 1) {
9246+
break;
9247+
}
9248+
}
9249+
}
9250+
if (prefixes1.size > 1) {
9251+
break;
9252+
}
9253+
}
9254+
for (const token2 of raw2) {
9255+
for (const t of token2) {
9256+
if (t[0] == ':') {
9257+
const matches = t.match(/::?-([a-z]+)-/);
9258+
if (matches == null) {
9259+
continue;
9260+
}
9261+
prefixes2.add(matches[1]);
9262+
if (prefixes2.size > 1) {
9263+
break;
9264+
}
9265+
}
9266+
}
9267+
if (prefixes2.size > 1) {
9268+
break;
9269+
}
9270+
}
9271+
if (prefixes1.size != prefixes2.size) {
9272+
return null;
9273+
}
9274+
for (const prefix of prefixes1) {
9275+
if (!prefixes2.has(prefix)) {
9276+
return null;
9277+
}
9278+
}
9279+
}
92079280
// @ts-ignore
92089281
node1 = { ...node1, chi: node1.chi.slice() };
92099282
node2 = { ...node2, chi: node2.chi.slice() };
@@ -9240,7 +9313,7 @@
92409313
const result = (intersect.length == 0 ? null : {
92419314
...node1,
92429315
// @ts-ignore
9243-
sel: [...new Set([...(n1?.raw?.reduce(reducer, []) || splitRule(n1.sel)).concat(n2?.raw?.reduce(reducer, []) || splitRule(n2.sel))])].join(','),
9316+
sel: [...new Set([...(n1?.raw?.reduce(reducer, []) ?? splitRule(n1.sel)).concat(n2?.raw?.reduce(reducer, []) ?? splitRule(n2.sel))])].join(','),
92449317
chi: intersect.reverse()
92459318
});
92469319
if (result == null || [n1, n2].reduce((acc, curr) => curr.chi.length == 0 ? acc : acc + doRender(curr, options).code.length, 0) <= [node1, node2, result].reduce((acc, curr) => curr.chi.length == 0 ? acc : acc + doRender(curr, options).code.length, 0)) {
@@ -9391,16 +9464,18 @@
93919464
return response.text();
93929465
}
93939466
async function load(url, currentFile) {
9467+
let t;
93949468
if (matchUrl.test(url)) {
9395-
return fetch(url).then(parseResponse);
9469+
t = new URL(url);
93969470
}
9397-
if (matchUrl.test(currentFile)) {
9398-
return fetch(new URL(url, currentFile)).then(parseResponse);
9471+
else if (matchUrl.test(currentFile)) {
9472+
t = new URL(url, currentFile);
9473+
}
9474+
else {
9475+
const path = resolve(url, currentFile).absolute;
9476+
t = new URL(path, self.origin);
93999477
}
9400-
const path = resolve(url, currentFile).absolute;
9401-
const t = new URL(path, self.origin);
9402-
// return fetch(new URL(url, new URL(currentFile, self.location.href).href)).then(parseResponse);
9403-
return fetch(url, t.origin != self.location.origin ? { mode: 'cors' } : {}).then(parseResponse);
9478+
return fetch(t, t.origin != self.origin ? { mode: 'cors' } : {}).then(parseResponse);
94049479
}
94059480

94069481
function render(data, options = {}) {

0 commit comments

Comments
 (0)