Skip to content

Commit

Permalink
strip universal selector when possible #43
Browse files Browse the repository at this point in the history
  • Loading branch information
tbela99 committed Sep 6, 2024
1 parent 5da79d5 commit 9676337
Show file tree
Hide file tree
Showing 14 changed files with 201 additions and 119 deletions.
22 changes: 11 additions & 11 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,17 @@
- [x] declarations
- [ ] declaration values
- [ ] exclude -webkit-* gradients
- [x] css selector validation
- [x] pseudo element
- [x] partial pseudo class validation. does not validate parameters
- [x] attribute selector
- [x] combinator
- [x] simple selector
- [x] nested selector
- [ ] strict mode: allow unknown items such as pseudo classes
- [x] allow unknown pseudo classes
- [x] allow unknown attribute selectors
- [ ] strip universal selector when possible
- [x] css selector validation
- [x] pseudo element
- [x] partial pseudo class validation. does not validate parameters
- [x] attribute selector
- [x] combinator
- [x] simple selector
- [x] nested selector
- [ ] strict mode: allow unknown items such as pseudo classes
- [x] allow unknown pseudo classes
- [x] allow unknown attribute selectors
- [x] strip universal selector when possible

# v0.6.0
- [x] light-dark() color
Expand Down
54 changes: 29 additions & 25 deletions dist/index-umd-web.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@

var ValidationLevel;
(function (ValidationLevel) {
ValidationLevel[ValidationLevel["None"] = 0] = "None";
ValidationLevel[ValidationLevel["Valid"] = 1] = "Valid";
ValidationLevel[ValidationLevel["Drop"] = 2] = "Drop";
ValidationLevel[ValidationLevel["Valid"] = 0] = "Valid";
ValidationLevel[ValidationLevel["Drop"] = 1] = "Drop";
})(ValidationLevel || (ValidationLevel = {}));
exports.EnumToken = void 0;
(function (EnumToken) {
Expand Down Expand Up @@ -59920,6 +59919,7 @@
if (delim.typ == exports.EnumToken.BlockStartTokenType) {
const position = map.get(tokens[0]);
const uniq = new Map;
// const uniqTokens: Token[][] = [[]];
parseTokens(tokens, { minify: true }).reduce((acc, curr, index, array) => {
if (curr.typ == exports.EnumToken.CommentTokenType) {
return acc;
Expand All @@ -59935,27 +59935,31 @@
let t = renderToken(curr, { minify: false });
if (t == ',') {
acc.push([]);
// uniqTokens.push([]);
}
else {
acc[acc.length - 1].push(t);
// uniqTokens[uniqTokens.length - 1].push(curr);
}
return acc;
}, [[]]).reduce((acc, curr) => {
// for (let i = 0; i < curr.length; i++) {
//
// if (curr[i] == '*' && i + 1 < curr.length) {
//
// curr.splice(i, curr[i + 1] == ' ' ? 2 : 1);
// i--;
// }
// }
let i = 0;
for (; i < curr.length; i++) {
if (i + 1 < curr.length && curr[i] == '*') {
if (curr[i] == '*') {
let index = curr[i + 1] == ' ' ? 2 : 1;
if (!['>', '~', '+'].includes(curr[index])) {
curr.splice(i, index);
}
}
}
}
acc.set(curr.join(''), curr);
return acc;
}, uniq);
const ruleType = context.typ == exports.EnumToken.AtRuleNodeType && context.nam == 'keyframes' ? exports.EnumToken.KeyFrameRuleNodeType : exports.EnumToken.RuleNodeType;
if (ruleType == exports.EnumToken.RuleNodeType) {
parseSelector(tokens);
const valid = validateSelector(tokens, options, context);
const valid = validateSelector(parseSelector(tokens), options, context);
if (valid.valid != ValidationLevel.Valid) {
const node = {
typ: exports.EnumToken.InvalidRuleTokenType,
Expand All @@ -59968,14 +59972,12 @@
message: valid.error + ' - "' + tokens.reduce((acc, curr) => acc + renderToken(curr, { minify: false }), '') + '"',
location: { src, ...(map.get(valid.node) ?? position) }
});
// @ts-ignore
context.chi.push(node);
return node;
}
}
const node = {
typ: ruleType,
// @ts-ignore
sel: [...uniq.keys()].join(','),
chi: []
};
Expand Down Expand Up @@ -60175,12 +60177,13 @@
}
}
let i = 0;
const combinators = [
exports.EnumToken.ChildCombinatorTokenType,
exports.EnumToken.NextSiblingCombinatorTokenType,
exports.EnumToken.SubsequentSiblingCombinatorTokenType
];
for (; i < tokens.length; i++) {
if ([
exports.EnumToken.ChildCombinatorTokenType,
exports.EnumToken.NextSiblingCombinatorTokenType,
exports.EnumToken.SubsequentSiblingCombinatorTokenType
].includes(tokens[i].typ)) {
if (combinators.includes(tokens[i].typ)) {
if (i + 1 < tokens.length && [exports.EnumToken.WhitespaceTokenType, exports.EnumToken.DescendantCombinatorTokenType].includes(tokens[i + 1].typ)) {
tokens.splice(i + 1, 1);
}
Expand Down Expand Up @@ -60952,7 +60955,12 @@
return 1;
}
return b == '&' ? -1 : 0;
}).reduce((acc, curr) => acc + (curr == '&' ? replace : curr), '');
}).reduce((acc, curr) => {
if (acc.length > 0 && curr == '&' && (replace.charAt(0) != '.' || replace.includes(' '))) {
return acc + ':is(' + replace + ')';
}
return acc + (curr == '&' ? replace : curr);
}, '');
}

