Get the descendants of each element in the current set of matched elements, filtered by a selector.
iex> ModestEx.find("<p><a>Hello</a> World</p>", "p a")
"<a>Hello</a>"
iex> ModestEx.find("<p><span>Hello</span><span>World</span></p>", "span")
["<span>Hello</span>", "<span>World</span>"]
Serialize any string with valid or broken html and return a valid html string. Use a custom scope to control serialization.
iex> ModestEx.serialize("<div>Hello<span>World", :body_children)
"<div>Hello<span>World</span></div>"
iex> ModestEx.serialize("", :html)
"<html><head></head><body></body></html>"
Get the value of an attribute for all elements in the set of matched elements.
iex> ModestEx.get_attribute("<a href=\"https://elixir-lang.org\">Hello</a>", "href")
"https://elixir-lang.org"
iex> ModestEx.get_attribute("<p><a href=\"https://elixir-lang.org\">Hello</a></p>", "p a", "href")
"https://elixir-lang.org"
Set one or more attributes for every matched element.
iex> ModestEx.set_attribute("<a href=\"\">Hello</a>", "href", "https://elixir-lang.org")
"<a href=\"https://elixir-lang.org\">Hello</a>"
iex> ModestEx.set_attribute("<p><a href=\"\">Hello</a></p>", "p a", "href", "https://elixir-lang.org")
"<p><a href=\"https://elixir-lang.org\">Hello</a></p>"
iex> ModestEx.set_attribute(["<p><a>Hello</a></p>", "<p><a>World</a></p>"], "p a", "href", ["https://elixir-lang.org", "https://google.de"])
["<p><a href=\"https://elixir-lang.org\">Hello</a></p>", "<p><a href=\"https://google.de\">World</a></p>"]
Get the combined text contents of each element in the set of matched elements, including their descendants.
iex> ModestEx.get_text("<p><span><a>Hello</a></span><span><a>World</a></span></p>")
["Hello", "World"]
iex> ModestEx.get_text("<p><span><a>Hello</a></span><span><a>World</a></span></p>", "span a")
["Hello", "World"]
Set the content of each element in the set of matched elements to the specified text.
iex> ModestEx.set_text("<p><span><a>Hello</a></span><span><a>Hello</a></span></p>", "a", "World")
"<p><span><a>World</a></span><span><a>World</a></span></p>"
Remove the set of matched elements from the html string.
iex> ModestEx.remove("<div><p>Hello</p>World</div>", "div p")
"<div>World</div>"
Insert content, specified by the parameter, to the end of each element in the set of matched elements.
iex> ModestEx.append("<div><p>Hello</p></div>", "div", "<p>World</p>")
"<div><p>Hello</p><p>World</p></div>"
Insert content, specified by the parameter, to the beginning of each element in the set of matched elements.
iex> ModestEx.prepend("<div><p>World</p></div>", "div", "<p>Hello</p>")
"<div><p>Hello</p><p>World</p></div>"
Insert every element in the set of matched elements after the target.
iex> ModestEx.insert_after("<div><p>Hello</p></div>", "div p", "<p>World</p>")
"<div><p>Hello</p><p>World</p></div>"
Insert every element in the set of matched elements before the target.
iex> ModestEx.insert_before("<div><p>World</p></div>", "div p", "<p>Hello</p>")
"<div><p>Hello</p><p>World</p></div>"
Replace each element in the set of matched elements with the provided new content and return the set of elements that was removed.
iex> ModestEx.replace("<div><p>Hello</p></div>", "div p", "<p>World</p>")
"<div><p>World</p></div>"
Reduce the set of matched elements to a subset specified by a range of indices.
iex> ModestEx.slice("<h1>Lorem ipsum</h1><p>dolor sit amet</p><ul><li>Coffee</li><li>Tea</li><li>Milk</li></ul><p>Sed ut perspiciatis</p><p>unde omnis iste natus</p>", "> *", 0, -1)
["<h1>Lorem ipsum</h1>", "<p>dolor sit amet</p>", "<ul><li>Coffee</li><li>Tea</li><li>Milk</li></ul>", "<p>Sed ut perspiciatis</p>", "<p>unde omnis iste natus</p>"]
Return position of the selected node in relation to its parent.
Can be used with slice()
.
iex> ModestEx.position("<p>Hello</p><div></div><p>World</p>", "p")
[1, 3]
Wrap an HTML structure around each element in the set of matched elements.
iex> ModestEx.wrap("<p>Hello</p><p>World</p>", "p", "<div class=\"wrapper\">")
"<div class=\"wrapper\"><p>Hello</p></div><div class=\"wrapper\"><p>World</p></div>"
Use IO.puts
to pretty print html tree to stdout
.
iex> ModestEx.pretty_print("<p>Hello World</p>")
"\e[31m<\e[0m\e[31mp\e[0m\e[31m>\e[0m\e[0mHello World\e[0m\e[31m</\e[0m\e[31mp\e[0m\e[31m>\e[0m\n"