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

fix(html/minifier): small merge fix #6443

Merged
Merged
Show file tree
Hide file tree
Changes from 3 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
39 changes: 18 additions & 21 deletions crates/swc_html_minifier/src/lib.rs
Expand Up @@ -1827,32 +1827,29 @@ impl Minifier<'_> {

let result = child_will_be_retained(&mut child, &mut new_children, children);

if result {
if self.options.remove_empty_metadata_elements
&& self.is_empty_metadata_element(&child)
{
let need_continue = {
let next_element = if let Some(Child::Element(element)) = children.get(0) {
Some(element)
} else if let Some(Child::Element(element)) = children.get(1) {
Some(element)
} else {
None
};
if self.options.remove_empty_metadata_elements {
if let Some(last_child @ Child::Element(_)) = new_children.last() {
if self.is_empty_metadata_element(last_child) {
new_children.pop();

if let Some(element) = next_element {
self.options.merge_metadata_elements
&& !self.allow_elements_to_merge(Some(&child), element)
} else {
true
}
};
if let Child::Text(text) = &mut child {
if let Some(Child::Text(prev_text)) = new_children.last_mut() {
let mut new_data =
String::with_capacity(prev_text.data.len() + text.data.len());

if need_continue {
continue;
new_data.push_str(&prev_text.data);
new_data.push_str(&text.data);

text.data = new_data.into();

new_children.pop();
}
}
}
}
}

if result {
new_children.push(child);
}
}
Expand Down
Expand Up @@ -215,5 +215,13 @@
var a = "test";console.log(a);console.log(a);console.log(a);
</script>

<div>breaker</div>
<script></script>
<script></script>

<div>breaker</div>
<script type="module"></script>
<script type="module"></script>

</body>
</html>
Expand Up @@ -90,4 +90,12 @@
<script type=module>var a="test";console.log(a),console.log(a),console.log(a)</script><script>var o="test";console.log(o);console.log(o);console.log(o)</script><script type=module>var a="test";console.log(a),console.log(a),console.log(a)</script>

<div>breaker</div>
<script>var o="test";console.log(o);console.log(o);console.log(o)</script><script type=module>var a="test";console.log(a),console.log(a),console.log(a)</script><script>var o="test";console.log(o);console.log(o);console.log(o)</script>
<script>var o="test";console.log(o);console.log(o);console.log(o)</script><script type=module>var a="test";console.log(a),console.log(a),console.log(a)</script><script>var o="test";console.log(o);console.log(o);console.log(o)</script>

<div>breaker</div>



<div>breaker</div>
<script type=module></script>
<script type=module></script>
Expand Up @@ -90,5 +90,41 @@ <h1>Text</h1>
<style type="unknown">a { color: red }</style><style type="unknown"></style>
<div>test</div>
<style type="text/css">a { color: red; }</style><style type=" text/css ">div { color: blue; }</style>
<div>EMPTY breaker</div>
<style></style>
<style></style>
<div>breaker</div>
<style></style>
<style></style>
<style></style>

<div>breaker style tags</div>

<style></style>

<style></style>

<style></style>

<style></style>

<style></style>

<div>breaker style tags</div>

<style></style><style></style><style></style><style></style><style></style>

<div>breaker</div>
<style></style>
<script></script>
<div>breaker</div>
<script></script>
<style></style>
<div>breaker</div>
<style></style>
<span>test</span>
<div>breaker</div>
<span>test</span>
<style></style>
</body>
</html>
Expand Up @@ -3,8 +3,7 @@
<style>a{color:red}b{color:blue}</style>
<div>test</div>
<style>a{color:red}</style>
<div>test</div>
<style>a{color:red}</style>
<div>test</div><style>a{color:red}</style>
<div>test</div>
<style>a,html,body{color:red}</style>
<div>test</div>
Expand All @@ -20,4 +19,20 @@
<div>test</div>
<style type=unknown>a { color: red }</style><style type=unknown></style>
<div>test</div>
<style>a{color:red}div{color:blue}</style>
<style>a{color:red}div{color:blue}</style>
<div>EMPTY breaker</div>
<div>breaker</div>

<div>breaker style tags</div>

<div>breaker style tags</div>



<div>breaker</div>
<div>breaker</div>
<div>breaker</div>

<span>test</span>
<div>breaker</div>
<span>test</span>