Skip to content

Commit

Permalink
fix: bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-akait committed Dec 13, 2022
1 parent 13d74db commit 53c066a
Show file tree
Hide file tree
Showing 9 changed files with 95 additions and 15 deletions.
6 changes: 6 additions & 0 deletions crates/swc_atoms/words.txt
Original file line number Diff line number Diff line change
Expand Up @@ -798,6 +798,7 @@ code
codebase
col
colgroup
collection
color
color-index
color-mix
Expand Down Expand Up @@ -893,6 +894,7 @@ ellipse
else
em
embed
embedded-opentype
empty-cells
encoding
end
Expand Down Expand Up @@ -1424,6 +1426,7 @@ onwebkitanimationstart
onwebkittransitionend
onwheel
opacity
opentype
optgroup
option
or
Expand Down Expand Up @@ -1790,6 +1793,7 @@ translateZ
transparent
tref
true
truetype
try
tspan
tt
Expand Down Expand Up @@ -1838,6 +1842,8 @@ widows
width
will-change
with
woff
woff2
word-break
word-spacing
word-wrap
Expand Down
16 changes: 16 additions & 0 deletions crates/swc_css_prefixer/data/prefixes_and_browsers.json
Original file line number Diff line number Diff line change
Expand Up @@ -3891,5 +3891,21 @@
"firefox": "69",
"samsung": "11.2"
}
],
"font-face-format-ident": [
{
"chrome": "4",
"edge": "12",
"firefox": "2",
"samsung": "4",
"ios": "3.2",
"opera": "10",
"safari": "3.1",
"ie": "6"
},
{
"safari": "3.2",
"ios": "4.3"
}
]
}
35 changes: 24 additions & 11 deletions crates/swc_css_prefixer/src/prefixer.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
#![allow(clippy::match_like_matches_macro)]

use core::f64::consts::PI;
use std::mem::take;

