Skip to content

Commit ac646d7

Browse files
committed
Parse template elements instead of panicking
1 parent 2209ba1 commit ac646d7

File tree

3 files changed

+34
-7
lines changed

3 files changed

+34
-7
lines changed

native/meeseeks_html5ever_nif/src/flat_dom.rs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ enum NodeEnum {
5252
Data(DataType, StrTendril),
5353
Doctype(StrTendril, StrTendril, StrTendril),
5454
Document,
55-
Element(QualName, Vec<Attribute>),
55+
Element(QualName, Vec<Attribute>, bool),
5656
ProcessingInstruction(StrTendril, StrTendril),
5757
Text(StrTendril),
5858
}
@@ -201,9 +201,13 @@ impl TreeSink for FlatDom {
201201
Id(0)
202202
}
203203

204-
// Not supported
205-
fn get_template_contents(&mut self, _target: &Self::Handle) -> Self::Handle {
206-
panic!("Templates not supported");
204+
fn get_template_contents(&mut self, target: &Self::Handle) -> Self::Handle {
205+
if let Element(_, _, true) = self.node(*target).node {
206+
// Use template element as document fragment
207+
target.clone()
208+
} else {
209+
panic!("not a template element!")
210+
}
207211
}
208212

209213
// Not supported
@@ -221,8 +225,8 @@ impl TreeSink for FlatDom {
221225
}
222226
}
223227

224-
fn create_element(&mut self, name: QualName, attrs: Vec<Attribute>, _flags: ElementFlags) -> Self::Handle {
225-
self.add_node(Element(name, attrs))
228+
fn create_element(&mut self, name: QualName, attrs: Vec<Attribute>, flags: ElementFlags) -> Self::Handle {
229+
self.add_node(Element(name, attrs, flags.template))
226230
}
227231

228232
fn create_comment(&mut self, text: StrTendril) -> Self::Handle {
@@ -479,7 +483,7 @@ impl NifEncoder for Node {
479483

480484
Document => unreachable!(),
481485

482-
Element(ref name, ref attributes) => {
486+
Element(ref name, ref attributes, ref _template) => {
483487
let namespace_atom = atoms::namespace().encode(env);
484488
let tag_atom = atoms::tag().encode(env);
485489
let attributes_atom = atoms::attributes().encode(env);

test/data/template.html

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<table id="producttable">
2+
<thead>
3+
<tr>
4+
<td>UPC_Code</td>
5+
<td>Product_Name</td>
6+
</tr>
7+
</thead>
8+
<tbody>
9+
<!-- existing data could optionally be included here -->
10+
</tbody>
11+
</table>
12+
13+
<template id="productrow">
14+
<tr>
15+
<td class="record"></td>
16+
<td></td>
17+
</tr>
18+
</template>

test/meeseeks_html5ever_test.exs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,11 @@ defmodule MeeseeksHtml5everTest do
100100
assert match?({:ok, _}, MeeseeksHtml5ever.parse_html(html))
101101
end
102102

103+
test "parse template element" do
104+
html = File.read!("test/data/template.html")
105+
assert match?({:ok, _}, MeeseeksHtml5ever.parse_html(html))
106+
end
107+
103108
test "parse xml" do
104109
xml = "<special:greeting>Hello, World!</special:greeting>"
105110
ret = {:ok,

0 commit comments

Comments
 (0)