lol-html icon indicating copy to clipboard operation
lol-html copied to clipboard

Question about mixing text and element handlers

Open kkszysiu opened this issue 4 years ago • 0 comments

Hey guys,

I have a case, where I need to put HTML <div> element after or before <p> element, based on how much words occured in previous <p> elements.

I am now trying to handle it this way:


    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 <p>. 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!

kkszysiu avatar Aug 17 '20 10:08 kkszysiu