Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 28 additions & 2 deletions crates/html/src/optimize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -389,10 +389,14 @@ pub fn optimize<'arena>(
remove_whitespace(node, &options);

if should_optimize_svg {
// Detach the node to prevent Oxvg from altering following siblings.
let parent = node.parent.take();
let previous_sibling = node.previous_sibling.take();
let next_sibling = node.next_sibling.take();

// Synthesize a fake document node to act as the root of the SVG.
let document = arena.alloc(Node::new(NodeData::Document, 0));
document.first_child.set(Some(node));
document.last_child.set(Some(node));
document.append(node);

let jobs = options.minify_svg.into_jobs(OxvgKind::Html);
match jobs.run(
Expand All @@ -402,6 +406,11 @@ pub fn optimize<'arena>(
Err(_err) => {}
Ok(()) => {}
}

// Reattach in original position.
node.parent.set(parent);
node.previous_sibling.set(previous_sibling);
node.next_sibling.set(next_sibling);
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

alternative approach:

            // Remember the original position
            let stub = arena.alloc(Node::new(NodeData::Document, 0));
            node.insert_after(stub);

            // Synthesize a fake document node to act as the root of the SVG.
            let document = arena.alloc(Node::new(NodeData::Document, 0));
            document.append(node);

            _ = options.minify_svg.into_jobs(OxvgKind::Html).run(
              &&*document,
              &oxvg_ast::visitor::Info::<crate::oxvg::Element>::new(arena),
            );

            // Reattach
            stub.insert_after(node);
            stub.detach();

}
}
_ => {
Expand Down Expand Up @@ -821,5 +830,22 @@ mod tests {
"<svg><style>.foo{fill:red}</style><rect width=100 height=100 class=foo /></svg>",
"<svg><rect width=100 height=100 style=fill:red /></svg>",
);
test(
"<p>a</p><svg></svg><p>b</p><p>c</p>",
"<p>a</p><svg></svg><p>b</p><p>c</p>",
);
test(
"<main><svg><style>.foo{fill:red}</style><rect width=100 height=100 class=foo /></svg><span>after</span></main>",
"<main><svg><rect width=100 height=100 style=fill:red /></svg><span>after</span></main>",
);
test(
r#"<svg xmlns:editor2="link2" fill="" b="" xmlns:xlink="http://www.w3.org/1999/xlink" class="foo" xmlns:editor1="link1" xmlns="" d="">
<rect editor2:b="" editor1:b="" editor2:a="" editor1:a="" />
</svg>
<div>
<svg fill="" id="a"></svg>
</div>"#,
"<svg class=foo><rect></rect></svg>\n <div>\n <svg id=a></svg>\n </div>",
);
}
}