### Extracting Text Content per Node Source: https://hexdocs.pm/lazy_html/LazyHTML.html Demonstrates how to get the text content for each individual node, often used after querying for specific elements. ```elixir iex> lazy_html = LazyHTML.from_fragment(~S|
Hello world
|) iex> spans = LazyHTML.query(lazy_html, "span") #LazyHTML< 2 nodes (from selector) #1 Hello #2 world > iex> Enum.map(spans, &LazyHTML.text/1) ["Hello", "world"] ``` -------------------------------- ### Get child nodes of root nodes Source: https://hexdocs.pm/lazy_html/LazyHTML.html Returns the child nodes of the root nodes in a LazyHTML structure. Can be called recursively to get deeper child nodes. ```elixir iex> lazy_html = LazyHTML.from_fragment(~S|
Hello world
|) iex> LazyHTML.child_nodes(lazy_html) #LazyHTML< 3 nodes (from selector) #1 Hello #2 [whitespace] #3 world > ``` ```elixir iex> LazyHTML.child_nodes(LazyHTML.child_nodes(lazy_html)) #LazyHTML< 2 nodes (from selector) #1 Hello #2 world > ``` -------------------------------- ### Get attributes of root elements Source: https://hexdocs.pm/lazy_html/LazyHTML.html Retrieves attribute lists for every root element in a LazyHTML structure. Text and comment nodes are ignored. ```elixir iex> lazy_html = ...> LazyHTML.from_fragment(""" ...>
...> Hello ...> world ...>
...> """) iex> spans = LazyHTML.query(lazy_html, "span") iex> LazyHTML.attributes(spans) [ [{"class", "text"}, {"data-id", "1"}], [] ] ``` ```elixir iex> lazy_html = ...> LazyHTML.from_fragment(""" ...> ...> Hello ...> world ...> """) iex> LazyHTML.attributes(lazy_html) [ [{"class", "text"}] ] ``` -------------------------------- ### Get parent nodes Source: https://hexdocs.pm/lazy_html/LazyHTML.html Returns the unique parent nodes of the root nodes in a LazyHTML structure. This function is useful for navigating up the HTML tree. ```elixir iex> lazy_html = LazyHTML.from_fragment(~S|
Hello
|) iex> LazyHTML.parent_node(LazyHTML.query(lazy_html, "span")) #LazyHTML< 1 node #1
Hello
> ``` -------------------------------- ### Get nth child position among siblings Source: https://hexdocs.pm/lazy_html/LazyHTML.html Returns the 1-based position among element siblings for every root node. Matches :nth-child CSS pseudo-class behavior. Text and comment nodes are ignored. ```elixir iex> lazy_html = LazyHTML.from_fragment(~S|
12
|) iex> spans = LazyHTML.query(lazy_html, "span") iex> LazyHTML.nth_child(spans) [1, 2] ``` -------------------------------- ### Create LazyHTML Document from Fragment Source: https://hexdocs.pm/lazy_html/LazyHTML.html Demonstrates creating a LazyHTML document from an HTML fragment and querying for all anchor tags. ```Elixir lazy_html = LazyHTML.from_fragment("""
Elixir Erlang
\n """) #=> #LazyHTML< #=> 1 node #=> #=> #1 #=>
#=> Elixir #=> Erlang #=>
#=> > hyperlinks = LazyHTML.query(lazy_html, "a") #=> #LazyHTML< #=> 2 nodes (from selector) #=> #=> #1 #=> Elixir #=> #=> #2 #=> Erlang #=> > LazyHTML.attribute(hyperlinks, "href") #=> ["https://elixir-lang.org", "https://www.erlang.org"] ``` -------------------------------- ### Querying Elements and Extracting Text Source: https://hexdocs.pm/lazy_html/LazyHTML.html Shows how to create a LazyHTML document and use the Access implementation to query for specific elements and extract their text content. ```Elixir lazy_html = LazyHTML.from_fragment(~S|

Hello, world!

|) #=> #LazyHTML< #=> 1 node #=> #=> #1 #=>

Hello, world!

#=> > lazy_html["strong, em"] #=> #LazyHTML< #=> 2 nodes (from selector) #=> #=> #1 #=> Hello #=> #=> #2 #=> world #=> > LazyHTML.text(lazy_html) #=> "Hello, world!" Enum.map(lazy_html["strong, em"], &LazyHTML.text/1) #=> ["Hello", "world"] ``` -------------------------------- ### Querying Parent Node Source: https://hexdocs.pm/lazy_html/LazyHTML.html Demonstrates how to query for elements and then find their parent node. Useful for navigating the HTML structure upwards. ```elixir iex> lazy_html = LazyHTML.from_fragment(~S|
Hello world
|) iex> spans = LazyHTML.query(lazy_html, "span") iex> LazyHTML.parent_node(spans) #LazyHTML< 1 node (from selector) #1
Hello world
> ``` -------------------------------- ### Convert HTML Document to Elixir Tree Source: https://hexdocs.pm/lazy_html/LazyHTML.html Demonstrates converting an HTML document string into an Elixir tree data structure. ```elixir iex> lazy_html = LazyHTML.from_document(~S|PageHello world|) iex> LazyHTML.to_tree(lazy_html) [{"html", [], [{"head", [], [{"title", [], ["Page"]}]}, {"body", [], ["Hello world"]}]}] ``` -------------------------------- ### Convert HTML Document to HTML String Source: https://hexdocs.pm/lazy_html/LazyHTML.html Demonstrates converting an HTML document string into a LazyHTML document and then back to an HTML string. ```elixir iex> lazy_html = LazyHTML.from_document(~S|Hello world!|) iex> LazyHTML.to_html(lazy_html) "Hello world!" ``` -------------------------------- ### Convert HTML Fragment to HTML String Source: https://hexdocs.pm/lazy_html/LazyHTML.html Shows how to convert an HTML fragment string into a LazyHTML fragment and then to an HTML string. ```elixir iex> lazy_html = LazyHTML.from_fragment(~S|Hello world|) iex> LazyHTML.to_html(lazy_html) "Hello world" ``` -------------------------------- ### Convert HTML Fragment to HTML String (with whitespace skipping) Source: https://hexdocs.pm/lazy_html/LazyHTML.html Illustrates converting a multi-line HTML fragment to an HTML string, skipping whitespace-only text nodes. ```elixir iex> lazy_html = ...> LazyHTML.from_fragment(""" ...>

