7 DOM::Tiny - Minimalistic HTML/XML DOM parser with CSS selectors
14 my $dom = DOM::Tiny->new('<div><p id="a">Test</p><p id="b">123</p></div>');
17 say $dom->at('#b')->text;
18 say $dom->find('p')->map('text')->join("\n");
19 say $dom->find('[id]')->map(attr => 'id')->join("\n");
22 $dom->find('p[id]')->reverse->each(sub { say $_->{id} });
25 for my $e ($dom->find('p[id]')->each) {
26 say $e->{id}, ':', $e->text;
30 $dom->find('div p')->last->append('<p id="c">456</p>');
31 $dom->find(':not(p)')->map('strip');
38 L<DOM::Tiny> is a minimalistic and relaxed pure-perl HTML/XML DOM parser with
39 support for the L<HTML Living Standard|https://html.spec.whatwg.org/> and
40 L<CSS3 selectors|http://www.w3.org/TR/selectors/> based on L<Mojo::DOM>. It
41 will even try to interpret broken HTML and XML, so you should not use it for
44 =head1 NODES AND ELEMENTS
46 When we parse an HTML/XML fragment, it gets turned into a tree of nodes.
50 <head><title>Hello</title></head>
54 There are currently eight different kinds of nodes, C<cdata>, C<comment>,
55 C<doctype>, C<pi>, C<raw>, C<root>, C<tag> and C<text>. Elements are nodes of
67 While all node types are represented as L<DOM::Tiny> objects, some methods like
68 L</"attr"> and L</"namespace"> only apply to elements.
70 =head1 CASE-SENSITIVITY
72 L<DOM::Tiny> defaults to HTML semantics, that means all tags and attribute
73 names are lowercased and selectors need to be lowercase as well.
76 my $dom = DOM::Tiny->new('<P ID="greeting">Hi!</P>');
77 say $dom->at('p[id]')->text;
79 If XML processing instructions are found, the parser will automatically switch
80 into XML mode and everything becomes case-sensitive.
83 my $dom = DOM::Tiny->new('<?xml version="1.0"?><P ID="greeting">Hi!</P>');
84 say $dom->at('P[ID]')->text;
86 XML detection can also be disabled with the L</"xml"> method.
89 my $dom = DOM::Tiny->new->xml(1)->parse('<P ID="greeting">Hi!</P>');
90 say $dom->at('P[ID]')->text;
92 # Force HTML semantics
93 my $dom = DOM::Tiny->new->xml(0)->parse('<P ID="greeting">Hi!</P>');
94 say $dom->at('p[id]')->text;
98 L<DOM::Tiny> implements the following methods.
102 my $dom = DOM::Tiny->new;
103 my $dom = DOM::Tiny->new('<foo bar="baz">I ♥ DOM::Tiny!</foo>');
105 Construct a new scalar-based L<DOM::Tiny> object and L</"parse"> HTML/XML
106 fragment if necessary.
110 my $trimmed = $dom->all_text;
111 my $untrimmed = $dom->all_text(0);
113 Extract text content from all descendant nodes of this element, smart
114 whitespace trimming is enabled by default.
117 $dom->parse("<div>foo\n<p>bar</p>baz\n</div>")->at('div')->all_text;
120 $dom->parse("<div>foo\n<p>bar</p>baz\n</div>")->at('div')->all_text(0);
124 my $collection = $dom->ancestors;
125 my $collection = $dom->ancestors('div ~ p');
127 Find all ancestor elements of this node matching the CSS selector and return a
128 L<DOM::Tiny::Collection> object containing these elements as L<DOM::Tiny>
129 objects. All selectors from L<DOM::Tiny::CSS/"SELECTORS"> are supported.
131 # List tag names of ancestor elements
132 say $dom->ancestors->map('tag')->join("\n");
136 $dom = $dom->append('<p>I ♥ DOM::Tiny!</p>');
138 Append HTML/XML fragment to this node.
140 # "<div><h1>Test</h1><h2>123</h2></div>"
141 $dom->parse('<div><h1>Test</h1></div>')
142 ->at('h1')->append('<h2>123</h2>')->root;
145 $dom->parse('<p>Test</p>')->at('p')
146 ->child_nodes->first->append(' 123')->root;
148 =head2 append_content
150 $dom = $dom->append_content('<p>I ♥ DOM::Tiny!</p>');
152 Append HTML/XML fragment (for C<root> and C<tag> nodes) or raw content to this
155 # "<div><h1>Test123</h1></div>"
156 $dom->parse('<div><h1>Test</h1></div>')
157 ->at('h1')->append_content('123')->root;
159 # "<!-- Test 123 --><br>"
160 $dom->parse('<!-- Test --><br>')
161 ->child_nodes->first->append_content('123 ')->root;
163 # "<p>Test<i>123</i></p>"
164 $dom->parse('<p>Test</p>')->at('p')->append_content('<i>123</i>')->root;
168 my $result = $dom->at('div ~ p');
170 Find first descendant element of this element matching the CSS selector and
171 return it as a L<DOM::Tiny> object or return C<undef> if none could be found.
172 All selectors from L<DOM::Tiny::CSS/"SELECTORS"> are supported.
174 # Find first element with "svg" namespace definition
175 my $namespace = $dom->at('[xmlns\:svg]')->{'xmlns:svg'};
179 my $hash = $dom->attr;
180 my $foo = $dom->attr('foo');
181 $dom = $dom->attr({foo => 'bar'});
182 $dom = $dom->attr(foo => 'bar');
184 This element's attributes.
186 # Remove an attribute
187 delete $dom->attr->{id};
189 # Attribute without value
190 $dom->attr(selected => undef);
193 say $dom->find('*')->map(attr => 'id')->compact->join("\n");
197 my $collection = $dom->child_nodes;
199 Return a L<DOM::Tiny::Collection> object containing all child nodes of this
200 element as L<DOM::Tiny> objects.
202 # "<p><b>123</b></p>"
203 $dom->parse('<p>Test<b>123</b></p>')->at('p')->child_nodes->first->remove;
206 $dom->parse('<!DOCTYPE html><b>123</b>')->child_nodes->first;
209 $dom->parse('<b>123</b><!-- Test -->')->child_nodes->last->content;
213 my $collection = $dom->children;
214 my $collection = $dom->children('div ~ p');
216 Find all child elements of this element matching the CSS selector and return a
217 L<DOM::Tiny::Collection> object containing these elements as L<DOM::Tiny>
218 objects. All selectors from L<DOM::Tiny::CSS/"SELECTORS"> are supported.
220 # Show tag name of random child element
221 say $dom->children->shuffle->first->tag;
225 my $str = $dom->content;
226 $dom = $dom->content('<p>I ♥ DOM::Tiny!</p>');
228 Return this node's content or replace it with HTML/XML fragment (for C<root>
229 and C<tag> nodes) or raw content.
232 $dom->parse('<div><b>Test</b></div>')->at('div')->content;
234 # "<div><h1>123</h1></div>"
235 $dom->parse('<div><h1>Test</h1></div>')->at('h1')->content('123')->root;
237 # "<p><i>123</i></p>"
238 $dom->parse('<p>Test</p>')->at('p')->content('<i>123</i>')->root;
240 # "<div><h1></h1></div>"
241 $dom->parse('<div><h1>Test</h1></div>')->at('h1')->content('')->root;
244 $dom->parse('<!-- Test --><br>')->child_nodes->first->content;
246 # "<div><!-- 123 -->456</div>"
247 $dom->parse('<div><!-- Test -->456</div>')
248 ->at('div')->child_nodes->first->content(' 123 ')->root;
250 =head2 descendant_nodes
252 my $collection = $dom->descendant_nodes;
254 Return a L<DOM::Tiny::Collection> object containing all descendant nodes of
255 this element as L<DOM::Tiny> objects.
257 # "<p><b>123</b></p>"
258 $dom->parse('<p><!-- Test --><b>123<!-- 456 --></b></p>')
259 ->descendant_nodes->grep(sub { $_->type eq 'comment' })
260 ->map('remove')->first;
262 # "<p><b>test</b>test</p>"
263 $dom->parse('<p><b>123</b>456</p>')
264 ->at('p')->descendant_nodes->grep(sub { $_->type eq 'text' })
265 ->map(content => 'test')->first->root;
269 my $collection = $dom->find('div ~ p');
271 Find all descendant elements of this element matching the CSS selector and
272 return a L<DOM::Tiny::Collection> object containing these elements as
273 L<DOM::Tiny> objects. All selectors from L<DOM::Tiny::CSS/"SELECTORS"> are
276 # Find a specific element and extract information
277 my $id = $dom->find('div')->[23]{id};
279 # Extract information from multiple elements
280 my @headers = $dom->find('h1, h2, h3')->map('text')->each;
282 # Count all the different tags
283 my $hash = $dom->find('*')->reduce(sub { $a->{$b->tag}++; $a }, {});
285 # Find elements with a class that contains dots
286 my @divs = $dom->find('div.foo\.bar')->each;
290 my $collection = $dom->following;
291 my $collection = $dom->following('div ~ p');
293 Find all sibling elements after this node matching the CSS selector and return
294 a L<DOM::Tiny::Collection> object containing these elements as L<DOM::Tiny>
295 objects. All selectors from L<DOM::Tiny::CSS/"SELECTORS"> are supported.
297 # List tags of sibling elements after this node
298 say $dom->following->map('tag')->join("\n");
300 =head2 following_nodes
302 my $collection = $dom->following_nodes;
304 Return a L<DOM::Tiny::Collection> object containing all sibling nodes after
305 this node as L<DOM::Tiny> objects.
308 $dom->parse('<p>A</p><!-- B -->C')->at('p')->following_nodes->last->content;
312 my $bool = $dom->matches('div ~ p');
314 Check if this element matches the CSS selector. All selectors from
315 L<DOM::Tiny::CSS/"SELECTORS"> are supported.
318 $dom->parse('<p class="a">A</p>')->at('p')->matches('.a');
319 $dom->parse('<p class="a">A</p>')->at('p')->matches('p[class]');
322 $dom->parse('<p class="a">A</p>')->at('p')->matches('.b');
323 $dom->parse('<p class="a">A</p>')->at('p')->matches('p[id]');
327 my $namespace = $dom->namespace;
329 Find this element's namespace or return C<undef> if none could be found.
331 # Find namespace for an element with namespace prefix
332 my $namespace = $dom->at('svg > svg\:circle')->namespace;
334 # Find namespace for an element that may or may not have a namespace prefix
335 my $namespace = $dom->at('svg > circle')->namespace;
339 my $sibling = $dom->next;
341 Return L<DOM::Tiny> object for next sibling element or C<undef> if there are no
345 $dom->parse('<div><h1>Test</h1><h2>123</h2></div>')->at('h1')->next;
349 my $sibling = $dom->next_node;
351 Return L<DOM::Tiny> object for next sibling node or C<undef> if there are no
355 $dom->parse('<p><b>123</b><!-- Test -->456</p>')
356 ->at('b')->next_node->next_node;
359 $dom->parse('<p><b>123</b><!-- Test -->456</p>')
360 ->at('b')->next_node->content;
364 my $parent = $dom->parent;
366 Return L<DOM::Tiny> object for parent of this node or C<undef> if this node has
371 $dom = $dom->parse('<foo bar="baz">I ♥ DOM::Tiny!</foo>');
373 Parse HTML/XML fragment with L<DOM::Tiny::HTML>.
376 my $dom = DOM::Tiny->new->xml(1)->parse($xml);
380 my $collection = $dom->preceding;
381 my $collection = $dom->preceding('div ~ p');
383 Find all sibling elements before this node matching the CSS selector and return
384 a L<DOM::Tiny::Collection> object containing these elements as L<DOM::Tiny>
385 objects. All selectors from L<DOM::Tiny::CSS/"SELECTORS"> are supported.
387 # List tags of sibling elements before this node
388 say $dom->preceding->map('tag')->join("\n");
390 =head2 preceding_nodes
392 my $collection = $dom->preceding_nodes;
394 Return a L<DOM::Tiny::Collection> object containing all sibling nodes before
395 this node as L<DOM::Tiny> objects.
398 $dom->parse('A<!-- B --><p>C</p>')->at('p')->preceding_nodes->first->content;
402 $dom = $dom->prepend('<p>I ♥ DOM::Tiny!</p>');
404 Prepend HTML/XML fragment to this node.
406 # "<div><h1>Test</h1><h2>123</h2></div>"
407 $dom->parse('<div><h2>123</h2></div>')
408 ->at('h2')->prepend('<h1>Test</h1>')->root;
411 $dom->parse('<p>123</p>')
412 ->at('p')->child_nodes->first->prepend('Test ')->root;
414 =head2 prepend_content
416 $dom = $dom->prepend_content('<p>I ♥ DOM::Tiny!</p>');
418 Prepend HTML/XML fragment (for C<root> and C<tag> nodes) or raw content to this
421 # "<div><h2>Test123</h2></div>"
422 $dom->parse('<div><h2>123</h2></div>')
423 ->at('h2')->prepend_content('Test')->root;
425 # "<!-- Test 123 --><br>"
426 $dom->parse('<!-- 123 --><br>')
427 ->child_nodes->first->prepend_content(' Test')->root;
429 # "<p><i>123</i>Test</p>"
430 $dom->parse('<p>Test</p>')->at('p')->prepend_content('<i>123</i>')->root;
434 my $sibling = $dom->previous;
436 Return L<DOM::Tiny> object for previous sibling element or C<undef> if there
437 are no more siblings.
440 $dom->parse('<div><h1>Test</h1><h2>123</h2></div>')->at('h2')->previous;
444 my $sibling = $dom->previous_node;
446 Return L<DOM::Tiny> object for previous sibling node or C<undef> if there are
450 $dom->parse('<p>123<!-- Test --><b>456</b></p>')
451 ->at('b')->previous_node->previous_node;
454 $dom->parse('<p>123<!-- Test --><b>456</b></p>')
455 ->at('b')->previous_node->content;
459 my $parent = $dom->remove;
461 Remove this node and return L</"root"> (for C<root> nodes) or L</"parent">.
464 $dom->parse('<div><h1>Test</h1></div>')->at('h1')->remove;
466 # "<p><b>456</b></p>"
467 $dom->parse('<p>123<b>456</b></p>')
468 ->at('p')->child_nodes->first->remove->root;
472 my $parent = $dom->replace('<div>I ♥ DOM::Tiny!</div>');
474 Replace this node with HTML/XML fragment and return L</"root"> (for C<root>
475 nodes) or L</"parent">.
477 # "<div><h2>123</h2></div>"
478 $dom->parse('<div><h1>Test</h1></div>')->at('h1')->replace('<h2>123</h2>');
480 # "<p><b>123</b></p>"
481 $dom->parse('<p>Test</p>')
482 ->at('p')->child_nodes->[0]->replace('<b>123</b>')->root;
486 my $root = $dom->root;
488 Return L<DOM::Tiny> object for C<root> node.
492 my $parent = $dom->strip;
494 Remove this element while preserving its content and return L</"parent">.
497 $dom->parse('<div><h1>Test</h1></div>')->at('h1')->strip;
502 $dom = $dom->tag('div');
504 This element's tag name.
506 # List tag names of child elements
507 say $dom->children->map('tag')->join("\n");
511 $dom = $dom->tap(sub {...});
513 Equivalent to L<Mojo::Base/"tap">.
517 my $trimmed = $dom->text;
518 my $untrimmed = $dom->text(0);
520 Extract text content from this element only (not including child elements),
521 smart whitespace trimming is enabled by default.
524 $dom->parse("<div>foo\n<p>bar</p>baz\n</div>")->at('div')->text;
527 $dom->parse("<div>foo\n<p>bar</p>baz\n</div>")->at('div')->text(0);
531 my $str = $dom->to_string;
533 Render this node and its content to HTML/XML.
536 $dom->parse('<div><b>Test</b></div>')->at('div b')->to_string;
540 my $tree = $dom->tree;
541 $dom = $dom->tree(['root']);
543 Document Object Model. Note that this structure should only be used very
544 carefully since it is very dynamic.
548 my $type = $dom->type;
550 This node's type, usually C<cdata>, C<comment>, C<doctype>, C<pi>, C<raw>,
551 C<root>, C<tag> or C<text>.
554 $dom->parse('<![CDATA[Test]]>')->child_nodes->first->type;
557 $dom->parse('<!-- Test -->')->child_nodes->first->type;
560 $dom->parse('<!DOCTYPE html>')->child_nodes->first->type;
563 $dom->parse('<?xml version="1.0"?>')->child_nodes->first->type;
566 $dom->parse('<title>Test</title>')->at('title')->child_nodes->first->type;
569 $dom->parse('<p>Test</p>')->type;
572 $dom->parse('<p>Test</p>')->at('p')->type;
575 $dom->parse('<p>Test</p>')->at('p')->child_nodes->first->type;
579 my $value = $dom->val;
581 Extract value from form element (such as C<button>, C<input>, C<option>,
582 C<select> and C<textarea>) or return C<undef> if this element has no value. In
583 the case of C<select> with C<multiple> attribute, find C<option> elements with
584 C<selected> attribute and return an array reference with all values or C<undef>
585 if none could be found.
588 $dom->parse('<input name="test" value="a">')->at('input')->val;
591 $dom->parse('<textarea>b</textarea>')->at('textarea')->val;
594 $dom->parse('<option value="c">Test</option>')->at('option')->val;
597 $dom->parse('<select><option selected>d</option></select>')
601 $dom->parse('<select multiple><option selected>e</option></select>')
602 ->at('select')->val->[0];
606 $dom = $dom->wrap('<div></div>');
608 Wrap HTML/XML fragment around this node, placing it as the last child of the
609 first innermost element.
611 # "<p>123<b>Test</b></p>"
612 $dom->parse('<b>Test</b>')->at('b')->wrap('<p>123</p>')->root;
614 # "<div><p><b>Test</b></p>123</div>"
615 $dom->parse('<b>Test</b>')->at('b')->wrap('<div><p></p>123</div>')->root;
617 # "<p><b>Test</b></p><p>123</p>"
618 $dom->parse('<b>Test</b>')->at('b')->wrap('<p></p><p>123</p>')->root;
620 # "<p><b>Test</b></p>"
621 $dom->parse('<p>Test</p>')->at('p')->child_nodes->first->wrap('<b>')->root;
625 $dom = $dom->wrap_content('<div></div>');
627 Wrap HTML/XML fragment around this node's content, placing it as the last
628 children of the first innermost element.
630 # "<p><b>123Test</b></p>"
631 $dom->parse('<p>Test<p>')->at('p')->wrap_content('<b>123</b>')->root;
633 # "<p><b>Test</b></p><p>123</p>"
634 $dom->parse('<b>Test</b>')->wrap_content('<p></p><p>123</p>');
638 my $bool = $dom->xml;
639 $dom = $dom->xml($bool);
641 Disable HTML semantics in parser and activate case-sensitivity, defaults to
642 auto detection based on processing instructions.
646 L<DOM::Tiny> overloads the following operators.
652 Alias for L</"child_nodes">.
655 $dom->parse('<!-- Test --><b>123</b>')->[0];
667 Alias for L</"attr">.
670 $dom->parse('<div id="test">Test</div>')->at('div')->{id};
676 Alias for L</"to_string">.
680 Report any issues on the public bugtracker.
684 Dan Book <dbook@cpan.org>
686 =head1 COPYRIGHT AND LICENSE
688 This software is Copyright (c) 2015 by Dan Book.
690 This is free software, licensed under:
692 The Artistic License 2.0 (GPL Compatible)
696 L<Mojo::DOM>, L<XML::LibXML>, L<XML::Twig>, L<HTML::TreeBuilder>, L<XML::Smart>