Struct lol_html::html_content::TextChunk
source · pub struct TextChunk<'i> { /* private fields */ }
Expand description
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 the whole text node
content where desired. The last chunk in a text node can be determined by calling
last_in_text_node
method of the chunk.
Note that in the sequence "<span>red-<b>or</b>-blue</span>"
the span
element contains three text
nodes: "red-"
, "or"
, and "-blue"
. Each of these can produce multiple text chunks and each will
produce one text chunk where last_in_text_node
returns true
. The last chunk in a text
node can have empty textual content. To perform an action once on the text contents of an
element, see Element::on_end_tag
.
Example
use lol_html::{HtmlRewriter, Settings, text};
let mut greeting = String::new();
{
let mut rewriter = HtmlRewriter::new(
Settings {
element_content_handlers: vec![
text!("div", |t| {
greeting += t.as_str();
if t.last_in_text_node() {
greeting += "!";
}
Ok(())
})
],
..Settings::default()
},
|_:&[u8]| {}
);
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!");
Implementations§
source§impl<'i> TextChunk<'i>
impl<'i> TextChunk<'i>
sourcepub fn as_mut_str(&mut self) -> &mut String
pub fn as_mut_str(&mut self) -> &mut String
Returns the textual content of the chunk that the caller can modify. Note that this can cause the string to be allocated.
sourcepub fn text_type(&self) -> TextType
pub fn text_type(&self) -> TextType
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();
sourcepub fn last_in_text_node(&self) -> bool
pub fn last_in_text_node(&self) -> bool
Returns true
if the chunk is last in a HTML text node.
Note that last chunk can have empty textual content.
sourcepub fn before(&mut self, content: &str, content_type: ContentType)
pub fn before(&mut self, content: &str, content_type: ContentType)
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>"#);
sourcepub fn after(&mut self, content: &str, content_type: ContentType)
pub fn after(&mut self, content: &str, content_type: ContentType)
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>"#);
sourcepub fn replace(&mut self, content: &str, content_type: ContentType)
pub fn replace(&mut self, content: &str, content_type: ContentType)
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>"#);