Struct tl::HTMLTag

source ·
pub struct HTMLTag<'a> { /* private fields */ }
Expand description

Represents a single HTML element

Implementations§

source§

impl<'a> HTMLTag<'a>

source

pub fn children(&self) -> Children<'a, '_>

Returns a wrapper around the children of this HTML tag

source

pub fn children_mut(&mut self) -> ChildrenMut<'a, '_>

Returns a mutable wrapper around the children of this HTML tag.

source

pub fn name(&self) -> &Bytes<'a>

Returns the name of this HTML tag

source

pub fn name_mut(&mut self) -> &mut Bytes<'a>

Returns a mutable reference to the name of this HTML tag

source

pub fn attributes(&self) -> &Attributes<'a>

Returns attributes of this HTML tag

source

pub fn attributes_mut(&mut self) -> &mut Attributes<'a>

Returns a mutable reference to the attributes of this HTML tag

source

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.

source

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.

source

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.

source

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>");
source

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

source

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");
}
source

pub fn find_node<F>(&self, parser: &Parser<'a>, f: &mut F) -> Option<NodeHandle>
where F: FnMut(&Node<'a>) -> bool,

Calls the given closure with each tag as parameter

The closure must return a boolean, indicating whether it should stop iterating Returning true will break the loop

Trait Implementations§

source§

impl<'a> Clone for HTMLTag<'a>

source§

fn clone(&self) -> HTMLTag<'a>

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl<'a> Debug for HTMLTag<'a>

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl<'a> QueryIterable<'a> for HTMLTag<'a>

source§

fn get<'b>( &'b self, parser: &'b Parser<'a>, index: usize ) -> Option<(&'b Node<'a>, NodeHandle)>

Gets a node at a specific index
source§

fn len(&self, parser: &Parser<'_>) -> usize

Gets or computes the length (number of nodes)
source§

fn start(&self) -> Option<InnerNodeHandle>

Gets the starting index

Auto Trait Implementations§

§

impl<'a> RefUnwindSafe for HTMLTag<'a>

§

impl<'a> !Send for HTMLTag<'a>

§

impl<'a> !Sync for HTMLTag<'a>

§

impl<'a> Unpin for HTMLTag<'a>

§

impl<'a> UnwindSafe for HTMLTag<'a>

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.