7
7
// option. This file may not be copied, modified, or distributed
8
8
// except according to those terms.
9
9
10
- /// Something which can be inserted into the DOM.
11
- ///
12
- /// Adjacent sibling text nodes are merged into a single node, so
13
- /// the sink may not want to allocate a `Handle` for each.
10
+ //! This module contains functionality for managing the DOM, including adding/removing nodes.
11
+ //!
12
+ //! It can be used by a parser to create the DOM graph structure in memory.
14
13
15
14
use std:: borrow:: Cow ;
16
15
use tendril:: StrTendril ;
@@ -28,11 +27,17 @@ pub enum NodeOrText<Handle> {
28
27
AppendText ( StrTendril ) ,
29
28
}
30
29
31
- /// A document's quirks mode.
30
+ /// A document's quirks mode, for compatibility with old browsers. See [quirks mode on wikipedia]
31
+ /// for more information.
32
+ ///
33
+ /// [quirks mode on wikipedia]: https://en.wikipedia.org/wiki/Quirks_mode
32
34
#[ derive( PartialEq , Eq , Copy , Clone , Hash , Debug ) ]
33
35
pub enum QuirksMode {
36
+ /// Full quirks mode
34
37
Quirks ,
38
+ /// Almost standards mode
35
39
LimitedQuirks ,
40
+ /// Standards mode
36
41
NoQuirks ,
37
42
}
38
43
@@ -47,23 +52,59 @@ pub enum NextParserState {
47
52
Continue ,
48
53
}
49
54
55
+ /// Special properties of an element, useful for tagging elements with this information.
50
56
#[ derive( Default ) ]
51
57
pub struct ElementFlags {
52
58
/// A document fragment should be created, associated with the element,
53
- /// and returned in TreeSink::get_template_contents
59
+ /// and returned in TreeSink::get_template_contents.
60
+ ///
61
+ /// See [template-contents in the whatwg spec][whatwg template-contents].
54
62
///
55
- /// https://html.spec.whatwg.org/multipage/#template-contents
63
+ /// [whatwg template-contents]: https://html.spec.whatwg.org/multipage/#template-contents
56
64
pub template : bool ,
57
65
58
66
/// This boolean should be recorded with the element and returned
59
67
/// in TreeSink::is_mathml_annotation_xml_integration_point
60
68
///
61
- /// https://html.spec.whatwg.org/multipage/#html-integration-point
69
+ /// See [html-integration-point in the whatwg spec][whatwg integration-point].
70
+ ///
71
+ /// [whatwg integration-point]: https://html.spec.whatwg.org/multipage/#html-integration-point
62
72
pub mathml_annotation_xml_integration_point : bool ,
63
73
74
+ // Prevent construction from outside module
64
75
_private : ( )
65
76
}
66
77
78
+ /// A constructor for an element.
79
+ ///
80
+ /// # Examples
81
+ ///
82
+ /// Create an element like `<div class="test-class-name"></div>`:
83
+ ///
84
+ /// ```
85
+ /// # #[macro_use] extern crate markup5ever;
86
+ ///
87
+ /// # fn main() {
88
+ /// use markup5ever::{rcdom, QualName, Attribute};
89
+ /// use markup5ever::interface::create_element;
90
+ ///
91
+ /// let mut dom = rcdom::RcDom::default();
92
+ /// let el = create_element(&mut dom,
93
+ /// // Namespaces and localnames use precomputed interned strings for
94
+ /// // speed. Use the macros ns! and local_name! to fetch them.
95
+ /// QualName::new(None, ns!(), local_name!("div")),
96
+ /// vec![
97
+ /// Attribute {
98
+ /// name: QualName::new(None, ns!(), local_name!("class")),
99
+ /// // In real scenarios, you would use a view onto an existing
100
+ /// // string if possible to avoid allocation. Tendrils have utilities
101
+ /// // for avoiding allocation & copying wherever possible.
102
+ /// value: String::from("test-class-name").into()
103
+ /// }
104
+ /// ]);
105
+ /// # }
106
+ ///
107
+ /// ```
67
108
pub fn create_element < Sink > ( sink : & mut Sink , name : QualName , attrs : Vec < Attribute > ) -> Sink :: Handle
68
109
where Sink : TreeSink {
69
110
let mut flags = ElementFlags :: default ( ) ;
@@ -84,6 +125,10 @@ where Sink: TreeSink {
84
125
sink. create_element ( name, attrs, flags)
85
126
}
86
127
128
+ /// Methods a parser can use to create the DOM. The DOM provider implements this trait.
129
+ ///
130
+ /// Having this as a trait potentially allows multiple implementations of the DOM to be used with
131
+ /// the same parser.
87
132
pub trait TreeSink {
88
133
/// `Handle` is a reference to a DOM node. The tree builder requires
89
134
/// that a `Handle` implements `Clone` to get another reference to
@@ -93,14 +138,14 @@ pub trait TreeSink {
93
138
/// The overall result of parsing.
94
139
///
95
140
/// This should default to Self, but default associated types are not stable yet.
96
- /// (https://github.com/rust-lang/rust/issues/29661)
141
+ /// [rust-lang/rust#29661] (https://github.com/rust-lang/rust/issues/29661)
97
142
type Output ;
98
143
99
144
/// Consume this sink and return the overall result of parsing.
100
145
///
101
146
/// TODO:This should default to `fn finish(self) -> Self::Output { self }`,
102
147
/// but default associated types are not stable yet.
103
- /// (https://github.com/rust-lang/rust/issues/29661)
148
+ /// [rust-lang/rust#29661] (https://github.com/rust-lang/rust/issues/29661)
104
149
fn finish ( self ) -> Self :: Output ;
105
150
106
151
/// Signal a parse error.
@@ -121,7 +166,9 @@ pub trait TreeSink {
121
166
/// an associated document fragment called the "template contents" should
122
167
/// also be created. Later calls to self.get_template_contents() with that
123
168
/// given element return it.
124
- /// https://html.spec.whatwg.org/multipage/#the-template-element
169
+ /// See [the template element in the whatwg spec][whatwg template].
170
+ ///
171
+ /// [whatwg template]: https://html.spec.whatwg.org/multipage/#the-template-element
125
172
fn create_element ( & mut self , name : QualName , attrs : Vec < Attribute > , flags : ElementFlags )
126
173
-> Self :: Handle ;
127
174
0 commit comments