cloudflare / cloudflare/lol-html

Question about mixing text and element handlers

Open
#63 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
Rust
Stars
2.1k
Forks
111
PR merge metrics
No merged PRs in 30d

Description

Hey guys,

I have a case, where I need to put HTML `

` element after or before `

` element, based on how much words occured in previous `

` elements.

I am now trying to handle it this way:
```rust

let mut words_count: usize = 0;
let mut previous_words_count: usize = 0;
let html = rewrite_str(
post.description.as_str(),
RewriteStrSettings {
element_content_handlers: vec![
text!("p", |t| {
previous_words_count = words_count.clone();
words_count += t.as_str().split(" ").count();

match html_elems
.iter()
.find(|x| previous_words_count < x.words_count && x.words_count <= words_count) {
None => (),
Some(x) => {
let platform_elem_string = x.by_platform(platform_str.as_str());
if !platform_elem_string.is_empty() {
t.after(platform_elem_string, ContentType::Html);
}
}
};

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

Sadly for `t.after` and `t.before` in given cases will insert these elements inside given element. I need to place them after or before `

`.
Usage of elements! macro here will be advised, but then I cannot count words, right?
Any idea how I can resolve it? Is it even possible at this moment?

Thanks!

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.