[][src]Struct lol_html::html_content::Element

pub struct Element<'r, 't> { /* fields omitted */ }

An HTML element rewritable unit.

Exposes API for examination and modification of a parsed HTML element.

Methods

impl<'r, 't> Element<'r, 't>[src]

pub fn tag_name(&self) -> String[src]

Returns the tag name of the element.

pub fn set_tag_name(&mut self, name: &str) -> Result<(), TagNameError>[src]

Sets the tag name of the element.

pub fn namespace_uri(&self) -> &'static str[src]

Returns the namespace URI of the element.

pub fn attributes(&self) -> &[Attribute<'t>][src]

Returns an immutable collection of element's attributes.

pub fn get_attribute(&self, name: &str) -> Option<String>[src]

Returns the value of an attribute with the name.

Returns None if the element doesn't have an attribute with the name.

pub fn has_attribute(&self, name: &str) -> bool[src]

Returns true if the element has an attribute with name.

pub fn set_attribute(
    &mut self,
    name: &str,
    value: &str
) -> Result<(), AttributeNameError>
[src]

Sets value of element's attribute with name.

If element doesn't have an attribute with the name, method adds new attribute to the element with name and value.

pub fn remove_attribute(&mut self, name: &str)[src]

Removes an attribute with the name if it is present.

pub fn before(&mut self, content: &str, content_type: ContentType)[src]

Inserts content before the element.

Consequent calls to the method append content to the previously inserted content.

Example

use lol_html::{rewrite_str, element, RewriteStrSettings};
use lol_html::html_content::ContentType;

let html = rewrite_str(
    r#"<div id="foo"></div>"#,
    RewriteStrSettings {
        element_content_handlers: vec![
            element!("#foo", |el| {
                el.before("<bar>", ContentType::Html);
                el.before("<qux>", ContentType::Html);
                el.before("<quz>", ContentType::Text);

                Ok(())
            })
        ],
        ..RewriteStrSettings::default()
    }
).unwrap();

assert_eq!(html, r#"<bar><qux>&lt;quz&gt;<div id="foo"></div>"#);

pub fn after(&mut self, content: &str, content_type: ContentType)[src]

Inserts content after the element.

Consequent calls to the method prepend content to the previously inserted content.

Example

use lol_html::{rewrite_str, element, RewriteStrSettings};
use lol_html::html_content::ContentType;

let html = rewrite_str(
    r#"<div id="foo"></div>"#,
    RewriteStrSettings {
        element_content_handlers: vec![
            element!("#foo", |el| {
                el.after("<bar>", ContentType::Html);
                el.after("<qux>", ContentType::Html);
                el.after("<quz>", ContentType::Text);

                Ok(())
            })
        ],
        ..RewriteStrSettings::default()
    }
).unwrap();

assert_eq!(html, r#"<div id="foo"></div>&lt;quz&gt;<qux><bar>"#);

pub fn prepend(&mut self, content: &str, content_type: ContentType)[src]

Prepends content to the element's inner content, i.e. inserts content right after the element's start tag.

Consequent calls to the method prepend content to the previously inserted content. A call to the method doesn't make any effect if the element is an empty element.

Example

use lol_html::{rewrite_str, element, RewriteStrSettings};
use lol_html::html_content::{ContentType, Element};

let handler = |el: &mut Element| {
    el.prepend("<bar>", ContentType::Html);
    el.prepend("<qux>", ContentType::Html);
    el.prepend("<quz>", ContentType::Text);

    Ok(())
};

let html = rewrite_str(
    r#"<div id="foo"><!-- content --></div><img>"#,
    RewriteStrSettings {
        element_content_handlers: vec![
            element!("#foo", handler),
            element!("img", handler),
        ],
        ..RewriteStrSettings::default()
    }
).unwrap();

assert_eq!(html, r#"<div id="foo">&lt;quz&gt;<qux><bar><!-- content --></div><img>"#);

pub fn append(&mut self, content: &str, content_type: ContentType)[src]

Appends content to the element's inner content, i.e. inserts content right before the element's end tag.

Consequent calls to the method append content to the previously inserted content. A call to the method doesn't make any effect if the element is an empty element.

Example

use lol_html::{rewrite_str, element, RewriteStrSettings};
use lol_html::html_content::{ContentType, Element};

let handler = |el: &mut Element| {
    el.append("<bar>", ContentType::Html);
    el.append("<qux>", ContentType::Html);
    el.append("<quz>", ContentType::Text);

    Ok(())
};

let html = rewrite_str(
    r#"<div id="foo"><!-- content --></div><img>"#,
    RewriteStrSettings {
        element_content_handlers: vec![
            element!("#foo", handler),
            element!("img", handler),
        ],
        ..RewriteStrSettings::default()
    }
).unwrap();

assert_eq!(html, r#"<div id="foo"><!-- content --><bar><qux>&lt;quz&gt;</div><img>"#);

pub fn set_inner_content(&mut self, content: &str, content_type: ContentType)[src]

Replaces inner content of the element with content.

Consequent calls to the method overwrite previously inserted content. A call to the method doesn't make any effect if the element is an empty element.

Example

use lol_html::{rewrite_str, element, RewriteStrSettings};
use lol_html::html_content::{ContentType, Element};

let handler = |el: &mut Element| {
    el.append("<!-- only one -->", ContentType::Html);
    el.set_inner_content("<!-- will -->", ContentType::Html);
    el.set_inner_content("<!-- survive -->", ContentType::Html);

    Ok(())
};

let html = rewrite_str(
    r#"<div id="foo"><!-- content --></div><img>"#,
    RewriteStrSettings {
        element_content_handlers: vec![
            element!("#foo", handler),
            element!("img", handler),
        ],
        ..RewriteStrSettings::default()
    }
).unwrap();

assert_eq!(html, r#"<div id="foo"><!-- survive --></div><img>"#);

pub fn replace(&mut self, content: &str, content_type: ContentType)[src]

Replaces the element and its inner content with content.

Consequent calls to the method overwrite previously inserted content.

Example

use lol_html::{rewrite_str, element, RewriteStrSettings};
use lol_html::html_content::ContentType;

let html = rewrite_str(
    r#"<div id="foo"></div>"#,
    RewriteStrSettings {
        element_content_handlers: vec![
            element!("#foo", |el| {
                el.replace("<span></span>", ContentType::Html);
                el.replace("Hello", ContentType::Text);

                Ok(())
            })
        ],
        ..RewriteStrSettings::default()
    }
).unwrap();

assert_eq!(html, r#"Hello"#);

pub fn remove(&mut self)[src]

Removes the element and its inner content.

pub fn remove_and_keep_content(&mut self)[src]

Removes the element, but keeps its content. I.e. remove start and end tags of the element.

Example

use lol_html::{rewrite_str, element, RewriteStrSettings};

let html = rewrite_str(
    r#"<div><span><!-- 42 --></span></div>"#,
    RewriteStrSettings {
        element_content_handlers: vec![
            element!("div", |el| {
                el.remove_and_keep_content();

                Ok(())
            })
        ],
        ..RewriteStrSettings::default()
    }
).unwrap();

assert_eq!(html, r#"<span><!-- 42 --></span>"#);

pub fn removed(&self) -> bool[src]

Returns true if the element has been removed or replaced with some content.

Trait Implementations

impl<'_, '_> Debug for Element<'_, '_>[src]

impl<'_, '_> UserData for Element<'_, '_>[src]

Auto Trait Implementations

impl<'r, 't> !RefUnwindSafe for Element<'r, 't>

impl<'r, 't> !Send for Element<'r, 't>

impl<'r, 't> !Sync for Element<'r, 't>

impl<'r, 't> Unpin for Element<'r, 't> where
    't: 'r, 

impl<'r, 't> !UnwindSafe for Element<'r, 't>

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

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

The type returned in the event of a conversion error.