...> Hello ...> world ...>

...> """) iex> LazyHTML.to_html(lazy_html, skip_whitespace_nodes: true) "

Hello world

" ``` -------------------------------- ### from_tree(LazyHTML.Tree.t()) Source: https://hexdocs.pm/lazy_html/LazyHTML.html Builds a lazy HTML document from an Elixir tree data structure. ```APIDOC ## from_tree(tree) ### Description Builds a lazy HTML document from an Elixir tree data structure. ### Function Signature ```elixir @spec from_tree(LazyHTML.Tree.t()) :: t() ``` ### Parameters #### Request Body - **tree** (LazyHTML.Tree.t()) - The Elixir tree structure to convert into LazyHTML. ### Examples ```elixir # Example 1: Building from a full HTML tree tree = [ {"html", [], [{"head", [], [{"title", [], ["Page"]}]}, {"body", [], ["Hello world"]}]} ] LazyHTML.from_tree(tree) # Expected output: #LazyHTML< # 1 node # #1 # PageHello world # > # Example 2: Building from a mixed tree with elements, comments, and attributes tree = [ {"div", [], []}, {:comment, " Link "}, {"a", [{"href", "https://elixir-lang.org"}], ["Elixir"]} ] LazyHTML.from_tree(tree) # Expected output: #LazyHTML< # 3 nodes # #1 #
# #2 # # #3 # Elixir # > ``` ``` -------------------------------- ### Convert Between LazyHTML and Elixir Tree Source: https://hexdocs.pm/lazy_html/LazyHTML.html Demonstrates converting a LazyHTML document to an Elixir tree data structure and back. ```Elixir lazy_html = LazyHTML.from_fragment("

Hello, world!

") #=> #LazyHTML< #=> 1 node #=> #=> #1 #=>

Hello, world!

#=> > tree = LazyHTML.to_tree(lazy_html) #=> [{"p", [], [{"strong", [], ["Hello"]}, ", ", {"em", [], ["world"]}, "!"]}] LazyHTML.from_tree(tree) #=> #LazyHTML< #=> 1 node #=> #1 #=>

Hello, world!

#=> > ``` -------------------------------- ### from_tree/1 Source: https://hexdocs.pm/lazy_html/LazyHTML.html Constructs a LazyHTML document from an Elixir tree data structure. ```APIDOC ## from_tree(tree) ### Description Builds a lazy HTML document from an Elixir tree data structure. ### Method `LazyHTML.from_tree(tree)` ### Parameters #### Path Parameters - `tree` (list()): An Elixir list representing the HTML tree structure. ### Response #### Success Response - `t()`: A LazyHTML document built from the provided tree. ### Request Example ```elixir tree = [{"p", [], [{"strong", [], ["Hello"]}, ", ", {"em", [], ["world"]}, "!"]}] LazyHTML.from_tree(tree) #=> #LazyHTML< #=> 1 node #=> #=> #1 #=>

Hello, world!

#=> > ``` ``` -------------------------------- ### Convert HTML Fragment to Sorted Elixir Tree Source: https://hexdocs.pm/lazy_html/LazyHTML.html Illustrates converting an HTML fragment with attributes to an Elixir tree, with attributes sorted alphabetically. ```elixir iex> lazy_html = LazyHTML.from_fragment(~S|
|) iex> LazyHTML.to_tree(lazy_html, sort_attributes: true) [{"div", [{"class", "layout"}, {"id", "root"}], []}] ``` -------------------------------- ### to_tree/2 Source: https://hexdocs.pm/lazy_html/LazyHTML.html Builds an Elixir tree data structure representing the lazy_html document. ```APIDOC ## to_tree/2 Builds an Elixir tree data structure representing the `lazy_html` document. ### Function Signature ```elixir @spec to_tree( t(), keyword() ) :: LazyHTML.Tree.t() ``` ### Options * `:sort_attributes` - when `true`, attributes lists are sorted alphabetically by name. Defaults to `false`. * `:skip_whitespace_nodes` - when `true`, ignores text nodes that consist entirely of whitespace, usually whitespace between tags. Defaults to `false`. ### Examples ```elixir iex> lazy_html = LazyHTML.from_document(~S|PageHello world|) iex> LazyHTML.to_tree(lazy_html) [{"html", [], [{"head", [], [{"title", [], ["Page"]}]}, {"body", [], ["Hello world"]}]}] iex> lazy_html = LazyHTML.from_fragment(~S|
Elixir
|) iex> LazyHTML.to_tree(lazy_html) [ {"div", [], [{:comment, " Link "}, {"a", [{"href", "https://elixir-lang.org"}], ["Elixir"]}]} ] ``` You can get a normalized tree by passing `sort_attributes: true`: ```elixir iex> lazy_html = LazyHTML.from_fragment(~S|
|) iex> LazyHTML.to_tree(lazy_html, sort_attributes: true) [{"div", [{"class", "layout"}, {"id", "root"}], []}] ``` ``` -------------------------------- ### to_html(lazy_html, opts \\ []) Source: https://hexdocs.pm/lazy_html/LazyHTML.html Serializes `lazy_html` into an HTML string. Whitespace nodes can be optionally skipped. ```APIDOC ## to_html(lazy_html, opts \\ []) ### Description Serializes `lazy_html` as an HTML string. Whitespace nodes can be optionally skipped. ### Parameters #### Path Parameters - **lazy_html** (t()) - Required - The LazyHTML struct to serialize. - **opts** (keyword()) - Optional - Options for serialization. * `:skip_whitespace_nodes` - When `true`, ignores text nodes that consist entirely of whitespace. Defaults to `false`. ### Request Example ```elixir # Assuming lazy_html is already defined LazyHTML.to_html(lazy_html, skip_whitespace_nodes: true) ``` ### Response #### Success Response (String.t()) Returns the HTML string representation of the LazyHTML struct. ### Response Example ```elixir "
Hello world
" ``` ``` -------------------------------- ### Convert HTML Fragment with Comments and Attributes to Elixir Tree Source: https://hexdocs.pm/lazy_html/LazyHTML.html Shows converting an HTML fragment containing comments and attributes into an Elixir tree. ```elixir iex> lazy_html = LazyHTML.from_fragment(~S|
Elixir
|) iex> LazyHTML.to_tree(lazy_html) [ {"div", [], [{:comment, " Link "}, {"a", [{"href", "https://elixir-lang.org"}], ["Elixir"]}]} ] ``` -------------------------------- ### from_fragment(String.t()) Source: https://hexdocs.pm/lazy_html/LazyHTML.html Parses a segment of an HTML document without adding any extra tags. ```APIDOC ## from_fragment(html) ### Description Parses a segment of an HTML document. As opposed to `from_document/1`, this function does not expect a full document and does not add any extra tags. ### Function Signature ```elixir @spec from_fragment(String.t()) :: t() ``` ### Parameters #### Request Body - **html** (String.t()) - The HTML fragment string to parse. ### Examples ```elixir # Example 1: Single element fragment LazyHTML.from_fragment(~S|Click me|) # Expected output: #LazyHTML< # 1 node # #1 # Click me # > # Example 2: Multiple nodes with whitespace LazyHTML.from_fragment(~S|Hello world|) # Expected output: #LazyHTML< # 3 nodes # #1 # Hello # #2 # [whitespace] # #3 # world # > ``` ``` -------------------------------- ### Parse HTML document Source: https://hexdocs.pm/lazy_html/LazyHTML.html Parses a complete HTML document, adding missing , , or tags if necessary. Use `from_fragment/1` for partial HTML. ```elixir iex> LazyHTML.from_document(~S|Hello world!|) #LazyHTML< 1 node #1 Hello world! > ``` ```elixir iex> LazyHTML.from_document(~S|
Hello world!
|) #LazyHTML< 1 node #1
Hello world!
> ``` -------------------------------- ### to_html/1 Source: https://hexdocs.pm/lazy_html/LazyHTML.html Converts a LazyHTML struct back into an HTML string. ```APIDOC ## to_html/1 Converts a LazyHTML struct back into an HTML string. ### Function Signature ```elixir @spec to_html(t()) :: String.t() ``` ### Examples ```elixir iex> lazy_html = LazyHTML.from_document(~S|Hello world!|) iex> LazyHTML.to_html(lazy_html) "Hello world!" iex> lazy_html = LazyHTML.from_fragment(~S|Hello world|) iex> LazyHTML.to_html(lazy_html) "Hello world" iex> lazy_html = ...> LazyHTML.from_fragment(""" ...>

