-
-
Notifications
You must be signed in to change notification settings - Fork 156
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
traverse_and_update is not applied to text nodes #338
Comments
maybe as one additional comment: I'm aware that others have exactly the opposite needs (#75) and that I can get ok results automatically when using |
@ppatrzyk thanks for opening the issue! 💜 I don't think this is a bug. I would say it was a decision from the time we added the feature, since you could update text nodes when capturing the HTML tags (there is an example below). Unfortunately this won't work for nodes that are in the root of the tree :/ The addition of this feature would be a breaking change, so I'm going to think about it. For your case, you could do this: Floki.traverse_and_update(doc, fn
{tag, attrs, children} ->
{tag, attrs,
Enum.reject(children, fn child ->
is_binary(child) && Regex.replace(~r/\n|[[:space:]]/, child, "") == ""
end)}
other ->
other
end) WDYT? |
@philss thanks for your reply! I'll adapt my code such that it works, thanks for your suggestion. At minimum what would be helpful is mentioning this behavior in the documentation which currently reads:
(emphasis mine) |
Description
When using
traverse_and_update
, specified function is not applied to text nodes, these are passed as is:floki/lib/floki/traversal.ex
Line 12 in 484565c
To Reproduce
My setup:
config.exs:
Expected behavior
I'd like to remove all empty text nodes from given document, i.e. to get the following:
Is the current behavior a bug or is there some reason why it was designed like that? Alternatively, is there a better way to achieve what I want here? Thank you for any suggestions!
The text was updated successfully, but these errors were encountered: