feat(css/minifier): Compress urange (#3668)

This commit is contained in:
Alexander Akait 2022-02-23 11:45:25 +03:00 committed by GitHub
parent 78b997bd90
commit b1476d2ac6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 218 additions and 5 deletions

View File

@ -15,8 +15,6 @@ struct CompressAngle {
in_keyframe_block: bool,
}
impl CompressAngle {}
impl VisitMut for CompressAngle {
fn visit_mut_keyframe_block(&mut self, keyframe_block: &mut KeyframeBlock) {
let old_in_block = self.in_keyframe_block;

View File

@ -7,4 +7,5 @@ pub mod keyframes;
pub mod selector;
pub mod time;
pub mod transform_function;
pub mod urange;
pub mod url;

View File

@ -0,0 +1,100 @@
use swc_css_ast::*;
use swc_css_visit::{VisitMut, VisitMutWith};
pub fn compress_urange() -> impl VisitMut {
CompressUrange {}
}
struct CompressUrange {}
impl CompressUrange {
fn remove_leading_zeros(&mut self, value: &str) -> String {
let mut result = String::new();
let mut is_leading = true;
for c in value.chars() {
if c == '0' && is_leading {
continue;
}
is_leading = false;
result.push(c);
}
result
}
fn merge_start_and_end(&mut self, start: &str, end: &str) -> Option<String> {
let mut minified = String::new();
let mut question_counter = 0;
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 {
minified.push(start_c);
} else if start_c == '0' && end_c.eq_ignore_ascii_case(&'f') {
question_counter += 1;
minified.push('?')
} else {
return None;
}
} else {
return None;
}
}
Some(minified)
}
}
// IE and Edge before 16 version ignore the unicode-range if the 'U' is
// lowercase
impl VisitMut for CompressUrange {
fn visit_mut_urange(&mut self, urange: &mut Urange) {
urange.visit_mut_children_with(self);
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 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();
} 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();
}
}
}

View File

@ -7,7 +7,8 @@ use self::compress::{
angle::compress_angle, declaration::compress_declaration,
easing_function::compress_easing_function, empty::compress_empty,
frequency::compress_frequency, keyframes::compress_keyframes, selector::compress_selector,
time::compress_time, transform_function::compress_transform_function, url::compress_url,
time::compress_time, transform_function::compress_transform_function, urange::compress_urange,
url::compress_url,
};
mod compress;
@ -18,10 +19,11 @@ 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_easing_function());
stylesheet.visit_mut_with(&mut compress_transform_function());
stylesheet.visit_mut_with(&mut compress_selector());
stylesheet.visit_mut_with(&mut compress_keyframes());
stylesheet.visit_mut_with(&mut compress_declaration());
stylesheet.visit_mut_with(&mut compress_selector());
stylesheet.visit_mut_with(&mut compress_keyframes());
stylesheet.visit_mut_with(&mut compress_selector());
}

View File

@ -0,0 +1,111 @@
@font-face {
unicode-range: U+26;
}
@font-face {
unicode-range: U+0-7F;
}
@font-face {
unicode-range: U+0025-00FF;
}
@font-face {
unicode-range: U+0025-00FF, U+4??;
}
@font-face {
unicode-range: U+100000-10FFFF;
}
@font-face {
unicode-range: U+2b00-2bff;
}
@font-face {
unicode-range: u+2b00-2bff;
}
@font-face {
unicode-range: U+1e00-1eff;
}
@font-face {
unicode-range: U+2125-2128;
}
@font-face {
unicode-range: U+400-4FF;
}
@font-face {
unicode-range: U+0025-00FF, U+4??;
}
@font-face {
unicode-range: U+450-4FF;
}
@font-face {
unicode-range: U+A5, U+4E00-9FFF, U+30??, U+FF00-FF9F;
}
@font-face {
unicode-range: U+????;
}
@font-face {
unicode-range: U+0000-FFFF;
}
@font-face {
unicode-range: U+10????;
}
@font-face {
unicode-range: U+100000-10FFFF;
}
@font-face {
unicode-range: U+1e1e?;
}
@font-face {
unicode-range: u+????, U+1????, U+10????;
}
@font-face {
unicode-range: U+0025-0025;
}
@font-face {
unicode-range: U+1e1e0-1E1EF;
}
@font-face {
unicode-range: U+0025;
}
@font-face {
unicode-range: U+0125;
}
@font-face {
unicode-range: U+0100-FFFF;
}
@font-face {
unicode-range: U+10125;
}
@font-face {
unicode-range: U+450-0004FF;
}
@font-face {
unicode-range: U+000450-4FF;
}
@font-face {
unicode-range: U+000450-0004FF;
}

View File

@ -0,0 +1 @@
@font-face{unicode-range:U+26}@font-face{unicode-range:U+-7F}@font-face{unicode-range:U+25-FF}@font-face{unicode-range:U+25-FF,U+4??}@font-face{unicode-range:U+10????}@font-face{unicode-range:U+2b??}@font-face{unicode-range:U+2b??}@font-face{unicode-range:U+1e??}@font-face{unicode-range:U+2125-2128}@font-face{unicode-range:U+4??}@font-face{unicode-range:U+25-FF,U+4??}@font-face{unicode-range:U+450-4FF}@font-face{unicode-range:U+A5,U+4E00-9FFF,U+30??,U+FF00-FF9F}@font-face{unicode-range:U+????}@font-face{unicode-range:U+????}@font-face{unicode-range:U+10????}@font-face{unicode-range:U+10????}@font-face{unicode-range:U+1e1e?}@font-face{unicode-range:U+????,U+1????,U+10????}@font-face{unicode-range:U+25}@font-face{unicode-range:U+1e1e?}@font-face{unicode-range:U+25}@font-face{unicode-range:U+125}@font-face{unicode-range:U+100-FFFF}@font-face{unicode-range:U+10125}@font-face{unicode-range:U+450-4FF}@font-face{unicode-range:U+450-4FF}@font-face{unicode-range:U+450-4FF}