[−][src]Struct lol_html::html_content::TextChunk
An HTML text node chunk.
Since the rewriter operates on a streaming input with minimal internal buffering, HTML text node can be represented by multiple text chunks. The size of a chunk depends on multiple parameters, such as decoding buffer size and input chunk size.
It is up to a user of the rewriter to buffer content of chunks to get whole text node content
where desired. Last chunk in a text node can be determined by calling last_in_text_node
method of the chunk.
Note that the last chunk in a text node can have empty textual content.
Example
use lol_html::{HtmlRewriter, Settings, text}; let mut greeting = String::new(); { let mut rewriter = HtmlRewriter::try_new( Settings { element_content_handlers: vec![ text!("div", |t| { greeting += t.as_str(); if t.last_in_text_node() { greeting += "!"; } Ok(()) }) ], ..Settings::default() }, |_:&[u8]| {} ).unwrap(); rewriter.write(b"<div>He").unwrap(); rewriter.write(b"llo w").unwrap(); rewriter.write(b"orld</div>").unwrap(); rewriter.end().unwrap(); } assert_eq!(greeting, "Hello world!");
Methods
impl<'i> TextChunk<'i>
[src]
pub fn as_str(&self) -> &str
[src]
Returns the textual content of the chunk.
pub fn text_type(&self) -> TextType
[src]
Returns the type of the text in the chunk.
The type of the text depends on the surrounding context of the text. E.g. regular visible
text and text inside a <script>
element will have different types. Refer to TextType
for more information about possible text types.
Example
use lol_html::{rewrite_str, text, RewriteStrSettings}; use lol_html::html_content::TextType; let html = rewrite_str( r#"<div>Hello</div><script>"use strict";</script>"#, RewriteStrSettings { element_content_handlers: vec![ text!("div", |t| { assert_eq!(t.text_type(), TextType::Data); Ok(()) }), text!("script", |t| { assert_eq!(t.text_type(), TextType::ScriptData); Ok(()) }) ], ..RewriteStrSettings::default() } ).unwrap();
pub fn last_in_text_node(&self) -> bool
[src]
Returns true
if the chunk is last in a HTML text node.
Note that last chunk can have empty textual content.
pub fn before(&mut self, content: &str, content_type: ContentType)
[src]
Inserts content
before the text chunk.
Consequent calls to the method append content
to the previously inserted content.
Example
use lol_html::{rewrite_str, text, RewriteStrSettings}; use lol_html::html_content::ContentType; let html = rewrite_str( r#"<div>world</div>"#, RewriteStrSettings { element_content_handlers: vec![ text!("div", |t| { if !t.last_in_text_node(){ t.before("<!-- 42 -->", ContentType::Html); t.before("Hello ", ContentType::Text); } Ok(()) }) ], ..RewriteStrSettings::default() } ).unwrap(); assert_eq!(html, r#"<div><!-- 42 -->Hello world</div>"#);
pub fn after(&mut self, content: &str, content_type: ContentType)
[src]
Inserts content
after the text chunk.
Consequent calls to the method prepend content
to the previously inserted content.
Example
use lol_html::{rewrite_str, text, RewriteStrSettings}; use lol_html::html_content::ContentType; let html = rewrite_str( r#"<div>Foo</div>"#, RewriteStrSettings { element_content_handlers: vec![ text!("div", |t| { if t.last_in_text_node(){ t.after("Bar", ContentType::Text); t.after("Qux", ContentType::Text); } Ok(()) }) ], ..RewriteStrSettings::default() } ).unwrap(); assert_eq!(html, r#"<div>FooQuxBar</div>"#);
pub fn replace(&mut self, content: &str, content_type: ContentType)
[src]
Replaces the text chunk with the content
.
Consequent calls to the method overwrite previous replacement content.
Example
use lol_html::{rewrite_str, text, RewriteStrSettings}; use lol_html::html_content::ContentType; let html = rewrite_str( r#"<div>Foo</div>"#, RewriteStrSettings { element_content_handlers: vec![ text!("div", |t| { if !t.last_in_text_node(){ t.replace("Bar", ContentType::Text); t.replace("Qux", ContentType::Text); } Ok(()) }) ], ..RewriteStrSettings::default() } ).unwrap(); assert_eq!(html, r#"<div>Qux</div>"#);
pub fn remove(&mut self)
[src]
Removes the text chunk.
pub fn removed(&self) -> bool
[src]
Returns true
if the text chunk has been replaced or removed.
Trait Implementations
impl<'_> Debug for TextChunk<'_>
[src]
impl<'_> UserData for TextChunk<'_>
[src]
fn user_data(&self) -> &dyn Any
[src]
fn user_data_mut(&mut self) -> &mut dyn Any
[src]
fn set_user_data(&mut self, data: impl Any)
[src]
Auto Trait Implementations
impl<'i> !RefUnwindSafe for TextChunk<'i>
impl<'i> !Send for TextChunk<'i>
impl<'i> !Sync for TextChunk<'i>
impl<'i> Unpin for TextChunk<'i>
impl<'i> !UnwindSafe for TextChunk<'i>
Blanket Implementations
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> From<T> for T
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,