var ValidationTokenEnum;
Expand Down Expand Up @@ -62632,7 +62640,6 @@
return null;
}
selector = selector.reduce((acc, curr) => {
// trim :is()
// @ts-ignore
if (curr.length > 0 && curr.at(-1).startsWith(':is(')) {
// @ts-ignore
Expand Down Expand Up @@ -62689,9 +62696,6 @@
if (optimized[1] == ' ') {
optimized.splice(0, 2);
}
// else if (combinators.includes(optimized[1])) {
//
// }
}
if (optimized.length == 0 ||
(optimized[0].charAt(0) == '&' ||
Expand Down
54 changes: 29 additions & 25 deletions dist/index.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@ var promises = require('node:fs/promises');

var ValidationLevel;
(function (ValidationLevel) {
ValidationLevel[ValidationLevel["None"] = 0] = "None";
ValidationLevel[ValidationLevel["Valid"] = 1] = "Valid";
ValidationLevel[ValidationLevel["Drop"] = 2] = "Drop";
ValidationLevel[ValidationLevel["Valid"] = 0] = "Valid";
ValidationLevel[ValidationLevel["Drop"] = 1] = "Drop";
})(ValidationLevel || (ValidationLevel = {}));
exports.EnumToken = void 0;
(function (EnumToken) {
Expand Down Expand Up @@ -59919,6 +59918,7 @@ async function parseNode(results, context, stats, options, errors, src, map) {
if (delim.typ == exports.EnumToken.BlockStartTokenType) {
const position = map.get(tokens[0]);
const uniq = new Map;
// const uniqTokens: Token[][] = [[]];
parseTokens(tokens, { minify: true }).reduce((acc, curr, index, array) => {
if (curr.typ == exports.EnumToken.CommentTokenType) {
return acc;
Expand All @@ -59934,27 +59934,31 @@ async function parseNode(results, context, stats, options, errors, src, map) {
let t = renderToken(curr, { minify: false });
if (t == ',') {
acc.push([]);
// uniqTokens.push([]);
}
else {
acc[acc.length - 1].push(t);
// uniqTokens[uniqTokens.length - 1].push(curr);
}
return acc;
}, [[]]).reduce((acc, curr) => {
// for (let i = 0; i < curr.length; i++) {
//
// if (curr[i] == '*' && i + 1 < curr.length) {
//
// curr.splice(i, curr[i + 1] == ' ' ? 2 : 1);
// i--;
// }
// }
let i = 0;
for (; i < curr.length; i++) {
if (i + 1 < curr.length && curr[i] == '*') {
if (curr[i] == '*') {
let index = curr[i + 1] == ' ' ? 2 : 1;
if (!['>', '~', '+'].includes(curr[index])) {
curr.splice(i, index);
}
}
}
}
acc.set(curr.join(''), curr);
return acc;
}, uniq);
const ruleType = context.typ == exports.EnumToken.AtRuleNodeType && context.nam == 'keyframes' ? exports.EnumToken.KeyFrameRuleNodeType : exports.EnumToken.RuleNodeType;
if (ruleType == exports.EnumToken.RuleNodeType) {
parseSelector(tokens);
const valid = validateSelector(tokens, options, context);
const valid = validateSelector(parseSelector(tokens), options, context);
if (valid.valid != ValidationLevel.Valid) {
const node = {
typ: exports.EnumToken.InvalidRuleTokenType,
Expand All @@ -59967,14 +59971,12 @@ async function parseNode(results, context, stats, options, errors, src, map) {
message: valid.error + ' - "' + tokens.reduce((acc, curr) => acc + renderToken(curr, { minify: false }), '') + '"',
location: { src, ...(map.get(valid.node) ?? position) }
});
// @ts-ignore
context.chi.push(node);
return node;
}
}
const node = {
typ: ruleType,
// @ts-ignore
sel: [...uniq.keys()].join(','),
chi: []
};
Expand Down Expand Up @@ -60174,12 +60176,13 @@ function parseSelector(tokens) {
}
}
let i = 0;
const combinators = [
exports.EnumToken.ChildCombinatorTokenType,
exports.EnumToken.NextSiblingCombinatorTokenType,
exports.EnumToken.SubsequentSiblingCombinatorTokenType
];
for (; i < tokens.length; i++) {
if ([
exports.EnumToken.ChildCombinatorTokenType,
exports.EnumToken.NextSiblingCombinatorTokenType,
exports.EnumToken.SubsequentSiblingCombinatorTokenType
].includes(tokens[i].typ)) {
if (combinators.includes(tokens[i].typ)) {
if (i + 1 < tokens.length && [exports.EnumToken.WhitespaceTokenType, exports.EnumToken.DescendantCombinatorTokenType].includes(tokens[i + 1].typ)) {
tokens.splice(i + 1, 1);
}
Expand Down Expand Up @@ -60951,7 +60954,12 @@ function replaceCompoundLiteral(selector, replace) {
return 1;
}
return b == '&' ? -1 : 0;
}).reduce((acc, curr) => acc + (curr == '&' ? replace : curr), '');
}).reduce((acc, curr) => {
if (acc.length > 0 && curr == '&' && (replace.charAt(0) != '.' || replace.includes(' '))) {
return acc + ':is(' + replace + ')';
}
return acc + (curr == '&' ? replace : curr);
}, '');
}

var ValidationTokenEnum;
Expand Down Expand Up @@ -62631,7 +62639,6 @@ function reduceSelector(selector) {
return null;
}
selector = selector.reduce((acc, curr) => {
// trim :is()
// @ts-ignore
if (curr.length > 0 && curr.at(-1).startsWith(':is(')) {
// @ts-ignore
Expand Down Expand Up @@ -62688,9 +62695,6 @@ function reduceSelector(selector) {
if (optimized[1] == ' ') {
optimized.splice(0, 2);
}
// else if (combinators.includes(optimized[1])) {
//
// }
}
if (optimized.length == 0 ||
(optimized[0].charAt(0) == '&' ||
Expand Down
7 changes: 6 additions & 1 deletion dist/lib/ast/expand.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,12 @@ function replaceCompoundLiteral(selector, replace) {
return 1;
}
return b == '&' ? -1 : 0;
}).reduce((acc, curr) => acc + (curr == '&' ? replace : curr), '');
}).reduce((acc, curr) => {
if (acc.length > 0 && curr == '&' && (replace.charAt(0) != '.' || replace.includes(' '))) {
return acc + ':is(' + replace + ')';
}
return acc + (curr == '&' ? replace : curr);
}, '');
}

export { expand, replaceCompound };
4 changes: 0 additions & 4 deletions dist/lib/ast/minify.js
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,6 @@ function reduceSelector(selector) {
return null;
}
selector = selector.reduce((acc, curr) => {
// trim :is()
// @ts-ignore
if (curr.length > 0 && curr.at(-1).startsWith(':is(')) {
// @ts-ignore
Expand Down Expand Up @@ -459,9 +458,6 @@ function reduceSelector(selector) {
if (optimized[1] == ' ') {
optimized.splice(0, 2);
}
// else if (combinators.includes(optimized[1])) {
//
// }
}
if (optimized.length == 0 ||
(optimized[0].charAt(0) == '&' ||
Expand Down
5 changes: 2 additions & 3 deletions dist/lib/ast/types.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
var ValidationLevel;
(function (ValidationLevel) {
ValidationLevel[ValidationLevel["None"] = 0] = "None";
ValidationLevel[ValidationLevel["Valid"] = 1] = "Valid";
ValidationLevel[ValidationLevel["Drop"] = 2] = "Drop";
ValidationLevel[ValidationLevel["Valid"] = 0] = "Valid";
ValidationLevel[ValidationLevel["Drop"] = 1] = "Drop";
})(ValidationLevel || (ValidationLevel = {}));
var EnumToken;
(function (EnumToken) {
Expand Down
Loading

0 comments on commit 9676337

Please sign in to comment.