-
Notifications
You must be signed in to change notification settings - Fork 8
Implement parent_nodes + nth_child #25
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
Changes from 4 commits
ad68952
f52b687
922e53b
f6a4964
a594dd9
50c0727
a98b1bc
83845cd
8669e1d
7e9d14e
0594c3f
e2dbf91
a4e7d69
1b73ce7
323deb7
9062d95
82de179
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -357,6 +357,60 @@ defmodule LazyHTML do | |
| LazyHTML.NIF.child_nodes(lazy_html) | ||
| end | ||
|
|
||
| @doc """ | ||
| Returns the (unique) parent nodes of the root nodes in `lazy_html`. | ||
|
|
||
| ## Examples | ||
|
|
||
| iex> lazy_html = LazyHTML.from_fragment(~S|<div><span>Hello</span> <span>world</span></div>|) | ||
| iex> spans = LazyHTML.query(lazy_html, "span") | ||
| iex> LazyHTML.parent_nodes(spans) | ||
| #LazyHTML< | ||
| 1 node (from selector) | ||
| #1 | ||
| <div><span>Hello</span> <span>world</span></div> | ||
| > | ||
|
|
||
| The root node is always <html>, even if initialized via `from_fragment/1`: | ||
|
|
||
| iex> lazy_html = LazyHTML.from_fragment(~S|<div>root</div>|) | ||
| iex> LazyHTML.parent_nodes(lazy_html) | ||
| #LazyHTML< | ||
| 1 node (from selector) | ||
| #1 | ||
| <html><div>root</div></html> | ||
| > | ||
|
||
|
|
||
| """ | ||
| @spec parent_nodes(t()) :: t() | ||
| def parent_nodes(lazy_html) do | ||
|
||
| LazyHTML.NIF.parent_nodes(lazy_html) | ||
| end | ||
|
|
||
| @doc """ | ||
| Returns the parent nodes of the root nodes in `lazy_html`. | ||
| Useful when you're expecting a single, shared parent. | ||
| """ | ||
| def parent_node(lazy_html) do | ||
spicychickensauce marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| parent = LazyHTML.NIF.parent_nodes(lazy_html) | ||
|
|
||
| case LazyHTML.NIF.num_nodes(parent) do | ||
| 0 -> {:ok, nil} | ||
| 1 -> {:ok, parent} | ||
| _ -> {:error, :multiple_parents} | ||
| end | ||
| end | ||
|
|
||
| @doc """ | ||
| Same as `parent_node/1` but raises on multiple parents | ||
| """ | ||
| def parent_node!(lazy_html) do | ||
| case parent_node(lazy_html) do | ||
| {:ok, res} -> res | ||
| {:error, :multiple_parents} -> raise "Selected nodes have multiple parents" | ||
| end | ||
| end | ||
|
|
||
| @doc """ | ||
| Returns the text content of all nodes in `lazy_html`. | ||
|
|
||
|
|
@@ -481,6 +535,29 @@ defmodule LazyHTML do | |
| LazyHTML.NIF.tag(lazy_html) | ||
| end | ||
|
|
||
| @doc """ | ||
| Returns true if the lazy_html is selecting the same nodes starting from the same document. | ||
|
|
||
| ## Examples | ||
|
|
||
| iex> lazy_html = LazyHTML.from_fragment(~S|<div><span id=1>Hello</span></div>|) | ||
| iex> a = LazyHTML.query(lazy_html, "#1") | ||
| iex> b = LazyHTML.query(lazy_html, "div > span") | ||
| iex> LazyHTML.equals?(a, b) | ||
| true | ||
|
|
||
| Note that if the lazy_htmls are created separately, they are never equal: | ||
|
|
||
| iex> html_a = LazyHTML.from_fragment(~S|<div>hello</div>|) | ||
| iex> html_b = LazyHTML.from_fragment(~S|<div>hello</div>|) | ||
| iex> LazyHTML.equals?(html_a, html_b) | ||
| false | ||
| """ | ||
| @spec equals?(t(), t()) :: boolean() | ||
| def equals?(html_a, html_b) do | ||
spicychickensauce marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| LazyHTML.NIF.equals(html_a, html_b) | ||
| end | ||
|
|
||
| @doc ~S""" | ||
| Escapes the given string to make a valid HTML text. | ||
|
|
||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.