Skip to content

last_in_text_node returns true at multiple positions in an element #93

@jongiddy

Description

@jongiddy

This code attempts to replace the last text node in a span:

fn main() {
    use lol_html::html_content::ContentType;
    use lol_html::{rewrite_str, text, RewriteStrSettings};

    let html = rewrite_str(
        r#"<span>Hello<b>hi</b></span>"#,
        RewriteStrSettings {
            element_content_handlers: vec![text!("span", |t| {
                if t.last_in_text_node() {
                    t.replace(" world", ContentType::Text);
                }
                Ok(())
            })],
            ..RewriteStrSettings::default()
        },
    )
    .unwrap();

    assert_eq!(html, r#"<span>Hello world<b>hi world</b></span>"#);
}

Since there's only one span in the input, I'd expected this code to replace text with " world" once, probably just before the </span> tag, but either of the two actual replacement points would also seem reasonable.

Replacing at two positions seems strange. Is this expected behavior? If yes, what determines if a node is last_in_text_node()? And is there a way to distinguish these two positions (maybe using a span > * rule)?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions