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.001';
23 my $self = bless \DOM::Tiny::HTML->new, ref $class || $class;
24 return @_ ? $self->parse(@_) : $self;
27 sub all_text { shift->_all_text(1, @_) }
29 sub ancestors { _select($_[0]->_collect($_[0]->_ancestors), $_[1]) }
31 sub append { shift->_add(1, @_) }
32 sub append_content { shift->_content(1, 0, @_) }
36 return undef unless my $result = $self->_css->select_one(@_);
37 return $self->_build($result, $self->xml);
44 my $tree = $self->tree;
45 my $attrs = $tree->[0] ne 'tag' ? {} : $tree->[2];
46 return $attrs unless @_;
49 return $attrs->{$_[0]} unless @_ > 1 || ref $_[0];
52 my $values = ref $_[0] ? $_[0] : {@_};
53 @$attrs{keys %$values} = values %$values;
58 sub child_nodes { $_[0]->_collect(_nodes($_[0]->tree)) }
60 sub children { _select($_[0]->_collect(_nodes($_[0]->tree, 1)), $_[1]) }
65 my $type = $self->type;
66 if ($type eq 'root' || $type eq 'tag') {
67 return $self->_content(0, 1, @_) if @_;
68 my $html = DOM::Tiny::HTML->new(xml => $self->xml);
69 return join '', map { $html->tree($_)->render } _nodes($self->tree);
72 return $self->tree->[1] unless @_;
73 $self->tree->[1] = shift;
77 sub descendant_nodes { $_[0]->_collect(_all(_nodes($_[0]->tree))) }
79 sub find { $_[0]->_collect(@{$_[0]->_css->select($_[1])}) }
81 sub following { _select($_[0]->_collect(@{$_[0]->_siblings(1)->[1]}), $_[1]) }
82 sub following_nodes { $_[0]->_collect(@{$_[0]->_siblings->[1]}) }
84 sub matches { shift->_css->matches(@_) }
89 return undef if (my $tree = $self->tree)->[0] ne 'tag';
91 # Extract namespace prefix and search parents
92 my $ns = $tree->[1] =~ /^(.*?):/ ? "xmlns:$1" : undef;
93 for my $node ($tree, $self->_ancestors) {
95 # Namespace for prefix
96 my $attrs = $node->[2];
97 if ($ns) { $_ eq $ns and return $attrs->{$_} for keys %$attrs }
100 elsif (defined $attrs->{xmlns}) { return $attrs->{xmlns} }
106 sub next { $_[0]->_maybe($_[0]->_siblings(1, 0)->[1]) }
107 sub next_node { $_[0]->_maybe($_[0]->_siblings(0, 0)->[1]) }
111 return undef if $self->tree->[0] eq 'root';
112 return $self->_build($self->_parent, $self->xml);
115 sub parse { shift->_delegate(parse => @_) }
117 sub preceding { _select($_[0]->_collect(@{$_[0]->_siblings(1)->[0]}), $_[1]) }
118 sub preceding_nodes { $_[0]->_collect(@{$_[0]->_siblings->[0]}) }
120 sub prepend { shift->_add(0, @_) }
121 sub prepend_content { shift->_content(0, 0, @_) }
123 sub previous { $_[0]->_maybe($_[0]->_siblings(1, -1)->[0]) }
124 sub previous_node { $_[0]->_maybe($_[0]->_siblings(0, -1)->[0]) }
126 sub remove { shift->replace('') }
129 my ($self, $new) = @_;
130 return $self->parse($new) if (my $tree = $self->tree)->[0] eq 'root';
131 return $self->_replace($self->_parent, $tree, _nodes($self->_parse($new)));
136 return $self unless my $tree = $self->_ancestors(1);
137 return $self->_build($tree, $self->xml);
142 return $self if (my $tree = $self->tree)->[0] ne 'tag';
143 return $self->_replace($tree->[3], $tree, _nodes($tree));
147 my ($self, $tag) = @_;
148 return undef if (my $tree = $self->tree)->[0] ne 'tag';
149 return $tree->[1] unless $tag;
154 sub tap { shift->DOM::Tiny::Collection::tap(@_) }
156 sub text { shift->_all_text(0, @_) }
158 sub to_string { shift->_delegate('render') }
160 sub tree { shift->_delegate(tree => @_) }
162 sub type { shift->tree->[0] }
168 return $self->{value} // $self->text if (my $tag = $self->tag) eq 'option';
170 # "textarea", "input" or "button"
171 return $tag eq 'textarea' ? $self->text : $self->{value} if $tag ne 'select';
174 my $v = $self->find('option:checked')->map('val');
175 return exists $self->{multiple} ? $v->size ? $v->to_array : undef : $v->last;
178 sub wrap { shift->_wrap(0, @_) }
179 sub wrap_content { shift->_wrap(1, @_) }
181 sub xml { shift->_delegate(xml => @_) }
184 my ($self, $offset, $new) = @_;
186 return $self if (my $tree = $self->tree)->[0] eq 'root';
188 my $parent = $self->_parent;
189 splice @$parent, _offset($parent, $tree) + $offset, 0,
190 _link($parent, _nodes($self->_parse($new)));
196 map { $_->[0] eq 'tag' ? ($_, _all(_nodes($_))) : ($_) } @_;
200 my ($self, $recurse, $trim) = @_;
203 my $tree = $self->tree;
204 $trim = 1 unless defined $trim;
205 map { $_->[1] eq 'pre' and $trim = 0 } $self->_ancestors, $tree
206 if $trim && $tree->[0] ne 'root';
208 return _text([_nodes($tree)], $recurse, $trim);
212 my ($self, $root) = @_;
214 return unless my $tree = $self->_parent;
216 do { push @ancestors, $tree }
217 while ($tree->[0] eq 'tag') && ($tree = $tree->[3]);
218 return $root ? $ancestors[-1] : @ancestors[0 .. $#ancestors - 1];
221 sub _build { shift->new->tree(shift)->xml(shift) }
225 my $xml = $self->xml;
226 return DOM::Tiny::Collection->new(map { $self->_build($_, $xml) } @_);
230 my ($self, $start, $offset, $new) = @_;
232 my $tree = $self->tree;
233 unless ($tree->[0] eq 'root' || $tree->[0] eq 'tag') {
234 my $old = $self->content;
235 return $self->content($start ? "$old$new" : "$new$old");
238 $start = $start ? ($#$tree + 1) : _start($tree);
239 $offset = $offset ? $#$tree : 0;
240 splice @$tree, $start, $offset, _link($tree, _nodes($self->_parse($new)));
245 sub _css { DOM::Tiny::CSS->new(tree => shift->tree) }
248 my ($self, $method) = (shift, shift);
249 return $$self->$method unless @_;
255 my ($parent, @children) = @_;
257 # Link parent to children
258 for my $node (@children) {
259 my $offset = $node->[0] eq 'tag' ? 3 : 2;
260 $node->[$offset] = $parent;
261 weaken $node->[$offset];
267 sub _maybe { $_[1] ? $_[0]->_build($_[1], $_[0]->xml) : undef }
270 return unless my $tree = shift;
271 my @nodes = @$tree[_start($tree) .. $#$tree];
272 return shift() ? grep { $_->[0] eq 'tag' } @nodes : @nodes;
276 my ($parent, $child) = @_;
277 my $i = _start($parent);
278 $_ eq $child ? last : $i++ for @$parent[$i .. $#$parent];
282 sub _parent { $_[0]->tree->[$_[0]->type eq 'tag' ? 3 : 2] }
284 sub _parse { DOM::Tiny::HTML->new(xml => shift->xml)->parse(shift)->tree }
287 my ($self, $parent, $child, @nodes) = @_;
288 splice @$parent, _offset($parent, $child), 1, _link($parent, @nodes);
289 return $self->parent;
293 my ($collection, $selector) = @_;
294 return $collection unless $selector;
295 return $collection->new(grep { $_->matches($selector) } @$collection);
299 my ($self, $tags, $i) = @_;
301 return [] unless my $parent = $self->parent;
303 my $tree = $self->tree;
304 my (@before, @after, $match);
305 for my $node (_nodes($parent->tree)) {
306 ++$match and next if !$match && $node eq $tree;
307 next if $tags && $node->[0] ne 'tag';
308 $match ? push @after, $node : push @before, $node;
311 return defined $i ? [$before[$i], $after[$i]] : [\@before, \@after];
322 sub _start { $_[0][0] eq 'root' ? 1 : 4 }
325 my ($nodes, $recurse, $trim) = @_;
327 # Merge successive text nodes
329 while (my $next = $nodes->[$i + 1]) {
330 ++$i and next unless $nodes->[$i][0] eq 'text' && $next->[0] eq 'text';
331 splice @$nodes, $i, 2, ['text', $nodes->[$i][1] . $next->[1]];
335 for my $node (@$nodes) {
336 my $type = $node->[0];
340 if ($type eq 'text') { $chunk = $trim ? _squish $node->[1] : $node->[1] }
343 elsif ($type eq 'cdata' || $type eq 'raw') { $chunk = $node->[1] }
346 elsif ($type eq 'tag' && $recurse) {
347 no warnings 'recursion';
348 $chunk = _text([_nodes($node)], 1, $node->[1] eq 'pre' ? 0 : $trim);
351 # Add leading whitespace if punctuation allows it
352 $chunk = " $chunk" if $text =~ /\S\z/ && $chunk =~ /^[^.!?,;:\s]+/;
354 # Trim whitespace blocks
355 $text .= $chunk if $chunk =~ /\S+/ || !$trim;
362 my ($self, $content, $new) = @_;
364 $content = 1 if (my $tree = $self->tree)->[0] eq 'root';
365 $content = 0 if $tree->[0] ne 'root' && $tree->[0] ne 'tag';
369 my $first = $new = $self->_parse($new);
370 $current = $first while $first = (_nodes($first, 1))[0];
371 return $self unless $current;
375 push @$current, _link($current, _nodes($tree));
376 splice @$tree, _start($tree), $#$tree, _link($tree, _nodes($new));
381 $self->_replace($self->_parent, $tree, _nodes($new));
382 push @$current, _link($current, $tree);
392 DOM::Tiny - Minimalistic HTML/XML DOM parser with CSS selectors
399 my $dom = DOM::Tiny->new('<div><p id="a">Test</p><p id="b">123</p></div>');
402 say $dom->at('#b')->text;
403 say $dom->find('p')->map('text')->join("\n");
404 say $dom->find('[id]')->map(attr => 'id')->join("\n");
407 $dom->find('p[id]')->reverse->each(sub { say $_->{id} });
410 for my $e ($dom->find('p[id]')->each) {
411 say $e->{id}, ':', $e->text;
415 $dom->find('div p')->last->append('<p id="c">456</p>');
416 $dom->find(':not(p)')->map('strip');
423 L<DOM::Tiny> is a minimalistic and relaxed HTML/XML DOM parser with CSS
424 selector support based on L<Mojo::DOM>. It will even try to interpret broken
425 HTML and XML, so you should not use it for validation.
427 =head1 NODES AND ELEMENTS
429 When we parse an HTML/XML fragment, it gets turned into a tree of nodes.
433 <head><title>Hello</title></head>
437 There are currently eight different kinds of nodes, C<cdata>, C<comment>,
438 C<doctype>, C<pi>, C<raw>, C<root>, C<tag> and C<text>. Elements are nodes of
450 While all node types are represented as L<DOM::Tiny> objects, some methods like
451 L</"attr"> and L</"namespace"> only apply to elements.
453 =head1 CASE-SENSITIVITY
455 L<DOM::Tiny> defaults to HTML semantics, that means all tags and attribute
456 names are lowercased and selectors need to be lowercase as well.
459 my $dom = DOM::Tiny->new('<P ID="greeting">Hi!</P>');
460 say $dom->at('p[id]')->text;
462 If XML processing instructions are found, the parser will automatically switch
463 into XML mode and everything becomes case-sensitive.
466 my $dom = DOM::Tiny->new('<?xml version="1.0"?><P ID="greeting">Hi!</P>');
467 say $dom->at('P[ID]')->text;
469 XML detection can also be disabled with the L</"xml"> method.
471 # Force XML semantics
472 my $dom = DOM::Tiny->new->xml(1)->parse('<P ID="greeting">Hi!</P>');
473 say $dom->at('P[ID]')->text;
475 # Force HTML semantics
476 my $dom = DOM::Tiny->new->xml(0)->parse('<P ID="greeting">Hi!</P>');
477 say $dom->at('p[id]')->text;
481 L<DOM::Tiny> implements the following methods.
485 my $dom = DOM::Tiny->new;
486 my $dom = DOM::Tiny->new('<foo bar="baz">I ♥ DOM::Tiny!</foo>');
488 Construct a new scalar-based L<DOM::Tiny> object and L</"parse"> HTML/XML
489 fragment if necessary.
493 my $trimmed = $dom->all_text;
494 my $untrimmed = $dom->all_text(0);
496 Extract text content from all descendant nodes of this element, smart
497 whitespace trimming is enabled by default.
500 $dom->parse("<div>foo\n<p>bar</p>baz\n</div>")->at('div')->all_text;
503 $dom->parse("<div>foo\n<p>bar</p>baz\n</div>")->at('div')->all_text(0);
507 my $collection = $dom->ancestors;
508 my $collection = $dom->ancestors('div ~ p');
510 Find all ancestor elements of this node matching the CSS selector and return a
511 L<DOM::Tiny::Collection> object containing these elements as L<DOM::Tiny>
512 objects. All selectors from L<DOM::Tiny::CSS/"SELECTORS"> are supported.
514 # List tag names of ancestor elements
515 say $dom->ancestors->map('tag')->join("\n");
519 $dom = $dom->append('<p>I ♥ DOM::Tiny!</p>');
521 Append HTML/XML fragment to this node.
523 # "<div><h1>Test</h1><h2>123</h2></div>"
524 $dom->parse('<div><h1>Test</h1></div>')
525 ->at('h1')->append('<h2>123</h2>')->root;
528 $dom->parse('<p>Test</p>')->at('p')
529 ->child_nodes->first->append(' 123')->root;
531 =head2 append_content
533 $dom = $dom->append_content('<p>I ♥ DOM::Tiny!</p>');
535 Append HTML/XML fragment (for C<root> and C<tag> nodes) or raw content to this
538 # "<div><h1>Test123</h1></div>"
539 $dom->parse('<div><h1>Test</h1></div>')
540 ->at('h1')->append_content('123')->root;
542 # "<!-- Test 123 --><br>"
543 $dom->parse('<!-- Test --><br>')
544 ->child_nodes->first->append_content('123 ')->root;
546 # "<p>Test<i>123</i></p>"
547 $dom->parse('<p>Test</p>')->at('p')->append_content('<i>123</i>')->root;
551 my $result = $dom->at('div ~ p');
553 Find first descendant element of this element matching the CSS selector and
554 return it as a L<DOM::Tiny> object or return C<undef> if none could be found.
555 All selectors from L<DOM::Tiny::CSS/"SELECTORS"> are supported.
557 # Find first element with "svg" namespace definition
558 my $namespace = $dom->at('[xmlns\:svg]')->{'xmlns:svg'};
562 my $hash = $dom->attr;
563 my $foo = $dom->attr('foo');
564 $dom = $dom->attr({foo => 'bar'});
565 $dom = $dom->attr(foo => 'bar');
567 This element's attributes.
569 # Remove an attribute
570 delete $dom->attr->{id};
572 # Attribute without value
573 $dom->attr(selected => undef);
576 say $dom->find('*')->map(attr => 'id')->compact->join("\n");
580 my $collection = $dom->child_nodes;
582 Return a L<DOM::Tiny::Collection> object containing all child nodes of this
583 element as L<DOM::Tiny> objects.
585 # "<p><b>123</b></p>"
586 $dom->parse('<p>Test<b>123</b></p>')->at('p')->child_nodes->first->remove;
589 $dom->parse('<!DOCTYPE html><b>123</b>')->child_nodes->first;
592 $dom->parse('<b>123</b><!-- Test -->')->child_nodes->last->content;
596 my $collection = $dom->children;
597 my $collection = $dom->children('div ~ p');
599 Find all child elements of this element matching the CSS selector and return a
600 L<DOM::Tiny::Collection> object containing these elements as L<DOM::Tiny>
601 objects. All selectors from L<DOM::Tiny::CSS/"SELECTORS"> are supported.
603 # Show tag name of random child element
604 say $dom->children->shuffle->first->tag;
608 my $str = $dom->content;
609 $dom = $dom->content('<p>I ♥ DOM::Tiny!</p>');
611 Return this node's content or replace it with HTML/XML fragment (for C<root>
612 and C<tag> nodes) or raw content.
615 $dom->parse('<div><b>Test</b></div>')->at('div')->content;
617 # "<div><h1>123</h1></div>"
618 $dom->parse('<div><h1>Test</h1></div>')->at('h1')->content('123')->root;
620 # "<p><i>123</i></p>"
621 $dom->parse('<p>Test</p>')->at('p')->content('<i>123</i>')->root;
623 # "<div><h1></h1></div>"
624 $dom->parse('<div><h1>Test</h1></div>')->at('h1')->content('')->root;
627 $dom->parse('<!-- Test --><br>')->child_nodes->first->content;
629 # "<div><!-- 123 -->456</div>"
630 $dom->parse('<div><!-- Test -->456</div>')
631 ->at('div')->child_nodes->first->content(' 123 ')->root;
633 =head2 descendant_nodes
635 my $collection = $dom->descendant_nodes;
637 Return a L<DOM::Tiny::Collection> object containing all descendant nodes of
638 this element as L<DOM::Tiny> objects.
640 # "<p><b>123</b></p>"
641 $dom->parse('<p><!-- Test --><b>123<!-- 456 --></b></p>')
642 ->descendant_nodes->grep(sub { $_->type eq 'comment' })
643 ->map('remove')->first;
645 # "<p><b>test</b>test</p>"
646 $dom->parse('<p><b>123</b>456</p>')
647 ->at('p')->descendant_nodes->grep(sub { $_->type eq 'text' })
648 ->map(content => 'test')->first->root;
652 my $collection = $dom->find('div ~ p');
654 Find all descendant elements of this element matching the CSS selector and
655 return a L<DOM::Tiny::Collection> object containing these elements as
656 L<DOM::Tiny> objects. All selectors from L<DOM::Tiny::CSS/"SELECTORS"> are
659 # Find a specific element and extract information
660 my $id = $dom->find('div')->[23]{id};
662 # Extract information from multiple elements
663 my @headers = $dom->find('h1, h2, h3')->map('text')->each;
665 # Count all the different tags
666 my $hash = $dom->find('*')->reduce(sub { $a->{$b->tag}++; $a }, {});
668 # Find elements with a class that contains dots
669 my @divs = $dom->find('div.foo\.bar')->each;
673 my $collection = $dom->following;
674 my $collection = $dom->following('div ~ p');
676 Find all sibling elements after this node matching the CSS selector and return
677 a L<DOM::Tiny::Collection> object containing these elements as L<DOM::Tiny>
678 objects. All selectors from L<DOM::Tiny::CSS/"SELECTORS"> are supported.
680 # List tags of sibling elements after this node
681 say $dom->following->map('tag')->join("\n");
683 =head2 following_nodes
685 my $collection = $dom->following_nodes;
687 Return a L<DOM::Tiny::Collection> object containing all sibling nodes after
688 this node as L<DOM::Tiny> objects.
691 $dom->parse('<p>A</p><!-- B -->C')->at('p')->following_nodes->last->content;
695 my $bool = $dom->matches('div ~ p');
697 Check if this element matches the CSS selector. All selectors from
698 L<DOM::Tiny::CSS/"SELECTORS"> are supported.
701 $dom->parse('<p class="a">A</p>')->at('p')->matches('.a');
702 $dom->parse('<p class="a">A</p>')->at('p')->matches('p[class]');
705 $dom->parse('<p class="a">A</p>')->at('p')->matches('.b');
706 $dom->parse('<p class="a">A</p>')->at('p')->matches('p[id]');
710 my $namespace = $dom->namespace;
712 Find this element's namespace or return C<undef> if none could be found.
714 # Find namespace for an element with namespace prefix
715 my $namespace = $dom->at('svg > svg\:circle')->namespace;
717 # Find namespace for an element that may or may not have a namespace prefix
718 my $namespace = $dom->at('svg > circle')->namespace;
722 my $sibling = $dom->next;
724 Return L<DOM::Tiny> object for next sibling element or C<undef> if there are no
728 $dom->parse('<div><h1>Test</h1><h2>123</h2></div>')->at('h1')->next;
732 my $sibling = $dom->next_node;
734 Return L<DOM::Tiny> object for next sibling node or C<undef> if there are no
738 $dom->parse('<p><b>123</b><!-- Test -->456</p>')
739 ->at('b')->next_node->next_node;
742 $dom->parse('<p><b>123</b><!-- Test -->456</p>')
743 ->at('b')->next_node->content;
747 my $parent = $dom->parent;
749 Return L<DOM::Tiny> object for parent of this node or C<undef> if this node has
754 $dom = $dom->parse('<foo bar="baz">I ♥ DOM::Tiny!</foo>');
756 Parse HTML/XML fragment with L<DOM::Tiny::HTML>.
759 my $dom = DOM::Tiny->new->xml(1)->parse($xml);
763 my $collection = $dom->preceding;
764 my $collection = $dom->preceding('div ~ p');
766 Find all sibling elements before this node matching the CSS selector and return
767 a L<DOM::Tiny::Collection> object containing these elements as L<DOM::Tiny>
768 objects. All selectors from L<DOM::Tiny::CSS/"SELECTORS"> are supported.
770 # List tags of sibling elements before this node
771 say $dom->preceding->map('tag')->join("\n");
773 =head2 preceding_nodes
775 my $collection = $dom->preceding_nodes;
777 Return a L<DOM::Tiny::Collection> object containing all sibling nodes before
778 this node as L<DOM::Tiny> objects.
781 $dom->parse('A<!-- B --><p>C</p>')->at('p')->preceding_nodes->first->content;
785 $dom = $dom->prepend('<p>I ♥ DOM::Tiny!</p>');
787 Prepend HTML/XML fragment to this node.
789 # "<div><h1>Test</h1><h2>123</h2></div>"
790 $dom->parse('<div><h2>123</h2></div>')
791 ->at('h2')->prepend('<h1>Test</h1>')->root;
794 $dom->parse('<p>123</p>')
795 ->at('p')->child_nodes->first->prepend('Test ')->root;
797 =head2 prepend_content
799 $dom = $dom->prepend_content('<p>I ♥ DOM::Tiny!</p>');
801 Prepend HTML/XML fragment (for C<root> and C<tag> nodes) or raw content to this
804 # "<div><h2>Test123</h2></div>"
805 $dom->parse('<div><h2>123</h2></div>')
806 ->at('h2')->prepend_content('Test')->root;
808 # "<!-- Test 123 --><br>"
809 $dom->parse('<!-- 123 --><br>')
810 ->child_nodes->first->prepend_content(' Test')->root;
812 # "<p><i>123</i>Test</p>"
813 $dom->parse('<p>Test</p>')->at('p')->prepend_content('<i>123</i>')->root;
817 my $sibling = $dom->previous;
819 Return L<DOM::Tiny> object for previous sibling element or C<undef> if there
820 are no more siblings.
823 $dom->parse('<div><h1>Test</h1><h2>123</h2></div>')->at('h2')->previous;
827 my $sibling = $dom->previous_node;
829 Return L<DOM::Tiny> object for previous sibling node or C<undef> if there are
833 $dom->parse('<p>123<!-- Test --><b>456</b></p>')
834 ->at('b')->previous_node->previous_node;
837 $dom->parse('<p>123<!-- Test --><b>456</b></p>')
838 ->at('b')->previous_node->content;
842 my $parent = $dom->remove;
844 Remove this node and return L</"root"> (for C<root> nodes) or L</"parent">.
847 $dom->parse('<div><h1>Test</h1></div>')->at('h1')->remove;
849 # "<p><b>456</b></p>"
850 $dom->parse('<p>123<b>456</b></p>')
851 ->at('p')->child_nodes->first->remove->root;
855 my $parent = $dom->replace('<div>I ♥ DOM::Tiny!</div>');
857 Replace this node with HTML/XML fragment and return L</"root"> (for C<root>
858 nodes) or L</"parent">.
860 # "<div><h2>123</h2></div>"
861 $dom->parse('<div><h1>Test</h1></div>')->at('h1')->replace('<h2>123</h2>');
863 # "<p><b>123</b></p>"
864 $dom->parse('<p>Test</p>')
865 ->at('p')->child_nodes->[0]->replace('<b>123</b>')->root;
869 my $root = $dom->root;
871 Return L<DOM::Tiny> object for C<root> node.
875 my $parent = $dom->strip;
877 Remove this element while preserving its content and return L</"parent">.
880 $dom->parse('<div><h1>Test</h1></div>')->at('h1')->strip;
885 $dom = $dom->tag('div');
887 This element's tag name.
889 # List tag names of child elements
890 say $dom->children->map('tag')->join("\n");
894 $dom = $dom->tap(sub {...});
896 Alias for L<Mojo::Base/"tap">.
900 my $trimmed = $dom->text;
901 my $untrimmed = $dom->text(0);
903 Extract text content from this element only (not including child elements),
904 smart whitespace trimming is enabled by default.
907 $dom->parse("<div>foo\n<p>bar</p>baz\n</div>")->at('div')->text;
910 $dom->parse("<div>foo\n<p>bar</p>baz\n</div>")->at('div')->text(0);
914 my $str = $dom->to_string;
916 Render this node and its content to HTML/XML.
919 $dom->parse('<div><b>Test</b></div>')->at('div b')->to_string;
923 my $tree = $dom->tree;
924 $dom = $dom->tree(['root']);
926 Document Object Model. Note that this structure should only be used very
927 carefully since it is very dynamic.
931 my $type = $dom->type;
933 This node's type, usually C<cdata>, C<comment>, C<doctype>, C<pi>, C<raw>,
934 C<root>, C<tag> or C<text>.
937 $dom->parse('<![CDATA[Test]]>')->child_nodes->first->type;
940 $dom->parse('<!-- Test -->')->child_nodes->first->type;
943 $dom->parse('<!DOCTYPE html>')->child_nodes->first->type;
946 $dom->parse('<?xml version="1.0"?>')->child_nodes->first->type;
949 $dom->parse('<title>Test</title>')->at('title')->child_nodes->first->type;
952 $dom->parse('<p>Test</p>')->type;
955 $dom->parse('<p>Test</p>')->at('p')->type;
958 $dom->parse('<p>Test</p>')->at('p')->child_nodes->first->type;
962 my $value = $dom->val;
964 Extract value from form element (such as C<button>, C<input>, C<option>,
965 C<select> and C<textarea>) or return C<undef> if this element has no value. In
966 the case of C<select> with C<multiple> attribute, find C<option> elements with
967 C<selected> attribute and return an array reference with all values or C<undef>
968 if none could be found.
971 $dom->parse('<input name="test" value="a">')->at('input')->val;
974 $dom->parse('<textarea>b</textarea>')->at('textarea')->val;
977 $dom->parse('<option value="c">Test</option>')->at('option')->val;
980 $dom->parse('<select><option selected>d</option></select>')
984 $dom->parse('<select multiple><option selected>e</option></select>')
985 ->at('select')->val->[0];
989 $dom = $dom->wrap('<div></div>');
991 Wrap HTML/XML fragment around this node, placing it as the last child of the
992 first innermost element.
994 # "<p>123<b>Test</b></p>"
995 $dom->parse('<b>Test</b>')->at('b')->wrap('<p>123</p>')->root;
997 # "<div><p><b>Test</b></p>123</div>"
998 $dom->parse('<b>Test</b>')->at('b')->wrap('<div><p></p>123</div>')->root;
1000 # "<p><b>Test</b></p><p>123</p>"
1001 $dom->parse('<b>Test</b>')->at('b')->wrap('<p></p><p>123</p>')->root;
1003 # "<p><b>Test</b></p>"
1004 $dom->parse('<p>Test</p>')->at('p')->child_nodes->first->wrap('<b>')->root;
1008 $dom = $dom->wrap_content('<div></div>');
1010 Wrap HTML/XML fragment around this node's content, placing it as the last
1011 children of the first innermost element.
1013 # "<p><b>123Test</b></p>"
1014 $dom->parse('<p>Test<p>')->at('p')->wrap_content('<b>123</b>')->root;
1016 # "<p><b>Test</b></p><p>123</p>"
1017 $dom->parse('<b>Test</b>')->wrap_content('<p></p><p>123</p>');
1021 my $bool = $dom->xml;
1022 $dom = $dom->xml($bool);
1024 Disable HTML semantics in parser and activate case-sensitivity, defaults to
1025 auto detection based on processing instructions.
1029 L<DOM::Tiny> overloads the following operators.
1035 Alias for L</"child_nodes">.
1038 $dom->parse('<!-- Test --><b>123</b>')->[0];
1050 Alias for L</"attr">.
1053 $dom->parse('<div id="test">Test</div>')->at('div')->{id};
1059 Alias for L</"to_string">.
1063 Report any issues on the public bugtracker.
1067 Dan Book <dbook@cpan.org>
1069 =head1 COPYRIGHT AND LICENSE
1071 This software is Copyright (c) 2015 by Dan Book.
1073 This is free software, licensed under:
1075 The Artistic License 2.0 (GPL Compatible)
1079 L<Mojo::DOM>, L<XML::LibXML>, L<XML::Twig>, L<HTML::TreeBuilder>, L<XML::Smart>