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

add Floki.find_by_id/2 #548

Merged
merged 1 commit into from
Mar 1, 2024
Merged

add Floki.find_by_id/2 #548

merged 1 commit into from
Mar 1, 2024

Conversation

SteffenDE
Copy link
Contributor

Related to the problem and discussion in phoenixframework/phoenix_live_view#3144.

In LiveViewTest we need to find elements by their id. Currently, we use Floki.find(html, "##{id}"), but this breaks as soon as there are special characters in the id. We could use Floki.find(html, %Floki.Selector{id: id}), but the Floki.Selector struct is not documented and therefore I'm not sure if it's something to rely on.

As document.getElementById is also a frequently used API in the browser, adding a find_by_id or similar function to Floki could be quite useful.

This PR adds a simple find_by_id function that internally uses the %Floki.Selector{id: id} struct.
As using find with a string is deprecated, I did not add this functionality to find_by_id.

@SteffenDE
Copy link
Contributor Author

Maybe we should also only return nil or the first element found? That would mirror the behavior of document.getElementById. Not sure what Floki does when ids are not unique. We could also raise if there are multiple results.

@ypconstante
Copy link
Contributor

I think returning the first element is the best option, in the future we could implement an optimization to stop the traversal on the first matching node, and has the same behaviour as getElementById.
The function could be called find_one_by_id or find_first_by_id to make it more explicit.

@philss
Copy link
Owner

philss commented Mar 1, 2024

Maybe we should also only return nil or the first element found?

@SteffenDE yeah, I agree. We don't need to return a list here.

The function could be called find_one_by_id or find_first_by_id to make it more explicit.

I believe if we document well, it's OK to use find_by_id. WDYT @josevalim ?

Copy link
Owner

@philss philss left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only minor points, but LGTM!

lib/floki.ex Outdated Show resolved Hide resolved
lib/floki.ex Outdated Show resolved Hide resolved
@josevalim
Copy link
Contributor

What about calling it “get” to mirror browsers then? Or get_by_id?

@philss
Copy link
Owner

philss commented Mar 1, 2024

What about calling it “get” to mirror browsers then? Or get_by_id?

I like the idea of using get_by_id! WDYT @SteffenDE ?

@SteffenDE
Copy link
Contributor Author

@philss yep, I like it too. Changed it.

Copy link
Owner

@philss philss left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚢

@philss philss merged commit bceb0e7 into philss:main Mar 1, 2024
9 checks passed
@philss
Copy link
Owner

philss commented Mar 1, 2024

@SteffenDE thank you for the contribution!

and thanks @ypconstante and @josevalim for the points and the discussion!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants