CINXE.COM

Mojo::DOM - Minimalistic HTML/XML DOM parser with CSS selectors

<!doctype html><html> <head> <link rel="apple-touch-icon" href="/mojolicious/touch-icon.png"> <link rel="apple-touch-icon" sizes="152x152" href="/mojolicious/touch-icon-152x152.png"> <link rel="apple-touch-icon" sizes="167x167" href="/mojolicious/touch-icon-167x167.png"> <link rel="apple-touch-icon" sizes="180x180" href="/mojolicious/touch-icon-180x180.png"> <link rel="search" type="application/opensearchdescription+xml" href="/opensearch.xml" title="Mojolicious" /> <meta name="viewport" content="width=device-width, initial-scale=1" /> <title>Mojo::DOM - Minimalistic HTML/XML DOM parser with CSS selectors</title> <script src="/mojolicious/jquery/jquery.js"></script> <script src="/mojolicious/highlight.js/highlight.min.js"></script> <link href="/mojolicious/highlight.js/highlight-mojo-light.css" rel="stylesheet"> <script>hljs.initHighlightingOnLoad();</script> <script src="/mojolicious/bootstrap/bootstrap.js"></script> <link href="/mojolicious/bootstrap/bootstrap.css" rel="stylesheet"> <link href="/mojolicious/fontawesome/fontawesome.css" rel="stylesheet"> <link href="/app.css?v=3" rel="stylesheet"> <link href="/mojodocs.css" rel="stylesheet"> </head> <body><a id="toc"></a> <header> <nav class="navbar navbar-expand-lg navbar-dark mojobar"> <a href="https://mojolicious.org" id="mojobar-brand" class="navbar-brand"> <picture> <img src="/mojo/logo-white.png" srcset="/mojo/logo-white-2x.png 2x"> </picture> </a> <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation"> <span class="navbar-toggler-icon"></span> </button> <div id="navbarNav" class="collapse navbar-collapse"> <ul class="navbar-nav mr-auto"> <li class="nav-item dropdown"> <a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false"> Documentation </a> <div class="dropdown-menu" aria-labelledby="navbarDropdown"> <a class="dropdown-item" href="https://docs.mojolicious.org">Overview</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="https://docs.mojolicious.org/Mojolicious/Guides/Tutorial">Tutorial</a> <a class="dropdown-item" href="https://docs.mojolicious.org/Mojolicious/Guides/Growing">Growing</a> <a class="dropdown-item" href="https://docs.mojolicious.org/Mojolicious/Guides/Routing">Routing</a> <a class="dropdown-item" href="https://docs.mojolicious.org/Mojolicious/Guides/Rendering">Rendering</a> <a class="dropdown-item" href="https://docs.mojolicious.org/Mojolicious/Guides/Testing">Testing</a> <a class="dropdown-item" href="https://docs.mojolicious.org/Mojolicious/Guides/Cookbook">Cookbook</a> <a class="dropdown-item" href="https://docs.mojolicious.org/Mojolicious/Guides/Contributing">Contributing</a> <a class="dropdown-item" href="https://docs.mojolicious.org/Mojolicious/Guides/FAQ">FAQ</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="https://docs.mojolicious.org#API">API</a> </div> </li> <li class="nav-item dropdown"> <a class="nav-link dropdown-toggle" href="#" id="communityDropdown" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false"> Community </a> <div class="dropdown-menu" aria-labelledby="communityDropdown"> <a class="dropdown-item" href="https://web.libera.chat/#mojo">IRC</a> <a class="dropdown-item" href="https://forum.mojolicious.org">Forum</a> <a class="dropdown-item" href="https://fosstodon.org/@mojolicious">Mastodon</a> <a class="dropdown-item" href="https://www.linkedin.com/groups/8963713/">LinkedIn</a> <a class="dropdown-item" href="https://github.com/mojolicious/mojo/wiki">Wiki</a> <a class="dropdown-item" href="https://metacpan.org/release/Mojolicious/">CPAN</a> </div> </li> <li class="nav-item"> <a class="nav-link" href="https://github.com/mojolicious/mojo/">Contribute on GitHub</a> </li> </ul> <a class="navbar-brand" href="https://mojojs.org"> <picture> <img src="/mojolicious/mojojs-white.png" srcset="/mojolicious/mojojs-white-2x.png 2x"> </picture> </a> <form action="https://www.google.com/cse" target="_blank" class="form-inline my-2 my-lg-0"> <input name="cx" type="hidden" value="014527573091551588235:pwfplkjpgbi"> <input name="ie" type="hidden" value="UTF-8"> <input name="q" placeholder="Search..." type="search"> </form> </div> </nav> </header> <div class="container-fluid"> <div class="row flex-wrap"> <aside class="col-sm-12 col-md-4 col-lg-2 mojo-sidebar"> <form class="mojo-version d-flex align-items-center"> <fieldset disabled> <input type="text" id="disabledTextInput" class="form-control" placeholder="v9.40"> </fieldset> </form> <ul> <li> <a href="#NAME"><b>NAME</b></a> </li> <li> <a href="#SYNOPSIS"><b>SYNOPSIS</b></a> </li> <li> <a href="#DESCRIPTION"><b>DESCRIPTION</b></a> </li> <li> <a href="#NODES-AND-ELEMENTS"><b>NODES AND ELEMENTS</b></a> </li> <li> <a href="#HTML-AND-XML"><b>HTML AND XML</b></a> </li> <li> <a href="#METHODS"><b>METHODS</b></a> <ul> <li><a href="#all_text">all_text</a></li> <li><a href="#ancestors">ancestors</a></li> <li><a href="#append">append</a></li> <li><a href="#append_content">append_content</a></li> <li><a href="#at">at</a></li> <li><a href="#attr">attr</a></li> <li><a href="#child_nodes">child_nodes</a></li> <li><a href="#children">children</a></li> <li><a href="#content">content</a></li> <li><a href="#descendant_nodes">descendant_nodes</a></li> <li><a href="#find">find</a></li> <li><a href="#following">following</a></li> <li><a href="#following_nodes">following_nodes</a></li> <li><a href="#matches">matches</a></li> <li><a href="#namespace">namespace</a></li> <li><a href="#new">new</a></li> <li><a href="#new_tag">new_tag</a></li> <li><a href="#next">next</a></li> <li><a href="#next_node">next_node</a></li> <li><a href="#parent">parent</a></li> <li><a href="#parse">parse</a></li> <li><a href="#preceding">preceding</a></li> <li><a href="#preceding_nodes">preceding_nodes</a></li> <li><a href="#prepend">prepend</a></li> <li><a href="#prepend_content">prepend_content</a></li> <li><a href="#previous">previous</a></li> <li><a href="#previous_node">previous_node</a></li> <li><a href="#remove">remove</a></li> <li><a href="#replace">replace</a></li> <li><a href="#root">root</a></li> <li><a href="#selector">selector</a></li> <li><a href="#strip">strip</a></li> <li><a href="#tag">tag</a></li> <li><a href="#tap">tap</a></li> <li><a href="#text">text</a></li> <li><a href="#to_string">to_string</a></li> <li><a href="#tree">tree</a></li> <li><a href="#type">type</a></li> <li><a href="#val">val</a></li> <li><a href="#with_roles">with_roles</a></li> <li><a href="#wrap">wrap</a></li> <li><a href="#wrap_content">wrap_content</a></li> <li><a href="#xml">xml</a></li> </ul> </li> <li> <a href="#OPERATORS"><b>OPERATORS</b></a> <ul> <li><a href="#array">array</a></li> <li><a href="#bool">bool</a></li> <li><a href="#hash">hash</a></li> <li><a href="#stringify">stringify</a></li> </ul> </li> <li> <a href="#SEE-ALSO"><b>SEE ALSO</b></a> </li> </ul> </aside> <main class="col-sm-12 col-md-8 col-lg-10 py-md-3 pl-md-5 mojo-main"> <div class="mojo-docinfo"> <div> <div class="mojo-docnav"> <a class="nav-link" alt="API" href="https://docs.mojolicious.org#API"><i class="fas fa-book"></i></a> </div> Module: <a href="http://docs.mojolicious.org/Mojo">Mojo</a>::<a href="http://docs.mojolicious.org/Mojo/DOM">DOM</a> </div> </div> <div class="mojo-external"> <a class="btn btn-outline-secondary btn-sm" href="http://docs.mojolicious.org/Mojo/DOM.txt" role="button"> Source </a> <a class="btn btn-outline-secondary btn-sm" href="https://metacpan.org/pod/Mojo::DOM" role="button">CPAN</a> </div> <div class="mojo-perldoc"> <h1 id="NAME"><a class="permalink" href="#NAME">#</a><a href="#toc">NAME</a></h1> <p>Mojo::DOM - Minimalistic HTML/XML DOM parser with CSS selectors</p> <h1 id="SYNOPSIS"><a class="permalink" href="#SYNOPSIS">#</a><a href="#toc">SYNOPSIS</a></h1> <pre><code>use Mojo::DOM; # Parse my $dom = Mojo::DOM-&gt;new(&#39;&lt;div&gt;&lt;p id=&quot;a&quot;&gt;Test&lt;/p&gt;&lt;p id=&quot;b&quot;&gt;123&lt;/p&gt;&lt;/div&gt;&#39;); # Find say $dom-&gt;at(&#39;#b&#39;)-&gt;text; say $dom-&gt;find(&#39;p&#39;)-&gt;map(&#39;text&#39;)-&gt;join(&quot;\n&quot;); say $dom-&gt;find(&#39;[id]&#39;)-&gt;map(attr =&gt; &#39;id&#39;)-&gt;join(&quot;\n&quot;); # Iterate $dom-&gt;find(&#39;p[id]&#39;)-&gt;reverse-&gt;each(sub { say $_-&gt;{id} }); # Loop for my $e ($dom-&gt;find(&#39;p[id]&#39;)-&gt;each) { say $e-&gt;{id}, &#39;:&#39;, $e-&gt;text; } # Modify $dom-&gt;find(&#39;div p&#39;)-&gt;last-&gt;append(&#39;&lt;p id=&quot;c&quot;&gt;456&lt;/p&gt;&#39;); $dom-&gt;at(&#39;#c&#39;)-&gt;prepend($dom-&gt;new_tag(&#39;p&#39;, id =&gt; &#39;d&#39;, &#39;789&#39;)); $dom-&gt;find(&#39;:not(p)&#39;)-&gt;map(&#39;strip&#39;); # Render say &quot;$dom&quot;;</code></pre> <h1 id="DESCRIPTION"><a class="permalink" href="#DESCRIPTION">#</a><a href="#toc">DESCRIPTION</a></h1> <p><a href="http://docs.mojolicious.org/Mojo/DOM">Mojo::DOM</a> is a minimalistic and relaxed HTML/XML DOM parser with CSS selector support. It will even try to interpret broken HTML and XML, so you should not use it for validation.</p> <h1 id="NODES-AND-ELEMENTS"><a class="permalink" href="#NODES-AND-ELEMENTS">#</a><a href="#toc">NODES AND ELEMENTS</a></h1> <p>When we parse an HTML/XML fragment, it gets turned into a tree of nodes.</p> <pre><code>&lt;!DOCTYPE html&gt; &lt;html&gt; &lt;head&gt;&lt;title&gt;Hello&lt;/title&gt;&lt;/head&gt; &lt;body&gt;World!&lt;/body&gt; &lt;/html&gt;</code></pre> <p>There are currently eight different kinds of nodes, <code>cdata</code>, <code>comment</code>, <code>doctype</code>, <code>pi</code>, <code>raw</code>, <code>root</code>, <code>tag</code> and <code>text</code>. Elements are nodes of the type <code>tag</code>.</p> <pre><code class="nohighlight">root |- doctype (html) +- tag (html) |- tag (head) | +- tag (title) | +- raw (Hello) +- tag (body) +- text (World!)</code></pre> <p>While all node types are represented as <a href="http://docs.mojolicious.org/Mojo/DOM">Mojo::DOM</a> objects, some methods like <a href="#attr">&quot;attr&quot;</a> and <a href="#namespace">&quot;namespace&quot;</a> only apply to elements.</p> <h1 id="HTML-AND-XML"><a class="permalink" href="#HTML-AND-XML">#</a><a href="#toc">HTML AND XML</a></h1> <p><a href="http://docs.mojolicious.org/Mojo/DOM">Mojo::DOM</a> defaults to HTML semantics, that means all tags and attribute names are lowercased and selectors need to be lowercase as well.</p> <pre><code># HTML semantics my $dom = Mojo::DOM-&gt;new(&#39;&lt;P ID=&quot;greeting&quot;&gt;Hi!&lt;/P&gt;&#39;); say $dom-&gt;at(&#39;p[id]&#39;)-&gt;text;</code></pre> <p>If an XML declaration is found, the parser will automatically switch into XML mode and everything becomes case-sensitive.</p> <pre><code># XML semantics my $dom = Mojo::DOM-&gt;new(&#39;&lt;?xml version=&quot;1.0&quot;?&gt;&lt;P ID=&quot;greeting&quot;&gt;Hi!&lt;/P&gt;&#39;); say $dom-&gt;at(&#39;P[ID]&#39;)-&gt;text;</code></pre> <p>HTML or XML semantics can also be forced with the <a href="#xml">&quot;xml&quot;</a> method.</p> <pre><code># Force HTML semantics my $dom = Mojo::DOM-&gt;new-&gt;xml(0)-&gt;parse(&#39;&lt;P ID=&quot;greeting&quot;&gt;Hi!&lt;/P&gt;&#39;); say $dom-&gt;at(&#39;p[id]&#39;)-&gt;text; # Force XML semantics my $dom = Mojo::DOM-&gt;new-&gt;xml(1)-&gt;parse(&#39;&lt;P ID=&quot;greeting&quot;&gt;Hi!&lt;/P&gt;&#39;); say $dom-&gt;at(&#39;P[ID]&#39;)-&gt;text;</code></pre> <h1 id="METHODS"><a class="permalink" href="#METHODS">#</a><a href="#toc">METHODS</a></h1> <p><a href="http://docs.mojolicious.org/Mojo/DOM">Mojo::DOM</a> implements the following methods.</p> <h2 id="all_text"><a class="permalink" href="#all_text">#</a><a href="#toc">all_text</a></h2> <pre><code>my $text = $dom-&gt;all_text;</code></pre> <p>Extract text content from all descendant nodes of this element. For HTML documents <code>script</code> and <code>style</code> elements are excluded.</p> <pre><code># &quot;foo\nbarbaz\n&quot; $dom-&gt;parse(&quot;&lt;div&gt;foo\n&lt;p&gt;bar&lt;/p&gt;baz\n&lt;/div&gt;&quot;)-&gt;at(&#39;div&#39;)-&gt;all_text;</code></pre> <h2 id="ancestors"><a class="permalink" href="#ancestors">#</a><a href="#toc">ancestors</a></h2> <pre><code>my $collection = $dom-&gt;ancestors; my $collection = $dom-&gt;ancestors(&#39;div ~ p&#39;);</code></pre> <p>Find all ancestor elements of this node matching the CSS selector and return a <a href="http://docs.mojolicious.org/Mojo/Collection">Mojo::Collection</a> object containing these elements as <a href="http://docs.mojolicious.org/Mojo/DOM">Mojo::DOM</a> objects. All selectors from <a href="http://docs.mojolicious.org/Mojo/DOM/CSS#SELECTORS">&quot;SELECTORS&quot; in Mojo::DOM::CSS</a> are supported.</p> <pre><code># List tag names of ancestor elements say $dom-&gt;ancestors-&gt;map(&#39;tag&#39;)-&gt;join(&quot;\n&quot;);</code></pre> <h2 id="append"><a class="permalink" href="#append">#</a><a href="#toc">append</a></h2> <pre><code>$dom = $dom-&gt;append(&#39;&lt;p&gt;I ♥ Mojolicious!&lt;/p&gt;&#39;); $dom = $dom-&gt;append(Mojo::DOM-&gt;new);</code></pre> <p>Append HTML/XML fragment to this node (for all node types other than <code>root</code>).</p> <pre><code># &quot;&lt;div&gt;&lt;h1&gt;Test&lt;/h1&gt;&lt;h2&gt;123&lt;/h2&gt;&lt;/div&gt;&quot; $dom-&gt;parse(&#39;&lt;div&gt;&lt;h1&gt;Test&lt;/h1&gt;&lt;/div&gt;&#39;) -&gt;at(&#39;h1&#39;)-&gt;append(&#39;&lt;h2&gt;123&lt;/h2&gt;&#39;)-&gt;root; # &quot;&lt;p&gt;Test 123&lt;/p&gt;&quot; $dom-&gt;parse(&#39;&lt;p&gt;Test&lt;/p&gt;&#39;)-&gt;at(&#39;p&#39;) -&gt;child_nodes-&gt;first-&gt;append(&#39; 123&#39;)-&gt;root;</code></pre> <h2 id="append_content"><a class="permalink" href="#append_content">#</a><a href="#toc">append_content</a></h2> <pre><code>$dom = $dom-&gt;append_content(&#39;&lt;p&gt;I ♥ Mojolicious!&lt;/p&gt;&#39;); $dom = $dom-&gt;append_content(Mojo::DOM-&gt;new);</code></pre> <p>Append HTML/XML fragment (for <code>root</code> and <code>tag</code> nodes) or raw content to this node&#39;s content.</p> <pre><code># &quot;&lt;div&gt;&lt;h1&gt;Test123&lt;/h1&gt;&lt;/div&gt;&quot; $dom-&gt;parse(&#39;&lt;div&gt;&lt;h1&gt;Test&lt;/h1&gt;&lt;/div&gt;&#39;) -&gt;at(&#39;h1&#39;)-&gt;append_content(&#39;123&#39;)-&gt;root; # &quot;&lt;!-- Test 123 --&gt;&lt;br&gt;&quot; $dom-&gt;parse(&#39;&lt;!-- Test --&gt;&lt;br&gt;&#39;) -&gt;child_nodes-&gt;first-&gt;append_content(&#39;123 &#39;)-&gt;root; # &quot;&lt;p&gt;Test&lt;i&gt;123&lt;/i&gt;&lt;/p&gt;&quot; $dom-&gt;parse(&#39;&lt;p&gt;Test&lt;/p&gt;&#39;)-&gt;at(&#39;p&#39;)-&gt;append_content(&#39;&lt;i&gt;123&lt;/i&gt;&#39;)-&gt;root;</code></pre> <h2 id="at"><a class="permalink" href="#at">#</a><a href="#toc">at</a></h2> <pre><code>my $result = $dom-&gt;at(&#39;div ~ p&#39;); my $result = $dom-&gt;at(&#39;svg|line&#39;, svg =&gt; &#39;http://www.w3.org/2000/svg&#39;);</code></pre> <p>Find first descendant element of this element matching the CSS selector and return it as a <a href="http://docs.mojolicious.org/Mojo/DOM">Mojo::DOM</a> object, or <code>undef</code> if none could be found. All selectors from <a href="http://docs.mojolicious.org/Mojo/DOM/CSS#SELECTORS">&quot;SELECTORS&quot; in Mojo::DOM::CSS</a> are supported.</p> <pre><code># Find first element with &quot;svg&quot; namespace definition my $namespace = $dom-&gt;at(&#39;[xmlns\:svg]&#39;)-&gt;{&#39;xmlns:svg&#39;};</code></pre> <p>Trailing key/value pairs can be used to declare xml namespace aliases.</p> <pre><code># &quot;&lt;rect /&gt;&quot; $dom-&gt;parse(&#39;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot;&gt;&lt;rect /&gt;&lt;/svg&gt;&#39;) -&gt;at(&#39;svg|rect&#39;, svg =&gt; &#39;http://www.w3.org/2000/svg&#39;);</code></pre> <h2 id="attr"><a class="permalink" href="#attr">#</a><a href="#toc">attr</a></h2> <pre><code>my $hash = $dom-&gt;attr; my $foo = $dom-&gt;attr(&#39;foo&#39;); $dom = $dom-&gt;attr({foo =&gt; &#39;bar&#39;}); $dom = $dom-&gt;attr(foo =&gt; &#39;bar&#39;);</code></pre> <p>This element&#39;s attributes.</p> <pre><code># Remove an attribute delete $dom-&gt;attr-&gt;{id}; # Attribute without value $dom-&gt;attr(selected =&gt; undef); # List id attributes say $dom-&gt;find(&#39;*&#39;)-&gt;map(attr =&gt; &#39;id&#39;)-&gt;compact-&gt;join(&quot;\n&quot;);</code></pre> <h2 id="child_nodes"><a class="permalink" href="#child_nodes">#</a><a href="#toc">child_nodes</a></h2> <pre><code>my $collection = $dom-&gt;child_nodes;</code></pre> <p>Return a <a href="http://docs.mojolicious.org/Mojo/Collection">Mojo::Collection</a> object containing all child nodes of this element as <a href="http://docs.mojolicious.org/Mojo/DOM">Mojo::DOM</a> objects.</p> <pre><code># &quot;&lt;p&gt;&lt;b&gt;123&lt;/b&gt;&lt;/p&gt;&quot; $dom-&gt;parse(&#39;&lt;p&gt;Test&lt;b&gt;123&lt;/b&gt;&lt;/p&gt;&#39;)-&gt;at(&#39;p&#39;)-&gt;child_nodes-&gt;first-&gt;remove; # &quot;&lt;!DOCTYPE html&gt;&quot; $dom-&gt;parse(&#39;&lt;!DOCTYPE html&gt;&lt;b&gt;123&lt;/b&gt;&#39;)-&gt;child_nodes-&gt;first; # &quot; Test &quot; $dom-&gt;parse(&#39;&lt;b&gt;123&lt;/b&gt;&lt;!-- Test --&gt;&#39;)-&gt;child_nodes-&gt;last-&gt;content;</code></pre> <h2 id="children"><a class="permalink" href="#children">#</a><a href="#toc">children</a></h2> <pre><code>my $collection = $dom-&gt;children; my $collection = $dom-&gt;children(&#39;div ~ p&#39;);</code></pre> <p>Find all child elements of this element matching the CSS selector and return a <a href="http://docs.mojolicious.org/Mojo/Collection">Mojo::Collection</a> object containing these elements as <a href="http://docs.mojolicious.org/Mojo/DOM">Mojo::DOM</a> objects. All selectors from <a href="http://docs.mojolicious.org/Mojo/DOM/CSS#SELECTORS">&quot;SELECTORS&quot; in Mojo::DOM::CSS</a> are supported.</p> <pre><code># Show tag name of random child element say $dom-&gt;children-&gt;shuffle-&gt;first-&gt;tag;</code></pre> <h2 id="content"><a class="permalink" href="#content">#</a><a href="#toc">content</a></h2> <pre><code>my $str = $dom-&gt;content; $dom = $dom-&gt;content(&#39;&lt;p&gt;I ♥ Mojolicious!&lt;/p&gt;&#39;); $dom = $dom-&gt;content(Mojo::DOM-&gt;new);</code></pre> <p>Return this node&#39;s content or replace it with HTML/XML fragment (for <code>root</code> and <code>tag</code> nodes) or raw content.</p> <pre><code># &quot;&lt;b&gt;Test&lt;/b&gt;&quot; $dom-&gt;parse(&#39;&lt;div&gt;&lt;b&gt;Test&lt;/b&gt;&lt;/div&gt;&#39;)-&gt;at(&#39;div&#39;)-&gt;content; # &quot;&lt;div&gt;&lt;h1&gt;123&lt;/h1&gt;&lt;/div&gt;&quot; $dom-&gt;parse(&#39;&lt;div&gt;&lt;h1&gt;Test&lt;/h1&gt;&lt;/div&gt;&#39;)-&gt;at(&#39;h1&#39;)-&gt;content(&#39;123&#39;)-&gt;root; # &quot;&lt;p&gt;&lt;i&gt;123&lt;/i&gt;&lt;/p&gt;&quot; $dom-&gt;parse(&#39;&lt;p&gt;Test&lt;/p&gt;&#39;)-&gt;at(&#39;p&#39;)-&gt;content(&#39;&lt;i&gt;123&lt;/i&gt;&#39;)-&gt;root; # &quot;&lt;div&gt;&lt;h1&gt;&lt;/h1&gt;&lt;/div&gt;&quot; $dom-&gt;parse(&#39;&lt;div&gt;&lt;h1&gt;Test&lt;/h1&gt;&lt;/div&gt;&#39;)-&gt;at(&#39;h1&#39;)-&gt;content(&#39;&#39;)-&gt;root; # &quot; Test &quot; $dom-&gt;parse(&#39;&lt;!-- Test --&gt;&lt;br&gt;&#39;)-&gt;child_nodes-&gt;first-&gt;content; # &quot;&lt;div&gt;&lt;!-- 123 --&gt;456&lt;/div&gt;&quot; $dom-&gt;parse(&#39;&lt;div&gt;&lt;!-- Test --&gt;456&lt;/div&gt;&#39;) -&gt;at(&#39;div&#39;)-&gt;child_nodes-&gt;first-&gt;content(&#39; 123 &#39;)-&gt;root;</code></pre> <h2 id="descendant_nodes"><a class="permalink" href="#descendant_nodes">#</a><a href="#toc">descendant_nodes</a></h2> <pre><code>my $collection = $dom-&gt;descendant_nodes;</code></pre> <p>Return a <a href="http://docs.mojolicious.org/Mojo/Collection">Mojo::Collection</a> object containing all descendant nodes of this element as <a href="http://docs.mojolicious.org/Mojo/DOM">Mojo::DOM</a> objects.</p> <pre><code># &quot;&lt;p&gt;&lt;b&gt;123&lt;/b&gt;&lt;/p&gt;&quot; $dom-&gt;parse(&#39;&lt;p&gt;&lt;!-- Test --&gt;&lt;b&gt;123&lt;!-- 456 --&gt;&lt;/b&gt;&lt;/p&gt;&#39;) -&gt;descendant_nodes-&gt;grep(sub { $_-&gt;type eq &#39;comment&#39; }) -&gt;map(&#39;remove&#39;)-&gt;first; # &quot;&lt;p&gt;&lt;b&gt;test&lt;/b&gt;test&lt;/p&gt;&quot; $dom-&gt;parse(&#39;&lt;p&gt;&lt;b&gt;123&lt;/b&gt;456&lt;/p&gt;&#39;) -&gt;at(&#39;p&#39;)-&gt;descendant_nodes-&gt;grep(sub { $_-&gt;type eq &#39;text&#39; }) -&gt;map(content =&gt; &#39;test&#39;)-&gt;first-&gt;root;</code></pre> <h2 id="find"><a class="permalink" href="#find">#</a><a href="#toc">find</a></h2> <pre><code>my $collection = $dom-&gt;find(&#39;div ~ p&#39;); my $collection = $dom-&gt;find(&#39;svg|line&#39;, svg =&gt; &#39;http://www.w3.org/2000/svg&#39;);</code></pre> <p>Find all descendant elements of this element matching the CSS selector and return a <a href="http://docs.mojolicious.org/Mojo/Collection">Mojo::Collection</a> object containing these elements as <a href="http://docs.mojolicious.org/Mojo/DOM">Mojo::DOM</a> objects. All selectors from <a href="http://docs.mojolicious.org/Mojo/DOM/CSS#SELECTORS">&quot;SELECTORS&quot; in Mojo::DOM::CSS</a> are supported.</p> <pre><code># Find a specific element and extract information my $id = $dom-&gt;find(&#39;div&#39;)-&gt;[23]{id}; # Extract information from multiple elements my @headers = $dom-&gt;find(&#39;h1, h2, h3&#39;)-&gt;map(&#39;text&#39;)-&gt;each; # Count all the different tags my $hash = $dom-&gt;find(&#39;*&#39;)-&gt;reduce(sub { $a-&gt;{$b-&gt;tag}++; $a }, {}); # Find elements with a class that contains dots my @divs = $dom-&gt;find(&#39;div.foo\.bar&#39;)-&gt;each;</code></pre> <p>Trailing key/value pairs can be used to declare xml namespace aliases.</p> <pre><code># &quot;&lt;rect /&gt;&quot; $dom-&gt;parse(&#39;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot;&gt;&lt;rect /&gt;&lt;/svg&gt;&#39;) -&gt;find(&#39;svg|rect&#39;, svg =&gt; &#39;http://www.w3.org/2000/svg&#39;)-&gt;first;</code></pre> <h2 id="following"><a class="permalink" href="#following">#</a><a href="#toc">following</a></h2> <pre><code>my $collection = $dom-&gt;following; my $collection = $dom-&gt;following(&#39;div ~ p&#39;);</code></pre> <p>Find all sibling elements after this node matching the CSS selector and return a <a href="http://docs.mojolicious.org/Mojo/Collection">Mojo::Collection</a> object containing these elements as <a href="http://docs.mojolicious.org/Mojo/DOM">Mojo::DOM</a> objects. All selectors from <a href="http://docs.mojolicious.org/Mojo/DOM/CSS#SELECTORS">&quot;SELECTORS&quot; in Mojo::DOM::CSS</a> are supported.</p> <pre><code># List tags of sibling elements after this node say $dom-&gt;following-&gt;map(&#39;tag&#39;)-&gt;join(&quot;\n&quot;);</code></pre> <h2 id="following_nodes"><a class="permalink" href="#following_nodes">#</a><a href="#toc">following_nodes</a></h2> <pre><code>my $collection = $dom-&gt;following_nodes;</code></pre> <p>Return a <a href="http://docs.mojolicious.org/Mojo/Collection">Mojo::Collection</a> object containing all sibling nodes after this node as <a href="http://docs.mojolicious.org/Mojo/DOM">Mojo::DOM</a> objects.</p> <pre><code># &quot;C&quot; $dom-&gt;parse(&#39;&lt;p&gt;A&lt;/p&gt;&lt;!-- B --&gt;C&#39;)-&gt;at(&#39;p&#39;)-&gt;following_nodes-&gt;last-&gt;content;</code></pre> <h2 id="matches"><a class="permalink" href="#matches">#</a><a href="#toc">matches</a></h2> <pre><code>my $bool = $dom-&gt;matches(&#39;div ~ p&#39;); my $bool = $dom-&gt;matches(&#39;svg|line&#39;, svg =&gt; &#39;http://www.w3.org/2000/svg&#39;);</code></pre> <p>Check if this element matches the CSS selector. All selectors from <a href="http://docs.mojolicious.org/Mojo/DOM/CSS#SELECTORS">&quot;SELECTORS&quot; in Mojo::DOM::CSS</a> are supported.</p> <pre><code># True $dom-&gt;parse(&#39;&lt;p class=&quot;a&quot;&gt;A&lt;/p&gt;&#39;)-&gt;at(&#39;p&#39;)-&gt;matches(&#39;.a&#39;); $dom-&gt;parse(&#39;&lt;p class=&quot;a&quot;&gt;A&lt;/p&gt;&#39;)-&gt;at(&#39;p&#39;)-&gt;matches(&#39;p[class]&#39;); # False $dom-&gt;parse(&#39;&lt;p class=&quot;a&quot;&gt;A&lt;/p&gt;&#39;)-&gt;at(&#39;p&#39;)-&gt;matches(&#39;.b&#39;); $dom-&gt;parse(&#39;&lt;p class=&quot;a&quot;&gt;A&lt;/p&gt;&#39;)-&gt;at(&#39;p&#39;)-&gt;matches(&#39;p[id]&#39;);</code></pre> <p>Trailing key/value pairs can be used to declare xml namespace aliases.</p> <pre><code># True $dom-&gt;parse(&#39;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot;&gt;&lt;rect /&gt;&lt;/svg&gt;&#39;) -&gt;matches(&#39;svg|rect&#39;, svg =&gt; &#39;http://www.w3.org/2000/svg&#39;);</code></pre> <h2 id="namespace"><a class="permalink" href="#namespace">#</a><a href="#toc">namespace</a></h2> <pre><code>my $namespace = $dom-&gt;namespace;</code></pre> <p>Find this element&#39;s namespace, or return <code>undef</code> if none could be found.</p> <pre><code># &quot;http://www.w3.org/2000/svg&quot; Mojo::DOM-&gt;new(&#39;&lt;svg xmlns:svg=&quot;http://www.w3.org/2000/svg&quot;&gt;&lt;svg:circle&gt;3.14&lt;/svg:circle&gt;&lt;/svg&gt;&#39;)-&gt;at(&#39;svg\:circle&#39;)-&gt;namespace; # Find namespace for an element with namespace prefix my $namespace = $dom-&gt;at(&#39;svg &gt; svg\:circle&#39;)-&gt;namespace; # Find namespace for an element that may or may not have a namespace prefix my $namespace = $dom-&gt;at(&#39;svg &gt; circle&#39;)-&gt;namespace;</code></pre> <h2 id="new"><a class="permalink" href="#new">#</a><a href="#toc">new</a></h2> <pre><code>my $dom = Mojo::DOM-&gt;new; my $dom = Mojo::DOM-&gt;new(&#39;&lt;foo bar=&quot;baz&quot;&gt;I ♥ Mojolicious!&lt;/foo&gt;&#39;);</code></pre> <p>Construct a new scalar-based <a href="http://docs.mojolicious.org/Mojo/DOM">Mojo::DOM</a> object and <a href="#parse">&quot;parse&quot;</a> HTML/XML fragment if necessary.</p> <h2 id="new_tag"><a class="permalink" href="#new_tag">#</a><a href="#toc">new_tag</a></h2> <pre><code>my $tag = Mojo::DOM-&gt;new_tag(&#39;div&#39;); my $tag = $dom-&gt;new_tag(&#39;div&#39;); my $tag = $dom-&gt;new_tag(&#39;div&#39;, id =&gt; &#39;foo&#39;, hidden =&gt; undef); my $tag = $dom-&gt;new_tag(&#39;div&#39;, &#39;safe content&#39;); my $tag = $dom-&gt;new_tag(&#39;div&#39;, id =&gt; &#39;foo&#39;, &#39;safe content&#39;); my $tag = $dom-&gt;new_tag(&#39;div&#39;, data =&gt; {mojo =&gt; &#39;rocks&#39;}, &#39;safe content&#39;); my $tag = $dom-&gt;new_tag(&#39;div&#39;, id =&gt; &#39;foo&#39;, sub { &#39;unsafe content&#39; });</code></pre> <p>Construct a new <a href="http://docs.mojolicious.org/Mojo/DOM">Mojo::DOM</a> object for an HTML/XML tag with or without attributes and content. The <code>data</code> attribute may contain a hash reference with key/value pairs to generate attributes from.</p> <pre><code># &quot;&lt;br&gt;&quot; $dom-&gt;new_tag(&#39;br&#39;); # &quot;&lt;div&gt;&lt;/div&gt;&quot; $dom-&gt;new_tag(&#39;div&#39;); # &quot;&lt;div id=&quot;foo&quot; hidden&gt;&lt;/div&gt;&quot; $dom-&gt;new_tag(&#39;div&#39;, id =&gt; &#39;foo&#39;, hidden =&gt; undef); # &quot;&lt;div&gt;test &amp;amp; 123&lt;/div&gt;&quot; $dom-&gt;new_tag(&#39;div&#39;, &#39;test &amp; 123&#39;); # &quot;&lt;div id=&quot;foo&quot;&gt;test &amp;amp; 123&lt;/div&gt;&quot; $dom-&gt;new_tag(&#39;div&#39;, id =&gt; &#39;foo&#39;, &#39;test &amp; 123&#39;); # &quot;&lt;div data-foo=&quot;1&quot; data-bar=&quot;test&quot;&gt;test &amp;amp; 123&lt;/div&gt;&quot;&quot; $dom-&gt;new_tag(&#39;div&#39;, data =&gt; {foo =&gt; 1, Bar =&gt; &#39;test&#39;}, &#39;test &amp; 123&#39;); # &quot;&lt;div id=&quot;foo&quot;&gt;test &amp; 123&lt;/div&gt;&quot; $dom-&gt;new_tag(&#39;div&#39;, id =&gt; &#39;foo&#39;, sub { &#39;test &amp; 123&#39; }); # &quot;&lt;div&gt;Hello&lt;b&gt;Mojo!&lt;/b&gt;&lt;/div&gt;&quot; $dom-&gt;parse(&#39;&lt;div&gt;Hello&lt;/div&gt;&#39;)-&gt;at(&#39;div&#39;) -&gt;append_content($dom-&gt;new_tag(&#39;b&#39;, &#39;Mojo!&#39;))-&gt;root;</code></pre> <h2 id="next"><a class="permalink" href="#next">#</a><a href="#toc">next</a></h2> <pre><code>my $sibling = $dom-&gt;next;</code></pre> <p>Return <a href="http://docs.mojolicious.org/Mojo/DOM">Mojo::DOM</a> object for next sibling element, or <code>undef</code> if there are no more siblings.</p> <pre><code># &quot;&lt;h2&gt;123&lt;/h2&gt;&quot; $dom-&gt;parse(&#39;&lt;div&gt;&lt;h1&gt;Test&lt;/h1&gt;&lt;h2&gt;123&lt;/h2&gt;&lt;/div&gt;&#39;)-&gt;at(&#39;h1&#39;)-&gt;next;</code></pre> <h2 id="next_node"><a class="permalink" href="#next_node">#</a><a href="#toc">next_node</a></h2> <pre><code>my $sibling = $dom-&gt;next_node;</code></pre> <p>Return <a href="http://docs.mojolicious.org/Mojo/DOM">Mojo::DOM</a> object for next sibling node, or <code>undef</code> if there are no more siblings.</p> <pre><code># &quot;456&quot; $dom-&gt;parse(&#39;&lt;p&gt;&lt;b&gt;123&lt;/b&gt;&lt;!-- Test --&gt;456&lt;/p&gt;&#39;) -&gt;at(&#39;b&#39;)-&gt;next_node-&gt;next_node; # &quot; Test &quot; $dom-&gt;parse(&#39;&lt;p&gt;&lt;b&gt;123&lt;/b&gt;&lt;!-- Test --&gt;456&lt;/p&gt;&#39;) -&gt;at(&#39;b&#39;)-&gt;next_node-&gt;content;</code></pre> <h2 id="parent"><a class="permalink" href="#parent">#</a><a href="#toc">parent</a></h2> <pre><code>my $parent = $dom-&gt;parent;</code></pre> <p>Return <a href="http://docs.mojolicious.org/Mojo/DOM">Mojo::DOM</a> object for parent of this node, or <code>undef</code> if this node has no parent.</p> <pre><code># &quot;&lt;b&gt;&lt;i&gt;Test&lt;/i&gt;&lt;/b&gt;&quot; $dom-&gt;parse(&#39;&lt;p&gt;&lt;b&gt;&lt;i&gt;Test&lt;/i&gt;&lt;/b&gt;&lt;/p&gt;&#39;)-&gt;at(&#39;i&#39;)-&gt;parent;</code></pre> <h2 id="parse"><a class="permalink" href="#parse">#</a><a href="#toc">parse</a></h2> <pre><code>$dom = $dom-&gt;parse(&#39;&lt;foo bar=&quot;baz&quot;&gt;I ♥ Mojolicious!&lt;/foo&gt;&#39;);</code></pre> <p>Parse HTML/XML fragment with <a href="http://docs.mojolicious.org/Mojo/DOM/HTML">Mojo::DOM::HTML</a>.</p> <pre><code># Parse XML my $dom = Mojo::DOM-&gt;new-&gt;xml(1)-&gt;parse(&#39;&lt;foo&gt;I ♥ Mojolicious!&lt;/foo&gt;&#39;);</code></pre> <h2 id="preceding"><a class="permalink" href="#preceding">#</a><a href="#toc">preceding</a></h2> <pre><code>my $collection = $dom-&gt;preceding; my $collection = $dom-&gt;preceding(&#39;div ~ p&#39;);</code></pre> <p>Find all sibling elements before this node matching the CSS selector and return a <a href="http://docs.mojolicious.org/Mojo/Collection">Mojo::Collection</a> object containing these elements as <a href="http://docs.mojolicious.org/Mojo/DOM">Mojo::DOM</a> objects. All selectors from <a href="http://docs.mojolicious.org/Mojo/DOM/CSS#SELECTORS">&quot;SELECTORS&quot; in Mojo::DOM::CSS</a> are supported.</p> <pre><code># List tags of sibling elements before this node say $dom-&gt;preceding-&gt;map(&#39;tag&#39;)-&gt;join(&quot;\n&quot;);</code></pre> <h2 id="preceding_nodes"><a class="permalink" href="#preceding_nodes">#</a><a href="#toc">preceding_nodes</a></h2> <pre><code>my $collection = $dom-&gt;preceding_nodes;</code></pre> <p>Return a <a href="http://docs.mojolicious.org/Mojo/Collection">Mojo::Collection</a> object containing all sibling nodes before this node as <a href="http://docs.mojolicious.org/Mojo/DOM">Mojo::DOM</a> objects.</p> <pre><code># &quot;A&quot; $dom-&gt;parse(&#39;A&lt;!-- B --&gt;&lt;p&gt;C&lt;/p&gt;&#39;)-&gt;at(&#39;p&#39;)-&gt;preceding_nodes-&gt;first-&gt;content;</code></pre> <h2 id="prepend"><a class="permalink" href="#prepend">#</a><a href="#toc">prepend</a></h2> <pre><code>$dom = $dom-&gt;prepend(&#39;&lt;p&gt;I ♥ Mojolicious!&lt;/p&gt;&#39;); $dom = $dom-&gt;prepend(Mojo::DOM-&gt;new);</code></pre> <p>Prepend HTML/XML fragment to this node (for all node types other than <code>root</code>).</p> <pre><code># &quot;&lt;div&gt;&lt;h1&gt;Test&lt;/h1&gt;&lt;h2&gt;123&lt;/h2&gt;&lt;/div&gt;&quot; $dom-&gt;parse(&#39;&lt;div&gt;&lt;h2&gt;123&lt;/h2&gt;&lt;/div&gt;&#39;) -&gt;at(&#39;h2&#39;)-&gt;prepend(&#39;&lt;h1&gt;Test&lt;/h1&gt;&#39;)-&gt;root; # &quot;&lt;p&gt;Test 123&lt;/p&gt;&quot; $dom-&gt;parse(&#39;&lt;p&gt;123&lt;/p&gt;&#39;) -&gt;at(&#39;p&#39;)-&gt;child_nodes-&gt;first-&gt;prepend(&#39;Test &#39;)-&gt;root;</code></pre> <h2 id="prepend_content"><a class="permalink" href="#prepend_content">#</a><a href="#toc">prepend_content</a></h2> <pre><code>$dom = $dom-&gt;prepend_content(&#39;&lt;p&gt;I ♥ Mojolicious!&lt;/p&gt;&#39;); $dom = $dom-&gt;prepend_content(Mojo::DOM-&gt;new);</code></pre> <p>Prepend HTML/XML fragment (for <code>root</code> and <code>tag</code> nodes) or raw content to this node&#39;s content.</p> <pre><code># &quot;&lt;div&gt;&lt;h2&gt;Test123&lt;/h2&gt;&lt;/div&gt;&quot; $dom-&gt;parse(&#39;&lt;div&gt;&lt;h2&gt;123&lt;/h2&gt;&lt;/div&gt;&#39;) -&gt;at(&#39;h2&#39;)-&gt;prepend_content(&#39;Test&#39;)-&gt;root; # &quot;&lt;!-- Test 123 --&gt;&lt;br&gt;&quot; $dom-&gt;parse(&#39;&lt;!-- 123 --&gt;&lt;br&gt;&#39;) -&gt;child_nodes-&gt;first-&gt;prepend_content(&#39; Test&#39;)-&gt;root; # &quot;&lt;p&gt;&lt;i&gt;123&lt;/i&gt;Test&lt;/p&gt;&quot; $dom-&gt;parse(&#39;&lt;p&gt;Test&lt;/p&gt;&#39;)-&gt;at(&#39;p&#39;)-&gt;prepend_content(&#39;&lt;i&gt;123&lt;/i&gt;&#39;)-&gt;root;</code></pre> <h2 id="previous"><a class="permalink" href="#previous">#</a><a href="#toc">previous</a></h2> <pre><code>my $sibling = $dom-&gt;previous;</code></pre> <p>Return <a href="http://docs.mojolicious.org/Mojo/DOM">Mojo::DOM</a> object for previous sibling element, or <code>undef</code> if there are no more siblings.</p> <pre><code># &quot;&lt;h1&gt;Test&lt;/h1&gt;&quot; $dom-&gt;parse(&#39;&lt;div&gt;&lt;h1&gt;Test&lt;/h1&gt;&lt;h2&gt;123&lt;/h2&gt;&lt;/div&gt;&#39;)-&gt;at(&#39;h2&#39;)-&gt;previous;</code></pre> <h2 id="previous_node"><a class="permalink" href="#previous_node">#</a><a href="#toc">previous_node</a></h2> <pre><code>my $sibling = $dom-&gt;previous_node;</code></pre> <p>Return <a href="http://docs.mojolicious.org/Mojo/DOM">Mojo::DOM</a> object for previous sibling node, or <code>undef</code> if there are no more siblings.</p> <pre><code># &quot;123&quot; $dom-&gt;parse(&#39;&lt;p&gt;123&lt;!-- Test --&gt;&lt;b&gt;456&lt;/b&gt;&lt;/p&gt;&#39;) -&gt;at(&#39;b&#39;)-&gt;previous_node-&gt;previous_node; # &quot; Test &quot; $dom-&gt;parse(&#39;&lt;p&gt;123&lt;!-- Test --&gt;&lt;b&gt;456&lt;/b&gt;&lt;/p&gt;&#39;) -&gt;at(&#39;b&#39;)-&gt;previous_node-&gt;content;</code></pre> <h2 id="remove"><a class="permalink" href="#remove">#</a><a href="#toc">remove</a></h2> <pre><code>my $parent = $dom-&gt;remove;</code></pre> <p>Remove this node and return <a href="#root">&quot;root&quot;</a> (for <code>root</code> nodes) or <a href="#parent">&quot;parent&quot;</a>.</p> <pre><code># &quot;&lt;div&gt;&lt;/div&gt;&quot; $dom-&gt;parse(&#39;&lt;div&gt;&lt;h1&gt;Test&lt;/h1&gt;&lt;/div&gt;&#39;)-&gt;at(&#39;h1&#39;)-&gt;remove; # &quot;&lt;p&gt;&lt;b&gt;456&lt;/b&gt;&lt;/p&gt;&quot; $dom-&gt;parse(&#39;&lt;p&gt;123&lt;b&gt;456&lt;/b&gt;&lt;/p&gt;&#39;) -&gt;at(&#39;p&#39;)-&gt;child_nodes-&gt;first-&gt;remove-&gt;root;</code></pre> <h2 id="replace"><a class="permalink" href="#replace">#</a><a href="#toc">replace</a></h2> <pre><code>my $parent = $dom-&gt;replace(&#39;&lt;div&gt;I ♥ Mojolicious!&lt;/div&gt;&#39;); my $parent = $dom-&gt;replace(Mojo::DOM-&gt;new);</code></pre> <p>Replace this node with HTML/XML fragment and return <a href="#root">&quot;root&quot;</a> (for <code>root</code> nodes) or <a href="#parent">&quot;parent&quot;</a>.</p> <pre><code># &quot;&lt;div&gt;&lt;h2&gt;123&lt;/h2&gt;&lt;/div&gt;&quot; $dom-&gt;parse(&#39;&lt;div&gt;&lt;h1&gt;Test&lt;/h1&gt;&lt;/div&gt;&#39;)-&gt;at(&#39;h1&#39;)-&gt;replace(&#39;&lt;h2&gt;123&lt;/h2&gt;&#39;); # &quot;&lt;p&gt;&lt;b&gt;123&lt;/b&gt;&lt;/p&gt;&quot; $dom-&gt;parse(&#39;&lt;p&gt;Test&lt;/p&gt;&#39;) -&gt;at(&#39;p&#39;)-&gt;child_nodes-&gt;[0]-&gt;replace(&#39;&lt;b&gt;123&lt;/b&gt;&#39;)-&gt;root;</code></pre> <h2 id="root"><a class="permalink" href="#root">#</a><a href="#toc">root</a></h2> <pre><code>my $root = $dom-&gt;root;</code></pre> <p>Return <a href="http://docs.mojolicious.org/Mojo/DOM">Mojo::DOM</a> object for <code>root</code> node.</p> <h2 id="selector"><a class="permalink" href="#selector">#</a><a href="#toc">selector</a></h2> <pre><code>my $selector = $dom-&gt;selector;</code></pre> <p>Get a unique CSS selector for this element.</p> <pre><code># &quot;ul:nth-child(1) &gt; li:nth-child(2)&quot; $dom-&gt;parse(&#39;&lt;ul&gt;&lt;li&gt;Test&lt;/li&gt;&lt;li&gt;123&lt;/li&gt;&lt;/ul&gt;&#39;)-&gt;find(&#39;li&#39;)-&gt;last-&gt;selector; # &quot;p:nth-child(1) &gt; b:nth-child(1) &gt; i:nth-child(1)&quot; $dom-&gt;parse(&#39;&lt;p&gt;&lt;b&gt;&lt;i&gt;Test&lt;/i&gt;&lt;/b&gt;&lt;/p&gt;&#39;)-&gt;at(&#39;i&#39;)-&gt;selector;</code></pre> <h2 id="strip"><a class="permalink" href="#strip">#</a><a href="#toc">strip</a></h2> <pre><code>my $parent = $dom-&gt;strip;</code></pre> <p>Remove this element while preserving its content and return <a href="#parent">&quot;parent&quot;</a>.</p> <pre><code># &quot;&lt;div&gt;Test&lt;/div&gt;&quot; $dom-&gt;parse(&#39;&lt;div&gt;&lt;h1&gt;Test&lt;/h1&gt;&lt;/div&gt;&#39;)-&gt;at(&#39;h1&#39;)-&gt;strip;</code></pre> <h2 id="tag"><a class="permalink" href="#tag">#</a><a href="#toc">tag</a></h2> <pre><code>my $tag = $dom-&gt;tag; $dom = $dom-&gt;tag(&#39;div&#39;);</code></pre> <p>This element&#39;s tag name.</p> <pre><code># List tag names of child elements say $dom-&gt;children-&gt;map(&#39;tag&#39;)-&gt;join(&quot;\n&quot;);</code></pre> <h2 id="tap"><a class="permalink" href="#tap">#</a><a href="#toc">tap</a></h2> <pre><code>$dom = $dom-&gt;tap(sub {...});</code></pre> <p>Alias for <a href="http://docs.mojolicious.org/Mojo/Base#tap">&quot;tap&quot; in Mojo::Base</a>.</p> <h2 id="text"><a class="permalink" href="#text">#</a><a href="#toc">text</a></h2> <pre><code>my $text = $dom-&gt;text;</code></pre> <p>Extract text content from this element only (not including child elements).</p> <pre><code># &quot;bar&quot; $dom-&gt;parse(&quot;&lt;div&gt;foo&lt;p&gt;bar&lt;/p&gt;baz&lt;/div&gt;&quot;)-&gt;at(&#39;p&#39;)-&gt;text; # &quot;foo\nbaz\n&quot; $dom-&gt;parse(&quot;&lt;div&gt;foo\n&lt;p&gt;bar&lt;/p&gt;baz\n&lt;/div&gt;&quot;)-&gt;at(&#39;div&#39;)-&gt;text;</code></pre> <p>To extract text content from all descendant nodes see <a href="#all_text">&quot;all_text&quot;</a>.</p> <h2 id="to_string"><a class="permalink" href="#to_string">#</a><a href="#toc">to_string</a></h2> <pre><code>my $str = $dom-&gt;to_string;</code></pre> <p>Render this node and its content to HTML/XML.</p> <pre><code># &quot;&lt;b&gt;Test&lt;/b&gt;&quot; $dom-&gt;parse(&#39;&lt;div&gt;&lt;b&gt;Test&lt;/b&gt;&lt;/div&gt;&#39;)-&gt;at(&#39;div b&#39;)-&gt;to_string;</code></pre> <h2 id="tree"><a class="permalink" href="#tree">#</a><a href="#toc">tree</a></h2> <pre><code>my $tree = $dom-&gt;tree; $dom = $dom-&gt;tree([&#39;root&#39;]);</code></pre> <p>Document Object Model. Note that this structure should only be used very carefully since it is very dynamic.</p> <h2 id="type"><a class="permalink" href="#type">#</a><a href="#toc">type</a></h2> <pre><code>my $type = $dom-&gt;type;</code></pre> <p>This node&#39;s type, usually <code>cdata</code>, <code>comment</code>, <code>doctype</code>, <code>pi</code>, <code>raw</code>, <code>root</code>, <code>tag</code> or <code>text</code>.</p> <pre><code># &quot;cdata&quot; $dom-&gt;parse(&#39;&lt;![CDATA[Test]]&gt;&#39;)-&gt;child_nodes-&gt;first-&gt;type; # &quot;comment&quot; $dom-&gt;parse(&#39;&lt;!-- Test --&gt;&#39;)-&gt;child_nodes-&gt;first-&gt;type; # &quot;doctype&quot; $dom-&gt;parse(&#39;&lt;!DOCTYPE html&gt;&#39;)-&gt;child_nodes-&gt;first-&gt;type; # &quot;pi&quot; $dom-&gt;parse(&#39;&lt;?xml version=&quot;1.0&quot;?&gt;&#39;)-&gt;child_nodes-&gt;first-&gt;type; # &quot;raw&quot; $dom-&gt;parse(&#39;&lt;title&gt;Test&lt;/title&gt;&#39;)-&gt;at(&#39;title&#39;)-&gt;child_nodes-&gt;first-&gt;type; # &quot;root&quot; $dom-&gt;parse(&#39;&lt;p&gt;Test&lt;/p&gt;&#39;)-&gt;type; # &quot;tag&quot; $dom-&gt;parse(&#39;&lt;p&gt;Test&lt;/p&gt;&#39;)-&gt;at(&#39;p&#39;)-&gt;type; # &quot;text&quot; $dom-&gt;parse(&#39;&lt;p&gt;Test&lt;/p&gt;&#39;)-&gt;at(&#39;p&#39;)-&gt;child_nodes-&gt;first-&gt;type;</code></pre> <h2 id="val"><a class="permalink" href="#val">#</a><a href="#toc">val</a></h2> <pre><code>my $value = $dom-&gt;val;</code></pre> <p>Extract value from form element (such as <code>button</code>, <code>input</code>, <code>option</code>, <code>select</code> and <code>textarea</code>), or return <code>undef</code> if this element has no value. In the case of <code>select</code> with <code>multiple</code> attribute, find <code>option</code> elements with <code>selected</code> attribute and return an array reference with all values, or <code>undef</code> if none could be found.</p> <pre><code># &quot;a&quot; $dom-&gt;parse(&#39;&lt;input name=test value=a&gt;&#39;)-&gt;at(&#39;input&#39;)-&gt;val; # &quot;b&quot; $dom-&gt;parse(&#39;&lt;textarea&gt;b&lt;/textarea&gt;&#39;)-&gt;at(&#39;textarea&#39;)-&gt;val; # &quot;c&quot; $dom-&gt;parse(&#39;&lt;option value=&quot;c&quot;&gt;Test&lt;/option&gt;&#39;)-&gt;at(&#39;option&#39;)-&gt;val; # &quot;d&quot; $dom-&gt;parse(&#39;&lt;select&gt;&lt;option selected&gt;d&lt;/option&gt;&lt;/select&gt;&#39;) -&gt;at(&#39;select&#39;)-&gt;val; # &quot;e&quot; $dom-&gt;parse(&#39;&lt;select multiple&gt;&lt;option selected&gt;e&lt;/option&gt;&lt;/select&gt;&#39;) -&gt;at(&#39;select&#39;)-&gt;val-&gt;[0]; # &quot;on&quot; $dom-&gt;parse(&#39;&lt;input name=test type=checkbox&gt;&#39;)-&gt;at(&#39;input&#39;)-&gt;val;</code></pre> <h2 id="with_roles"><a class="permalink" href="#with_roles">#</a><a href="#toc">with_roles</a></h2> <pre><code>my $new_class = Mojo::DOM-&gt;with_roles(&#39;Mojo::DOM::Role::One&#39;); my $new_class = Mojo::DOM-&gt;with_roles(&#39;+One&#39;, &#39;+Two&#39;); $dom = $dom-&gt;with_roles(&#39;+One&#39;, &#39;+Two&#39;);</code></pre> <p>Alias for <a href="http://docs.mojolicious.org/Mojo/Base#with_roles">&quot;with_roles&quot; in Mojo::Base</a>.</p> <h2 id="wrap"><a class="permalink" href="#wrap">#</a><a href="#toc">wrap</a></h2> <pre><code>$dom = $dom-&gt;wrap(&#39;&lt;div&gt;&lt;/div&gt;&#39;); $dom = $dom-&gt;wrap(Mojo::DOM-&gt;new);</code></pre> <p>Wrap HTML/XML fragment around this node (for all node types other than <code>root</code>), placing it as the last child of the first innermost element.</p> <pre><code># &quot;&lt;p&gt;123&lt;b&gt;Test&lt;/b&gt;&lt;/p&gt;&quot; $dom-&gt;parse(&#39;&lt;b&gt;Test&lt;/b&gt;&#39;)-&gt;at(&#39;b&#39;)-&gt;wrap(&#39;&lt;p&gt;123&lt;/p&gt;&#39;)-&gt;root; # &quot;&lt;div&gt;&lt;p&gt;&lt;b&gt;Test&lt;/b&gt;&lt;/p&gt;123&lt;/div&gt;&quot; $dom-&gt;parse(&#39;&lt;b&gt;Test&lt;/b&gt;&#39;)-&gt;at(&#39;b&#39;)-&gt;wrap(&#39;&lt;div&gt;&lt;p&gt;&lt;/p&gt;123&lt;/div&gt;&#39;)-&gt;root; # &quot;&lt;p&gt;&lt;b&gt;Test&lt;/b&gt;&lt;/p&gt;&lt;p&gt;123&lt;/p&gt;&quot; $dom-&gt;parse(&#39;&lt;b&gt;Test&lt;/b&gt;&#39;)-&gt;at(&#39;b&#39;)-&gt;wrap(&#39;&lt;p&gt;&lt;/p&gt;&lt;p&gt;123&lt;/p&gt;&#39;)-&gt;root; # &quot;&lt;p&gt;&lt;b&gt;Test&lt;/b&gt;&lt;/p&gt;&quot; $dom-&gt;parse(&#39;&lt;p&gt;Test&lt;/p&gt;&#39;)-&gt;at(&#39;p&#39;)-&gt;child_nodes-&gt;first-&gt;wrap(&#39;&lt;b&gt;&#39;)-&gt;root;</code></pre> <h2 id="wrap_content"><a class="permalink" href="#wrap_content">#</a><a href="#toc">wrap_content</a></h2> <pre><code>$dom = $dom-&gt;wrap_content(&#39;&lt;div&gt;&lt;/div&gt;&#39;); $dom = $dom-&gt;wrap_content(Mojo::DOM-&gt;new);</code></pre> <p>Wrap HTML/XML fragment around this node&#39;s content (for <code>root</code> and <code>tag</code> nodes), placing it as the last children of the first innermost element.</p> <pre><code># &quot;&lt;p&gt;&lt;b&gt;123Test&lt;/b&gt;&lt;/p&gt;&quot; $dom-&gt;parse(&#39;&lt;p&gt;Test&lt;p&gt;&#39;)-&gt;at(&#39;p&#39;)-&gt;wrap_content(&#39;&lt;b&gt;123&lt;/b&gt;&#39;)-&gt;root; # &quot;&lt;p&gt;&lt;b&gt;Test&lt;/b&gt;&lt;/p&gt;&lt;p&gt;123&lt;/p&gt;&quot; $dom-&gt;parse(&#39;&lt;b&gt;Test&lt;/b&gt;&#39;)-&gt;wrap_content(&#39;&lt;p&gt;&lt;/p&gt;&lt;p&gt;123&lt;/p&gt;&#39;);</code></pre> <h2 id="xml"><a class="permalink" href="#xml">#</a><a href="#toc">xml</a></h2> <pre><code>my $bool = $dom-&gt;xml; $dom = $dom-&gt;xml($bool);</code></pre> <p>Disable HTML semantics in parser and activate case-sensitivity, defaults to auto-detection based on XML declarations.</p> <h1 id="OPERATORS"><a class="permalink" href="#OPERATORS">#</a><a href="#toc">OPERATORS</a></h1> <p><a href="http://docs.mojolicious.org/Mojo/DOM">Mojo::DOM</a> overloads the following operators.</p> <h2 id="array"><a class="permalink" href="#array">#</a><a href="#toc">array</a></h2> <pre><code>my @nodes = @$dom;</code></pre> <p>Alias for <a href="#child_nodes">&quot;child_nodes&quot;</a>.</p> <pre><code># &quot;&lt;!-- Test --&gt;&quot; $dom-&gt;parse(&#39;&lt;!-- Test --&gt;&lt;b&gt;123&lt;/b&gt;&#39;)-&gt;[0];</code></pre> <h2 id="bool"><a class="permalink" href="#bool">#</a><a href="#toc">bool</a></h2> <pre><code>my $bool = !!$dom;</code></pre> <p>Always true.</p> <h2 id="hash"><a class="permalink" href="#hash">#</a><a href="#toc">hash</a></h2> <pre><code>my %attrs = %$dom;</code></pre> <p>Alias for <a href="#attr">&quot;attr&quot;</a>.</p> <pre><code># &quot;test&quot; $dom-&gt;parse(&#39;&lt;div id=&quot;test&quot;&gt;Test&lt;/div&gt;&#39;)-&gt;at(&#39;div&#39;)-&gt;{id};</code></pre> <h2 id="stringify"><a class="permalink" href="#stringify">#</a><a href="#toc">stringify</a></h2> <pre><code>my $str = &quot;$dom&quot;;</code></pre> <p>Alias for <a href="#to_string">&quot;to_string&quot;</a>.</p> <h1 id="SEE-ALSO"><a class="permalink" href="#SEE-ALSO">#</a><a href="#toc">SEE ALSO</a></h1> <p><a href="http://docs.mojolicious.org/Mojolicious">Mojolicious</a>, <a href="http://docs.mojolicious.org/Mojolicious/Guides">Mojolicious::Guides</a>, <a href="https://mojolicious.org">https://mojolicious.org</a>.</p> </div> </main> </div> </div> <footer> <div class="container-fluid p-3 mojo-footer"> <div class="row"> <div class="col-sm align-self-center text-center mojo-free"> <b>Free</b> and <b>Open Source</b>. </div> <div class="col-sm align-self-center text-center mojo-copy"> <i class="far fa-copyright"></i> 2008-2023 Sebastian Riedel and the <a href="https://docs.mojolicious.org/Mojolicious#AUTHORS">Mojolicious contributors</a>. </div> <div class="col-sm align-self-center text-center mojo-social"> <a alt="GitHub" href="https://github.com/mojolicious/mojo"><i class="fab fa-github-alt"></i></a> <a alt="Mastodon" rel="me" href="https://fosstodon.org/@mojolicious"><i class="fab fa-mastodon"></i></a> <a alt="LinkedIn" href="https://www.linkedin.com/groups/8963713/"><i class="fab fa-linkedin"></i></a> </div> </div> </div> </footer> <script> const links = document.querySelectorAll('.mojo-sidebar ul li a[href^="#"]'); const linkById = Object.fromEntries(Array.from(links).map(e => [e.getAttribute('href').substr(1), e])); const sections = Array.from(links).map(e => document.getElementById(e.getAttribute('href').substr(1))).reverse(); let lastSectionId; let mojoOnScroll = () => { const pos = (document.documentElement.scrollTop || document.body.scrollTop) - 63; const section = sections.find(e => e.offsetTop <= pos); if (!section) return; if (section.id == lastSectionId) return; lastSectionId = section.id; links.forEach(e => e.classList.remove('font-weight-bold')); linkById[section.id].classList.add('font-weight-bold'); }; window.onscroll = mojoOnScroll; mojoOnScroll(); </script> </body> </html>

Pages: 1 2 3 4 5 6 7 8 9 10