7 '@{}' => sub { shift->child_nodes },
8 '%{}' => sub { shift->attr },
10 '""' => sub { shift->to_string },
14 use DOM::Tiny::_Collection;
17 use Scalar::Util qw(blessed weaken);
19 our $VERSION = '0.003';
23 my $self = bless \DOM::Tiny::_HTML->new, ref $class || $class;
24 return @_ ? $self->parse(@_) : $self;
27 sub TO_JSON { shift->_delegate('render') }
29 sub all_text { shift->_all_text(1, @_) }
31 sub ancestors { _select($_[0]->_collect($_[0]->_ancestors), $_[1]) }
33 sub append { shift->_add(1, @_) }
34 sub append_content { shift->_content(1, 0, @_) }
38 return undef unless my $result = $self->_css->select_one(@_);
39 return $self->_build($result, $self->xml);
46 my $tree = $self->tree;
47 my $attrs = $tree->[0] ne 'tag' ? {} : $tree->[2];
48 return $attrs unless @_;
51 return $attrs->{$_[0]} unless @_ > 1 || ref $_[0];
54 my $values = ref $_[0] ? $_[0] : {@_};
55 @$attrs{keys %$values} = values %$values;
60 sub child_nodes { $_[0]->_collect(_nodes($_[0]->tree)) }
62 sub children { _select($_[0]->_collect(_nodes($_[0]->tree, 1)), $_[1]) }
67 my $type = $self->type;
68 if ($type eq 'root' || $type eq 'tag') {
69 return $self->_content(0, 1, @_) if @_;
70 my $html = DOM::Tiny::_HTML->new(xml => $self->xml);
71 return join '', map { $html->tree($_)->render } _nodes($self->tree);
74 return $self->tree->[1] unless @_;
75 $self->tree->[1] = shift;
79 sub descendant_nodes { $_[0]->_collect(_all(_nodes($_[0]->tree))) }
81 sub find { $_[0]->_collect(@{$_[0]->_css->select($_[1])}) }
83 sub following { _select($_[0]->_collect(@{$_[0]->_siblings(1)->[1]}), $_[1]) }
84 sub following_nodes { $_[0]->_collect(@{$_[0]->_siblings->[1]}) }
86 sub matches { shift->_css->matches(@_) }
91 return undef if (my $tree = $self->tree)->[0] ne 'tag';
93 # Extract namespace prefix and search parents
94 my $ns = $tree->[1] =~ /^(.*?):/ ? "xmlns:$1" : undef;
95 for my $node ($tree, $self->_ancestors) {
97 # Namespace for prefix
98 my $attrs = $node->[2];
99 if ($ns) { $_ eq $ns and return $attrs->{$_} for keys %$attrs }
101 # Namespace attribute
102 elsif (defined $attrs->{xmlns}) { return $attrs->{xmlns} }
108 sub next { $_[0]->_maybe($_[0]->_siblings(1, 0)->[1]) }
109 sub next_node { $_[0]->_maybe($_[0]->_siblings(0, 0)->[1]) }
113 return undef if $self->tree->[0] eq 'root';
114 return $self->_build($self->_parent, $self->xml);
117 sub parse { shift->_delegate(parse => @_) }
119 sub preceding { _select($_[0]->_collect(@{$_[0]->_siblings(1)->[0]}), $_[1]) }
120 sub preceding_nodes { $_[0]->_collect(@{$_[0]->_siblings->[0]}) }
122 sub prepend { shift->_add(0, @_) }
123 sub prepend_content { shift->_content(0, 0, @_) }
125 sub previous { $_[0]->_maybe($_[0]->_siblings(1, -1)->[0]) }
126 sub previous_node { $_[0]->_maybe($_[0]->_siblings(0, -1)->[0]) }
128 sub remove { shift->replace('') }
131 my ($self, $new) = @_;
132 return $self->parse($new) if (my $tree = $self->tree)->[0] eq 'root';
133 return $self->_replace($self->_parent, $tree, _nodes($self->_parse($new)));
138 return $self unless my $tree = $self->_ancestors(1);
139 return $self->_build($tree, $self->xml);
144 return $self if (my $tree = $self->tree)->[0] ne 'tag';
145 return $self->_replace($tree->[3], $tree, _nodes($tree));
149 my ($self, $tag) = @_;
150 return undef if (my $tree = $self->tree)->[0] ne 'tag';
151 return $tree->[1] unless $tag;
156 sub tap { DOM::Tiny::_Collection::tap(@_) }
158 sub text { shift->_all_text(0, @_) }
160 sub to_string { shift->_delegate('render') }
162 sub tree { shift->_delegate(tree => @_) }
164 sub type { shift->tree->[0] }
170 return defined($self->{value}) ? $self->{value} : $self->text
171 if (my $tag = $self->tag) eq 'option';
173 # "input" ("type=checkbox" and "type=radio")
174 my $type = $self->{type} || '';
175 return defined $self->{value} ? $self->{value} : 'on'
176 if $tag eq 'input' && ($type eq 'radio' || $type eq 'checkbox');
178 # "textarea", "input" or "button"
179 return $tag eq 'textarea' ? $self->text : $self->{value} if $tag ne 'select';
182 my $v = $self->find('option:checked')->map('val');
183 return exists $self->{multiple} ? $v->size ? $v->to_array : undef : $v->last;
186 sub wrap { shift->_wrap(0, @_) }
187 sub wrap_content { shift->_wrap(1, @_) }
189 sub xml { shift->_delegate(xml => @_) }
192 my ($self, $offset, $new) = @_;
194 return $self if (my $tree = $self->tree)->[0] eq 'root';
196 my $parent = $self->_parent;
197 splice @$parent, _offset($parent, $tree) + $offset, 0,
198 _link($parent, _nodes($self->_parse($new)));
204 map { $_->[0] eq 'tag' ? ($_, _all(_nodes($_))) : ($_) } @_;
208 my ($self, $recurse, $trim) = @_;
211 my $tree = $self->tree;
212 $trim = 1 unless defined $trim;
213 map { $_->[1] eq 'pre' and $trim = 0 } $self->_ancestors, $tree
214 if $trim && $tree->[0] ne 'root';
216 return _text([_nodes($tree)], $recurse, $trim);
220 my ($self, $root) = @_;
222 return () unless my $tree = $self->_parent;
224 do { push @ancestors, $tree }
225 while ($tree->[0] eq 'tag') && ($tree = $tree->[3]);
226 return $root ? $ancestors[-1] : @ancestors[0 .. $#ancestors - 1];
229 sub _build { shift->new->tree(shift)->xml(shift) }
233 my $xml = $self->xml;
234 return DOM::Tiny::_Collection->new(map { $self->_build($_, $xml) } @_);
238 my ($self, $start, $offset, $new) = @_;
240 my $tree = $self->tree;
241 unless ($tree->[0] eq 'root' || $tree->[0] eq 'tag') {
242 my $old = $self->content;
243 return $self->content($start ? $old . $new : $new . $old);
246 $start = $start ? ($#$tree + 1) : _start($tree);
247 $offset = $offset ? $#$tree : 0;
248 splice @$tree, $start, $offset, _link($tree, _nodes($self->_parse($new)));
253 sub _css { DOM::Tiny::_CSS->new(tree => shift->tree) }
256 my ($self, $method) = (shift, shift);
257 return $$self->$method unless @_;
263 my ($parent, @children) = @_;
265 # Link parent to children
266 for my $node (@children) {
267 my $offset = $node->[0] eq 'tag' ? 3 : 2;
268 $node->[$offset] = $parent;
269 weaken $node->[$offset];
275 sub _maybe { $_[1] ? $_[0]->_build($_[1], $_[0]->xml) : undef }
278 return () unless my $tree = shift;
279 my @nodes = @$tree[_start($tree) .. $#$tree];
280 return shift() ? grep { $_->[0] eq 'tag' } @nodes : @nodes;
284 my ($parent, $child) = @_;
285 my $i = _start($parent);
286 $_ eq $child ? last : $i++ for @$parent[$i .. $#$parent];
290 sub _parent { $_[0]->tree->[$_[0]->type eq 'tag' ? 3 : 2] }
292 sub _parse { DOM::Tiny::_HTML->new(xml => shift->xml)->parse(shift)->tree }
295 my ($self, $parent, $child, @nodes) = @_;
296 splice @$parent, _offset($parent, $child), 1, _link($parent, @nodes);
297 return $self->parent;
301 my ($collection, $selector) = @_;
302 return $collection unless $selector;
303 return $collection->new(grep { $_->matches($selector) } @$collection);
307 my ($self, $tags, $i) = @_;
309 return [] unless my $parent = $self->parent;
311 my $tree = $self->tree;
312 my (@before, @after, $match);
313 for my $node (_nodes($parent->tree)) {
314 ++$match and next if !$match && $node eq $tree;
315 next if $tags && $node->[0] ne 'tag';
316 $match ? push @after, $node : push @before, $node;
319 return defined $i ? [$before[$i], $after[$i]] : [\@before, \@after];
330 sub _start { $_[0][0] eq 'root' ? 1 : 4 }
333 my ($nodes, $recurse, $trim) = @_;
335 # Merge successive text nodes
337 while (my $next = $nodes->[$i + 1]) {
338 ++$i and next unless $nodes->[$i][0] eq 'text' && $next->[0] eq 'text';
339 splice @$nodes, $i, 2, ['text', $nodes->[$i][1] . $next->[1]];
343 for my $node (@$nodes) {
344 my $type = $node->[0];
348 if ($type eq 'text') { $chunk = $trim ? _squish $node->[1] : $node->[1] }
351 elsif ($type eq 'cdata' || $type eq 'raw') { $chunk = $node->[1] }
354 elsif ($type eq 'tag' && $recurse) {
355 no warnings 'recursion';
356 $chunk = _text([_nodes($node)], 1, $node->[1] eq 'pre' ? 0 : $trim);
359 # Add leading whitespace if punctuation allows it
360 $chunk = " $chunk" if $text =~ /\S\z/ && $chunk =~ /^[^.!?,;:\s]+/;
362 # Trim whitespace blocks
363 $text .= $chunk if $chunk =~ /\S+/ || !$trim;
370 my ($self, $content, $new) = @_;
372 return $self if (my $tree = $self->tree)->[0] eq 'root' && !$content;
373 return $self if $tree->[0] ne 'root' && $tree->[0] ne 'tag' && $content;
377 my $first = $new = $self->_parse($new);
378 $current = $first while $first = (_nodes($first, 1))[0];
379 return $self unless $current;
383 push @$current, _link($current, _nodes($tree));
384 splice @$tree, _start($tree), $#$tree, _link($tree, _nodes($new));
389 $self->_replace($self->_parent, $tree, _nodes($new));
390 push @$current, _link($current, $tree);
400 DOM::Tiny - Minimalistic HTML/XML DOM parser with CSS selectors
407 my $dom = DOM::Tiny->new('<div><p id="a">Test</p><p id="b">123</p></div>');
410 say $dom->at('#b')->text;
411 say $dom->find('p')->map('text')->join("\n");
412 say $dom->find('[id]')->map(attr => 'id')->join("\n");
415 $dom->find('p[id]')->reverse->each(sub { say $_->{id} });
418 for my $e ($dom->find('p[id]')->each) {
419 say $e->{id}, ':', $e->text;
423 $dom->find('div p')->last->append('<p id="c">456</p>');
424 $dom->find(':not(p)')->map('strip');
431 L<DOM::Tiny> is a minimalistic and relaxed pure-perl HTML/XML DOM parser based
432 on L<Mojo::DOM>. It supports the L<HTML Living Standard|https://html.spec.whatwg.org/>
433 and L<Extensible Markup Language (XML) 1.0|http://www.w3.org/TR/xml/>, and
434 matching based on L<CSS3 selectors|http://www.w3.org/TR/selectors/>. It will
435 even try to interpret broken HTML and XML, so you should not use it for
438 =head1 NODES AND ELEMENTS
440 When we parse an HTML/XML fragment, it gets turned into a tree of nodes.
444 <head><title>Hello</title></head>
448 There are currently eight different kinds of nodes, C<cdata>, C<comment>,
449 C<doctype>, C<pi>, C<raw>, C<root>, C<tag> and C<text>. Elements are nodes of
461 While all node types are represented as L<DOM::Tiny> objects, some methods like
462 L</"attr"> and L</"namespace"> only apply to elements.
464 =head1 CASE-SENSITIVITY
466 L<DOM::Tiny> defaults to HTML semantics, that means all tags and attribute
467 names are lowercased and selectors need to be lowercase as well.
470 my $dom = DOM::Tiny->new('<P ID="greeting">Hi!</P>');
471 say $dom->at('p[id]')->text;
473 If an XML declaration is found, the parser will automatically switch into XML
474 mode and everything becomes case-sensitive.
477 my $dom = DOM::Tiny->new('<?xml version="1.0"?><P ID="greeting">Hi!</P>');
478 say $dom->at('P[ID]')->text;
480 XML detection can also be disabled with the L</"xml"> method.
482 # Force XML semantics
483 my $dom = DOM::Tiny->new->xml(1)->parse('<P ID="greeting">Hi!</P>');
484 say $dom->at('P[ID]')->text;
486 # Force HTML semantics
487 my $dom = DOM::Tiny->new->xml(0)->parse('<P ID="greeting">Hi!</P>');
488 say $dom->at('p[id]')->text;
492 L<DOM::Tiny> uses a CSS selector engine based on L<Mojo::DOM::CSS>. All CSS
493 selectors that make sense for a standalone parser are supported.
501 my $all = $dom->find('*');
505 An element of type C<E>.
507 my $title = $dom->at('title');
511 An C<E> element with a C<foo> attribute.
513 my $links = $dom->find('a[href]');
517 An C<E> element whose C<foo> attribute value is exactly equal to C<bar>.
519 my $case_sensitive = $dom->find('input[type="hidden"]');
520 my $case_sensitive = $dom->find('input[type=hidden]');
524 An C<E> element whose C<foo> attribute value is exactly equal to any
525 (ASCII-range) case-permutation of C<bar>. Note that this selector is
526 EXPERIMENTAL and might change without warning!
528 my $case_insensitive = $dom->find('input[type="hidden" i]');
529 my $case_insensitive = $dom->find('input[type=hidden i]');
530 my $case_insensitive = $dom->find('input[class~="foo" i]');
532 This selector is part of
533 L<Selectors Level 4|http://dev.w3.org/csswg/selectors-4>, which is still a work
538 An C<E> element whose C<foo> attribute value is a list of whitespace-separated
539 values, one of which is exactly equal to C<bar>.
541 my $foo = $dom->find('input[class~="foo"]');
542 my $foo = $dom->find('input[class~=foo]');
546 An C<E> element whose C<foo> attribute value begins exactly with the string
549 my $begins_with = $dom->find('input[name^="f"]');
550 my $begins_with = $dom->find('input[name^=f]');
554 An C<E> element whose C<foo> attribute value ends exactly with the string
557 my $ends_with = $dom->find('input[name$="o"]');
558 my $ends_with = $dom->find('input[name$=o]');
562 An C<E> element whose C<foo> attribute value contains the substring C<bar>.
564 my $contains = $dom->find('input[name*="fo"]');
565 my $contains = $dom->find('input[name*=fo]');
569 An C<E> element, root of the document.
571 my $root = $dom->at(':root');
575 An C<E> element, the C<n-th> child of its parent.
577 my $third = $dom->find('div:nth-child(3)');
578 my $odd = $dom->find('div:nth-child(odd)');
579 my $even = $dom->find('div:nth-child(even)');
580 my $top3 = $dom->find('div:nth-child(-n+3)');
582 =item E:nth-last-child(n)
584 An C<E> element, the C<n-th> child of its parent, counting from the last one.
586 my $third = $dom->find('div:nth-last-child(3)');
587 my $odd = $dom->find('div:nth-last-child(odd)');
588 my $even = $dom->find('div:nth-last-child(even)');
589 my $bottom3 = $dom->find('div:nth-last-child(-n+3)');
591 =item E:nth-of-type(n)
593 An C<E> element, the C<n-th> sibling of its type.
595 my $third = $dom->find('div:nth-of-type(3)');
596 my $odd = $dom->find('div:nth-of-type(odd)');
597 my $even = $dom->find('div:nth-of-type(even)');
598 my $top3 = $dom->find('div:nth-of-type(-n+3)');
600 =item E:nth-last-of-type(n)
602 An C<E> element, the C<n-th> sibling of its type, counting from the last one.
604 my $third = $dom->find('div:nth-last-of-type(3)');
605 my $odd = $dom->find('div:nth-last-of-type(odd)');
606 my $even = $dom->find('div:nth-last-of-type(even)');
607 my $bottom3 = $dom->find('div:nth-last-of-type(-n+3)');
611 An C<E> element, first child of its parent.
613 my $first = $dom->find('div p:first-child');
617 An C<E> element, last child of its parent.
619 my $last = $dom->find('div p:last-child');
621 =item E:first-of-type
623 An C<E> element, first sibling of its type.
625 my $first = $dom->find('div p:first-of-type');
629 An C<E> element, last sibling of its type.
631 my $last = $dom->find('div p:last-of-type');
635 An C<E> element, only child of its parent.
637 my $lonely = $dom->find('div p:only-child');
641 An C<E> element, only sibling of its type.
643 my $lonely = $dom->find('div p:only-of-type');
647 An C<E> element that has no children (including text nodes).
649 my $empty = $dom->find(':empty');
653 A user interface element C<E> which is checked (for instance a radio-button or
656 my $input = $dom->find(':checked');
660 An C<E> element whose class is "warning".
662 my $warning = $dom->find('div.warning');
666 An C<E> element with C<ID> equal to "myid".
668 my $foo = $dom->at('div#foo');
672 An C<E> element that does not match simple selector C<s>.
674 my $others = $dom->find('div p:not(:first-child)');
678 An C<F> element descendant of an C<E> element.
680 my $headlines = $dom->find('div h1');
684 An C<F> element child of an C<E> element.
686 my $headlines = $dom->find('html > body > div > h1');
690 An C<F> element immediately preceded by an C<E> element.
692 my $second = $dom->find('h1 + h2');
696 An C<F> element preceded by an C<E> element.
698 my $second = $dom->find('h1 ~ h2');
702 Elements of type C<E>, C<F> and C<G>.
704 my $headlines = $dom->find('h1, h2, h3');
706 =item E[foo=bar][bar=baz]
708 An C<E> element whose attributes match all following attribute selectors.
710 my $links = $dom->find('a[foo^=b][foo$=ar]');
716 L<DOM::Tiny> overloads the following operators.
722 Alias for L</"child_nodes">.
725 $dom->parse('<!-- Test --><b>123</b>')->[0];
737 Alias for L</"attr">.
740 $dom->parse('<div id="test">Test</div>')->at('div')->{id};
746 Alias for L</"to_string">.
750 L<DOM::Tiny> implements the following methods.
754 my $dom = DOM::Tiny->new;
755 my $dom = DOM::Tiny->new('<foo bar="baz">I ♥ DOM::Tiny!</foo>');
757 Construct a new scalar-based L<DOM::Tiny> object and L</"parse"> HTML/XML
758 fragment if necessary.
762 my $trimmed = $dom->all_text;
763 my $untrimmed = $dom->all_text(0);
765 Extract text content from all descendant nodes of this element, smart
766 whitespace trimming is enabled by default.
769 $dom->parse("<div>foo\n<p>bar</p>baz\n</div>")->at('div')->all_text;
772 $dom->parse("<div>foo\n<p>bar</p>baz\n</div>")->at('div')->all_text(0);
776 my $collection = $dom->ancestors;
777 my $collection = $dom->ancestors('div ~ p');
779 Find all ancestor elements of this node matching the CSS selector and return a
780 L<collection|/"COLLECTION METHODS"> containing these elements as L<DOM::Tiny>
781 objects. All selectors listed in L</"SELECTORS"> are supported.
783 # List tag names of ancestor elements
784 say $dom->ancestors->map('tag')->join("\n");
788 $dom = $dom->append('<p>I ♥ DOM::Tiny!</p>');
790 Append HTML/XML fragment to this node (for all node types other than C<root>).
792 # "<div><h1>Test</h1><h2>123</h2></div>"
793 $dom->parse('<div><h1>Test</h1></div>')
794 ->at('h1')->append('<h2>123</h2>')->root;
797 $dom->parse('<p>Test</p>')->at('p')
798 ->child_nodes->first->append(' 123')->root;
800 =head2 append_content
802 $dom = $dom->append_content('<p>I ♥ DOM::Tiny!</p>');
804 Append HTML/XML fragment (for C<root> and C<tag> nodes) or raw content to this
807 # "<div><h1>Test123</h1></div>"
808 $dom->parse('<div><h1>Test</h1></div>')
809 ->at('h1')->append_content('123')->root;
811 # "<!-- Test 123 --><br>"
812 $dom->parse('<!-- Test --><br>')
813 ->child_nodes->first->append_content('123 ')->root;
815 # "<p>Test<i>123</i></p>"
816 $dom->parse('<p>Test</p>')->at('p')->append_content('<i>123</i>')->root;
820 my $result = $dom->at('div ~ p');
822 Find first descendant element of this element matching the CSS selector and
823 return it as a L<DOM::Tiny> object, or C<undef> if none could be found. All
824 selectors listed in L</"SELECTORS"> are supported.
826 # Find first element with "svg" namespace definition
827 my $namespace = $dom->at('[xmlns\:svg]')->{'xmlns:svg'};
831 my $hash = $dom->attr;
832 my $foo = $dom->attr('foo');
833 $dom = $dom->attr({foo => 'bar'});
834 $dom = $dom->attr(foo => 'bar');
836 This element's attributes.
838 # Remove an attribute
839 delete $dom->attr->{id};
841 # Attribute without value
842 $dom->attr(selected => undef);
845 say $dom->find('*')->map(attr => 'id')->compact->join("\n");
849 my $collection = $dom->child_nodes;
851 Return a L<collection|/"COLLECTION METHODS"> containing all child nodes of this
852 element as L<DOM::Tiny> objects.
854 # "<p><b>123</b></p>"
855 $dom->parse('<p>Test<b>123</b></p>')->at('p')->child_nodes->first->remove;
858 $dom->parse('<!DOCTYPE html><b>123</b>')->child_nodes->first;
861 $dom->parse('<b>123</b><!-- Test -->')->child_nodes->last->content;
865 my $collection = $dom->children;
866 my $collection = $dom->children('div ~ p');
868 Find all child elements of this element matching the CSS selector and return a
869 L<collection|/"COLLECTION METHODS"> containing these elements as L<DOM::Tiny>
870 objects. All selectors listed in L</"SELECTORS"> are supported.
872 # Show tag name of random child element
873 say $dom->children->shuffle->first->tag;
877 my $str = $dom->content;
878 $dom = $dom->content('<p>I ♥ DOM::Tiny!</p>');
880 Return this node's content or replace it with HTML/XML fragment (for C<root>
881 and C<tag> nodes) or raw content.
884 $dom->parse('<div><b>Test</b></div>')->at('div')->content;
886 # "<div><h1>123</h1></div>"
887 $dom->parse('<div><h1>Test</h1></div>')->at('h1')->content('123')->root;
889 # "<p><i>123</i></p>"
890 $dom->parse('<p>Test</p>')->at('p')->content('<i>123</i>')->root;
892 # "<div><h1></h1></div>"
893 $dom->parse('<div><h1>Test</h1></div>')->at('h1')->content('')->root;
896 $dom->parse('<!-- Test --><br>')->child_nodes->first->content;
898 # "<div><!-- 123 -->456</div>"
899 $dom->parse('<div><!-- Test -->456</div>')
900 ->at('div')->child_nodes->first->content(' 123 ')->root;
902 =head2 descendant_nodes
904 my $collection = $dom->descendant_nodes;
906 Return a L<collection|/"COLLECTION METHODS"> containing all descendant nodes of
907 this element as L<DOM::Tiny> objects.
909 # "<p><b>123</b></p>"
910 $dom->parse('<p><!-- Test --><b>123<!-- 456 --></b></p>')
911 ->descendant_nodes->grep(sub { $_->type eq 'comment' })
912 ->map('remove')->first;
914 # "<p><b>test</b>test</p>"
915 $dom->parse('<p><b>123</b>456</p>')
916 ->at('p')->descendant_nodes->grep(sub { $_->type eq 'text' })
917 ->map(content => 'test')->first->root;
921 my $collection = $dom->find('div ~ p');
923 Find all descendant elements of this element matching the CSS selector and
924 return a L<collection|/"COLLECTION METHODS"> containing these elements as
925 L<DOM::Tiny> objects. All selectors listed in L</"SELECTORS"> are supported.
927 # Find a specific element and extract information
928 my $id = $dom->find('div')->[23]{id};
930 # Extract information from multiple elements
931 my @headers = $dom->find('h1, h2, h3')->map('text')->each;
933 # Count all the different tags
934 my $hash = $dom->find('*')->reduce(sub { $a->{$b->tag}++; $a }, {});
936 # Find elements with a class that contains dots
937 my @divs = $dom->find('div.foo\.bar')->each;
941 my $collection = $dom->following;
942 my $collection = $dom->following('div ~ p');
944 Find all sibling elements after this node matching the CSS selector and return
945 a L<collection|/"COLLECTION METHODS"> containing these elements as L<DOM::Tiny>
946 objects. All selectors listen in L</"SELECTORS"> are supported.
948 # List tags of sibling elements after this node
949 say $dom->following->map('tag')->join("\n");
951 =head2 following_nodes
953 my $collection = $dom->following_nodes;
955 Return a L<collection|/"COLLECTION METHODS"> containing all sibling nodes after
956 this node as L<DOM::Tiny> objects.
959 $dom->parse('<p>A</p><!-- B -->C')->at('p')->following_nodes->last->content;
963 my $bool = $dom->matches('div ~ p');
965 Check if this element matches the CSS selector. All selectors listed in
966 L</"SELECTORS"> are supported.
969 $dom->parse('<p class="a">A</p>')->at('p')->matches('.a');
970 $dom->parse('<p class="a">A</p>')->at('p')->matches('p[class]');
973 $dom->parse('<p class="a">A</p>')->at('p')->matches('.b');
974 $dom->parse('<p class="a">A</p>')->at('p')->matches('p[id]');
978 my $namespace = $dom->namespace;
980 Find this element's namespace, or return C<undef> if none could be found.
982 # Find namespace for an element with namespace prefix
983 my $namespace = $dom->at('svg > svg\:circle')->namespace;
985 # Find namespace for an element that may or may not have a namespace prefix
986 my $namespace = $dom->at('svg > circle')->namespace;
990 my $sibling = $dom->next;
992 Return L<DOM::Tiny> object for next sibling element, or C<undef> if there are
996 $dom->parse('<div><h1>Test</h1><h2>123</h2></div>')->at('h1')->next;
1000 my $sibling = $dom->next_node;
1002 Return L<DOM::Tiny> object for next sibling node, or C<undef> if there are no
1006 $dom->parse('<p><b>123</b><!-- Test -->456</p>')
1007 ->at('b')->next_node->next_node;
1010 $dom->parse('<p><b>123</b><!-- Test -->456</p>')
1011 ->at('b')->next_node->content;
1015 my $parent = $dom->parent;
1017 Return L<DOM::Tiny> object for parent of this node, or C<undef> if this node
1020 # "<b><i>Test</i></b>"
1021 $dom->parse('<p><b><i>Test</i></b></p>')->at('i')->parent;
1025 $dom = $dom->parse('<foo bar="baz">I ♥ DOM::Tiny!</foo>');
1027 Parse HTML/XML fragment.
1030 my $dom = DOM::Tiny->new->xml(1)->parse('<foo>I ♥ DOM::Tiny!</foo>');
1034 my $collection = $dom->preceding;
1035 my $collection = $dom->preceding('div ~ p');
1037 Find all sibling elements before this node matching the CSS selector and return
1038 a L<collection|/"COLLECTION METHODS"> containing these elements as L<DOM::Tiny>
1039 objects. All selectors listed in L</"SELECTORS"> are supported.
1041 # List tags of sibling elements before this node
1042 say $dom->preceding->map('tag')->join("\n");
1044 =head2 preceding_nodes
1046 my $collection = $dom->preceding_nodes;
1048 Return a L<collection|/"COLLECTION METHODS"> containing all sibling nodes
1049 before this node as L<DOM::Tiny> objects.
1052 $dom->parse('A<!-- B --><p>C</p>')->at('p')->preceding_nodes->first->content;
1056 $dom = $dom->prepend('<p>I ♥ DOM::Tiny!</p>');
1058 Prepend HTML/XML fragment to this node (for all node types other than C<root>).
1060 # "<div><h1>Test</h1><h2>123</h2></div>"
1061 $dom->parse('<div><h2>123</h2></div>')
1062 ->at('h2')->prepend('<h1>Test</h1>')->root;
1065 $dom->parse('<p>123</p>')
1066 ->at('p')->child_nodes->first->prepend('Test ')->root;
1068 =head2 prepend_content
1070 $dom = $dom->prepend_content('<p>I ♥ DOM::Tiny!</p>');
1072 Prepend HTML/XML fragment (for C<root> and C<tag> nodes) or raw content to this
1075 # "<div><h2>Test123</h2></div>"
1076 $dom->parse('<div><h2>123</h2></div>')
1077 ->at('h2')->prepend_content('Test')->root;
1079 # "<!-- Test 123 --><br>"
1080 $dom->parse('<!-- 123 --><br>')
1081 ->child_nodes->first->prepend_content(' Test')->root;
1083 # "<p><i>123</i>Test</p>"
1084 $dom->parse('<p>Test</p>')->at('p')->prepend_content('<i>123</i>')->root;
1088 my $sibling = $dom->previous;
1090 Return L<DOM::Tiny> object for previous sibling element, or C<undef> if there
1091 are no more siblings.
1094 $dom->parse('<div><h1>Test</h1><h2>123</h2></div>')->at('h2')->previous;
1096 =head2 previous_node
1098 my $sibling = $dom->previous_node;
1100 Return L<DOM::Tiny> object for previous sibling node, or C<undef> if there are
1104 $dom->parse('<p>123<!-- Test --><b>456</b></p>')
1105 ->at('b')->previous_node->previous_node;
1108 $dom->parse('<p>123<!-- Test --><b>456</b></p>')
1109 ->at('b')->previous_node->content;
1113 my $parent = $dom->remove;
1115 Remove this node and return L</"root"> (for C<root> nodes) or L</"parent">.
1118 $dom->parse('<div><h1>Test</h1></div>')->at('h1')->remove;
1120 # "<p><b>456</b></p>"
1121 $dom->parse('<p>123<b>456</b></p>')
1122 ->at('p')->child_nodes->first->remove->root;
1126 my $parent = $dom->replace('<div>I ♥ DOM::Tiny!</div>');
1128 Replace this node with HTML/XML fragment and return L</"root"> (for C<root>
1129 nodes) or L</"parent">.
1131 # "<div><h2>123</h2></div>"
1132 $dom->parse('<div><h1>Test</h1></div>')->at('h1')->replace('<h2>123</h2>');
1134 # "<p><b>123</b></p>"
1135 $dom->parse('<p>Test</p>')
1136 ->at('p')->child_nodes->[0]->replace('<b>123</b>')->root;
1140 my $root = $dom->root;
1142 Return L<DOM::Tiny> object for C<root> node.
1146 my $parent = $dom->strip;
1148 Remove this element while preserving its content and return L</"parent">.
1151 $dom->parse('<div><h1>Test</h1></div>')->at('h1')->strip;
1155 my $tag = $dom->tag;
1156 $dom = $dom->tag('div');
1158 This element's tag name.
1160 # List tag names of child elements
1161 say $dom->children->map('tag')->join("\n");
1165 $dom = $dom->tap(sub {...});
1167 Equivalent to L<Mojo::Base/"tap">.
1171 my $trimmed = $dom->text;
1172 my $untrimmed = $dom->text(0);
1174 Extract text content from this element only (not including child elements),
1175 smart whitespace trimming is enabled by default.
1178 $dom->parse("<div>foo\n<p>bar</p>baz\n</div>")->at('div')->text;
1181 $dom->parse("<div>foo\n<p>bar</p>baz\n</div>")->at('div')->text(0);
1185 my $str = $dom->to_string;
1187 Render this node and its content to HTML/XML.
1190 $dom->parse('<div><b>Test</b></div>')->at('div b')->to_string;
1194 my $tree = $dom->tree;
1195 $dom = $dom->tree(['root']);
1197 Document Object Model. Note that this structure should only be used very
1198 carefully since it is very dynamic.
1202 my $type = $dom->type;
1204 This node's type, usually C<cdata>, C<comment>, C<doctype>, C<pi>, C<raw>,
1205 C<root>, C<tag> or C<text>.
1208 $dom->parse('<![CDATA[Test]]>')->child_nodes->first->type;
1211 $dom->parse('<!-- Test -->')->child_nodes->first->type;
1214 $dom->parse('<!DOCTYPE html>')->child_nodes->first->type;
1217 $dom->parse('<?xml version="1.0"?>')->child_nodes->first->type;
1220 $dom->parse('<title>Test</title>')->at('title')->child_nodes->first->type;
1223 $dom->parse('<p>Test</p>')->type;
1226 $dom->parse('<p>Test</p>')->at('p')->type;
1229 $dom->parse('<p>Test</p>')->at('p')->child_nodes->first->type;
1233 my $value = $dom->val;
1235 Extract value from form element (such as C<button>, C<input>, C<option>,
1236 C<select> and C<textarea>), or return C<undef> if this element has no value. In
1237 the case of C<select> with C<multiple> attribute, find C<option> elements with
1238 C<selected> attribute and return an array reference with all values, or
1239 C<undef> if none could be found.
1242 $dom->parse('<input name=test value=a>')->at('input')->val;
1245 $dom->parse('<textarea>b</textarea>')->at('textarea')->val;
1248 $dom->parse('<option value="c">Test</option>')->at('option')->val;
1251 $dom->parse('<select><option selected>d</option></select>')
1252 ->at('select')->val;
1255 $dom->parse('<select multiple><option selected>e</option></select>')
1256 ->at('select')->val->[0];
1259 $dom->parse('<input name=test type=checkbox>')->at('input')->val;
1263 $dom = $dom->wrap('<div></div>');
1265 Wrap HTML/XML fragment around this node (for all node types other than C<root>),
1266 placing it as the last child of the first innermost element.
1268 # "<p>123<b>Test</b></p>"
1269 $dom->parse('<b>Test</b>')->at('b')->wrap('<p>123</p>')->root;
1271 # "<div><p><b>Test</b></p>123</div>"
1272 $dom->parse('<b>Test</b>')->at('b')->wrap('<div><p></p>123</div>')->root;
1274 # "<p><b>Test</b></p><p>123</p>"
1275 $dom->parse('<b>Test</b>')->at('b')->wrap('<p></p><p>123</p>')->root;
1277 # "<p><b>Test</b></p>"
1278 $dom->parse('<p>Test</p>')->at('p')->child_nodes->first->wrap('<b>')->root;
1282 $dom = $dom->wrap_content('<div></div>');
1284 Wrap HTML/XML fragment around this node's content (for C<root> and C<tag>
1285 nodes), placing it as the last children of the first innermost element.
1287 # "<p><b>123Test</b></p>"
1288 $dom->parse('<p>Test<p>')->at('p')->wrap_content('<b>123</b>')->root;
1290 # "<p><b>Test</b></p><p>123</p>"
1291 $dom->parse('<b>Test</b>')->wrap_content('<p></p><p>123</p>');
1295 my $bool = $dom->xml;
1296 $dom = $dom->xml($bool);
1298 Disable HTML semantics in parser and activate case-sensitivity, defaults to
1299 auto detection based on XML declarations.
1301 =head1 COLLECTION METHODS
1303 Some L<DOM::Tiny> methods return an array-based collection object based on
1304 L<Mojo::Collection>, which can either be accessed directly as an array
1305 reference, or with the following methods.
1308 $collection->map(sub { ucfirst })->shuffle->each(sub {
1309 my ($word, $num) = @_;
1313 # Access array directly to manipulate collection
1314 $collection->[23] += 100;
1315 say for @$collection;
1319 my $new = $collection->compact;
1321 Create a new collection with all elements that are defined and not an empty
1324 # $collection contains (0, 1, undef, 2, '', 3)
1325 $collection->compact->join(', '); # "0, 1, 2, 3"
1329 my @elements = $collection->each;
1330 $collection = $collection->each(sub {...});
1332 Evaluate callback for each element in collection or return all elements as a
1333 list if none has been provided. The element will be the first argument passed
1334 to the callback and is also available as C<$_>.
1336 # Make a numbered list
1337 $collection->each(sub {
1344 my $first = $collection->first;
1345 my $first = $collection->first(qr/foo/);
1346 my $first = $collection->first(sub {...});
1347 my $first = $collection->first($method);
1348 my $first = $collection->first($method, @args);
1350 Evaluate regular expression/callback for, or call method on, each element in
1351 collection and return the first one that matched the regular expression, or for
1352 which the callback/method returned true. The element will be the first argument
1353 passed to the callback and is also available as C<$_>.
1356 my $first = $collection->first(sub { $_->$method(@args) });
1358 # Find first value that contains the word "tiny"
1359 my $interesting = $collection->first(qr/tiny/i);
1361 # Find first value that is greater than 5
1362 my $greater = $collection->first(sub { $_ > 5 });
1366 my $new = $collection->flatten;
1368 Flatten nested collections/arrays recursively and create a new collection with
1371 # $collection contains (1, [2, [3, 4], 5, [6]], 7)
1372 $collection->flatten->join(', '); # "1, 2, 3, 4, 5, 6, 7"
1376 my $new = $collection->grep(qr/foo/);
1377 my $new = $collection->grep(sub {...});
1378 my $new = $collection->grep($method);
1379 my $new = $collection->grep($method, @args);
1381 Evaluate regular expression/callback for, or call method on, each element in
1382 collection and create a new collection with all elements that matched the
1383 regular expression, or for which the callback/method returned true. The element
1384 will be the first argument passed to the callback and is also available as
1388 my $new = $collection->grep(sub { $_->$method(@args) });
1390 # Find all values that contain the word "tiny"
1391 my $interesting = $collection->grep(qr/tiny/i);
1393 # Find all values that are greater than 5
1394 my $greater = $collection->grep(sub { $_ > 5 });
1398 my $stream = $collection->join;
1399 my $stream = $collection->join("\n");
1401 Turn collection into string.
1403 # Join all values with commas
1404 $collection->join(', ');
1408 my $last = $collection->last;
1410 Return the last element in collection.
1414 my $new = $collection->map(sub {...});
1415 my $new = $collection->map($method);
1416 my $new = $collection->map($method, @args);
1418 Evaluate callback for, or call method on, each element in collection and create
1419 a new collection from the results. The element will be the first argument
1420 passed to the callback and is also available as C<$_>.
1423 my $new = $collection->map(sub { $_->$method(@args) });
1425 # Append the word "tiny" to all values
1426 my $domified = $collection->map(sub { $_ . 'tiny' });
1430 my $result = $collection->reduce(sub {...});
1431 my $result = $collection->reduce(sub {...}, $initial);
1433 Reduce elements in collection with callback, the first element will be used as
1434 initial value if none has been provided.
1436 # Calculate the sum of all values
1437 my $sum = $collection->reduce(sub { $a + $b });
1439 # Count how often each value occurs in collection
1440 my $hash = $collection->reduce(sub { $a->{$b}++; $a }, {});
1444 my $new = $collection->reverse;
1446 Create a new collection with all elements in reverse order.
1450 my $new = $collection->slice(4 .. 7);
1452 Create a new collection with all selected elements.
1454 # $collection contains ('A', 'B', 'C', 'D', 'E')
1455 $collection->slice(1, 2, 4)->join(' '); # "B C E"
1459 my $new = $collection->shuffle;
1461 Create a new collection with all elements in random order.
1465 my $size = $collection->size;
1467 Number of elements in collection.
1471 my $new = $collection->sort;
1472 my $new = $collection->sort(sub {...});
1474 Sort elements based on return value of callback and create a new collection
1477 # Sort values case-insensitive
1478 my $case_insensitive = $collection->sort(sub { uc($a) cmp uc($b) });
1482 $collection = $collection->tap(sub {...});
1484 Equivalent to L<Mojo::Base/"tap">.
1488 my $array = $collection->to_array;
1490 Turn collection into array reference.
1494 my $new = $collection->uniq;
1495 my $new = $collection->uniq(sub {...});
1496 my $new = $collection->uniq($method);
1497 my $new = $collection->uniq($method, @args);
1499 Create a new collection without duplicate elements, using the string
1500 representation of either the elements or the return value of the
1504 my $new = $collection->uniq(sub { $_->$method(@args) });
1506 # $collection contains ('foo', 'bar', 'bar', 'baz')
1507 $collection->uniq->join(' '); # "foo bar baz"
1509 # $collection contains ([1, 2], [2, 1], [3, 2])
1510 $collection->uniq(sub{ $_->[1] })->to_array; # "[[1, 2], [2, 1]]"
1514 Report any issues on the public bugtracker.
1518 Dan Book <dbook@cpan.org>
1520 Code and tests adapted from L<Mojo::DOM>, a lightweight DOM parser by the L<Mojolicious> team.
1526 =item Matt S Trout (mst)
1530 =head1 COPYRIGHT AND LICENSE
1532 Copyright (c) 2008-2015 Sebastian Riedel.
1534 Copyright (c) 2015 L</"AUTHOR"> and L</"CONTRIBUTORS"> for adaptation to standalone format.
1536 This is free software, licensed under:
1538 The Artistic License 2.0 (GPL Compatible)
1542 L<Mojo::DOM>, L<HTML::TreeBuilder>, L<XML::LibXML>, L<XML::Twig>, L<XML::Smart>
1544 =for Pod::Coverage TO_JSON