From ee1deddd70956170da3c8db00af8a58ab5e7266b Mon Sep 17 00:00:00 2001 From: Philip Sampaio Date: Mon, 25 Mar 2024 14:40:49 -0300 Subject: [PATCH] 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 https://github.com/rusterlium/html5ever_elixir/issues/120 * Fix clippy issue --- native/html5ever_nif/src/flat_dom.rs | 16 +++++++++++++-- test/html5ever_test.exs | 29 ++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 2 deletions(-) diff --git a/native/html5ever_nif/src/flat_dom.rs b/native/html5ever_nif/src/flat_dom.rs index d8f3adb..da2eee3 100644 --- a/native/html5ever_nif/src/flat_dom.rs +++ b/native/html5ever_nif/src/flat_dom.rs @@ -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 { diff --git a/test/html5ever_test.exs b/test/html5ever_test.exs index 00d0784..9fbbae5 100644 --- a/test/html5ever_test.exs +++ b/test/html5ever_test.exs @@ -279,4 +279,33 @@ defmodule Html5everTest do ]} ]} = parsed end + + test "parse html with a template tag ignores template content" do + html = """ + + + With template + +

Document

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