diff options
Diffstat (limited to 'htmlua-parser')
| -rw-r--r-- | htmlua-parser/src/render.rs | 37 | ||||
| -rw-r--r-- | htmlua-parser/tests/components/multi1_1.html | 1 | ||||
| -rw-r--r-- | htmlua-parser/tests/components/multi1_2.html | 2 | ||||
| -rw-r--r-- | htmlua-parser/tests/components/multi1_3.html | 1 |
4 files changed, 39 insertions, 2 deletions
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::<Vec<_>>() { 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) } @@ -266,6 +270,35 @@ mod tests { } #[test] + fn multiple_include() { + let page = r#" + <!DOCTYPE html> + <html> + <head> + <title>Basic HTML Page</title> + </head> + <body> + <h1>Hello World</h1> + <p>This is a paragraph.</p> + <include path="multi1_1.html"></include> + <include path="multi1_2.html"></include> + <include path="multi1_3.html"></include> + </body> + </html>"#; + 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#" <!DOCTYPE html> 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 @@ +<span id="inc1">included1</span> 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 @@ + +<span id="inc2">included2</span> 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 @@ +<span id="inc3">included3</span> |
