diff --git a/htmlua-parser/src/render.rs b/htmlua-parser/src/render.rs index 2e4ed87..6444d60 100644 --- a/htmlua-parser/src/render.rs +++ b/htmlua-parser/src/render.rs @@ -119,6 +119,7 @@ pub fn expand_template(document: NodeRef, component_path: &PathBuf, include_from for i in document .select("include") .map_err(|()| anyhow!("Error finding include"))? + .collect::>() { let attrs = match i.as_node().as_element() { Some(e) => e.attributes.borrow(), @@ -129,10 +130,13 @@ pub fn expand_template(document: NodeRef, component_path: &PathBuf, include_from item_path.push(include_path); let new_node = read_doc_from_file(item_path)?; let replaced_node = expand_template(new_node, component_path, Some(i.as_node()))?; - i.as_node().insert_before(replaced_node); - i.as_node().detach(); + replaced_node.select_first("html").map_err(|()| anyhow!("Error finding html"))?.as_node().children().rev().for_each(|c| i.as_node().insert_after(c)); + } } + while let Ok(i) = document.select_first("include") { + i.as_node().detach(); + } Ok(document) } @@ -265,6 +269,35 @@ mod tests { assert!(d.select_first("include").is_err()); } + #[test] + fn multiple_include() { + let page = r#" + + + + Basic HTML Page + + +

Hello World

+

This is a paragraph.

+ + + + + "#; + let mut p = PathBuf::from(env!("CARGO_MANIFEST_DIR")); + p.push("tests/components"); + let document = kuchikiki::parse_html().one(page); + let d = expand_template(document, &p, None).unwrap(); + let text = d.select_first("#inc1").unwrap().as_node().text_contents(); + assert_eq!(text, "included1"); + let text = d.select_first("#inc2").unwrap().as_node().text_contents(); + assert_eq!(text, "included2"); + let text = d.select_first("#inc3").unwrap().as_node().text_contents(); + assert_eq!(text, "included3"); + assert!(d.select_first("include").is_err()); + } + #[test] fn basic_lua_include() { let page = r#" diff --git a/htmlua-parser/tests/components/multi1_1.html b/htmlua-parser/tests/components/multi1_1.html new file mode 100644 index 0000000..4110604 --- /dev/null +++ b/htmlua-parser/tests/components/multi1_1.html @@ -0,0 +1 @@ +included1 diff --git a/htmlua-parser/tests/components/multi1_2.html b/htmlua-parser/tests/components/multi1_2.html new file mode 100644 index 0000000..0799d2f --- /dev/null +++ b/htmlua-parser/tests/components/multi1_2.html @@ -0,0 +1,2 @@ + +included2 diff --git a/htmlua-parser/tests/components/multi1_3.html b/htmlua-parser/tests/components/multi1_3.html new file mode 100644 index 0000000..05b221c --- /dev/null +++ b/htmlua-parser/tests/components/multi1_3.html @@ -0,0 +1 @@ +included3