Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(css/parser): normalize selector AST #6657

Merged
merged 3 commits into from Dec 15, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 12 additions & 0 deletions crates/swc_atoms/words.txt
Expand Up @@ -835,6 +835,9 @@ createClass
createReactClass
crossorigin
cubic-bezier
cue
cue-region
current
currentColor
currentcolor
cursor
Expand Down Expand Up @@ -1019,6 +1022,7 @@ frameset
from
from-image
function
future
g
get
global
Expand Down Expand Up @@ -1050,14 +1054,18 @@ h4
h5
h6
hanging-punctuation
has
head
header
headers
height
hgroup
highlight
historical-forms
hkern
horizontal-tb
host
host-context
hr
href
hsl
Expand Down Expand Up @@ -1303,7 +1311,9 @@ noscript
not
nowrap
nth-child
nth-col
nth-last-child
nth-last-col
nth-last-of-type
nth-of-type
null
Expand Down Expand Up @@ -1470,6 +1480,7 @@ paint-order
panose-1
param
part
past
path
pathLength
pathlength
Expand Down Expand Up @@ -1653,6 +1664,7 @@ skewx
skewy
slice
slot
slotted
small
solid
solidColor
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 9 additions & 9 deletions crates/swc_css_minifier/src/compressor/mod.rs
Expand Up @@ -255,14 +255,14 @@ impl VisitMut for Compressor {
fn visit_mut_pseudo_class_selector(&mut self, n: &mut PseudoClassSelector) {
match &n.name {
Ident { value, .. }
if matches_eq_ignore_ascii_case!(
value,
js_word!("not"),
js_word!("is"),
js_word!("where"),
js_word!("matches"),
js_word!("-moz-any"),
js_word!("-webkit-any")
if matches!(
*value,
js_word!("not")
| js_word!("is")
| js_word!("where")
| js_word!("matches")
| js_word!("-moz-any")
| js_word!("-webkit-any")
) =>
{
n.visit_mut_children_with(&mut *self.with_ctx(Ctx {
Expand All @@ -279,7 +279,7 @@ impl VisitMut for Compressor {
fn visit_mut_selector_list(&mut self, n: &mut SelectorList) {
n.visit_mut_children_with(self);

self.comrpess_selector_list(n);
self.compress_selector_list(n);
}

fn visit_mut_forgiving_selector_list(&mut self, n: &mut ForgivingSelectorList) {
Expand Down
36 changes: 11 additions & 25 deletions crates/swc_css_minifier/src/compressor/selector.rs
Expand Up @@ -6,7 +6,7 @@ use super::Compressor;
use crate::util::dedup;

impl Compressor {
pub(super) fn comrpess_selector_list(&mut self, selector_list: &mut SelectorList) {
pub(super) fn compress_selector_list(&mut self, selector_list: &mut SelectorList) {
dedup(&mut selector_list.children);
}

Expand Down Expand Up @@ -62,9 +62,7 @@ impl Compressor {
});
}
// `even` => `2n`
AnPlusB::Ident(Ident { value, span, .. })
if value.eq_ignore_ascii_case(&js_word!("even")) =>
{
AnPlusB::Ident(Ident { value, span, .. }) if *value == js_word!("even") => {
*an_plus_b = AnPlusB::AnPlusBNotation(AnPlusBNotation {
span: *span,
a: Some(2),
Expand Down Expand Up @@ -110,12 +108,12 @@ impl Compressor {
pub(super) fn compress_subclass_selector(&mut self, subclass_selector: &mut SubclassSelector) {
match &subclass_selector {
SubclassSelector::PseudoElement(PseudoElementSelector { name, span, .. }) => {
if matches_eq_ignore_ascii_case!(
if matches!(
name.value,
js_word!("before"),
js_word!("after"),
js_word!("first-letter"),
js_word!("first-line")
js_word!("before")
| js_word!("after")
| js_word!("first-letter")
| js_word!("first-line")
) {
*subclass_selector = SubclassSelector::PseudoClass(PseudoClassSelector {
span: *span,
Expand All @@ -129,9 +127,7 @@ impl Compressor {
children: Some(children),
span,
..
}) if name.value.eq_ignore_ascii_case(&js_word!("nth-child"))
&& children.len() == 1 =>
{
}) if name.value == js_word!("nth-child") && children.len() == 1 => {
match children.get(0) {
Some(PseudoClassSelectorChildren::AnPlusB(AnPlusB::AnPlusBNotation(
AnPlusBNotation {
Expand All @@ -158,11 +154,7 @@ impl Compressor {
children: Some(children),
span,
..
}) if name
.value
.eq_str_ignore_ascii_case(&js_word!("nth-last-child"))
&& children.len() == 1 =>
{
}) if name.value == js_word!("nth-last-child") && children.len() == 1 => {
match children.get(0) {
Some(PseudoClassSelectorChildren::AnPlusB(AnPlusB::AnPlusBNotation(
AnPlusBNotation {
Expand All @@ -189,9 +181,7 @@ impl Compressor {
children: Some(children),
span,
..
}) if name.value.eq_ignore_ascii_case(&js_word!("nth-of-type"))
&& children.len() == 1 =>
{
}) if name.value == js_word!("nth-of-type") && children.len() == 1 => {
match children.get(0) {
Some(PseudoClassSelectorChildren::AnPlusB(AnPlusB::AnPlusBNotation(
AnPlusBNotation {
Expand All @@ -218,11 +208,7 @@ impl Compressor {
children: Some(children),
span,
..
}) if name
.value
.eq_ignore_ascii_case(&js_word!("nth-last-of-type"))
&& children.len() == 1 =>
{
}) if name.value == js_word!("nth-last-of-type") && children.len() == 1 => {
match children.get(0) {
Some(PseudoClassSelectorChildren::AnPlusB(AnPlusB::AnPlusBNotation(
AnPlusBNotation {
Expand Down
Expand Up @@ -186,4 +186,7 @@ a:has(> a, > a) {color:red}
.alpha > .beta {& + & {order: 2}}
a {& *.bar {color: red}}
a {&*.bar {color: red}}
.class {&*.bar {color: red}}
.class {&*.bar {color: red}}

h1,H1{color:red}
*:NOT(H1, H1) {color: blue}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 8 additions & 8 deletions crates/swc_css_modules/tests/fixture/url/url.transform.json
Expand Up @@ -53,10 +53,10 @@
"name": "__local__root-relative"
}
],
"my-background": [
"highlight": [
{
"type": "local",
"name": "__local__my-background"
"name": "__local__highlight"
}
],
"aliases": [
Expand All @@ -71,6 +71,12 @@
"name": "__local__strange"
}
],
"my-background": [
{
"type": "local",
"name": "__local__my-background"
}
],
"other-test-case": [
{
"type": "local",
Expand Down Expand Up @@ -106,11 +112,5 @@
"type": "local",
"name": "__local__pure-url"
}
],
"highlight": [
{
"type": "local",
"name": "__local__highlight"
}
]
}