use crate::html; use html5ever::rcdom::{Handle, NodeData}; use html5ever::serialize::{serialize, SerializeOpts}; use reqwest::blocking::Client; use std::collections::HashMap; #[test] fn is_icon() { assert_eq!(html::is_icon("icon"), true); assert_eq!(html::is_icon("Shortcut Icon"), true); assert_eq!(html::is_icon("ICON"), true); assert_eq!(html::is_icon("mask-icon"), true); assert_eq!(html::is_icon("fluid-icon"), true); assert_eq!(html::is_icon("stylesheet"), false); assert_eq!(html::is_icon(""), false); } #[test] fn get_parent_node_get_node_name() { let html = "

"; let dom = html::html_to_dom(&html); let mut count = 0; fn test_walk(node: &Handle, i: &mut i8) { *i += 1; match &node.data { NodeData::Document => { for child in node.children.borrow().iter() { test_walk(child, &mut *i); } } NodeData::Element { ref name, .. } => { let node_name = name.local.as_ref().to_string(); let parent = html::get_parent_node(node); let parent_node_name = html::get_node_name(&parent); if node_name == "head" || node_name == "body" { assert_eq!(parent_node_name, "html"); } else if node_name == "div" { assert_eq!(parent_node_name, "body"); } else if node_name == "p" { assert_eq!(parent_node_name, "div"); } println!("{}", node_name); for child in node.children.borrow().iter() { test_walk(child, &mut *i); } } _ => (), }; } test_walk(&dom.document, &mut count); assert_eq!(count, 7); } #[test] fn walk_and_embed_assets() { let cache = &mut HashMap::new(); let html = "

"; let dom = html::html_to_dom(&html); let url = "http://localhost"; let opt_no_css: bool = false; let opt_no_frames: bool = false; let opt_no_js: bool = false; let opt_no_images: bool = false; let opt_silent = true; let client = Client::new(); html::walk_and_embed_assets( cache, &client, &url, &dom.document, opt_no_css, opt_no_js, opt_no_images, opt_silent, opt_no_frames, ); let mut buf: Vec = Vec::new(); serialize(&mut buf, &dom.document, SerializeOpts::default()).unwrap(); assert_eq!( buf.iter().map(|&c| c as char).collect::(), "

" ); } #[test] fn walk_and_embed_assets_ensure_no_recursive_iframe() { let html = "

"; let dom = html::html_to_dom(&html); let url = "http://localhost"; let cache = &mut HashMap::new(); let opt_no_css: bool = false; let opt_no_frames: bool = false; let opt_no_js: bool = false; let opt_no_images: bool = false; let opt_silent = true; let client = Client::new(); html::walk_and_embed_assets( cache, &client, &url, &dom.document, opt_no_css, opt_no_js, opt_no_images, opt_silent, opt_no_frames, ); let mut buf: Vec = Vec::new(); serialize(&mut buf, &dom.document, SerializeOpts::default()).unwrap(); assert_eq!( buf.iter().map(|&c| c as char).collect::(), "

