Skip to content

Commit

Permalink
feat(css/ast): Improve type definitions for unicode ranges (#3757)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-akait committed Feb 27, 2022
1 parent 7974f51 commit 91cf965
Show file tree
Hide file tree
Showing 15 changed files with 640 additions and 168 deletions.
12 changes: 7 additions & 5 deletions crates/swc_css_ast/src/value.rs
Expand Up @@ -48,8 +48,8 @@ pub enum Value {
#[tag("Url")]
Url(Url),

#[tag("Urange")]
Urange(Urange),
#[tag("UnicodeRange")]
UnicodeRange(UnicodeRange),

#[tag("ComplexSelector")]
ComplexSelector(ComplexSelector),
Expand Down Expand Up @@ -275,10 +275,12 @@ pub enum UrlModifier {
Function(Function),
}

#[ast_node("Urange")]
pub struct Urange {
#[ast_node("UnicodeRange")]
pub struct UnicodeRange {
pub span: Span,
pub value: JsWord,
pub prefix: char,
pub start: JsWord,
pub end: Option<JsWord>,
}

#[ast_node("CalcSum")]
Expand Down
17 changes: 14 additions & 3 deletions crates/swc_css_codegen/src/lib.rs
Expand Up @@ -795,7 +795,7 @@ where
Value::CalcSum(n) => emit!(self, n),
Value::Url(n) => emit!(self, n),
Value::Delimiter(n) => emit!(self, n),
Value::Urange(n) => emit!(self, n),
Value::UnicodeRange(n) => emit!(self, n),
Value::ComplexSelector(n) => emit!(self, n),
Value::PreservedToken(n) => emit!(self, n),
}
Expand Down Expand Up @@ -1508,8 +1508,19 @@ where
}

#[emitter]
fn emit_urange(&mut self, n: &Urange) -> Result {
self.wr.write_raw(Some(n.span), &n.value)?;
fn emit_unicode_range(&mut self, n: &UnicodeRange) -> Result {
let mut value = String::new();

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

if let Some(end) = &n.end {
value.push('-');
value.push_str(end);
}

self.wr.write_raw(Some(n.span), &value)?;
}

#[emitter]
Expand Down
2 changes: 1 addition & 1 deletion crates/swc_css_minifier/src/compress/mod.rs
Expand Up @@ -9,5 +9,5 @@ pub mod length;
pub mod selector;
pub mod time;
pub mod transform_function;
pub mod urange;
pub mod unicore_range;
pub mod url;
@@ -1,13 +1,13 @@
use swc_css_ast::*;
use swc_css_visit::{VisitMut, VisitMutWith};

pub fn compress_urange() -> impl VisitMut {
CompressUrange {}
pub fn compress_unicode_range() -> impl VisitMut {
CompressUnicodeRange {}
}

struct CompressUrange {}
struct CompressUnicodeRange {}

impl CompressUrange {
impl CompressUnicodeRange {
fn remove_leading_zeros(&mut self, value: &str) -> String {
let mut result = String::new();
let mut is_leading = true;
Expand Down Expand Up @@ -49,52 +49,26 @@ impl CompressUrange {
}
}

// IE and Edge before 16 version ignore the unicode-range if the 'U' is
// lowercase
impl VisitMut for CompressUnicodeRange {
fn visit_mut_unicode_range(&mut self, unicode_range: &mut UnicodeRange) {
unicode_range.visit_mut_children_with(self);

impl VisitMut for CompressUrange {
fn visit_mut_urange(&mut self, urange: &mut Urange) {
urange.visit_mut_children_with(self);
if unicode_range.end.is_none() {
unicode_range.start = self.remove_leading_zeros(&*unicode_range.start).into();

let str_value = &urange.value[2..];

if !urange.value.contains('-') {
let mut value = String::new();

value.push_str("U+");
value.push_str(&self.remove_leading_zeros(str_value));

urange.value = value.into();

return;
}

let parts: Vec<&str> = str_value.split('-').collect();

if parts.len() != 2 {
return;
}

let start = parts[0];
let end = parts[1];
let start = &unicode_range.start;
let end = unicode_range.end.as_ref().unwrap();
let merged = self.merge_start_and_end(start, end);

if let Some(merged) = &merged {
let mut value = String::new();

value.push_str("U+");
value.push_str(&self.remove_leading_zeros(merged));

urange.value = value.into();
unicode_range.start = self.remove_leading_zeros(merged).into();
unicode_range.end = None;
} else {
let mut value = String::new();

value.push_str("U+");
value.push_str(&self.remove_leading_zeros(start));
value.push('-');
value.push_str(&self.remove_leading_zeros(end));

urange.value = value.into();
unicode_range.start = self.remove_leading_zeros(start).into();
unicode_range.end = Some(self.remove_leading_zeros(end).into());
}
}
}
5 changes: 3 additions & 2 deletions crates/swc_css_minifier/src/lib.rs
Expand Up @@ -8,7 +8,8 @@ use self::compress::{
easing_function::compress_easing_function, empty::compress_empty,
frequency::compress_frequency, keyframes::compress_keyframes, length::compress_length,
selector::compress_selector, time::compress_time,
transform_function::compress_transform_function, urange::compress_urange, url::compress_url,
transform_function::compress_transform_function, unicore_range::compress_unicode_range,
url::compress_url,
};

mod compress;
Expand All @@ -21,7 +22,7 @@ pub fn minify(stylesheet: &mut Stylesheet) {
stylesheet.visit_mut_with(&mut compress_time());
stylesheet.visit_mut_with(&mut compress_frequency());
stylesheet.visit_mut_with(&mut compress_url());
stylesheet.visit_mut_with(&mut compress_urange());
stylesheet.visit_mut_with(&mut compress_unicode_range());
stylesheet.visit_mut_with(&mut compress_easing_function());
stylesheet.visit_mut_with(&mut compress_transform_function());
stylesheet.visit_mut_with(&mut compress_declaration());
Expand Down

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

1 comment on commit 91cf965

@github-actions
Copy link

Choose a reason for hiding this comment

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

Benchmark

Benchmark suite Current: 91cf965 Previous: 7de7eca Ratio
full_es2015 171701291 ns/iter (± 8669134) 207802600 ns/iter (± 30779069) 0.83
full_es2016 158713336 ns/iter (± 27292179) 210351014 ns/iter (± 24463329) 0.75
full_es2017 152437458 ns/iter (± 28847202) 213228999 ns/iter (± 34720329) 0.71
full_es2018 152042405 ns/iter (± 31896012) 207420220 ns/iter (± 21868299) 0.73
full_es2019 174950959 ns/iter (± 28672825) 216279652 ns/iter (± 33548346) 0.81
full_es2020 142242661 ns/iter (± 19954513) 194909611 ns/iter (± 27038786) 0.73
full_es3 218802452 ns/iter (± 21002468) 259906496 ns/iter (± 31165727) 0.84
full_es5 217978457 ns/iter (± 30164379) 273734743 ns/iter (± 36898092) 0.80
parser 671159 ns/iter (± 15878) 878587 ns/iter (± 261337) 0.76

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.