.\" -*- mode: troff; coding: utf-8 -*- .\" Automatically generated by Pod::Man 5.01 (Pod::Simple 3.43) .\" .\" Standard preamble: .\" ======================================================================== .de Sp \" Vertical space (when we can't use .PP) .if t .sp .5v .if n .sp .. .de Vb \" Begin verbatim text .ft CW .nf .ne \\$1 .. .de Ve \" End verbatim text .ft R .fi .. .\" \*(C` and \*(C' are quotes in nroff, nothing in troff, for use with C<>. .ie n \{\ . ds C` "" . ds C' "" 'br\} .el\{\ . ds C` . ds C' 'br\} .\" .\" Escape single quotes in literal strings from groff's Unicode transform. .ie \n(.g .ds Aq \(aq .el .ds Aq ' .\" .\" If the F register is >0, we'll generate index entries on stderr for .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index .\" entries marked with X<> in POD. Of course, you'll have to process the .\" output yourself in some meaningful fashion. .\" .\" Avoid warning from groff about undefined register 'F'. .de IX .. .nr rF 0 .if \n(.g .if rF .nr rF 1 .if (\n(rF:(\n(.g==0)) \{\ . if \nF \{\ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . if !\nF==2 \{\ . nr % 0 . nr F 2 . \} . \} .\} .rr rF .\" ======================================================================== .\" .IX Title "Mojo::DOM 3" .TH Mojo::DOM 3 2024-03-22 "perl v5.38.2" "User Contributed Perl Documentation" .\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" way too many mistakes in technical documents. .if n .ad l .nh .SH NAME Mojo::DOM \- Minimalistic HTML/XML DOM parser with CSS selectors .SH SYNOPSIS .IX Header "SYNOPSIS" .Vb 1 \& use Mojo::DOM; \& \& # Parse \& my $dom = Mojo::DOM\->new(\*(Aq
Test
123
456
\*(Aq); \& $dom\->at(\*(Aq#c\*(Aq)\->prepend($dom\->new_tag(\*(Aqp\*(Aq, id => \*(Aqd\*(Aq, \*(Aq789\*(Aq)); \& $dom\->find(\*(Aq:not(p)\*(Aq)\->map(\*(Aqstrip\*(Aq); \& \& # Render \& say "$dom"; .Ve .SH DESCRIPTION .IX Header "DESCRIPTION" Mojo::DOM 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. .SH "NODES AND ELEMENTS" .IX Header "NODES AND ELEMENTS" When we parse an HTML/XML fragment, it gets turned into a tree of nodes. .PP .Vb 5 \& \& \&Hi!
\*(Aq); \& say $dom\->at(\*(Aqp[id]\*(Aq)\->text; .Ve .PP If an XML declaration is found, the parser will automatically switch into XML mode and everything becomes case-sensitive. .PP .Vb 3 \& # XML semantics \& my $dom = Mojo::DOM\->new(\*(AqHi!
\*(Aq); \& say $dom\->at(\*(AqP[ID]\*(Aq)\->text; .Ve .PP HTML or XML semantics can also be forced with the "xml" method. .PP .Vb 3 \& # Force HTML semantics \& my $dom = Mojo::DOM\->new\->xml(0)\->parse(\*(AqHi!
\*(Aq); \& say $dom\->at(\*(Aqp[id]\*(Aq)\->text; \& \& # Force XML semantics \& my $dom = Mojo::DOM\->new\->xml(1)\->parse(\*(AqHi!
\*(Aq); \& say $dom\->at(\*(AqP[ID]\*(Aq)\->text; .Ve .SH METHODS .IX Header "METHODS" Mojo::DOM implements the following methods. .SS all_text .IX Subsection "all_text" .Vb 1 \& my $text = $dom\->all_text; .Ve .PP Extract text content from all descendant nodes of this element. For HTML documents \f(CW\*(C`script\*(C'\fR and \f(CW\*(C`style\*(C'\fR elements are excluded. .PP .Vb 2 \& # "foo\enbarbaz\en" \& $dom\->parse("bar
baz\enI ♥ Mojolicious!
\*(Aq); \& $dom = $dom\->append(Mojo::DOM\->new); .Ve .PP Append HTML/XML fragment to this node (for all node types other than \f(CW\*(C`root\*(C'\fR). .PP .Vb 3 \& # "Test 123
" \& $dom\->parse(\*(AqTest
\*(Aq)\->at(\*(Aqp\*(Aq) \& \->child_nodes\->first\->append(\*(Aq 123\*(Aq)\->root; .Ve .SS append_content .IX Subsection "append_content" .Vb 2 \& $dom = $dom\->append_content(\*(AqI ♥ Mojolicious!
\*(Aq); \& $dom = $dom\->append_content(Mojo::DOM\->new); .Ve .PP Append HTML/XML fragment (for \f(CW\*(C`root\*(C'\fR and \f(CW\*(C`tag\*(C'\fR nodes) or raw content to this node's content. .PP .Vb 3 \& # "Test123
" \& $dom\->parse(\*(AqTest
\*(Aq)\->at(\*(Aqp\*(Aq)\->append_content(\*(Aq123\*(Aq)\->root; .Ve .SS at .IX Subsection "at" .Vb 2 \& my $result = $dom\->at(\*(Aqdiv ~ p\*(Aq); \& my $result = $dom\->at(\*(Aqsvg|line\*(Aq, svg => \*(Aqhttp://www.w3.org/2000/svg\*(Aq); .Ve .PP Find first descendant element of this element matching the CSS selector and return it as a Mojo::DOM object, or \&\f(CW\*(C`undef\*(C'\fR if none could be found. All selectors from "SELECTORS" in Mojo::DOM::CSS are supported. .PP .Vb 2 \& # Find first element with "svg" namespace definition \& my $namespace = $dom\->at(\*(Aq[xmlns\e:svg]\*(Aq)\->{\*(Aqxmlns:svg\*(Aq}; .Ve .PP Trailing key/value pairs can be used to declare xml namespace aliases. .PP .Vb 3 \& # "123
" \& $dom\->parse(\*(AqTest123
\*(Aq)\->at(\*(Aqp\*(Aq)\->child_nodes\->first\->remove; \& \& # "" \& $dom\->parse(\*(Aq123\*(Aq)\->child_nodes\->first; \& \& # " Test " \& $dom\->parse(\*(Aq123\*(Aq)\->child_nodes\->last\->content; .Ve .SS children .IX Subsection "children" .Vb 2 \& my $collection = $dom\->children; \& my $collection = $dom\->children(\*(Aqdiv ~ p\*(Aq); .Ve .PP Find all child elements of this element matching the CSS selector and return a Mojo::Collection object containing these elements as Mojo::DOM objects. All selectors from "SELECTORS" in Mojo::DOM::CSS are supported. .PP .Vb 2 \& # Show tag name of random child element \& say $dom\->children\->shuffle\->first\->tag; .Ve .SS content .IX Subsection "content" .Vb 3 \& my $str = $dom\->content; \& $dom = $dom\->content(\*(AqI ♥ Mojolicious!
\*(Aq); \& $dom = $dom\->content(Mojo::DOM\->new); .Ve .PP Return this node's content or replace it with HTML/XML fragment (for \f(CW\*(C`root\*(C'\fR and \f(CW\*(C`tag\*(C'\fR nodes) or raw content. .PP .Vb 2 \& # "Test" \& $dom\->parse(\*(Aq123
" \& $dom\->parse(\*(AqTest
\*(Aq)\->at(\*(Aqp\*(Aq)\->content(\*(Aq123\*(Aq)\->root; \& \& # "123
" \& $dom\->parse(\*(Aq123
\*(Aq) \& \->descendant_nodes\->grep(sub { $_\->type eq \*(Aqcomment\*(Aq }) \& \->map(\*(Aqremove\*(Aq)\->first; \& \& # "testtest
" \& $dom\->parse(\*(Aq123456
\*(Aq) \& \->at(\*(Aqp\*(Aq)\->descendant_nodes\->grep(sub { $_\->type eq \*(Aqtext\*(Aq }) \& \->map(content => \*(Aqtest\*(Aq)\->first\->root; .Ve .SS find .IX Subsection "find" .Vb 2 \& my $collection = $dom\->find(\*(Aqdiv ~ p\*(Aq); \& my $collection = $dom\->find(\*(Aqsvg|line\*(Aq, svg => \*(Aqhttp://www.w3.org/2000/svg\*(Aq); .Ve .PP Find all descendant elements of this element matching the CSS selector and return a Mojo::Collection object containing these elements as Mojo::DOM objects. All selectors from "SELECTORS" in Mojo::DOM::CSS are supported. .PP .Vb 2 \& # Find a specific element and extract information \& my $id = $dom\->find(\*(Aqdiv\*(Aq)\->[23]{id}; \& \& # Extract information from multiple elements \& my @headers = $dom\->find(\*(Aqh1, h2, h3\*(Aq)\->map(\*(Aqtext\*(Aq)\->each; \& \& # Count all the different tags \& my $hash = $dom\->find(\*(Aq*\*(Aq)\->reduce(sub { $a\->{$b\->tag}++; $a }, {}); \& \& # Find elements with a class that contains dots \& my @divs = $dom\->find(\*(Aqdiv.foo\e.bar\*(Aq)\->each; .Ve .PP Trailing key/value pairs can be used to declare xml namespace aliases. .PP .Vb 3 \& # "A
C\*(Aq)\->at(\*(Aqp\*(Aq)\->following_nodes\->last\->content; .Ve .SS matches .IX Subsection "matches" .Vb 2 \& my $bool = $dom\->matches(\*(Aqdiv ~ p\*(Aq); \& my $bool = $dom\->matches(\*(Aqsvg|line\*(Aq, svg => \*(Aqhttp://www.w3.org/2000/svg\*(Aq); .Ve .PP Check if this element matches the CSS selector. All selectors from "SELECTORS" in Mojo::DOM::CSS are supported. .PP .Vb 3 \& # True \& $dom\->parse(\*(AqA
\*(Aq)\->at(\*(Aqp\*(Aq)\->matches(\*(Aq.a\*(Aq); \& $dom\->parse(\*(AqA
\*(Aq)\->at(\*(Aqp\*(Aq)\->matches(\*(Aqp[class]\*(Aq); \& \& # False \& $dom\->parse(\*(AqA
\*(Aq)\->at(\*(Aqp\*(Aq)\->matches(\*(Aq.b\*(Aq); \& $dom\->parse(\*(AqA
\*(Aq)\->at(\*(Aqp\*(Aq)\->matches(\*(Aqp[id]\*(Aq); .Ve .PP Trailing key/value pairs can be used to declare xml namespace aliases. .PP .Vb 3 \& # True \& $dom\->parse(\*(Aq\*(Aq) \& \->matches(\*(Aqsvg|rect\*(Aq, svg => \*(Aqhttp://www.w3.org/2000/svg\*(Aq); .Ve .SS namespace .IX Subsection "namespace" .Vb 1 \& my $namespace = $dom\->namespace; .Ve .PP Find this element's namespace, or return \f(CW\*(C`undef\*(C'\fR if none could be found. .PP .Vb 2 \& # "http://www.w3.org/2000/svg" \& Mojo::DOM\->new(\*(Aq\*(Aq)\->at(\*(Aqsvg\e:circle\*(Aq)\->namespace; \& \& # Find namespace for an element with namespace prefix \& my $namespace = $dom\->at(\*(Aqsvg > svg\e:circle\*(Aq)\->namespace; \& \& # Find namespace for an element that may or may not have a namespace prefix \& my $namespace = $dom\->at(\*(Aqsvg > circle\*(Aq)\->namespace; .Ve .SS new .IX Subsection "new" .Vb 2 \& my $dom = Mojo::DOM\->new; \& my $dom = Mojo::DOM\->new(\*(Aq123456
\*(Aq) \& \->at(\*(Aqb\*(Aq)\->next_node\->next_node; \& \& # " Test " \& $dom\->parse(\*(Aq123456
\*(Aq) \& \->at(\*(Aqb\*(Aq)\->next_node\->content; .Ve .SS parent .IX Subsection "parent" .Vb 1 \& my $parent = $dom\->parent; .Ve .PP Return Mojo::DOM object for parent of this node, or \f(CW\*(C`undef\*(C'\fR if this node has no parent. .PP .Vb 2 \& # "Test" \& $dom\->parse(\*(AqTest
\*(Aq)\->at(\*(Aqi\*(Aq)\->parent; .Ve .SS parse .IX Subsection "parse" .Vb 1 \& $dom = $dom\->parse(\*(AqC
\*(Aq)\->at(\*(Aqp\*(Aq)\->preceding_nodes\->first\->content; .Ve .SS prepend .IX Subsection "prepend" .Vb 2 \& $dom = $dom\->prepend(\*(AqI ♥ Mojolicious!
\*(Aq); \& $dom = $dom\->prepend(Mojo::DOM\->new); .Ve .PP Prepend HTML/XML fragment to this node (for all node types other than \f(CW\*(C`root\*(C'\fR). .PP .Vb 3 \& # "Test 123
" \& $dom\->parse(\*(Aq123
\*(Aq) \& \->at(\*(Aqp\*(Aq)\->child_nodes\->first\->prepend(\*(AqTest \*(Aq)\->root; .Ve .SS prepend_content .IX Subsection "prepend_content" .Vb 2 \& $dom = $dom\->prepend_content(\*(AqI ♥ Mojolicious!
\*(Aq); \& $dom = $dom\->prepend_content(Mojo::DOM\->new); .Ve .PP Prepend HTML/XML fragment (for \f(CW\*(C`root\*(C'\fR and \f(CW\*(C`tag\*(C'\fR nodes) or raw content to this node's content. .PP .Vb 3 \& # "123Test
" \& $dom\->parse(\*(AqTest
\*(Aq)\->at(\*(Aqp\*(Aq)\->prepend_content(\*(Aq123\*(Aq)\->root; .Ve .SS previous .IX Subsection "previous" .Vb 1 \& my $sibling = $dom\->previous; .Ve .PP Return Mojo::DOM object for previous sibling element, or \f(CW\*(C`undef\*(C'\fR if there are no more siblings. .PP .Vb 2 \& # "123456
\*(Aq) \& \->at(\*(Aqb\*(Aq)\->previous_node\->previous_node; \& \& # " Test " \& $dom\->parse(\*(Aq123456
\*(Aq) \& \->at(\*(Aqb\*(Aq)\->previous_node\->content; .Ve .SS remove .IX Subsection "remove" .Vb 1 \& my $parent = $dom\->remove; .Ve .PP Remove this node and return "root" (for \f(CW\*(C`root\*(C'\fR nodes) or "parent". .PP .Vb 2 \& # "" \& $dom\->parse(\*(Aq456
" \& $dom\->parse(\*(Aq123456
\*(Aq) \& \->at(\*(Aqp\*(Aq)\->child_nodes\->first\->remove\->root; .Ve .SS replace .IX Subsection "replace" .Vb 2 \& my $parent = $dom\->replace(\*(Aq123
" \& $dom\->parse(\*(AqTest
\*(Aq) \& \->at(\*(Aqp\*(Aq)\->child_nodes\->[0]\->replace(\*(Aq123\*(Aq)\->root; .Ve .SS root .IX Subsection "root" .Vb 1 \& my $root = $dom\->root; .Ve .PP Return Mojo::DOM object for \f(CW\*(C`root\*(C'\fR node. .SS selector .IX Subsection "selector" .Vb 1 \& my $selector = $dom\->selector; .Ve .PP Get a unique CSS selector for this element. .PP .Vb 2 \& # "ul:nth\-child(1) > li:nth\-child(2)" \& $dom\->parse(\*(AqTest
\*(Aq)\->at(\*(Aqi\*(Aq)\->selector; .Ve .SS strip .IX Subsection "strip" .Vb 1 \& my $parent = $dom\->strip; .Ve .PP Remove this element while preserving its content and return "parent". .PP .Vb 2 \& # "bar
bazbar
baz\enTest
\*(Aq)\->type; \& \& # "tag" \& $dom\->parse(\*(AqTest
\*(Aq)\->at(\*(Aqp\*(Aq)\->type; \& \& # "text" \& $dom\->parse(\*(AqTest
\*(Aq)\->at(\*(Aqp\*(Aq)\->child_nodes\->first\->type; .Ve .SS val .IX Subsection "val" .Vb 1 \& my $value = $dom\->val; .Ve .PP Extract value from form element (such as \f(CW\*(C`button\*(C'\fR, \f(CW\*(C`input\*(C'\fR, \f(CW\*(C`option\*(C'\fR, \f(CW\*(C`select\*(C'\fR and \f(CW\*(C`textarea\*(C'\fR), or return \f(CW\*(C`undef\*(C'\fR if this element has no value. In the case of \f(CW\*(C`select\*(C'\fR with \f(CW\*(C`multiple\*(C'\fR attribute, find \f(CW\*(C`option\*(C'\fR elements with \&\f(CW\*(C`selected\*(C'\fR attribute and return an array reference with all values, or \f(CW\*(C`undef\*(C'\fR if none could be found. .PP .Vb 2 \& # "a" \& $dom\->parse(\*(Aq\*(Aq)\->at(\*(Aqinput\*(Aq)\->val; \& \& # "b" \& $dom\->parse(\*(Aq\*(Aq)\->at(\*(Aqtextarea\*(Aq)\->val; \& \& # "c" \& $dom\->parse(\*(Aq\*(Aq)\->at(\*(Aqoption\*(Aq)\->val; \& \& # "d" \& $dom\->parse(\*(Aq\*(Aq) \& \->at(\*(Aqselect\*(Aq)\->val; \& \& # "e" \& $dom\->parse(\*(Aq\*(Aq) \& \->at(\*(Aqselect\*(Aq)\->val\->[0]; \& \& # "on" \& $dom\->parse(\*(Aq\*(Aq)\->at(\*(Aqinput\*(Aq)\->val; .Ve .SS with_roles .IX Subsection "with_roles" .Vb 3 \& my $new_class = Mojo::DOM\->with_roles(\*(AqMojo::DOM::Role::One\*(Aq); \& my $new_class = Mojo::DOM\->with_roles(\*(Aq+One\*(Aq, \*(Aq+Two\*(Aq); \& $dom = $dom\->with_roles(\*(Aq+One\*(Aq, \*(Aq+Two\*(Aq); .Ve .PP Alias for "with_roles" in Mojo::Base. .SS wrap .IX Subsection "wrap" .Vb 2 \& $dom = $dom\->wrap(\*(Aq\*(Aq); \& $dom = $dom\->wrap(Mojo::DOM\->new); .Ve .PP Wrap HTML/XML fragment around this node (for all node types other than \f(CW\*(C`root\*(C'\fR), placing it as the last child of the first innermost element. .PP .Vb 2 \& # "123Test
" \& $dom\->parse(\*(AqTest\*(Aq)\->at(\*(Aqb\*(Aq)\->wrap(\*(Aq123
\*(Aq)\->root; \& \& # "Test
123Test
123
" \& $dom\->parse(\*(AqTest\*(Aq)\->at(\*(Aqb\*(Aq)\->wrap(\*(Aq123
\*(Aq)\->root; \& \& # "Test
" \& $dom\->parse(\*(AqTest
\*(Aq)\->at(\*(Aqp\*(Aq)\->child_nodes\->first\->wrap(\*(Aq\*(Aq)\->root; .Ve .SS wrap_content .IX Subsection "wrap_content" .Vb 2 \& $dom = $dom\->wrap_content(\*(Aq\*(Aq); \& $dom = $dom\->wrap_content(Mojo::DOM\->new); .Ve .PP Wrap HTML/XML fragment around this node's content (for \f(CW\*(C`root\*(C'\fR and \f(CW\*(C`tag\*(C'\fR nodes), placing it as the last children of the first innermost element. .PP .Vb 2 \& # "123Test
" \& $dom\->parse(\*(AqTest
\*(Aq)\->at(\*(Aqp\*(Aq)\->wrap_content(\*(Aq123\*(Aq)\->root; \& \& # "
Test
123
" \& $dom\->parse(\*(AqTest\*(Aq)\->wrap_content(\*(Aq123
\*(Aq); .Ve .SS xml .IX Subsection "xml" .Vb 2 \& my $bool = $dom\->xml; \& $dom = $dom\->xml($bool); .Ve .PP Disable HTML semantics in parser and activate case-sensitivity, defaults to auto-detection based on XML declarations. .SH OPERATORS .IX Header "OPERATORS" Mojo::DOM overloads the following operators. .SS array .IX Subsection "array" .Vb 1 \& my @nodes = @$dom; .Ve .PP Alias for "child_nodes". .PP .Vb 2 \& # "" \& $dom\->parse(\*(Aq123\*(Aq)\->[0]; .Ve .SS bool .IX Subsection "bool" .Vb 1 \& my $bool = !!$dom; .Ve .PP Always true. .SS hash .IX Subsection "hash" .Vb 1 \& my %attrs = %$dom; .Ve .PP Alias for "attr". .PP .Vb 2 \& # "test" \& $dom\->parse(\*(Aq