" ); } #[test] fn walk_and_embed_assets_ensure_no_recursive_frame() { let html = ""; let dom = html::html_to_dom(&html); let url = "http://localhost"; let cache = &mut HashMap::new(); let opt_no_css: bool = false; let opt_no_frames: bool = false; let opt_no_js: bool = false; let opt_no_images: bool = false; let opt_silent = true; let client = Client::new(); html::walk_and_embed_assets( cache, &client, &url, &dom.document, opt_no_css, opt_no_js, opt_no_images, opt_silent, opt_no_frames, ); let mut buf: Vec = Vec::new(); serialize(&mut buf, &dom.document, SerializeOpts::default()).unwrap(); assert_eq!( buf.iter().map(|&c| c as char).collect::(), "" ); } #[test] fn walk_and_embed_assets_no_css() { let html = "\ \
"; let dom = html::html_to_dom(&html); let url = "http://localhost"; let cache = &mut HashMap::new(); let opt_no_css: bool = true; let opt_no_frames: bool = false; let opt_no_js: bool = false; let opt_no_images: bool = false; let opt_silent = true; let client = Client::new(); html::walk_and_embed_assets( cache, &client, &url, &dom.document, opt_no_css, opt_no_js, opt_no_images, opt_silent, opt_no_frames, ); let mut buf: Vec = Vec::new(); serialize(&mut buf, &dom.document, SerializeOpts::default()).unwrap(); assert_eq!( buf.iter().map(|&c| c as char).collect::(), "\ \ \ \ \ \
\ \ " ); } #[test] fn walk_and_embed_assets_no_images() { let html = "\
"; let dom = html::html_to_dom(&html); let url = "http://localhost"; let cache = &mut HashMap::new(); let opt_no_css: bool = false; let opt_no_frames: bool = false; let opt_no_js: bool = false; let opt_no_images: bool = true; let opt_silent = true; let client = Client::new(); html::walk_and_embed_assets( cache, &client, &url, &dom.document, opt_no_css, opt_no_js, opt_no_images, opt_silent, opt_no_frames, ); let mut buf: Vec = Vec::new(); serialize(&mut buf, &dom.document, SerializeOpts::default()).unwrap(); assert_eq!( buf.iter().map(|&c| c as char).collect::(), "\ \ \ \ \
\ \
\ \ " ); } #[test] fn walk_and_embed_assets_no_body_background_images() { let html = ""; let dom = html::html_to_dom(&html); let url = "http://localhost"; let cache = &mut HashMap::new(); let opt_no_css: bool = false; let opt_no_frames: bool = false; let opt_no_js: bool = false; let opt_no_images: bool = true; let opt_silent = true; let client = Client::new(); html::walk_and_embed_assets( cache, &client, &url, &dom.document, opt_no_css, opt_no_js, opt_no_images, opt_silent, opt_no_frames, ); let mut buf: Vec = Vec::new(); serialize(&mut buf, &dom.document, SerializeOpts::default()).unwrap(); assert_eq!( buf.iter().map(|&c| c as char).collect::(), "" ); } #[test] fn walk_and_embed_assets_no_frames() { let html = ""; let dom = html::html_to_dom(&html); let url = "http://localhost"; let cache = &mut HashMap::new(); let opt_no_css: bool = false; let opt_no_frames: bool = true; let opt_no_js: bool = false; let opt_no_images: bool = false; let opt_silent = true; let client = Client::new(); html::walk_and_embed_assets( cache, &client, &url, &dom.document, opt_no_css, opt_no_js, opt_no_images, opt_silent, opt_no_frames, ); let mut buf: Vec = Vec::new(); serialize(&mut buf, &dom.document, SerializeOpts::default()).unwrap(); assert_eq!( buf.iter().map(|&c| c as char).collect::(), "" ); } #[test] fn walk_and_embed_assets_no_iframes() { let html = ""; let dom = html::html_to_dom(&html); let url = "http://localhost"; let cache = &mut HashMap::new(); let opt_no_css: bool = false; let opt_no_frames: bool = true; let opt_no_js: bool = false; let opt_no_images: bool = false; let opt_silent = true; let client = Client::new(); html::walk_and_embed_assets( cache, &client, &url, &dom.document, opt_no_css, opt_no_js, opt_no_images, opt_silent, opt_no_frames, ); let mut buf: Vec = Vec::new(); serialize(&mut buf, &dom.document, SerializeOpts::default()).unwrap(); assert_eq!( buf.iter().map(|&c| c as char).collect::(), "" ); } #[test] fn walk_and_embed_assets_no_js() { let html = "
\ \ \
"; let dom = html::html_to_dom(&html); let url = "http://localhost"; let cache = &mut HashMap::new(); let opt_no_css: bool = false; let opt_no_frames: bool = false; let opt_no_js: bool = true; let opt_no_images: bool = false; let opt_silent = true; let client = Client::new(); html::walk_and_embed_assets( cache, &client, &url, &dom.document, opt_no_css, opt_no_js, opt_no_images, opt_silent, opt_no_frames, ); let mut buf: Vec = Vec::new(); serialize(&mut buf, &dom.document, SerializeOpts::default()).unwrap(); assert_eq!( buf.iter().map(|&c| c as char).collect::(), "
\
" ); } #[test] fn walk_and_embed_with_no_integrity() { let html = "No integrity\ \ "; let dom = html::html_to_dom(&html); let url = "http://localhost"; let cache = &mut HashMap::new(); let client = Client::new(); let opt_no_css: bool = true; let opt_no_frames: bool = true; let opt_no_js: bool = true; let opt_no_images: bool = true; let opt_silent = true; html::walk_and_embed_assets( cache, &client, &url, &dom.document, opt_no_css, opt_no_js, opt_no_images, opt_silent, opt_no_frames, ); let mut buf: Vec = Vec::new(); serialize(&mut buf, &dom.document, SerializeOpts::default()).unwrap(); assert_eq!( buf.iter().map(|&c| c as char).collect::(), "\ No integrity\ \ " ); } #[test] fn stringify_document() { let html = "
"; let dom = html::html_to_dom(&html); let opt_no_css: bool = false; let opt_no_frames: bool = false; let opt_no_js: bool = false; let opt_no_images: bool = false; let opt_isolate: bool = false; assert_eq!( html::stringify_document( &dom.document, opt_no_css, opt_no_frames, opt_no_js, opt_no_images, opt_isolate, ), "
" ); } #[test] fn stringify_document_isolate() { let html = "Isolated document\ \ \
"; let dom = html::html_to_dom(&html); let opt_no_css: bool = false; let opt_no_frames: bool = false; let opt_no_js: bool = false; let opt_no_images: bool = false; let opt_isolate: bool = true; assert_eq!( html::stringify_document( &dom.document, opt_no_css, opt_no_frames, opt_no_js, opt_no_images, opt_isolate, ), "\ \ \ Isolated document\ \ \ \ \
\ \
\ \ " ); } #[test] fn stringify_document_no_css() { let html = "\ Unstyled document\ \
"; let dom = html::html_to_dom(&html); let opt_no_css: bool = true; let opt_no_frames: bool = false; let opt_no_js: bool = false; let opt_no_images: bool = false; let opt_isolate: bool = false; assert_eq!( html::stringify_document( &dom.document, opt_no_css, opt_no_frames, opt_no_js, opt_no_images, opt_isolate, ), "\ \ \ \ Unstyled document\ \ \
\ " ); } #[test] fn stringify_document_no_frames() { let html = "\ Frameless document\ \
"; let dom = html::html_to_dom(&html); let opt_no_css: bool = false; let opt_no_frames: bool = true; let opt_no_js: bool = false; let opt_no_images: bool = false; let opt_isolate: bool = false; assert_eq!( html::stringify_document( &dom.document, opt_no_css, opt_no_frames, opt_no_js, opt_no_images, opt_isolate, ), "\ \ \ \ Frameless document\ \ \
\ " ); } #[test] fn stringify_document_isolate_no_frames_no_js_no_css_no_images() { let html = "\ no-frame no-css no-js no-image isolated document\ \ \
\ \ \ \
"; let dom = html::html_to_dom(&html); let opt_isolate: bool = true; let opt_no_css: bool = true; let opt_no_frames: bool = true; let opt_no_js: bool = true; let opt_no_images: bool = true; assert_eq!( html::stringify_document( &dom.document, opt_no_css, opt_no_frames, opt_no_js, opt_no_images, opt_isolate, ), "\ \ \ \ no-frame no-css no-js no-image isolated document\ \ \ \ \
\ \ \ \
\ \ " ); }