pub enum Node {
Document,
Fragment,
Doctype(Doctype),
Comment(Comment),
Text(Text),
Element(Element),
ProcessingInstruction(ProcessingInstruction),
}Expand description
An HTML node.
Variants§
Document
The document root.
Fragment
The fragment root.
Doctype(Doctype)
A doctype.
Comment(Comment)
A comment.
Text(Text)
Text.
Element(Element)
An element.
ProcessingInstruction(ProcessingInstruction)
A processing instruction.
Implementations§
source§impl Node
impl Node
sourcepub fn is_document(&self) -> bool
pub fn is_document(&self) -> bool
Returns true if node is the document root.
sourcepub fn is_fragment(&self) -> bool
pub fn is_fragment(&self) -> bool
Returns true if node is the fragment root.
sourcepub fn is_doctype(&self) -> bool
pub fn is_doctype(&self) -> bool
Returns true if node is a doctype.
sourcepub fn is_comment(&self) -> bool
pub fn is_comment(&self) -> bool
Returns true if node is a comment.
sourcepub fn is_text(&self) -> bool
pub fn is_text(&self) -> bool
Returns true if node is text.
Examples found in repository?
More examples
src/html/tree_sink.rs (line 111)
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
fn append(&mut self, parent: &Self::Handle, child: NodeOrText<Self::Handle>) {
let mut parent = self.tree.get_mut(*parent).unwrap();
match child {
NodeOrText::AppendNode(id) => {
parent.append_id(id);
}
NodeOrText::AppendText(text) => {
let can_concat = parent
.last_child()
.map_or(false, |mut n| n.value().is_text());
if can_concat {
let mut last_child = parent.last_child().unwrap();
match *last_child.value() {
Node::Text(ref mut t) => t.text.push_tendril(&text),
_ => unreachable!(),
}
} else {
parent.append(Node::Text(Text { text }));
}
}
}
}
// Append a node as the sibling immediately before the given node. If that node has no parent,
// do nothing and return Err(new_node).
//
// The tree builder promises that sibling is not a text node. However its old previous sibling,
// which would become the new node's previous sibling, could be a text node. If the new node is
// also a text node, the two should be merged, as in the behavior of append.
//
// NB: new_node may have an old parent, from which it should be removed.
fn append_before_sibling(
&mut self,
sibling: &Self::Handle,
new_node: NodeOrText<Self::Handle>,
) {
if let NodeOrText::AppendNode(id) = new_node {
self.tree.get_mut(id).unwrap().detach();
}
let mut sibling = self.tree.get_mut(*sibling).unwrap();
if sibling.parent().is_some() {
match new_node {
NodeOrText::AppendNode(id) => {
sibling.insert_id_before(id);
}
NodeOrText::AppendText(text) => {
let can_concat = sibling
.prev_sibling()
.map_or(false, |mut n| n.value().is_text());
if can_concat {
let mut prev_sibling = sibling.prev_sibling().unwrap();
match *prev_sibling.value() {
Node::Text(ref mut t) => t.text.push_tendril(&text),
_ => unreachable!(),
}
} else {
sibling.insert_before(Node::Text(Text { text }));
}
}
}
}
}sourcepub fn is_element(&self) -> bool
pub fn is_element(&self) -> bool
Returns true if node is an element.
Examples found in repository?
src/element_ref/element.rs (line 51)
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
fn prev_sibling_element(&self) -> Option<Self> {
self.prev_siblings()
.find(|sibling| sibling.value().is_element())
.map(ElementRef::new)
}
fn next_sibling_element(&self) -> Option<Self> {
self.next_siblings()
.find(|sibling| sibling.value().is_element())
.map(ElementRef::new)
}
fn is_html_element_in_html_document(&self) -> bool {
// FIXME: Is there more to this?
self.value().name.ns == ns!(html)
}
fn has_local_name(&self, name: &LocalName) -> bool {
&self.value().name.local == name
}
fn has_namespace(&self, namespace: &Namespace) -> bool {
&self.value().name.ns == namespace
}
fn attr_matches(
&self,
ns: &NamespaceConstraint<&Namespace>,
local_name: &LocalName,
operation: &AttrSelectorOperation<&String>,
) -> bool {
self.value().attrs.iter().any(|(key, value)| {
!matches!(*ns, NamespaceConstraint::Specific(url) if *url != key.ns)
&& *local_name == key.local
&& operation.eval_str(value)
})
}
fn match_non_ts_pseudo_class<F>(
&self,
_pc: &NonTSPseudoClass,
_context: &mut matching::MatchingContext<Self::Impl>,
_flags_setter: &mut F,
) -> bool {
false
}
fn match_pseudo_element(
&self,
_pe: &PseudoElement,
_context: &mut matching::MatchingContext<Self::Impl>,
) -> bool {
false
}
fn is_link(&self) -> bool {
self.value().name() == "link"
}
fn is_html_slot_element(&self) -> bool {
true
}
fn has_id(&self, id: &LocalName, case_sensitivity: CaseSensitivity) -> bool {
match self.value().id {
Some(ref val) => case_sensitivity.eq(id.as_bytes(), val.as_bytes()),
None => false,
}
}
fn has_class(&self, name: &LocalName, case_sensitivity: CaseSensitivity) -> bool {
self.value().has_class(name, case_sensitivity)
}
fn is_empty(&self) -> bool {
!self
.children()
.any(|child| child.value().is_element() || child.value().is_text())
}More examples
sourcepub fn as_doctype(&self) -> Option<&Doctype>
pub fn as_doctype(&self) -> Option<&Doctype>
Returns self as a doctype.
sourcepub fn as_comment(&self) -> Option<&Comment>
pub fn as_comment(&self) -> Option<&Comment>
Returns self as a comment.
sourcepub fn as_element(&self) -> Option<&Element>
pub fn as_element(&self) -> Option<&Element>
Returns self as an element.
Examples found in repository?
More examples
src/node/serializable.rs (line 44)
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
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(())
}sourcepub fn as_processing_instruction(&self) -> Option<&ProcessingInstruction>
pub fn as_processing_instruction(&self) -> Option<&ProcessingInstruction>
Returns self as an element.