use once_cell::sync::Lazy;
use preset_env_base::{query::targets_to_versions, version::Version, BrowserData, Versions};
use swc_atoms::js_word;
use swc_atoms::{js_word, JsWord};
use swc_common::{
collections::{AHashMap, AHashSet},
EqIgnoreSpan, DUMMY_SP,
Expand Down Expand Up @@ -265,7 +264,7 @@ impl VisitMut for ImageSetFunctionReplacerOnLegacyVariant<'_> {
fn visit_mut_function(&mut self, n: &mut Function) {
let old_in_function = self.in_function;

self.in_function = true;
self.in_function = n.name.value.eq_str_ignore_ascii_case(self.from);

n.visit_mut_children_with(self);

Expand Down Expand Up @@ -515,9 +514,9 @@ where
node.visit_mut_with(&mut MediaFeatureResolutionReplacerOnLegacyVariant { from, to });
}

pub struct FontFaceFormat {}
pub struct FontFaceFormatOldSyntax {}

impl VisitMut for FontFaceFormat {
impl VisitMut for FontFaceFormatOldSyntax {
fn visit_mut_function(&mut self, n: &mut Function) {
n.visit_mut_children_with(self);

Expand All @@ -530,8 +529,22 @@ impl VisitMut for FontFaceFormat {
}

if let Some(ComponentValue::Ident(box ident)) = n.value.get(0) {
let new_value: JsWord = ident.value.clone().to_ascii_lowercase();
let new_value = match new_value {
js_word!("woff")
| js_word!("truetype")
| js_word!("opentype")
| js_word!("woff2")
| js_word!("embedded-opentype")
| js_word!("collection")
| js_word!("svg") => new_value,
_ => {
return;
}
};

let new_value = Str {
value: ident.value.clone(),
value: new_value,
span: ident.span,
raw: None,
};
Expand All @@ -541,11 +554,11 @@ impl VisitMut for FontFaceFormat {
}
}

pub fn font_face_format<N>(node: &mut N)
pub fn font_face_format_old_syntax<N>(node: &mut N)
where
N: VisitMutWith<FontFaceFormat>,
N: VisitMutWith<FontFaceFormatOldSyntax>,
{
node.visit_mut_with(&mut FontFaceFormat {});
node.visit_mut_with(&mut FontFaceFormatOldSyntax {});
}

macro_rules! to_ident {
Expand Down Expand Up @@ -3090,10 +3103,10 @@ impl VisitMut for Prefixer {
add_declaration!(Prefix::Moz, "-moz-border-radius-bottomleft", None);
}

"src" => {
"src" if should_prefix("font-face-format-ident", self.env, true) => {
let mut new_declaration = n.clone();

font_face_format(&mut new_declaration);
font_face_format_old_syntax(&mut new_declaration);

if n.value != new_declaration.value {
self.added_declarations.push(Box::new(new_declaration));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,12 @@
@font-face {
src: format("woff");
}

@font-face {
src: url(a) format("woff"), url(b) format(svg);
}

@font-face {
src: url(a) format("woff"), url(b) format("svg");
}

Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,16 @@
src: format(woff), format(truetype), format(opentype), format(woff2), format(embedded-opentype), format(collection), format(svg);
}
@Font-face{
src: FORMAT("WOFF");
src: FORMAT("woff");
src: FORMAT(WOFF);
}
@font-face{
src: format(url("woff"));
src: format("woff");
}
@font-face{
src: url(a) format("woff"), url(b) format("svg");
src: url(a) format("woff"), url(b) format(svg);
}
@font-face{
src: url(a) format("woff"), url(b) format("svg");
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,16 @@
src: format(woff), format(truetype), format(opentype), format(woff2), format(embedded-opentype), format(collection), format(svg);
}
@Font-face{
src: FORMAT("WOFF");
src: FORMAT("woff");
src: FORMAT(WOFF);
}
@font-face{
src: format(url("woff"));
src: format("woff");
}
@font-face{
src: url(a) format("woff"), url(b) format("svg");
src: url(a) format("woff"), url(b) format(svg);
}
@font-face{
src: url(a) format("woff"), url(b) format("svg");
}
8 changes: 8 additions & 0 deletions crates/swc_css_prefixer/tests/fixture/image-set/input.css
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,12 @@ h1 {

.broken {
content: image-set('foo@1x.png' 1x, "foo@2x.png" 2x) "test";
}

a {
background-image: image-set("foo@1x.png" 1x, url(foo@2x.png) 2x);
}

a {
background-image: image-set(url(foo@1x.png) 1x, unknown("foo@2x.png") 2x);
}
8 changes: 8 additions & 0 deletions crates/swc_css_prefixer/tests/fixture/image-set/output.css
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,11 @@ h1 {
content: -webkit-image-set(url("foo@1x.png") 1x, url("foo@2x.png") 2x) "test";
content: image-set('foo@1x.png' 1x, "foo@2x.png" 2x) "test";
}
a {
background-image: -webkit-image-set(url("foo@1x.png") 1x, url(foo@2x.png) 2x);
background-image: image-set("foo@1x.png" 1x, url(foo@2x.png) 2x);
}
a {
background-image: -webkit-image-set(url(foo@1x.png) 1x, unknown("foo@2x.png") 2x);
background-image: image-set(url(foo@1x.png) 1x, unknown("foo@2x.png") 2x);
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,11 @@ h1 {
content: -webkit-image-set(url("foo@1x.png") 1x, url("foo@2x.png") 2x) "test";
content: image-set('foo@1x.png' 1x, "foo@2x.png" 2x) "test";
}
a {
background-image: -webkit-image-set(url("foo@1x.png") 1x, url(foo@2x.png) 2x);
background-image: image-set("foo@1x.png" 1x, url(foo@2x.png) 2x);
}
a {
background-image: -webkit-image-set(url(foo@1x.png) 1x, unknown("foo@2x.png") 2x);
background-image: image-set(url(foo@1x.png) 1x, unknown("foo@2x.png") 2x);
}

0 comments on commit 53c066a

Please sign in to comment.