WARNING: THIS SITE IS A MIRROR OF GITHUB.COM / IT CANNOT LOGIN OR REGISTER ACCOUNTS / THE CONTENTS ARE PROVIDED AS-IS / THIS SITE ASSUMES NO RESPONSIBILITY FOR ANY DISPLAYED CONTENT OR LINKS / IF YOU FOUND SOMETHING MAY NOT GOOD FOR EVERYONE, CONTACT ADMIN AT ilovescratch@foxmail.com
Skip to content

Commit ee7026d

Browse files
authored
Preserve siblings after <svg> (#10234)
1 parent 7689f7d commit ee7026d

File tree

1 file changed

+28
-2
lines changed

1 file changed

+28
-2
lines changed

crates/html/src/optimize.rs

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -389,10 +389,14 @@ pub fn optimize<'arena>(
389389
remove_whitespace(node, &options);
390390

391391
if should_optimize_svg {
392+
// Detach the node to prevent Oxvg from altering following siblings.
393+
let parent = node.parent.take();
394+
let previous_sibling = node.previous_sibling.take();
395+
let next_sibling = node.next_sibling.take();
396+
392397
// Synthesize a fake document node to act as the root of the SVG.
393398
let document = arena.alloc(Node::new(NodeData::Document, 0));
394-
document.first_child.set(Some(node));
395-
document.last_child.set(Some(node));
399+
document.append(node);
396400

397401
let jobs = options.minify_svg.into_jobs(OxvgKind::Html);
398402
match jobs.run(
@@ -402,6 +406,11 @@ pub fn optimize<'arena>(
402406
Err(_err) => {}
403407
Ok(()) => {}
404408
}
409+
410+
// Reattach in original position.
411+
node.parent.set(parent);
412+
node.previous_sibling.set(previous_sibling);
413+
node.next_sibling.set(next_sibling);
405414
}
406415
}
407416
_ => {
@@ -821,5 +830,22 @@ mod tests {
821830
"<svg><style>.foo{fill:red}</style><rect width=100 height=100 class=foo /></svg>",
822831
"<svg><rect width=100 height=100 style=fill:red /></svg>",
823832
);
833+
test(
834+
"<p>a</p><svg></svg><p>b</p><p>c</p>",
835+
"<p>a</p><svg></svg><p>b</p><p>c</p>",
836+
);
837+
test(
838+
"<main><svg><style>.foo{fill:red}</style><rect width=100 height=100 class=foo /></svg><span>after</span></main>",
839+
"<main><svg><rect width=100 height=100 style=fill:red /></svg><span>after</span></main>",
840+
);
841+
test(
842+
r#"<svg xmlns:editor2="link2" fill="" b="" xmlns:xlink="http://www.w3.org/1999/xlink" class="foo" xmlns:editor1="link1" xmlns="" d="">
843+
<rect editor2:b="" editor1:b="" editor2:a="" editor1:a="" />
844+
</svg>
845+
<div>
846+
<svg fill="" id="a"></svg>
847+
</div>"#,
848+
"<svg class=foo><rect></rect></svg>\n <div>\n <svg id=a></svg>\n </div>",
849+
);
824850
}
825851
}

0 commit comments

Comments
 (0)