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

Avoid panic when parsing contents with templates #142

Merged
merged 2 commits into from
Mar 25, 2024
Merged
Show file tree
Hide file tree
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
16 changes: 14 additions & 2 deletions native/html5ever_nif/src/flat_dom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -214,8 +214,20 @@ impl TreeSink for FlatSink {
fn get_document(&mut self) -> Self::Handle {
NodeHandle(0)
}
fn get_template_contents(&mut self, _target: &Self::Handle) -> Self::Handle {
panic!("Templates not supported");
fn get_template_contents(&mut self, target: &Self::Handle) -> Self::Handle {
// Inspired in https://github.com/servo/html5ever/blob/1a62a39879a1def200dcb87b900265993e6c1c83/rcdom/lib.rs#L235
// It is not getting the templates contents. But is printing the empty tag.
// TODO: print the contents as text.
let node = self.node(*target);
if let NodeData::Element {
ref template_contents,
..
} = node.data
{
*template_contents.as_ref().expect("not a template element!")
} else {
panic!("not a template element!")
}
}

fn same_node(&self, x: &Self::Handle, y: &Self::Handle) -> bool {
Expand Down
29 changes: 29 additions & 0 deletions test/html5ever_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -279,4 +279,33 @@ defmodule Html5everTest do
]}
]} = parsed
end

test "parse html with a template tag ignores template content" do
html = """
<!doctype html>
<html>
<head><title>With template</title></head>
<body>
<h1>Document</h1>
<template>
<h2>Flower</h2>
<img src="img_white_flower.jpg" width="214" height="204">
</template>
</body>
</html>
"""

assert Html5ever.parse(html) ==
{:ok,
[
{:doctype, "html", "", ""},
{"html", [],
[
{"head", [], [{"title", [], ["With template"]}]},
"\n",
{"body", [],
["\n", {"h1", [], ["Document"]}, "\n", {"template", [], []}, "\n", "\n", "\n"]}
]}
]}
end
end