Skip to content

Commit

Permalink
fix: logic
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-akait committed Oct 18, 2022
1 parent 6233b77 commit 22777e8
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 24 deletions.
49 changes: 29 additions & 20 deletions crates/swc_html_minifier/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -663,13 +663,11 @@ impl Minifier<'_> {
}
}

fn is_javascript_url_attribute(&self, element: &Element, value: &str) -> bool {
if value.trim().starts_with("javascript:") {
match (element.namespace, &element.tag_name) {
(Namespace::HTML | Namespace::SVG, &js_word!("a")) => return true,
(Namespace::HTML, &js_word!("iframe")) => return true,
_ => {}
}
fn is_javascript_url_element(&self, element: &Element) -> bool {
match (element.namespace, &element.tag_name) {
(Namespace::HTML | Namespace::SVG, &js_word!("a")) => return true,
(Namespace::HTML, &js_word!("iframe")) => return true,
_ => {}
}

false
Expand Down Expand Up @@ -2393,31 +2391,42 @@ impl VisitMut for Minifier<'_> {
_ if self.is_trimable_separated_attribute(current_element, &n.name) => {
let mut value = value.to_string();

if self.options.normalize_attributes {
value = value.trim().to_string();
}
let fallback = |n: &mut Attribute| {
if self.options.normalize_attributes {
n.value = Some(value.trim().into());
}
};

if self.need_minify_css() && n.name == js_word!("style") && !value.is_empty() {
if let Some(minified) =
self.minify_css(value, CssMinificationMode::ListOfDeclarations)
let value = value.trim();

if let Some(minified) = self
.minify_css(value.to_string(), CssMinificationMode::ListOfDeclarations)
{
n.value = Some(minified.into());
} else {
fallback(n);
}
} else if self.need_minify_js()
&& self.is_javascript_url_attribute(current_element, &value)
&& self.is_javascript_url_element(current_element)
{
let value = value.replace("javascript:", "");
if value.trim().to_lowercase().starts_with("javascript:") {
value = value.trim().chars().skip(11).collect();

if let Some(minified) = self.minify_js(value, false, true) {
let mut with_javascript = String::with_capacity(11 + minified.len());
if let Some(minified) = self.minify_js(value, false, true) {
let mut with_javascript =
String::with_capacity(11 + minified.len());

with_javascript.push_str("javascript:");
with_javascript.push_str(&minified);
with_javascript.push_str("javascript:");
with_javascript.push_str(&minified);

n.value = Some(with_javascript.into());
n.value = Some(with_javascript.into());
}
} else {
fallback(n);
}
} else {
n.value = Some(value.into());
fallback(n);
}
}
_ if self.options.minify_additional_attributes.is_some() => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
<title>Document</title>
</head>
<body>
<a href=" javascript: alert( 'test' ) ">zz</a>
<a href=" javascript: alert( 'test' ) ">a</a>
<a href=" JAVASCRIPT: alert( 'test' ) ">b</a>
<iframe src="javascript: alert( 'test' ) " frameborder="0"></iframe>
<svg viewBox="0 0 100 100" xmlns="http://www.w3.org/2000/svg">
<!-- A link around a shape -->
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<!doctype html><html lang=en><title>Document</title><a href='javascript:alert("test");'>zz</a>
<!doctype html><html lang=en><title>Document</title><a href='javascript:alert("test");'>a</a>
<a href='javascript:alert("test");'>b</a>
<iframe src='javascript:alert("test");' frameborder=0></iframe>
<svg viewBox="0 0 100 100">

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
<title>Document</title>
</head>
<body>
<a href=" javascript: alert( 'test' ) ">zz</a>
<a href=" javascript: alert( 'test' ) ">a</a>
<a href=" JAVASCRIPT: alert( 'test' ) ">b</a>
<a href=" JAVASCRIPT: broken;;( ">b</a>
<iframe src="javascript: alert( 'test' ) " frameborder="0"></iframe>
<svg viewBox="0 0 100 100" xmlns="http://www.w3.org/2000/svg">
<!-- A link around a shape -->
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<!doctype html><html lang=en><title>Document</title><a href='javascript:alert("test");'>zz</a>
<!doctype html><html lang=en><title>Document</title><a href='javascript:alert("test");'>a</a>
<a href='javascript:alert("test");'>b</a>
<a href=" JAVASCRIPT: broken;;( ">b</a>
<iframe src='javascript:alert("test");' frameborder=0></iframe>
<svg viewBox="0 0 100 100">

Expand Down

0 comments on commit 22777e8

Please sign in to comment.