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/ast): improve unicode range AST #3757

Merged
merged 3 commits into from Feb 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
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.