Skip to content

Commit ba7e78e

Browse files
author
Richard Dodd
committed
More documentation
1 parent 8f29ec6 commit ba7e78e

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

markup5ever/rcdom.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ pub struct Node {
8484
}
8585

8686
impl Node {
87+
/// Create a new node from its contents
8788
fn new(data: NodeData) -> Rc<Self> {
8889
Rc::new(Node {
8990
data: data,
@@ -99,12 +100,15 @@ pub type Handle = Rc<Node>;
99100
/// Weak reference to a DOM node, used for parent pointers.
100101
pub type WeakHandle = Weak<Node>;
101102

103+
/// Append a parentless node to another nodes' children
102104
fn append(new_parent: &Handle, child: Handle) {
103105
let previous_parent = child.parent.replace(Some(Rc::downgrade(new_parent)));
106+
// Invariant: child cannot have existing parent
104107
assert!(previous_parent.is_none());
105108
new_parent.children.borrow_mut().push(child);
106109
}
107110

111+
/// If the node has a parent, get it and this node's position in its children
108112
fn get_parent_and_index(target: &Handle) -> Option<(Handle, usize)> {
109113
if let Some(weak) = target.parent.take() {
110114
let parent = weak.upgrade().expect("dangling weak pointer");

markup5ever/serialize.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,35 +6,62 @@
66
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
77
// option. This file may not be copied, modified, or distributed
88
// except according to those terms.
9+
//! Traits for serializing elements. The serializer expects the data to be xml-like (with a name,
10+
//! and optional children, attrs, text, comments, doctypes, and [processing instructions]). It uses
11+
//! the visitor pattern, where the serializer and the serializable objects are decoupled and
12+
//! implement their own traits.
13+
//!
14+
//! [processing instructions]: https://en.wikipedia.org/wiki/Processing_Instruction
915
1016
use QualName;
1117
use std::io;
1218

1319
//§ serializing-html-fragments
20+
/// Used as a parameter to `serialize`, telling it if we want to skip the parent.
21+
/// TODO check: Currently the parameter to `ChildrenOnly` is unused.
1422
#[derive(Clone, PartialEq)]
1523
pub enum TraversalScope {
24+
/// Include the parent node when serializing.
1625
IncludeNode,
26+
/// Only serialize the children of the node.
1727
ChildrenOnly(Option<QualName>)
1828
}
1929

30+
/// Types that can be serialized (according to the xml-like scheme in `Serializer`) implement this
31+
/// trait.
2032
pub trait Serialize {
33+
/// Take the serializer and call its methods to serialize this type. The type will dictate
34+
/// which methods are called and with what parameters.
2135
fn serialize<S>(&self, serializer: &mut S, traversal_scope: TraversalScope) -> io::Result<()>
2236
where S: Serializer;
2337
}
2438

39+
/// Types that are capable of serializing implement this trait
2540
pub trait Serializer {
41+
/// Serialize the start of an element, for example `<div class="test">`.
2642
fn start_elem<'a, AttrIter>(&mut self, name: QualName, attrs: AttrIter) -> io::Result<()>
2743
where AttrIter: Iterator<Item=AttrRef<'a>>;
2844

45+
/// Serialize the end of an element, for example `</div>`.
2946
fn end_elem(&mut self, name: QualName) -> io::Result<()>;
3047

48+
/// Serialize a plain text node.
3149
fn write_text(&mut self, text: &str) -> io::Result<()>;
3250

51+
/// Serialize a comment node, for example `<!-- comment -->`.
3352
fn write_comment(&mut self, text: &str) -> io::Result<()>;
3453

54+
/// Serialize a doctype node, for example `<!doctype html>`.
3555
fn write_doctype(&mut self, name: &str) -> io::Result<()>;
3656

57+
/// Serialize a processing instruction node, for example
58+
/// `<?xml-stylesheet type="text/xsl" href="style.xsl"?>`.
3759
fn write_processing_instruction(&mut self, target: &str, data: &str) -> io::Result<()>;
3860
}
3961

62+
/// A type alias for an attribute name and value (e.g. the `class="test"` in `<div class="test">`
63+
/// is represented as `(<QualName of type class>, test).
64+
///
65+
/// This is used in [`Serializer::start_elem`] where the value being serialized must supply an
66+
/// iterator over the attributes for the current element
4067
pub type AttrRef<'a> = (&'a QualName, &'a str);

0 commit comments

Comments
 (0)