fix(html/minifier): Fix script/style merging (#6443)

This commit is contained in:
Alexander Akait 2022-11-16 06:31:03 +03:00 committed by GitHub
parent 520c5ca0e8
commit 1dfd8cef88
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 89 additions and 25 deletions

View File

@ -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());
new_data.push_str(&prev_text.data);
new_data.push_str(&text.data);
text.data = new_data.into();
new_children.pop();
}
}
};
if need_continue {
continue;
}
}
}
if result {
new_children.push(child);
}
}

View File

@ -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>

View File

@ -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>

View File

@ -90,5 +90,41 @@
<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>

View File

@ -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>
@ -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>