fn sanitize(doc: &mut RcDom, node: &Handle, par_node: &Handle, convert_image_flag: bool) {
    let next_par = match &node.data {
        NodeData::Text { contents } => {
            let contents = contents.borrow().clone();
            doc.append(&par_node, AppendText(contents));
            None
        }
        NodeData::Comment { contents } => {
            let contents = contents.clone();
            let e = doc.create_comment(contents);
            doc.append(&par_node, AppendNode(e));
            None
        }
        NodeData::Element { name, attrs, .. } => {
            let tag_name: String = name.local.to_string();
            if WHITELIST.contains_key(&tag_name) {
                let attr_list = &WHITELIST[&tag_name];
                let v: Vec<Attribute> = attrs
                    .borrow()
                    .iter()
                    .filter(|a| {
                        let attr_name = a.name.local.to_string();
                        attr_list.contains(&attr_name)
                    })
                    .cloned()
                    .map(|mut a| {
                        if convert_image_flag {
                            let attr_name = a.name.local.to_string();
                            if &tag_name == "img" && &attr_name == "src" {
                                let src = a.value.to_string();
                                let data = convert_img_to_base64(src);
                                a.value = data.into();
                            }
                        }
                        a
                    })
                    .collect();
                let e = doc.create_element(name.clone(), v, Default::default());
                doc.append(&par_node, AppendNode(e.clone()));
                Some(e)
            } else {
                info!("invalid element discarded: [{}]", &tag_name);
                None
            }
        }
        _ => {
            info!("node skipped : {:?}", &node.data);
            None
        }
    };

    let next_par = next_par.as_ref().unwrap_or(par_node);
    for ch in node.children.borrow().iter() {
        sanitize(doc, ch, &next_par, convert_image_flag);
    }
}
/// walk on dom tree and sanitize elements
walk on dom tree and sanitize elements
walk on dom tree and sanitize elements
fn sanitize(doc: &mut RcDom, node: &Handle, par_node: &Handle, convert_image_flag: bool) {
    let next_par = match &node.data {
        NodeData::Text { contents } => {
            let contents = contents.borrow().clone();
            doc.append(&par_node, AppendText(contents));
            None
        }
        NodeData::Comment { contents } => {
            let contents = contents.clone();
            let e = doc.create_comment(contents);
            doc.append(&par_node, AppendNode(e));
            None
        }
        NodeData::Element { name, attrs, .. } => {
            let tag_name: String = name.local.to_string();
            if WHITELIST.contains_key(&tag_name) {
                let attr_list = &WHITELIST[&tag_name];
                let v: Vec<Attribute> = attrs
                    .borrow()
                    .iter()
                    .filter(|a| {
                        let attr_name = a.name.local.to_string();
                        attr_list.contains(&attr_name)
                    })
                    .cloned()
                    .map(|mut a| {
                        if convert_image_flag {
                            let attr_name = a.name.local.to_string();
                            if &tag_name == "img" && &attr_name == "src" {
                                let src = a.value.to_string();
                                let data = convert_img_to_base64(src);
                                a.value = data.into();
                            }
                        }
                        a
                    })
                    .collect();
                let e = doc.create_element(name.clone(), v, Default::default());
                doc.append(&par_node, AppendNode(e.clone()));
                Some(e)
            } else {
                info!("invalid element discarded: [{}]", &tag_name);
                None
            }
        }
        _ => {
            info!("node skipped : {:?}", &node.data);
            None
        }
    };
    let next_par = next_par.as_ref().unwrap_or(par_node);
    for ch in node.children.borrow().iter() {
        sanitize(doc, ch, &next_par, convert_image_flag);
    }
}
fn sanitize ( doc : & mut RcDom , node : & Handle , par_node : & Handle , convert_image_flag : bool ) { let next_par = match & node . data { NodeData :: Text { contents } => { let contents = contents . borrow ( ) . clone ( ) ; doc . append ( & par_node , AppendText ( contents ) ) ; None } NodeData :: Comment { contents } => { let contents = contents . clone ( ) ; let e = doc . create_comment ( contents ) ; doc . append ( & par_node , AppendNode ( e ) ) ; None } NodeData :: Element { name , attrs , .. } => { let tag_name : String = name . local . to_string ( ) ; if WHITELIST . contains_key ( & tag_name ) { let attr_list = & WHITELIST [ & tag_name ] ; let v : Vec < Attribute > = attrs . borrow ( ) . iter ( ) . filter ( | a | { let attr_name = a . name . local . to_string ( ) ; attr_list . contains ( & attr_name ) } ) . cloned ( ) . map ( | mut a | { if convert_image_flag { let attr_name = a . name . local . to_string ( ) ; if & tag_name == " " && & attr_name == " " { let src = a . value . to_string ( ) ; let data = convert_img_to_base64 ( src ) ; a . value = data . into ( ) ; } } a } ) . collect ( ) ; let e = doc . create_element ( name . clone ( ) , v , Default :: default ( ) ) ; doc . append ( & par_node , AppendNode ( e . clone ( ) ) ) ; Some ( e ) } else { info ! ( " " tag_name ) ; None } } _ => { info ! ( " " node data ) ; None } } ; let next_par = next_par . as_ref ( ) . unwrap_or ( par_node ) ; for ch in node . children . borrow ( ) . iter ( ) { sanitize ( doc , ch , & next_par , convert_image_flag ) ; } }
walk on dom tree and sanitize elements
walk on dom tree and sanitize elements
doc
&mut RcDom
node
&Handle
par_node
&Handle
convert_image_flag
bool