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

update dom elements #2

Closed
cj opened this issue Nov 18, 2014 · 4 comments
Closed

update dom elements #2

cj opened this issue Nov 18, 2014 · 4 comments
Labels

Comments

@cj
Copy link

cj commented Nov 18, 2014

Is it possible to update the dom elements?

@philss
Copy link
Owner

philss commented Nov 19, 2014

@cj do you mean transform the HTML and returning the modified version as string?
If so, this version can't do that. But it's an interesting idea.

Can you show an example of usage?

@philss philss closed this as completed Jan 1, 2015
@brweber2
Copy link

    html = """
        <div>
            <span>hello</span>
        </div>
    """
    Floki.parse(html)
    |> Floki.transform("span", fn element -> {"div", [], ["goodbye"]} end )
    #
    # <div>
    #     <div>goodbye</div>
    # </div>
    # 

Rather than just find the elements that match and return them as a list, it would use the original parsed HTML data structure as the accumulator and apply the callback function to replace each match.
The implementation would be very similar to find only it would have an additional argument (the callback function) and the accumulator would be the entire HTML document and not just the list of matches.
Is something like this possible today?

@philss
Copy link
Owner

philss commented Aug 3, 2016

@brweber2 Thanks for the proposal.

Yes, this is possible. I think for this feature we need something more flexible, like a "protocol" for saying that we want to "add" attributes, or change the tag name (type), or "push" a new child into the tree.

Something like:

Floki.transform(html_tree, "span", fn element -> {:add_child, "a text"} end)
# or
Floki.transform(html_tree, "span", fn element -> {:replace_type, "div"} end)
# or
Floki.transform(html_tree, "span", fn element -> {:add_attribute, {"class", "farawell"}} end)

What do you think?

@philss philss reopened this Aug 3, 2016
@philss
Copy link
Owner

philss commented Jan 10, 2017

Since there was not much discussion here I'm closing this issue.
Please open another issue to keep the discussion if needed. Thanks!

@philss philss closed this as completed Jan 10, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants