Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 14 additions & 7 deletions src/Parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,15 +93,22 @@ export function parse (value, root, parent, rule, rules, rulesets, pseudo, point
if (character === 123)
if (offset === 0)
parse(characters, root, reference, reference, props, rulesets, length, points, children)
else
switch (atrule === 99 && charat(characters, 3) === 110 ? 100 : atrule) {
// d l m s
case 100: case 108: case 109: case 115:
parse(value, reference, reference, rule && append(ruleset(value, reference, reference, 0, 0, rules, points, type, rules, props = [], length, children), children), rules, children, length, points, rule ? props : children)
break
else {
switch (atrule) {
// c(ontainer)
case 99:
if (charat(characters, 3) === 110) break
// l(ayer)
case 108:
if (charat(characters, 2) === 97) break
default:
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is a default case that's not the last one 😅 I tried to code-golf this as much as possible and this is my best version so far

I was testing the size increase on dist/umd/stylis.js (since it's already minified) using wc -c and gzip -c | wc -c. Those are results:

ref wc -c gzip -c | wc -c
master 10571 4396
9c57efb 10607 4413
2a519d3 10619 4409
ebd509a 10606 4406

Copy link
Collaborator Author

@Andarist Andarist Jan 12, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The idea behind the fix here is that for non-layer and non-container (this case was already covered) we "reset" offset. We do the same in the default case and that's why the code falls through from those when it doesn't break

Then based on the offset (if it survived or not) the correct parsing invocation is selected

parse(characters, reference, reference, reference, [''], children, 0, points, children)
offset = 0
// d(ocument) m(edia) s(upports)
case 100: case 109: case 115:
}
if (offset) parse(value, reference, reference, rule && append(ruleset(value, reference, reference, 0, 0, rules, points, type, rules, props = [], length, children), children), rules, children, length, points, rule ? props : children)
else parse(characters, reference, reference, reference, [''], children, 0, points, children)
}
}

index = offset = property = 0, variable = ampersand = 1, type = characters = '', length = pseudo
Expand Down
8 changes: 7 additions & 1 deletion test/Parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,12 @@ describe('Parser', () => {
margin-right: 1cm;
}
width: none;
@right-bottom {
content: "Content";
}
@left-bottom {
content: "Content";
}
}
`)
).to.equal([
Expand All @@ -79,7 +85,7 @@ describe('Parser', () => {
`@viewport{min-width:640px;max-width:800px;}`,
`@counter-style list{system:fixed;symbols:url();suffix:" ";}`,
`@-moz-document url-prefix(){.user .selector{color:lime;}}`,
`@page{color:red;@bottom-right{content:counter(pages);margin-right:1cm;}width:none;}`
`@page{color:red;@bottom-right{content:counter(pages);margin-right:1cm;}width:none;@right-bottom{content:"Content";}@left-bottom{content:"Content";}}`
].join(''))
})

Expand Down
Loading