...> Hello ...> world ...>

...> """) iex> LazyHTML.to_html(lazy_html, skip_whitespace_nodes: true) "

Hello world

" ``` ``` -------------------------------- ### query/2 Source: https://hexdocs.pm/lazy_html/LazyHTML.html Finds elements within a LazyHTML document that match a given CSS selector. ```APIDOC ## query(lazy_html, selector) ### Description Finds elements in `lazy_html` matching the given CSS selector. ### Method `LazyHTML.query(lazy_html, selector)` ### Parameters #### Path Parameters - `lazy_html` (t()): The LazyHTML document or node list to query. - `selector` (String.t()): The CSS selector to find elements. ### Response #### Success Response - `t()`: A LazyHTML document containing the found elements. ### Request Example ```elixir lazy_html = LazyHTML.from_fragment("""
Elixir Erlang
""") hyperlinks = LazyHTML.query(lazy_html, "a") #=> #LazyHTML< #=> 2 nodes (from selector) #=> #=> #1 #=> Elixir #=> #=> #2 #=> Erlang #=> > ``` ``` -------------------------------- ### to_html/2 Source: https://hexdocs.pm/lazy_html/LazyHTML.html Serializes a LazyHTML document into an HTML string. ```APIDOC ## to_html(lazy_html, opts \\ []) ### Description Serializes `lazy_html` as an HTML string. ### Method `LazyHTML.to_html(lazy_html, opts \\ [])` ### Parameters #### Path Parameters - `lazy_html` (t()): The LazyHTML document or node list to serialize. - `opts` (keyword()): Optional arguments. (Default: `[]`) ### Response #### Success Response - `String.t()`: The HTML string representation of the LazyHTML document. ``` -------------------------------- ### Serializing to HTML Source: https://hexdocs.pm/lazy_html/LazyHTML.html Converts a LazyHTML structure back into an HTML string. Supports options like skipping whitespace nodes. ```elixir iex> lazy_html = LazyHTML.from_fragment(~S|
Hello world
|) iex> LazyHTML.to_html(lazy_html) ``` -------------------------------- ### to_tree/2 Source: https://hexdocs.pm/lazy_html/LazyHTML.html Converts a LazyHTML document into an Elixir tree data structure. ```APIDOC ## to_tree(lazy_html, opts \\ []) ### Description Builds an Elixir tree data structure representing the `lazy_html` document. ### Method `LazyHTML.to_tree(lazy_html, opts \\ [])` ### Parameters #### Path Parameters - `lazy_html` (t()): The LazyHTML document or node list to convert. - `opts` (keyword()): Optional arguments. (Default: `[]`) ### Response #### Success Response - `list(tuple())`: An Elixir list representing the HTML tree structure. ### Request Example ```elixir lazy_html = LazyHTML.from_fragment("

Hello, world!

") LazyHTML.to_tree(lazy_html) #=> [{"p", [], [{"strong", [], ["Hello"]}, ", ", {"em", [], ["world"]}, "!"]}] ``` ``` -------------------------------- ### from_document/1 Source: https://hexdocs.pm/lazy_html/LazyHTML.html Parses a complete HTML document string into a LazyHTML document. ```APIDOC ## from_document(html) ### Description Parses an HTML document. ### Method `LazyHTML.from_document(html)` ### Parameters #### Path Parameters - `html` (String.t()): The HTML document string to parse. ### Response #### Success Response - `t()`: A LazyHTML document representation of the parsed HTML. ``` -------------------------------- ### Querying Elements with Complex Selectors Source: https://hexdocs.pm/lazy_html/LazyHTML.html Illustrates querying elements based on their position relative to siblings, such as ':first-child'. This is useful for styling or selecting elements based on document structure. ```elixir iex> lazy_html = ...> LazyHTML.from_fragment(""" ...>
...> Hello ...>
...>
...> World ...>
...> """) iex> spans = LazyHTML.query(lazy_html, "span") #LazyHTML< 2 nodes (from selector) #1 Hello #2 World > iex> LazyHTML.query(spans, ":first-child") #LazyHTML< 2 nodes (from selector) #1 Hello #2 World > ``` -------------------------------- ### from_document(String.t()) Source: https://hexdocs.pm/lazy_html/LazyHTML.html Parses an HTML document, adding missing ``, ``, or `` tags to match browser behavior. ```APIDOC ## from_document(html) ### Description Parses an HTML document. This function expects a complete document, therefore if either of ``, `` or `` tags is missing, it will be added, which matches the usual browser behaviour. To parse a part of an HTML document, use `from_fragment/1` instead. ### Function Signature ```elixir @spec from_document(String.t()) :: t() ``` ### Parameters #### Request Body - **html** (String.t()) - The HTML document string to parse. ### Examples ```elixir # Example 1: Complete HTML document LazyHTML.from_document(~S|Hello world!|) # Expected output: #LazyHTML< # 1 node # #1 # Hello world! # > # Example 2: HTML fragment, tags will be added LazyHTML.from_document(~S|
Hello world!
|) # Expected output: #LazyHTML< # 1 node # #1 #
Hello world!
# > ``` ``` -------------------------------- ### from_fragment/1 Source: https://hexdocs.pm/lazy_html/LazyHTML.html Parses a fragment of an HTML document into a LazyHTML document. ```APIDOC ## from_fragment(html) ### Description Parses a segment of an HTML document. ### Method `LazyHTML.from_fragment(html)` ### Parameters #### Path Parameters - `html` (String.t()): The HTML fragment string to parse. ### Response #### Success Response - `t()`: A LazyHTML document representation of the parsed HTML fragment. ### Request Example ```elixir lazy_html = LazyHTML.from_fragment("""
Elixir Erlang
""") #=> #LazyHTML< #=> 1 node #=> #=> #1 #=>
#=> Elixir #=> Erlang #=>
#=> > ``` ``` -------------------------------- ### attributes/1 Source: https://hexdocs.pm/lazy_html/LazyHTML.html Retrieves attribute lists for every root element in the provided LazyHTML document. ```APIDOC ## attributes(lazy_html) ### Description Returns attribute lists for every root element in `lazy_html`. ### Method `LazyHTML.attributes(lazy_html)` ### Parameters #### Path Parameters - `lazy_html` (t()): The LazyHTML document or node list. ### Response #### Success Response - `list(tuple(String.t(), String.t()))`: A list of attribute key-value pairs for each root element. ``` -------------------------------- ### Extracting Specific Attribute Values Source: https://hexdocs.pm/lazy_html/LazyHTML.html Shows how to extract all values for a given attribute from a set of LazyHTML nodes. ```Elixir iex> lazy_html = ...> LazyHTML.from_fragment(""" ...>
...> Hello ...> world ...> ! ...>
...> """ iex> spans = LazyHTML.query(lazy_html, "span") iex> LazyHTML.attribute(spans, "data-id") ["1", "2"] iex> LazyHTML.attribute(spans, "data-other") [] ``` -------------------------------- ### Querying Elements by CSS Selector Source: https://hexdocs.pm/lazy_html/LazyHTML.html Finds elements within an HTML structure that match a given CSS selector. Handles multiple root nodes and respects element location. ```elixir iex> lazy_html = ...> LazyHTML.from_fragment(""" ...>
...> Hello ...> world ...>
...> """) iex> LazyHTML.query(lazy_html, "span") #LazyHTML< 2 nodes (from selector) #1 Hello #2 world > iex> LazyHTML.query(lazy_html, ".layout") #LazyHTML< 1 node (from selector) #1
Hello world
> ``` -------------------------------- ### tag/1 Source: https://hexdocs.pm/lazy_html/LazyHTML.html Retrieves the tag name for every root element in a LazyHTML document. ```APIDOC ## tag(lazy_html) ### Description Returns tag name for every root element in `lazy_html`. ### Method `LazyHTML.tag(lazy_html)` ### Parameters #### Path Parameters - `lazy_html` (t()): The LazyHTML document or node list. ### Response #### Success Response - `[String.t()]`: A list of strings, where each string is the tag name of a root element. ``` -------------------------------- ### tag(lazy_html) Source: https://hexdocs.pm/lazy_html/LazyHTML.html Returns the tag names for every root element in `lazy_html`. Text and comment root nodes are ignored. ```APIDOC ## tag(lazy_html) ### Description Returns tag name for every root element in `lazy_html`. Note that if there are text or comment root nodes, they are ignored, and they have no corresponding list in the result. ### Parameters #### Path Parameters - **lazy_html** (t()) - Required - The LazyHTML struct to query. ### Request Example ```elixir # Assuming lazy_html is already defined LazyHTML.tag(lazy_html) ``` ### Response #### Success Response ([String.t()]) Returns a list of tag names. ### Response Example ```elixir ["div"] ``` ``` -------------------------------- ### html_escape(String.t()) Source: https://hexdocs.pm/lazy_html/LazyHTML.html Escapes the given string to make it a valid HTML text, converting special characters. ```APIDOC ## html_escape(string) ### Description Escapes the given string to make a valid HTML text. ### Function Signature ```elixir @spec html_escape(String.t()) :: String.t() ``` ### Parameters #### Request Body - **string** (String.t()) - The string to escape. ### Examples ```elixir # Example 1: Basic string LazyHTML.html_escape("foo") # Expected output: "foo" # Example 2: String with angle brackets LazyHTML.html_escape("") # Expected output: "<foo>" # Example 3: String with quotes and ampersand LazyHTML.html_escape("quotes: \" & '\"") # Expected output: "quotes: " & '" ``` ``` -------------------------------- ### Build LazyHTML from Elixir tree Source: https://hexdocs.pm/lazy_html/LazyHTML.html Builds a LazyHTML document from an Elixir tree data structure. Supports nested elements, attributes, and comments. ```elixir iex> tree = [ ...> {"html", [], [{"head", [], [{"title", [], ["Page"]}]}, {"body", [], ["Hello world"]}]} ...> ] iex> LazyHTML.from_tree(tree) #LazyHTML< 1 node #1 PageHello world > ``` ```elixir iex> tree = [ ...> {"div", [], []}, ...> {:comment, " Link "}, ...> {"a", ["href", "https://elixir-lang.org"], ["Elixir"]} ...> ] iex> LazyHTML.from_tree(tree) #LazyHTML< 3 nodes #1
#2 #3 Elixir > ``` -------------------------------- ### html_escape/1 Source: https://hexdocs.pm/lazy_html/LazyHTML.html Escapes a given string to make it a valid HTML text content. ```APIDOC ## html_escape(string) ### Description Escapes the given string to make a valid HTML text. ### Method `LazyHTML.html_escape(string)` ### Parameters #### Path Parameters - `string` (String.t()): The string to escape. ### Response #### Success Response - `String.t()`: The HTML-escaped string. ``` -------------------------------- ### query(lazy_html, selector) Source: https://hexdocs.pm/lazy_html/LazyHTML.html Finds elements in `lazy_html` matching the given CSS selector. The search includes root nodes and returns them if they match. ```APIDOC ## query(lazy_html, selector) ### Description Finds elements in `lazy_html` matching the given CSS selector. Since `lazy_html` may have multiple root nodes, the root nodes are included in the search and they will appear in the result if they match the given selector. ### Parameters #### Path Parameters - **lazy_html** (t()) - Required - The LazyHTML struct to query. - **selector** (String.t()) - Required - The CSS selector to match. ### Request Example ```elixir # Assuming lazy_html is already defined LazyHTML.query(lazy_html, "span") ``` ### Response #### Success Response (t()) Returns a new LazyHTML struct containing the matched nodes. ### Response Example ```elixir #LazyHTML< 2 nodes (from selector) #1 Hello #2 world > ``` ``` -------------------------------- ### HTML escape a string Source: https://hexdocs.pm/lazy_html/LazyHTML.html Escapes a given string to make it a valid HTML text. Handles special characters like quotes and ampersands. ```elixir iex> LazyHTML.html_escape("foo") "foo" ``` ```elixir iex> LazyHTML.html_escape("") "<foo>" ``` ```elixir iex> LazyHTML.html_escape("quotes: \" & '\"") "quotes: " & '" ``` -------------------------------- ### query_by_id/2 Source: https://hexdocs.pm/lazy_html/LazyHTML.html Finds elements in a LazyHTML document that match a specific ID. ```APIDOC ## query_by_id(lazy_html, id) ### Description Finds elements in `lazy_html` matching the given id. ### Method `LazyHTML.query_by_id(lazy_html, id)` ### Parameters #### Path Parameters - `lazy_html` (t()): The LazyHTML document or node list to query. - `id` (String.t()): The ID of the element to find. ### Response #### Success Response - `t()`: A LazyHTML document containing the element with the matching ID. ``` -------------------------------- ### Querying Elements by ID Source: https://hexdocs.pm/lazy_html/LazyHTML.html Finds elements matching a specific ID. This function is optimized for ID lookups and accepts unescaped ID strings. ```elixir iex> lazy_html = ...> LazyHTML.from_fragment(""" ...>
...> Hello ...> world ...>
...> """) iex> LazyHTML.query_by_id(lazy_html, "hello") #LazyHTML< 1 node (from selector) #1 Hello > ``` -------------------------------- ### text(lazy_html, opts \\ []) Source: https://hexdocs.pm/lazy_html/LazyHTML.html Returns the text content of all nodes in `lazy_html`. An optional separator can be provided to join the text content. ```APIDOC ## text(lazy_html, opts \\ []) ### Description Returns the text content of all nodes in `lazy_html`. An optional separator can be provided to join the text content. ### Parameters #### Path Parameters - **lazy_html** (t()) - Required - The LazyHTML struct to query. - **opts** (keyword()) - Optional - Options for retrieving text content. * `:separator` - A separator used to join the text content from individual nodes. Defaults to no separator. ### Request Example ```elixir # Assuming lazy_html is already defined LazyHTML.text(lazy_html, separator: ", ") ``` ### Response #### Success Response (String.t()) Returns the combined text content as a string. ### Response Example ```elixir "Hello world" ``` ``` -------------------------------- ### query_by_id(lazy_html, id) Source: https://hexdocs.pm/lazy_html/LazyHTML.html Finds elements in `lazy_html` matching the given ID. This function is similar to `query/2` but accepts an unescaped ID string. ```APIDOC ## query_by_id(lazy_html, id) ### Description Finds elements in `lazy_html` matching the given id. This function is similar to `query/2`, but it accepts unescaped id string. Note that while technically there should be only a single element with the given id, if there are multiple elements, all of them are included in the result. ### Parameters #### Path Parameters - **lazy_html** (t()) - Required - The LazyHTML struct to query. - **id** (String.t()) - Required - The ID of the element to find. ### Request Example ```elixir # Assuming lazy_html is already defined LazyHTML.query_by_id(lazy_html, "some_id") ``` ### Response #### Success Response (t()) Returns a new LazyHTML struct containing the matched nodes. ### Response Example ```elixir #LazyHTML< 1 node (from selector) #1 Hello > ``` ``` -------------------------------- ### attributes(t()) Source: https://hexdocs.pm/lazy_html/LazyHTML.html Returns attribute lists for every root element in lazy_html. Text or comment root nodes are ignored. ```APIDOC ## attributes(t()) ### Description Returns attribute lists for every root element in `lazy_html`. Text or comment root nodes are ignored. ### Function Signature ```elixir @spec attributes(t()) :: [[{String.t(), String.t()}]] ``` ### Examples ```elixir # Example 1: Basic usage with spans lazy_html = LazyHTML.from_fragment("""
Hello world
""") spans = LazyHTML.query(lazy_html, "span") LazyHTML.attributes(spans) # Expected output: [[{"class", "text"}, {"data-id", "1"}], []] # Example 2: With comments and text nodes lazy_html = LazyHTML.from_fragment(""" Hello world """) LazyHTML.attributes(lazy_html) # Expected output: [[{"class", "text"}]] ``` ``` -------------------------------- ### text/1 Source: https://hexdocs.pm/lazy_html/LazyHTML.html Extracts the text content from all nodes within a LazyHTML document. ```APIDOC ## text(lazy_html, opts \\ []) ### Description Returns the text content of all nodes in `lazy_html`. ### Method `LazyHTML.text(lazy_html, opts \\ [])` ### Parameters #### Path Parameters - `lazy_html` (t()): The LazyHTML document or node list. - `opts` (keyword()): Optional arguments. (Default: `[]`) ### Response #### Success Response - `String.t()`: The concatenated text content of all nodes. ### Request Example ```elixir lazy_html = LazyHTML.from_fragment(~S|

Hello, world!

|) LazyHTML.text(lazy_html) #=> "Hello, world!" ``` ``` -------------------------------- ### attribute/2 Source: https://hexdocs.pm/lazy_html/LazyHTML.html Retrieves all values for a specified attribute from the root nodes of a LazyHTML document. If an attribute is present but has no value, an empty string is returned. ```APIDOC ## attribute(lazy_html, name) ### Description Returns all values of the given attribute on the `lazy_html` root nodes. ### Method `LazyHTML.attribute(lazy_html, name)` ### Parameters #### Path Parameters - `lazy_html` (t()): The LazyHTML document or node list to query. - `name` (String.t()): The name of the attribute to retrieve. ### Request Example ```elixir lazy_html = LazyHTML.from_fragment("""
Hello world !
""") spans = LazyHTML.query(lazy_html, "span") LazyHTML.attribute(spans, "data-id") #=> ["1", "2"] LazyHTML.attribute(spans, "data-other") #=> [] lazy_html_button = LazyHTML.from_fragment(~S|
|) button = LazyHTML.query(lazy_html_button, "button") LazyHTML.attribute(button, "disabled") #=> [""] ``` ### Response #### Success Response - `[String.t()]`: A list of strings, where each string is a value of the specified attribute. Returns an empty list if the attribute is not found on any node. ``` -------------------------------- ### Handling Attributes Without Values Source: https://hexdocs.pm/lazy_html/LazyHTML.html Illustrates how LazyHTML handles attributes that are present but do not have an explicit value, such as the 'disabled' attribute. ```Elixir iex> lazy_html = LazyHTML.from_fragment(~S|
|) iex> button = LazyHTML.query(lazy_html, "button") iex> LazyHTML.attribute(button, "disabled") [""] ``` -------------------------------- ### nth_child/1 Source: https://hexdocs.pm/lazy_html/LazyHTML.html Determines the position among siblings for each root element in a LazyHTML document. ```APIDOC ## nth_child(lazy_html) ### Description Returns the position among its siblings for every root element in `lazy_html`. ### Method `LazyHTML.nth_child(lazy_html)` ### Parameters #### Path Parameters - `lazy_html` (t()): The LazyHTML document or node list. ### Response #### Success Response - `list(integer())`: A list of integers representing the sibling position (1-based index) for each root element. ``` -------------------------------- ### Extracting Text Content Source: https://hexdocs.pm/lazy_html/LazyHTML.html Retrieves the combined text content from all nodes within a LazyHTML structure. Supports custom separators for joining text. ```elixir iex> lazy_html = LazyHTML.from_fragment(~S|
Hello world
|) iex> LazyHTML.text(lazy_html) "Hello world" iex> lazy_html = LazyHTML.from_fragment(~S|
123
|) iex> LazyHTML.text(lazy_html, separator: ", ") "1, 2, 3" iex> lazy_html = LazyHTML.from_fragment(~S|
12
|) iex> LazyHTML.text(lazy_html, separator: ", ") "1, 2" ``` -------------------------------- ### Retrieving Tag Names Source: https://hexdocs.pm/lazy_html/LazyHTML.html Extracts the tag names of all root elements in a LazyHTML structure. Text and comment nodes are ignored. ```elixir iex> lazy_html = LazyHTML.from_fragment(~S|
Hello world
|) iex> LazyHTML.tag(lazy_html) ["div"] iex> lazy_html = LazyHTML.from_fragment(~S|Hello world|) iex> LazyHTML.tag(lazy_html) ["span", "span"] ``` -------------------------------- ### Parse HTML fragment Source: https://hexdocs.pm/lazy_html/LazyHTML.html Parses a segment of an HTML document without adding any extra tags. Suitable for snippets of HTML. ```elixir iex> LazyHTML.from_fragment(~S|Click me|) #LazyHTML< 1 node #1 Click me > ``` ```elixir iex> LazyHTML.from_fragment(~S|Hello world|) #LazyHTML< 3 nodes #1 Hello #2 [whitespace] #3 world > ``` -------------------------------- ### child_nodes/1 Source: https://hexdocs.pm/lazy_html/LazyHTML.html Returns the child nodes of the root nodes within a LazyHTML document. ```APIDOC ## child_nodes(lazy_html) ### Description Returns the child_nodes nodes of the root nodes in `lazy_html`. ### Method `LazyHTML.child_nodes(lazy_html)` ### Parameters #### Path Parameters - `lazy_html` (t()): The LazyHTML document or node list. ### Response #### Success Response - `t()`: A LazyHTML document containing the child nodes. ``` -------------------------------- ### parent_node/1 Source: https://hexdocs.pm/lazy_html/LazyHTML.html Retrieves the unique parent node for each root node in a LazyHTML document. ```APIDOC ## parent_node(lazy_html) ### Description Returns the (unique) parent nodes of the root nodes in `lazy_html`. ### Method `LazyHTML.parent_node(lazy_html)` ### Parameters #### Path Parameters - `lazy_html` (t()): The LazyHTML document or node list. ### Response #### Success Response - `t()`: A LazyHTML document containing the parent nodes. ``` -------------------------------- ### nth_child(t()) Source: https://hexdocs.pm/lazy_html/LazyHTML.html Returns the position among its siblings for every root element in `lazy_html`, matching `:nth-child` CSS pseudo-class. ```APIDOC ## nth_child(lazy_html) ### Description Returns the position among its siblings for every root element in `lazy_html`. The position numbering is 1-based and only considers siblings that are elements, as to match the `:nth-child` CSS pseudo-class. Text or comment root nodes are ignored. ### Function Signature ```elixir @spec nth_child(t()) :: [integer()] ``` ### Parameters #### Path Parameters - **lazy_html** (t()) - The LazyHTML structure to analyze. ### Examples ```elixir lazy_html = LazyHTML.from_fragment(~S|
12
|) spans = LazyHTML.query(lazy_html, "span") LazyHTML.nth_child(spans) # Expected output: [1, 2] ``` ``` -------------------------------- ### filter/2 Source: https://hexdocs.pm/lazy_html/LazyHTML.html Filters the root nodes of a LazyHTML document, retaining only those that match a given CSS selector. ```APIDOC ## filter(lazy_html, selector) ### Description Filters `lazy_html` root nodes, keeping only elements that match the given CSS selector. ### Method `LazyHTML.filter(lazy_html, selector)` ### Parameters #### Path Parameters - `lazy_html` (t()): The LazyHTML document or node list to filter. - `selector` (String.t()): The CSS selector to filter by. ### Response #### Success Response - `t()`: A new LazyHTML document containing only the nodes that match the selector. ``` -------------------------------- ### filter(t(), String.t()) Source: https://hexdocs.pm/lazy_html/LazyHTML.html Filters `lazy_html` root nodes, keeping only elements that match the given CSS selector. ```APIDOC ## filter(lazy_html, selector) ### Description Filters `lazy_html` root nodes, keeping only elements that match the given CSS selector. ### Function Signature ```elixir @spec filter(t(), String.t()) :: t() ``` ### Parameters #### Path Parameters - **lazy_html** (t()) - The LazyHTML structure to filter. - **selector** (String.t()) - The CSS selector to filter by. ### Examples ```elixir lazy_html = LazyHTML.from_fragment(""" Hello
nested
world """) LazyHTML.filter(lazy_html, "span") # Expected output: #LazyHTML< # 2 nodes (from selector) # #1 # Hello # #2 # world # > ``` ```