atmos/os_lib/
dom_fragment.rs1use alloc::string::String;
7use alloc::format;
8use crate::os_lib::dom::NodeType;
9
10pub fn context_wrapper(context_tag: &str, html: &str) -> (String, usize) {
17 let lower = context_tag.trim().to_ascii_lowercase();
18 match lower.as_str() {
19 "table" => (format!("<table>{}</table>", html), 1),
20 "tbody" | "thead" | "tfoot" => (format!("<table><tbody>{}</tbody></table>", html), 2),
21 "tr" => (format!("<table><tbody><tr>{}</tr></tbody></table>", html), 3),
22 "colgroup" => (format!("<table><colgroup>{}</colgroup></table>", html), 2),
23 "select" | "optgroup" => (format!("<select>{}</select>", html), 1),
24 _ => (format!("<div>{}</div>", html), 1),
25 }
26}
27
28pub fn extract_fragment_children<'a>(
30 root: &'a crate::os_lib::dom::Node,
31 depth: usize,
32) -> &'a [crate::os_lib::dom::Node] {
33 let mut current = root;
34 for _ in 0..depth {
35 if let Some(first) = current.children.first() {
36 current = first;
37 } else {
38 return &root.children;
39 }
40 }
41 if let NodeType::Element { tag_name, .. } = ¤t.node_type {
44 if tag_name == "table" && current.children.len() == 1 {
45 if let Some(first) = current.children.first() {
46 if let NodeType::Element { tag_name: sub_tag, .. } = &first.node_type {
47 if sub_tag == "tbody" {
48 return &first.children;
49 }
50 }
51 }
52 }
53 }
54 ¤t.children
55}