pub struct HTMLTag<'a> { /* private fields */ }
Expand description
Represents a single HTML element
Implementations§
source§impl<'a> HTMLTag<'a>
impl<'a> HTMLTag<'a>
sourcepub fn children(&self) -> Children<'a, '_>
pub fn children(&self) -> Children<'a, '_>
Returns a wrapper around the children of this HTML tag
sourcepub fn children_mut(&mut self) -> ChildrenMut<'a, '_>
pub fn children_mut(&mut self) -> ChildrenMut<'a, '_>
Returns a mutable wrapper around the children of this HTML tag.
sourcepub fn name_mut(&mut self) -> &mut Bytes<'a>
pub fn name_mut(&mut self) -> &mut Bytes<'a>
Returns a mutable reference to the name of this HTML tag
sourcepub fn attributes(&self) -> &Attributes<'a>
pub fn attributes(&self) -> &Attributes<'a>
Returns attributes of this HTML tag
sourcepub fn attributes_mut(&mut self) -> &mut Attributes<'a>
pub fn attributes_mut(&mut self) -> &mut Attributes<'a>
Returns a mutable reference to the attributes of this HTML tag
sourcepub fn outer_html<'p>(&'p self, parser: &'p Parser<'a>) -> String
pub fn outer_html<'p>(&'p self, parser: &'p Parser<'a>) -> String
Returns the contained markup
§Limitations
- The order of tag attributes is not guaranteed
- Spaces within the tag are not preserved (i.e.
<img src="">
may become<img src="">
)
Equivalent to Element#outerHTML in browsers.
sourcepub fn inner_html<'p>(&'p self, parser: &'p Parser<'a>) -> String
pub fn inner_html<'p>(&'p self, parser: &'p Parser<'a>) -> String
Returns the contained markup
§Limitations
- The order of tag attributes is not guaranteed
- Spaces within the tag are not preserved (i.e.
<img src="">
may become<img src="">
)
Equivalent to Element#innerHTML in browsers.
sourcepub fn raw(&self) -> &Bytes<'a>
pub fn raw(&self) -> &Bytes<'a>
Returns the raw HTML of this tag.
This is a cheaper version of HTMLTag::inner_html
if you never mutate any nodes.
Note: Mutating this tag does not re-compute the HTML representation of this tag. This simply returns a reference to the substring.
sourcepub fn boundaries(&self, parser: &Parser<'a>) -> (usize, usize)
pub fn boundaries(&self, parser: &Parser<'a>) -> (usize, usize)
Returns the boundaries/position (start, end)
of this HTML tag in the source string.
§Example
let source = "<p><span>hello</span></p>";
let dom = tl::parse(source, Default::default()).unwrap();
let parser = dom.parser();
let span = dom.nodes().iter().filter_map(|n| n.as_tag()).find(|n| n.name() == "span").unwrap();
let (start, end) = span.boundaries(parser);
assert_eq!((start, end), (3, 20));
assert_eq!(&source[start..=end], "<span>hello</span>");
sourcepub fn inner_text<'p>(&self, parser: &'p Parser<'a>) -> Cow<'p, str>
pub fn inner_text<'p>(&self, parser: &'p Parser<'a>) -> Cow<'p, str>
Returns the contained text of this element, excluding any markup. Equivalent to Element#innerText in browsers. This function may not allocate memory for a new string as it can just return the part of the tag that doesn’t have markup. For tags that do have more than one subnode, this will allocate memory
sourcepub fn query_selector<'b>(
&'b self,
parser: &'b Parser<'a>,
selector: &'b str
) -> Option<QuerySelectorIterator<'a, 'b, Self>>
pub fn query_selector<'b>( &'b self, parser: &'b Parser<'a>, selector: &'b str ) -> Option<QuerySelectorIterator<'a, 'b, Self>>
Tries to parse the query selector and returns an iterator over elements that match the given query selector.
§Example
let dom = tl::parse(r#"
<div class="x">
<div class="y">
<div class="z">MATCH</div>
<div class="z">MATCH</div>
<div class="z">MATCH</div>
</div>
</div>
<div class="z">NO MATCH</div>
<div class="z">NO MATCH</div>
<div class="z">NO MATCH</div>
"#, Default::default()).unwrap();
let parser = dom.parser();
let outer = dom
.get_elements_by_class_name("y")
.next()
.unwrap()
.get(parser)
.unwrap()
.as_tag()
.unwrap();
let inner_z = outer.query_selector(parser, ".z").unwrap();
assert_eq!(inner_z.clone().count(), 3);
for handle in inner_z {
let node = handle.get(parser).unwrap().as_tag().unwrap();
assert_eq!(node.inner_text(parser), "MATCH");
}