kuchiki-rs / kuchiki

(朽木) HTML/XML tree manipulation library for Rust

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Appending to node ref doesn't change document

quesurifn opened this issue · comments

commented

Hello! Given the following code, the end result of document doesn't have the changes to head BUT when I log the children of head after the loop, I can see the changes are in head, but the head in document never gets modified. Does anyone know what I'm doing wrong?

    fn add_style_tags(&self, html: String, parts: HashMap<String, MessagePart>) -> Result<String, Box<dyn Error>> {
        let document = kuchiki::parse_html().one(html.to_string());
        let head = match document.select_first("head") {
            Ok(h) => h,
            Err(_) => return Err(ParserError::new("No selector matches head").into()),
        }.as_node().clone();

        for (_key, value) in parts {
            let content_type = match value.content_type {
                Some(c) => c,
                None => continue,
            };
            println!("Content type: {}", content_type);
            if content_type != TEXT_CSS.to_string() {
                continue;
            }
            let style_content = kuchiki::NodeRef::new_text(value.content.unwrap());
            let style = kuchiki::NodeRef::new_element(QualName::new(None, ns!(html), local_name!("style")), vec![]);
            style.append(style_content);
            head.append(style);
        }
        
 
        Ok(document.to_string())
    }

I predict it's the clone() that's causing the problem.

I will soon archive this repository and make it read-only, so this issue will not be addressed: https://github.com/kuchiki-rs/kuchiki#archived