Better impl Display for Node, fix test
This commit is contained in:
parent
c9c89f3622
commit
87c1832896
@ -220,6 +220,27 @@ impl<'arena> fmt::Display for Node<'arena> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<'arena> fmt::Display for NodeData<'arena> {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
match self {
|
||||
NodeData::Document => write!(f, "Document"),
|
||||
NodeData::Doctype { name, .. } => write!(f, "Doctype: {}", name),
|
||||
NodeData::Text { contents } => write!(
|
||||
f,
|
||||
"Text: {}...",
|
||||
&contents.borrow().chars().take(10).collect::<String>()
|
||||
),
|
||||
NodeData::ProcessingInstruction { .. } => write!(f, "ProcessingInstruction: ..."),
|
||||
NodeData::Comment { contents } => write!(
|
||||
f,
|
||||
"Comment: {}...",
|
||||
&contents.chars().take(10).collect::<String>()
|
||||
),
|
||||
NodeData::Element { ref name, .. } => write!(f, "Element: {}", &name.local),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn write_node<'arena>(
|
||||
node: &Node<'arena>,
|
||||
indent: usize,
|
||||
@ -227,16 +248,44 @@ fn write_node<'arena>(
|
||||
) -> fmt::Result {
|
||||
let indent_str = " ".repeat(indent);
|
||||
writeln!(f, "{}Node {{", &indent_str)?;
|
||||
writeln!(f, "{} data: {:?}", &indent_str, node.data)?;
|
||||
let mut child = node.first_child.get();
|
||||
if child.is_some() {
|
||||
writeln!(f, "{} children: [", &indent_str)?;
|
||||
while let Some(next_child) = child {
|
||||
write_node(next_child, indent + 2, f)?;
|
||||
child = next_child.next_sibling.get();
|
||||
writeln!(f, "{} data: {}", &indent_str, node.data)?;
|
||||
|
||||
if let Some(parent) = node.parent.get() {
|
||||
writeln!(f, "{} parent: ", &indent_str)?;
|
||||
write_linked_node(parent, indent + 2, f)?;
|
||||
}
|
||||
writeln!(f, "{} ]", &indent_str)?;
|
||||
|
||||
if let Some(next_sibling) = node.next_sibling.get() {
|
||||
writeln!(f, "{} next_sibling: ", &indent_str)?;
|
||||
write_linked_node(next_sibling, indent + 2, f)?;
|
||||
}
|
||||
|
||||
if let Some(previous_sibling) = node.previous_sibling.get() {
|
||||
writeln!(f, "{} previous_sibling: ", &indent_str)?;
|
||||
write_linked_node(previous_sibling, indent + 2, f)?;
|
||||
}
|
||||
|
||||
if let Some(first_child) = node.first_child.get() {
|
||||
writeln!(f, "{} first_child: ", &indent_str)?;
|
||||
write_linked_node(first_child, indent + 2, f)?;
|
||||
}
|
||||
|
||||
if let Some(last_child) = node.last_child.get() {
|
||||
writeln!(f, "{} last_child: ", &indent_str)?;
|
||||
write_linked_node(last_child, indent + 2, f)?;
|
||||
}
|
||||
|
||||
writeln!(f, "{}}}", &indent_str)
|
||||
}
|
||||
|
||||
fn write_linked_node<'arena>(
|
||||
node: &Node<'arena>,
|
||||
indent: usize,
|
||||
f: &mut fmt::Formatter<'_>,
|
||||
) -> fmt::Result {
|
||||
let indent_str = " ".repeat(indent);
|
||||
writeln!(f, "{}Node {{", &indent_str)?;
|
||||
writeln!(f, "{} data: {}", &indent_str, node.data)?;
|
||||
writeln!(f, "{}}}", &indent_str)
|
||||
}
|
||||
|
||||
|
@ -97,13 +97,18 @@ impl<'arena> Sanitizer<'arena> {
|
||||
if self.should_unwrap_node(node) {
|
||||
let sibling = node.next_sibling.get();
|
||||
|
||||
println!("unwrapping node");
|
||||
if self.should_remove_contents_when_unwrapped(node) {
|
||||
println!("detaching node");
|
||||
node.detach();
|
||||
println!("post-detach: {}", &node);
|
||||
} else if let Some(unwrapped_node) = node.unwrap() {
|
||||
println!("traversing unwrapped node");
|
||||
self.traverse(unwrapped_node);
|
||||
}
|
||||
|
||||
if let Some(sibling) = sibling {
|
||||
println!("traversing sibling");
|
||||
self.traverse(sibling);
|
||||
}
|
||||
|
||||
@ -316,7 +321,6 @@ mod test {
|
||||
assert_eq!(str::from_utf8(&output).unwrap(), "<html><div></div></html>");
|
||||
}
|
||||
|
||||
// FIXME: this is failing, need to fix the traversal & detach algorithm
|
||||
#[test]
|
||||
fn remove_script_elements_and_content_in_separate_sub_trees() {
|
||||
let mut disallow_script_config = EMPTY_CONFIG.clone();
|
||||
@ -328,7 +332,7 @@ mod test {
|
||||
.insert(local_name!("script"));
|
||||
let sanitizer = Sanitizer::new(&disallow_script_config, vec![]);
|
||||
let mut mock_data = MockRead::new(
|
||||
"<div><script>alert('haX0rz')</script><div><div><script>two</script></div>",
|
||||
"<div><script>alert('haX0rz')</script></div><div><script>two</script></div>",
|
||||
);
|
||||
let mut output = vec![];
|
||||
sanitizer
|
||||
|
Loading…
Reference in New Issue
Block a user