View Source Pages.Html (Pages v0.2.2)
Some simple HTML query functions, originally intended for unit tests. Delegates the hard work to Floki.
The main query functions are:
all/2
: returns all elements matching the selectorfind/2
: returns the first element that matches the selectorfind!/2
: likefind/2
but raises if more than one element matches the selector
Selectors can be a valid CSS selector string, or can be a keyword list. See Pages.Css
for keyword list syntax.
The attr/2
function can be used to extract attr values, and the text/1
function can be used to extract
the text of an HTML fragment.
examples
Examples
Get the value of a selected option:
iex> html = ~s|<select> <option value="a" selected>apples</option> <option value="b">bananas</option> </select>|
iex> Pages.Html.find(html, "select option[selected]") |> Pages.Html.attr("value")
"a"
Get the text of a selected option, raising if there are more than one:
iex> html = ~s|<select> <option value="a" selected>apples</option> <option value="b">bananas</option> </select>|
iex> Pages.Html.find!(html, "select option[selected]") |> Pages.Html.text()
"apples"
Get the text of all the options:
iex> html = ~s|<select> <option value="a" selected>apples</option> <option value="b">bananas</option> </select>|
iex> Pages.Html.all(html, "select option") |> Enum.map(&Pages.Html.text/1)
["apples", "bananas"]
Use a keyword list as the selector:
iex> html = ~s|<div> <a href="/logout" test-role="logout-link">logout</a> </div>|
iex> Pages.Html.find!(html, test_role: "logout-link") |> Pages.Html.attr("href")
"/logout"
Link to this section Summary
Functions
Finds all elements in html
that match selector
. Returns a
Floki HTML tree, which is a list of
Floki HTML nodes.
Returns the value of attr
from the outermost element of html
Like find/2
but raises unless exactly one element is found.
Finds the first element in html
that matches selector
. Returns a
Floki HTML node.
Returns a map containing the form fields of form selector
in html
.
Prints prettified html
with a label, and then returns the original html.
Extracts all the meta tags from html
.
Parses and then re-stringifies html
, increasing the liklihood that two equivalent HTML strings can
be considered equal.
Parses an HTML fragment using Floki.parse_fragment!/1
, returning a
Floki HTML tree.
Parses an HTML document using Floki.parse_document!/1
, returning a
Floki HTML tree.
Pretty-ifies html
using Floki.raw_html/2
and its pretty: true
option.
Returns the text value of html
Link to this section Types
Link to this section Functions
@spec all(html(), selector()) :: Floki.html_tree()
Finds all elements in html
that match selector
. Returns a
Floki HTML tree, which is a list of
Floki HTML nodes.
iex> html = ~s|<select> <option value="a" selected>apples</option> <option value="b">bananas</option> </select>|
iex> Pages.Html.all(html, "option")
[
{"option", [{"value", "a"}, {"selected", "selected"}], ["apples"]},
{"option", [{"value", "b"}], ["bananas"]}
]
Returns the value of attr
from the outermost element of html
iex> html = ~s|<div> <a href="/logout" test-role="logout-link">logout</a> </div>|
iex> Pages.Html.find!(html, test_role: "logout-link") |> Pages.Html.attr("href")
"/logout"
@spec find!(html(), selector()) :: Floki.html_node()
Like find/2
but raises unless exactly one element is found.
@spec find(html(), selector()) :: Floki.html_node()
Finds the first element in html
that matches selector
. Returns a
Floki HTML node.
iex> html = ~s|<select> <option value="a" selected>apples</option> <option value="b">bananas</option> </select>|
iex> Pages.Html.find(html, "select option[selected]")
{"option", [{"value", "a"}, {"selected", "selected"}], ["apples"]}
Returns a map containing the form fields of form selector
in html
.
iex> html = ~s|<form> <input type="text" name="color" value="green"> <textarea name="desc">A tree</textarea> </form>|
iex> Pages.Html.form_fields(html, "form")
%{color: "green", desc: "A tree"}
Prints prettified html
with a label, and then returns the original html.
Extracts all the meta tags from html
.
iex> html = ~s|<head> <meta charset="utf-8"/> <meta http-equiv="X-UA-Compatible" content="IE=edge"/> </head>|
iex> Pages.Html.meta_tags(html)
[%{"charset" => "utf-8"}, %{"content" => "IE=edge", "http-equiv" => "X-UA-Compatible"}]
Parses and then re-stringifies html
, increasing the liklihood that two equivalent HTML strings can
be considered equal.
iex> a = ~s|<p id="color">green</p>|
iex> b = ~s|<p id = "color" >green</p>|
iex> a == b
false
iex> Pages.Html.normalize(a) == Pages.Html.normalize(b)
true
@spec parse(html()) :: Floki.html_tree()
Parses an HTML fragment using Floki.parse_fragment!/1
, returning a
Floki HTML tree.
html
can be an HTML string, a Floki HTML tree, a Floki HTML node, or any struct that implements String.Chars
.
@spec parse_doc(binary()) :: Floki.html_tree()
Parses an HTML document using Floki.parse_document!/1
, returning a
Floki HTML tree.
Pretty-ifies html
using Floki.raw_html/2
and its pretty: true
option.
Returns the text value of html
iex> html = ~s|<select> <option value="a" selected>apples</option> <option value="b">bananas</option> </select>|
iex> Pages.Html.find!(html, "select option[selected]") |> Pages.Html.text()
"apples"