-
Notifications
You must be signed in to change notification settings - Fork 96
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5578819
commit dfb3930
Showing
5 changed files
with
98 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,53 +1,15 @@ | ||
use std::io::Error; | ||
|
||
use ego_tree::iter::Edge; | ||
use html5ever::serialize::{Serialize, Serializer, TraversalScope}; | ||
|
||
use crate::{ElementRef, Node}; | ||
use crate::ElementRef; | ||
|
||
impl<'a> Serialize for ElementRef<'a> { | ||
fn serialize<S: Serializer>( | ||
&self, | ||
serializer: &mut S, | ||
traversal_scope: TraversalScope, | ||
) -> Result<(), Error> { | ||
for edge in self.traverse() { | ||
match edge { | ||
Edge::Open(node) => { | ||
if node == **self && traversal_scope == TraversalScope::ChildrenOnly(None) { | ||
continue; | ||
} | ||
|
||
match *node.value() { | ||
Node::Doctype(ref doctype) => { | ||
serializer.write_doctype(doctype.name())?; | ||
} | ||
Node::Comment(ref comment) => { | ||
serializer.write_comment(comment)?; | ||
} | ||
Node::Text(ref text) => { | ||
serializer.write_text(text)?; | ||
} | ||
Node::Element(ref elem) => { | ||
let attrs = elem.attrs.iter().map(|(k, v)| (k, &v[..])); | ||
serializer.start_elem(elem.name.clone(), attrs)?; | ||
} | ||
_ => (), | ||
} | ||
} | ||
|
||
Edge::Close(node) => { | ||
if node == **self && traversal_scope == TraversalScope::ChildrenOnly(None) { | ||
continue; | ||
} | ||
|
||
if let Some(elem) = node.value().as_element() { | ||
serializer.end_elem(elem.name.clone())?; | ||
} | ||
} | ||
} | ||
} | ||
|
||
Ok(()) | ||
crate::node::serializable::serialize(**self, serializer, traversal_scope) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
use std::io::Error; | ||
|
||
use html5ever::serialize::{Serialize, Serializer, TraversalScope}; | ||
|
||
use crate::Html; | ||
|
||
impl Serialize for Html { | ||
fn serialize<S: Serializer>( | ||
&self, | ||
serializer: &mut S, | ||
traversal_scope: TraversalScope, | ||
) -> Result<(), Error> { | ||
crate::node::serializable::serialize(self.tree.root(), serializer, traversal_scope) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use crate::Html; | ||
|
||
#[test] | ||
fn test_serialize() { | ||
let src = r#"<!DOCTYPE html><html lang="en"><head><meta charset="utf-8"></head><body><p>Hello world!</p></body></html>"#; | ||
let html = Html::parse_document(src); | ||
assert_eq!(html.html(), src); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -360,3 +360,5 @@ impl Deref for ProcessingInstruction { | |
self.data.deref() | ||
} | ||
} | ||
|
||
pub(crate) mod serializable; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
use std::io::Error; | ||
|
||
use ego_tree::{iter::Edge, NodeRef}; | ||
use html5ever::serialize::{Serializer, TraversalScope}; | ||
|
||
use crate::Node; | ||
|
||
/// Serialize an HTML node using html5ever serializer. | ||
pub(crate) fn serialize<S: Serializer>( | ||
self_node: NodeRef<Node>, | ||
serializer: &mut S, | ||
traversal_scope: TraversalScope, | ||
) -> Result<(), Error> { | ||
for edge in self_node.traverse() { | ||
match edge { | ||
Edge::Open(node) => { | ||
if node == self_node && traversal_scope == TraversalScope::ChildrenOnly(None) { | ||
continue; | ||
} | ||
|
||
match *node.value() { | ||
Node::Doctype(ref doctype) => { | ||
serializer.write_doctype(doctype.name())?; | ||
} | ||
Node::Comment(ref comment) => { | ||
serializer.write_comment(comment)?; | ||
} | ||
Node::Text(ref text) => { | ||
serializer.write_text(text)?; | ||
} | ||
Node::Element(ref elem) => { | ||
let attrs = elem.attrs.iter().map(|(k, v)| (k, &v[..])); | ||
serializer.start_elem(elem.name.clone(), attrs)?; | ||
} | ||
_ => (), | ||
} | ||
} | ||
|
||
Edge::Close(node) => { | ||
if node == self_node && traversal_scope == TraversalScope::ChildrenOnly(None) { | ||
continue; | ||
} | ||
|
||
if let Some(elem) = node.value().as_element() { | ||
serializer.end_elem(elem.name.clone())?; | ||
} | ||
} | ||
} | ||
} | ||
|
||
Ok(()) | ||
} |