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 urange #6704

Merged
merged 4 commits into from
Dec 27, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 9 additions & 2 deletions crates/swc_css_ast/src/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -454,14 +454,21 @@ pub enum UrlModifier {
}

#[ast_node("UnicodeRange")]
#[derive(Eq, Hash, EqIgnoreSpan)]
#[derive(Eq, Hash)]
pub struct UnicodeRange {
pub span: Span,
pub prefix: char,
#[cfg_attr(feature = "rkyv", with(swc_atoms::EncodeJsWord))]
pub start: JsWord,
#[cfg_attr(feature = "rkyv", with(swc_atoms::EncodeJsWord))]
pub end: Option<JsWord>,
pub raw: Option<Atom>,
}

impl EqIgnoreSpan for UnicodeRange {
#[inline]
fn eq_ignore_span(&self, other: &Self) -> bool {
self.start == other.start && self.end == other.end
}
}

#[ast_node("CalcSum")]
Expand Down
3 changes: 1 addition & 2 deletions crates/swc_css_codegen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1998,8 +1998,7 @@ where
+ 2,
);

value.push(n.prefix);
value.push('+');
value.push_str("u+");
value.push_str(&n.start);

if let Some(end) = &n.end {
Expand Down
38 changes: 38 additions & 0 deletions crates/swc_css_codegen/tests/fixture.rs
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,44 @@ impl VisitMut for NormalizeTest {
n.raw = None;
}

fn visit_mut_unicode_range(&mut self, n: &mut UnicodeRange) {
n.visit_mut_children_with(self);

n.raw = None;
}

fn visit_mut_declaration(&mut self, n: &mut Declaration) {
n.visit_mut_children_with(self);

if let DeclarationName::Ident(name) = &mut n.name {
name.value = name.value.to_lowercase().into();
}
}

fn visit_mut_pseudo_class_selector(&mut self, n: &mut PseudoClassSelector) {
n.visit_mut_children_with(self);

n.name.value = n.name.value.to_lowercase().into();
}

fn visit_mut_pseudo_element_selector(&mut self, n: &mut PseudoElementSelector) {
n.visit_mut_children_with(self);

n.name.value = n.name.value.to_lowercase().into();
}

fn visit_mut_tag_name_selector(&mut self, n: &mut TagNameSelector) {
n.visit_mut_children_with(self);

n.name.value.value = n.name.value.value.to_lowercase().into();
}

fn visit_mut_attribute_selector_modifier(&mut self, n: &mut AttributeSelectorModifier) {
n.visit_mut_children_with(self);

n.value.value = n.value.value.to_lowercase().into();
}

fn visit_mut_an_plus_b_notation(&mut self, n: &mut AnPlusBNotation) {
n.visit_mut_children_with(self);

Expand Down
4 changes: 2 additions & 2 deletions crates/swc_css_minifier/src/compressor/unicode_range.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ impl Compressor {

for (idx, start_c) in start.chars().enumerate() {
if let Some(end_c) = &end.chars().nth(idx) {
if start_c.eq_ignore_ascii_case(end_c) && question_counter == 0 {
if start_c == *end_c && question_counter == 0 {
minified.push(start_c);
} else if start_c == '0' && end_c.eq_ignore_ascii_case(&'f') {
} else if start_c == '0' && *end_c == 'f' {
question_counter += 1;

minified.push('?')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,3 +109,8 @@
@font-face {
unicode-range: U+000450-0004FF;
}

@font-face {
unicode-range: U+26;
unicode-range: u+26;
}

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

20 changes: 10 additions & 10 deletions crates/swc_css_parser/src/parser/values_and_units/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2726,14 +2726,14 @@ where
// should start with `u` or `U`
match cur!(self) {
Token::Ident { value, .. } if matches_eq_ignore_ascii_case!(value, js_word!("u")) => {
let ident = match bump!(self) {
let u = match bump!(self) {
Token::Ident { value, .. } => value,
_ => {
unreachable!();
}
};

unicode_range.push_str(&ident);
unicode_range.push_str(&u);
}
_ => {
return Err(Error::new(span, ErrorKind::Expected("'u' ident token")));
Expand Down Expand Up @@ -2915,12 +2915,12 @@ where

// 1. Skipping the first u token, concatenate the representations of all the
// tokens in the production together. Let this be text.
let prefix = chars.next().unwrap();

let mut next = chars.next();
chars.next();

// 2. If the first character of text is U+002B PLUS SIGN, consume it. Otherwise,
// this is an invalid <urange>, and this algorithm must exit.
let mut next = chars.next();

if next != Some('+') {
return Err(Error::new(
span,
Expand All @@ -2944,7 +2944,7 @@ where
next = chars.next();
}
Some(c @ 'A'..='F') | Some(c @ 'a'..='f') => {
start.push(c);
start.push(c.to_ascii_lowercase());

next = chars.next();
}
Expand Down Expand Up @@ -2999,9 +2999,9 @@ where
// 4. Exit this algorithm.
return Ok(UnicodeRange {
span: span!(self, span.lo),
prefix,
start: start.into(),
end: None,
raw: Some(unicode_range.into()),
});
}

Expand All @@ -3013,9 +3013,9 @@ where
if next.is_none() {
return Ok(UnicodeRange {
span: span!(self, span.lo),
prefix,
start: start.into(),
end: None,
raw: Some(unicode_range.into()),
});
}

Expand Down Expand Up @@ -3044,7 +3044,7 @@ where
next = chars.next();
}
Some(c @ 'A'..='F') | Some(c @ 'a'..='f') => {
end.push(c);
end.push(c.to_ascii_lowercase());
next = chars.next();
}
_ => {
Expand Down Expand Up @@ -3073,9 +3073,9 @@ where

return Ok(UnicodeRange {
span: span!(self, span.lo),
prefix,
start: start.into(),
end: Some(end.into()),
raw: Some(unicode_range.into()),
});
}
}
Expand Down