Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Element.getTextRecursively returns empty string #3668

Open
mvysny opened this issue Mar 8, 2018 · 3 comments
Open

Element.getTextRecursively returns empty string #3668

mvysny opened this issue Mar 8, 2018 · 3 comments

Comments

@mvysny
Copy link
Member

mvysny commented Mar 8, 2018

Calling getTextRecursively() on an Element always returns an empty string. That is to be expected since it uses getChildren() for recursive traversal, however that method only returns Elements, so a Text node is never found and thus no text is discovered.

@mvysny
Copy link
Member Author

mvysny commented Mar 8, 2018

Perhaps I'm wrong and Element can in fact be a Textual node. Regardless, I have an Element whose toString() says <template class="header"> Name </template> (it's basically Grid's header made sortable), but calling getTextRecursively() on this element returns an empty string.

@mvysny
Copy link
Member Author

mvysny commented Mar 8, 2018

Workaround: This Kotlin code works properly.

val Element.textRecursively2: String get() {
    val node = ElementUtil.toJsoup(Document(""), this)
    return node.textRecursively
}

val Node.textRecursively: String get() = when (this) {
    is TextNode -> this.text()
    else -> childNodes().joinToString(separator = "", transform = { it.textRecursively })
}

@Legioth
Copy link
Member

Legioth commented Mar 9, 2018

We have a bunch of tests in com.vaadin.flow.dom.ElementTest that assert that getTextRecursively() returns various non-empty values.

The reason in your case is most likely that the <template> element is configured with an innerHTML property instead of having a proper text node child. <template> elements work in a special way in the browser, so regular child elements are ignored. We must instead pass the contents as innerHTML to make it work.

@pleku pleku added this to the 1.0 Maintenance milestone Mar 25, 2018
@pleku pleku removed this from the 1.0 Maintenance milestone Apr 24, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: 🔎 Investigation
Development

No branches or pull requests

3 participants