Skip to content

Commit ee1dedd

Browse files
authored
Avoid panic when parsing contents with templates (#142)
* Avoid panic when parsing contents with templates This is not getting the contents of templates, but is parsing with an empty "template" tag. Closes #120 * Fix clippy issue
1 parent 515daa0 commit ee1dedd

File tree

2 files changed

+43
-2
lines changed

2 files changed

+43
-2
lines changed

native/html5ever_nif/src/flat_dom.rs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -214,8 +214,20 @@ impl TreeSink for FlatSink {
214214
fn get_document(&mut self) -> Self::Handle {
215215
NodeHandle(0)
216216
}
217-
fn get_template_contents(&mut self, _target: &Self::Handle) -> Self::Handle {
218-
panic!("Templates not supported");
217+
fn get_template_contents(&mut self, target: &Self::Handle) -> Self::Handle {
218+
// Inspired in https://github.com/servo/html5ever/blob/1a62a39879a1def200dcb87b900265993e6c1c83/rcdom/lib.rs#L235
219+
// It is not getting the templates contents. But is printing the empty tag.
220+
// TODO: print the contents as text.
221+
let node = self.node(*target);
222+
if let NodeData::Element {
223+
ref template_contents,
224+
..
225+
} = node.data
226+
{
227+
*template_contents.as_ref().expect("not a template element!")
228+
} else {
229+
panic!("not a template element!")
230+
}
219231
}
220232

221233
fn same_node(&self, x: &Self::Handle, y: &Self::Handle) -> bool {

test/html5ever_test.exs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,4 +279,33 @@ defmodule Html5everTest do
279279
]}
280280
]} = parsed
281281
end
282+
283+
test "parse html with a template tag ignores template content" do
284+
html = """
285+
<!doctype html>
286+
<html>
287+
<head><title>With template</title></head>
288+
<body>
289+
<h1>Document</h1>
290+
<template>
291+
<h2>Flower</h2>
292+
<img src="img_white_flower.jpg" width="214" height="204">
293+
</template>
294+
</body>
295+
</html>
296+
"""
297+
298+
assert Html5ever.parse(html) ==
299+
{:ok,
300+
[
301+
{:doctype, "html", "", ""},
302+
{"html", [],
303+
[
304+
{"head", [], [{"title", [], ["With template"]}]},
305+
"\n",
306+
{"body", [],
307+
["\n", {"h1", [], ["Document"]}, "\n", {"template", [], []}, "\n", "\n", "\n"]}
308+
]}
309+
]}
310+
end
282311
end

0 commit comments

Comments
 (0)