5 =for Pod::Coverage TO_JSON
9 DOM::Tiny - Minimalistic HTML/XML DOM parser with CSS selectors
16 my $dom = DOM::Tiny->new('<div><p id="a">Test</p><p id="b">123</p></div>');
19 say $dom->at('#b')->text;
20 say $dom->find('p')->map('text')->join("\n");
21 say $dom->find('[id]')->map(attr => 'id')->join("\n");
24 $dom->find('p[id]')->reverse->each(sub { say $_->{id} });
27 for my $e ($dom->find('p[id]')->each) {
28 say $e->{id}, ':', $e->text;
32 $dom->find('div p')->last->append('<p id="c">456</p>');
33 $dom->find(':not(p)')->map('strip');
40 L<DOM::Tiny> is a minimalistic and relaxed pure-perl HTML/XML DOM parser with
41 support for the L<HTML Living Standard|https://html.spec.whatwg.org/> and
42 L<CSS3 selectors|http://www.w3.org/TR/selectors/> based on L<Mojo::DOM>. It
43 will even try to interpret broken HTML and XML, so you should not use it for
46 =head1 NODES AND ELEMENTS
48 When we parse an HTML/XML fragment, it gets turned into a tree of nodes.
52 <head><title>Hello</title></head>
56 There are currently eight different kinds of nodes, C<cdata>, C<comment>,
57 C<doctype>, C<pi>, C<raw>, C<root>, C<tag> and C<text>. Elements are nodes of
69 While all node types are represented as L<DOM::Tiny> objects, some methods like
70 L</"attr"> and L</"namespace"> only apply to elements.
72 =head1 CASE-SENSITIVITY
74 L<DOM::Tiny> defaults to HTML semantics, that means all tags and attribute
75 names are lowercased and selectors need to be lowercase as well.
78 my $dom = DOM::Tiny->new('<P ID="greeting">Hi!</P>');
79 say $dom->at('p[id]')->text;
81 If XML processing instructions are found, the parser will automatically switch
82 into XML mode and everything becomes case-sensitive.
85 my $dom = DOM::Tiny->new('<?xml version="1.0"?><P ID="greeting">Hi!</P>');
86 say $dom->at('P[ID]')->text;
88 XML detection can also be disabled with the L</"xml"> method.
91 my $dom = DOM::Tiny->new->xml(1)->parse('<P ID="greeting">Hi!</P>');
92 say $dom->at('P[ID]')->text;
94 # Force HTML semantics
95 my $dom = DOM::Tiny->new->xml(0)->parse('<P ID="greeting">Hi!</P>');
96 say $dom->at('p[id]')->text;
100 L<DOM::Tiny> implements the following methods.
104 my $dom = DOM::Tiny->new;
105 my $dom = DOM::Tiny->new('<foo bar="baz">I ♥ DOM::Tiny!</foo>');
107 Construct a new scalar-based L<DOM::Tiny> object and L</"parse"> HTML/XML
108 fragment if necessary.
112 my $trimmed = $dom->all_text;
113 my $untrimmed = $dom->all_text(0);
115 Extract text content from all descendant nodes of this element, smart
116 whitespace trimming is enabled by default.
119 $dom->parse("<div>foo\n<p>bar</p>baz\n</div>")->at('div')->all_text;
122 $dom->parse("<div>foo\n<p>bar</p>baz\n</div>")->at('div')->all_text(0);
126 my $collection = $dom->ancestors;
127 my $collection = $dom->ancestors('div ~ p');
129 Find all ancestor elements of this node matching the CSS selector and return a
130 L<DOM::Tiny::Collection> object containing these elements as L<DOM::Tiny>
131 objects. All selectors from L<DOM::Tiny::CSS/"SELECTORS"> are supported.
133 # List tag names of ancestor elements
134 say $dom->ancestors->map('tag')->join("\n");
138 $dom = $dom->append('<p>I ♥ DOM::Tiny!</p>');
140 Append HTML/XML fragment to this node.
142 # "<div><h1>Test</h1><h2>123</h2></div>"
143 $dom->parse('<div><h1>Test</h1></div>')
144 ->at('h1')->append('<h2>123</h2>')->root;
147 $dom->parse('<p>Test</p>')->at('p')
148 ->child_nodes->first->append(' 123')->root;
150 =head2 append_content
152 $dom = $dom->append_content('<p>I ♥ DOM::Tiny!</p>');
154 Append HTML/XML fragment (for C<root> and C<tag> nodes) or raw content to this
157 # "<div><h1>Test123</h1></div>"
158 $dom->parse('<div><h1>Test</h1></div>')
159 ->at('h1')->append_content('123')->root;
161 # "<!-- Test 123 --><br>"
162 $dom->parse('<!-- Test --><br>')
163 ->child_nodes->first->append_content('123 ')->root;
165 # "<p>Test<i>123</i></p>"
166 $dom->parse('<p>Test</p>')->at('p')->append_content('<i>123</i>')->root;
170 my $result = $dom->at('div ~ p');
172 Find first descendant element of this element matching the CSS selector and
173 return it as a L<DOM::Tiny> object or return C<undef> if none could be found.
174 All selectors from L<DOM::Tiny::CSS/"SELECTORS"> are supported.
176 # Find first element with "svg" namespace definition
177 my $namespace = $dom->at('[xmlns\:svg]')->{'xmlns:svg'};
181 my $hash = $dom->attr;
182 my $foo = $dom->attr('foo');
183 $dom = $dom->attr({foo => 'bar'});
184 $dom = $dom->attr(foo => 'bar');
186 This element's attributes.
188 # Remove an attribute
189 delete $dom->attr->{id};
191 # Attribute without value
192 $dom->attr(selected => undef);
195 say $dom->find('*')->map(attr => 'id')->compact->join("\n");
199 my $collection = $dom->child_nodes;
201 Return a L<DOM::Tiny::Collection> object containing all child nodes of this
202 element as L<DOM::Tiny> objects.
204 # "<p><b>123</b></p>"
205 $dom->parse('<p>Test<b>123</b></p>')->at('p')->child_nodes->first->remove;
208 $dom->parse('<!DOCTYPE html><b>123</b>')->child_nodes->first;
211 $dom->parse('<b>123</b><!-- Test -->')->child_nodes->last->content;
215 my $collection = $dom->children;
216 my $collection = $dom->children('div ~ p');
218 Find all child elements of this element matching the CSS selector and return a
219 L<DOM::Tiny::Collection> object containing these elements as L<DOM::Tiny>
220 objects. All selectors from L<DOM::Tiny::CSS/"SELECTORS"> are supported.
222 # Show tag name of random child element
223 say $dom->children->shuffle->first->tag;
227 my $str = $dom->content;
228 $dom = $dom->content('<p>I ♥ DOM::Tiny!</p>');
230 Return this node's content or replace it with HTML/XML fragment (for C<root>
231 and C<tag> nodes) or raw content.
234 $dom->parse('<div><b>Test</b></div>')->at('div')->content;
236 # "<div><h1>123</h1></div>"
237 $dom->parse('<div><h1>Test</h1></div>')->at('h1')->content('123')->root;
239 # "<p><i>123</i></p>"
240 $dom->parse('<p>Test</p>')->at('p')->content('<i>123</i>')->root;
242 # "<div><h1></h1></div>"
243 $dom->parse('<div><h1>Test</h1></div>')->at('h1')->content('')->root;
246 $dom->parse('<!-- Test --><br>')->child_nodes->first->content;
248 # "<div><!-- 123 -->456</div>"
249 $dom->parse('<div><!-- Test -->456</div>')
250 ->at('div')->child_nodes->first->content(' 123 ')->root;
252 =head2 descendant_nodes
254 my $collection = $dom->descendant_nodes;
256 Return a L<DOM::Tiny::Collection> object containing all descendant nodes of
257 this element as L<DOM::Tiny> objects.
259 # "<p><b>123</b></p>"
260 $dom->parse('<p><!-- Test --><b>123<!-- 456 --></b></p>')
261 ->descendant_nodes->grep(sub { $_->type eq 'comment' })
262 ->map('remove')->first;
264 # "<p><b>test</b>test</p>"
265 $dom->parse('<p><b>123</b>456</p>')
266 ->at('p')->descendant_nodes->grep(sub { $_->type eq 'text' })
267 ->map(content => 'test')->first->root;
271 my $collection = $dom->find('div ~ p');
273 Find all descendant elements of this element matching the CSS selector and
274 return a L<DOM::Tiny::Collection> object containing these elements as
275 L<DOM::Tiny> objects. All selectors from L<DOM::Tiny::CSS/"SELECTORS"> are
278 # Find a specific element and extract information
279 my $id = $dom->find('div')->[23]{id};
281 # Extract information from multiple elements
282 my @headers = $dom->find('h1, h2, h3')->map('text')->each;
284 # Count all the different tags
285 my $hash = $dom->find('*')->reduce(sub { $a->{$b->tag}++; $a }, {});
287 # Find elements with a class that contains dots
288 my @divs = $dom->find('div.foo\.bar')->each;
292 my $collection = $dom->following;
293 my $collection = $dom->following('div ~ p');
295 Find all sibling elements after this node matching the CSS selector and return
296 a L<DOM::Tiny::Collection> object containing these elements as L<DOM::Tiny>
297 objects. All selectors from L<DOM::Tiny::CSS/"SELECTORS"> are supported.
299 # List tags of sibling elements after this node
300 say $dom->following->map('tag')->join("\n");
302 =head2 following_nodes
304 my $collection = $dom->following_nodes;
306 Return a L<DOM::Tiny::Collection> object containing all sibling nodes after
307 this node as L<DOM::Tiny> objects.
310 $dom->parse('<p>A</p><!-- B -->C')->at('p')->following_nodes->last->content;
314 my $bool = $dom->matches('div ~ p');
316 Check if this element matches the CSS selector. All selectors from
317 L<DOM::Tiny::CSS/"SELECTORS"> are supported.
320 $dom->parse('<p class="a">A</p>')->at('p')->matches('.a');
321 $dom->parse('<p class="a">A</p>')->at('p')->matches('p[class]');
324 $dom->parse('<p class="a">A</p>')->at('p')->matches('.b');
325 $dom->parse('<p class="a">A</p>')->at('p')->matches('p[id]');
329 my $namespace = $dom->namespace;
331 Find this element's namespace or return C<undef> if none could be found.
333 # Find namespace for an element with namespace prefix
334 my $namespace = $dom->at('svg > svg\:circle')->namespace;
336 # Find namespace for an element that may or may not have a namespace prefix
337 my $namespace = $dom->at('svg > circle')->namespace;
341 my $sibling = $dom->next;
343 Return L<DOM::Tiny> object for next sibling element or C<undef> if there are no
347 $dom->parse('<div><h1>Test</h1><h2>123</h2></div>')->at('h1')->next;
351 my $sibling = $dom->next_node;
353 Return L<DOM::Tiny> object for next sibling node or C<undef> if there are no
357 $dom->parse('<p><b>123</b><!-- Test -->456</p>')
358 ->at('b')->next_node->next_node;
361 $dom->parse('<p><b>123</b><!-- Test -->456</p>')
362 ->at('b')->next_node->content;
366 my $parent = $dom->parent;
368 Return L<DOM::Tiny> object for parent of this node or C<undef> if this node has
373 $dom = $dom->parse('<foo bar="baz">I ♥ DOM::Tiny!</foo>');
375 Parse HTML/XML fragment with L<DOM::Tiny::HTML>.
378 my $dom = DOM::Tiny->new->xml(1)->parse($xml);
382 my $collection = $dom->preceding;
383 my $collection = $dom->preceding('div ~ p');
385 Find all sibling elements before this node matching the CSS selector and return
386 a L<DOM::Tiny::Collection> object containing these elements as L<DOM::Tiny>
387 objects. All selectors from L<DOM::Tiny::CSS/"SELECTORS"> are supported.
389 # List tags of sibling elements before this node
390 say $dom->preceding->map('tag')->join("\n");
392 =head2 preceding_nodes
394 my $collection = $dom->preceding_nodes;
396 Return a L<DOM::Tiny::Collection> object containing all sibling nodes before
397 this node as L<DOM::Tiny> objects.
400 $dom->parse('A<!-- B --><p>C</p>')->at('p')->preceding_nodes->first->content;
404 $dom = $dom->prepend('<p>I ♥ DOM::Tiny!</p>');
406 Prepend HTML/XML fragment to this node.
408 # "<div><h1>Test</h1><h2>123</h2></div>"
409 $dom->parse('<div><h2>123</h2></div>')
410 ->at('h2')->prepend('<h1>Test</h1>')->root;
413 $dom->parse('<p>123</p>')
414 ->at('p')->child_nodes->first->prepend('Test ')->root;
416 =head2 prepend_content
418 $dom = $dom->prepend_content('<p>I ♥ DOM::Tiny!</p>');
420 Prepend HTML/XML fragment (for C<root> and C<tag> nodes) or raw content to this
423 # "<div><h2>Test123</h2></div>"
424 $dom->parse('<div><h2>123</h2></div>')
425 ->at('h2')->prepend_content('Test')->root;
427 # "<!-- Test 123 --><br>"
428 $dom->parse('<!-- 123 --><br>')
429 ->child_nodes->first->prepend_content(' Test')->root;
431 # "<p><i>123</i>Test</p>"
432 $dom->parse('<p>Test</p>')->at('p')->prepend_content('<i>123</i>')->root;
436 my $sibling = $dom->previous;
438 Return L<DOM::Tiny> object for previous sibling element or C<undef> if there
439 are no more siblings.
442 $dom->parse('<div><h1>Test</h1><h2>123</h2></div>')->at('h2')->previous;
446 my $sibling = $dom->previous_node;
448 Return L<DOM::Tiny> object for previous sibling node or C<undef> if there are
452 $dom->parse('<p>123<!-- Test --><b>456</b></p>')
453 ->at('b')->previous_node->previous_node;
456 $dom->parse('<p>123<!-- Test --><b>456</b></p>')
457 ->at('b')->previous_node->content;
461 my $parent = $dom->remove;
463 Remove this node and return L</"root"> (for C<root> nodes) or L</"parent">.
466 $dom->parse('<div><h1>Test</h1></div>')->at('h1')->remove;
468 # "<p><b>456</b></p>"
469 $dom->parse('<p>123<b>456</b></p>')
470 ->at('p')->child_nodes->first->remove->root;
474 my $parent = $dom->replace('<div>I ♥ DOM::Tiny!</div>');
476 Replace this node with HTML/XML fragment and return L</"root"> (for C<root>
477 nodes) or L</"parent">.
479 # "<div><h2>123</h2></div>"
480 $dom->parse('<div><h1>Test</h1></div>')->at('h1')->replace('<h2>123</h2>');
482 # "<p><b>123</b></p>"
483 $dom->parse('<p>Test</p>')
484 ->at('p')->child_nodes->[0]->replace('<b>123</b>')->root;
488 my $root = $dom->root;
490 Return L<DOM::Tiny> object for C<root> node.
494 my $parent = $dom->strip;
496 Remove this element while preserving its content and return L</"parent">.
499 $dom->parse('<div><h1>Test</h1></div>')->at('h1')->strip;
504 $dom = $dom->tag('div');
506 This element's tag name.
508 # List tag names of child elements
509 say $dom->children->map('tag')->join("\n");
513 $dom = $dom->tap(sub {...});
515 Equivalent to L<Mojo::Base/"tap">.
519 my $trimmed = $dom->text;
520 my $untrimmed = $dom->text(0);
522 Extract text content from this element only (not including child elements),
523 smart whitespace trimming is enabled by default.
526 $dom->parse("<div>foo\n<p>bar</p>baz\n</div>")->at('div')->text;
529 $dom->parse("<div>foo\n<p>bar</p>baz\n</div>")->at('div')->text(0);
533 my $str = $dom->to_string;
535 Render this node and its content to HTML/XML.
538 $dom->parse('<div><b>Test</b></div>')->at('div b')->to_string;
542 my $tree = $dom->tree;
543 $dom = $dom->tree(['root']);
545 Document Object Model. Note that this structure should only be used very
546 carefully since it is very dynamic.
550 my $type = $dom->type;
552 This node's type, usually C<cdata>, C<comment>, C<doctype>, C<pi>, C<raw>,
553 C<root>, C<tag> or C<text>.
556 $dom->parse('<![CDATA[Test]]>')->child_nodes->first->type;
559 $dom->parse('<!-- Test -->')->child_nodes->first->type;
562 $dom->parse('<!DOCTYPE html>')->child_nodes->first->type;
565 $dom->parse('<?xml version="1.0"?>')->child_nodes->first->type;
568 $dom->parse('<title>Test</title>')->at('title')->child_nodes->first->type;
571 $dom->parse('<p>Test</p>')->type;
574 $dom->parse('<p>Test</p>')->at('p')->type;
577 $dom->parse('<p>Test</p>')->at('p')->child_nodes->first->type;
581 my $value = $dom->val;
583 Extract value from form element (such as C<button>, C<input>, C<option>,
584 C<select> and C<textarea>) or return C<undef> if this element has no value. In
585 the case of C<select> with C<multiple> attribute, find C<option> elements with
586 C<selected> attribute and return an array reference with all values or C<undef>
587 if none could be found.
590 $dom->parse('<input name="test" value="a">')->at('input')->val;
593 $dom->parse('<textarea>b</textarea>')->at('textarea')->val;
596 $dom->parse('<option value="c">Test</option>')->at('option')->val;
599 $dom->parse('<select><option selected>d</option></select>')
603 $dom->parse('<select multiple><option selected>e</option></select>')
604 ->at('select')->val->[0];
608 $dom = $dom->wrap('<div></div>');
610 Wrap HTML/XML fragment around this node, placing it as the last child of the
611 first innermost element.
613 # "<p>123<b>Test</b></p>"
614 $dom->parse('<b>Test</b>')->at('b')->wrap('<p>123</p>')->root;
616 # "<div><p><b>Test</b></p>123</div>"
617 $dom->parse('<b>Test</b>')->at('b')->wrap('<div><p></p>123</div>')->root;
619 # "<p><b>Test</b></p><p>123</p>"
620 $dom->parse('<b>Test</b>')->at('b')->wrap('<p></p><p>123</p>')->root;
622 # "<p><b>Test</b></p>"
623 $dom->parse('<p>Test</p>')->at('p')->child_nodes->first->wrap('<b>')->root;
627 $dom = $dom->wrap_content('<div></div>');
629 Wrap HTML/XML fragment around this node's content, placing it as the last
630 children of the first innermost element.
632 # "<p><b>123Test</b></p>"
633 $dom->parse('<p>Test<p>')->at('p')->wrap_content('<b>123</b>')->root;
635 # "<p><b>Test</b></p><p>123</p>"
636 $dom->parse('<b>Test</b>')->wrap_content('<p></p><p>123</p>');
640 my $bool = $dom->xml;
641 $dom = $dom->xml($bool);
643 Disable HTML semantics in parser and activate case-sensitivity, defaults to
644 auto detection based on processing instructions.
648 L<DOM::Tiny> overloads the following operators.
654 Alias for L</"child_nodes">.
657 $dom->parse('<!-- Test --><b>123</b>')->[0];
669 Alias for L</"attr">.
672 $dom->parse('<div id="test">Test</div>')->at('div')->{id};
678 Alias for L</"to_string">.
680 =head1 COLLECTION METHODS
682 Some L<DOM::Tiny> methods return an array-based collection object, which can
683 either be accessed directly as an array reference, or with the following
687 $collection->map(sub { ucfirst })->shuffle->each(sub {
688 my ($word, $num) = @_;
692 # Access array directly to manipulate collection
693 $collection->[23] += 100;
694 say for @$collection;
698 my $new = $collection->compact;
700 Create a new collection with all elements that are defined and not an empty
703 # $collection contains (0, 1, undef, 2, '', 3)
704 $collection->compact->join(', '); # "0, 1, 2, 3"
708 my @elements = $collection->each;
709 $collection = $collection->each(sub {...});
711 Evaluate callback for each element in collection or return all elements as a
712 list if none has been provided. The element will be the first argument passed
713 to the callback and is also available as C<$_>.
715 # Make a numbered list
716 $collection->each(sub {
723 my $first = $collection->first;
724 my $first = $collection->first(qr/foo/);
725 my $first = $collection->first(sub {...});
726 my $first = $collection->first($method);
727 my $first = $collection->first($method, @args);
729 Evaluate regular expression/callback for, or call method on, each element in
730 collection and return the first one that matched the regular expression, or for
731 which the callback/method returned true. The element will be the first argument
732 passed to the callback and is also available as C<$_>.
735 my $first = $collection->first(sub { $_->$method(@args) });
737 # Find first value that contains the word "dom"
738 my $interesting = $collection->first(qr/dom/i);
740 # Find first value that is greater than 5
741 my $greater = $collection->first(sub { $_ > 5 });
745 my $new = $collection->flatten;
747 Flatten nested collections/arrays recursively and create a new collection with
750 # $collection contains (1, [2, [3, 4], 5, [6]], 7)
751 $collection->flatten->join(', '); # "1, 2, 3, 4, 5, 6, 7"
755 my $new = $collection->grep(qr/foo/);
756 my $new = $collection->grep(sub {...});
757 my $new = $collection->grep($method);
758 my $new = $collection->grep($method, @args);
760 Evaluate regular expression/callback for, or call method on, each element in
761 collection and create a new collection with all elements that matched the
762 regular expression, or for which the callback/method returned true. The element
763 will be the first argument passed to the callback and is also available as
767 my $new = $collection->grep(sub { $_->$method(@args) });
769 # Find all values that contain the word "dom"
770 my $interesting = $collection->grep(qr/dom/i);
772 # Find all values that are greater than 5
773 my $greater = $collection->grep(sub { $_ > 5 });
777 my $stream = $collection->join;
778 my $stream = $collection->join("\n");
780 Turn collection into string.
782 # Join all values with commas
783 $collection->join(', ');
787 my $last = $collection->last;
789 Return the last element in collection.
793 my $new = $collection->map(sub {...});
794 my $new = $collection->map($method);
795 my $new = $collection->map($method, @args);
797 Evaluate callback for, or call method on, each element in collection and create
798 a new collection from the results. The element will be the first argument
799 passed to the callback and is also available as C<$_>.
802 my $new = $collection->map(sub { $_->$method(@args) });
804 # Append the word "dom" to all values
805 my $domified = $collection->map(sub { $_ . 'dom' });
809 my $result = $collection->reduce(sub {...});
810 my $result = $collection->reduce(sub {...}, $initial);
812 Reduce elements in collection with callback, the first element will be used as
813 initial value if none has been provided.
815 # Calculate the sum of all values
816 my $sum = $collection->reduce(sub { $a + $b });
818 # Count how often each value occurs in collection
819 my $hash = $collection->reduce(sub { $a->{$b}++; $a }, {});
823 my $new = $collection->reverse;
825 Create a new collection with all elements in reverse order.
829 my $new = $collection->slice(4 .. 7);
831 Create a new collection with all selected elements.
833 # $collection contains ('A', 'B', 'C', 'D', 'E')
834 $collection->slice(1, 2, 4)->join(' '); # "B C E"
838 my $new = $collection->shuffle;
840 Create a new collection with all elements in random order.
844 my $size = $collection->size;
846 Number of elements in collection.
850 my $new = $collection->sort;
851 my $new = $collection->sort(sub {...});
853 Sort elements based on return value of callback and create a new collection
856 # Sort values case-insensitive
857 my $case_insensitive = $collection->sort(sub { uc($a) cmp uc($b) });
861 $collection = $collection->tap(sub {...});
863 Equivalent to L<Mojo::Base/"tap">.
867 my $array = $collection->to_array;
869 Turn collection into array reference.
873 my $new = $collection->uniq;
874 my $new = $collection->uniq(sub {...});
875 my $new = $collection->uniq($method);
876 my $new = $collection->uniq($method, @args);
878 Create a new collection without duplicate elements, using the string
879 representation of either the elements or the return value of the
883 my $new = $collection->uniq(sub { $_->$method(@args) });
885 # $collection contains ('foo', 'bar', 'bar', 'baz')
886 $collection->uniq->join(' '); # "foo bar baz"
888 # $collection contains ([1, 2], [2, 1], [3, 2])
889 $collection->uniq(sub{ $_->[1] })->to_array; # "[[1, 2], [2, 1]]"
893 Report any issues on the public bugtracker.
897 Dan Book <dbook@cpan.org>
899 =head1 COPYRIGHT AND LICENSE
901 This software is Copyright (c) 2015 by Dan Book.
903 This is free software, licensed under:
905 The Artistic License 2.0 (GPL Compatible)
909 L<Mojo::DOM>, L<XML::LibXML>, L<XML::Twig>, L<HTML::TreeBuilder>, L<XML::Smart>