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 based
39 on L<Mojo::DOM>. It supports the L<HTML Living Standard|https://html.spec.whatwg.org/>
40 and L<Extensible Markup Language (XML) 1.0|http://www.w3.org/TR/xml/>, and
41 matching based on L<CSS3 selectors|http://www.w3.org/TR/selectors/>. It will
42 even try to interpret broken HTML and XML, so you should not use it for
45 =head1 NODES AND ELEMENTS
47 When we parse an HTML/XML fragment, it gets turned into a tree of nodes.
51 <head><title>Hello</title></head>
55 There are currently eight different kinds of nodes, C<cdata>, C<comment>,
56 C<doctype>, C<pi>, C<raw>, C<root>, C<tag> and C<text>. Elements are nodes of
68 While all node types are represented as L<DOM::Tiny> objects, some methods like
69 L</"attr"> and L</"namespace"> only apply to elements.
71 =head1 CASE-SENSITIVITY
73 L<DOM::Tiny> defaults to HTML semantics, that means all tags and attribute
74 names are lowercased and selectors need to be lowercase as well.
77 my $dom = DOM::Tiny->new('<P ID="greeting">Hi!</P>');
78 say $dom->at('p[id]')->text;
80 If XML processing instructions are found, the parser will automatically switch
81 into XML mode and everything becomes case-sensitive.
84 my $dom = DOM::Tiny->new('<?xml version="1.0"?><P ID="greeting">Hi!</P>');
85 say $dom->at('P[ID]')->text;
87 XML detection can also be disabled with the L</"xml"> method.
90 my $dom = DOM::Tiny->new->xml(1)->parse('<P ID="greeting">Hi!</P>');
91 say $dom->at('P[ID]')->text;
93 # Force HTML semantics
94 my $dom = DOM::Tiny->new->xml(0)->parse('<P ID="greeting">Hi!</P>');
95 say $dom->at('p[id]')->text;
99 L<DOM::Tiny> overloads the following operators.
105 Alias for L</"child_nodes">.
108 $dom->parse('<!-- Test --><b>123</b>')->[0];
120 Alias for L</"attr">.
123 $dom->parse('<div id="test">Test</div>')->at('div')->{id};
129 Alias for L</"to_string">.
133 L<DOM::Tiny> implements the following methods.
137 my $dom = DOM::Tiny->new;
138 my $dom = DOM::Tiny->new('<foo bar="baz">I ♥ DOM::Tiny!</foo>');
140 Construct a new scalar-based L<DOM::Tiny> object and L</"parse"> HTML/XML
141 fragment if necessary.
145 my $trimmed = $dom->all_text;
146 my $untrimmed = $dom->all_text(0);
148 Extract text content from all descendant nodes of this element, smart
149 whitespace trimming is enabled by default.
152 $dom->parse("<div>foo\n<p>bar</p>baz\n</div>")->at('div')->all_text;
155 $dom->parse("<div>foo\n<p>bar</p>baz\n</div>")->at('div')->all_text(0);
159 my $collection = $dom->ancestors;
160 my $collection = $dom->ancestors('div ~ p');
162 Find all ancestor elements of this node matching the CSS selector and return a
163 L<collection|/"COLLECTION METHODS"> containing these elements as L<DOM::Tiny>
164 objects. All selectors listed in L</"SELECTORS"> are supported.
166 # List tag names of ancestor elements
167 say $dom->ancestors->map('tag')->join("\n");
171 $dom = $dom->append('<p>I ♥ DOM::Tiny!</p>');
173 Append HTML/XML fragment to this node.
175 # "<div><h1>Test</h1><h2>123</h2></div>"
176 $dom->parse('<div><h1>Test</h1></div>')
177 ->at('h1')->append('<h2>123</h2>')->root;
180 $dom->parse('<p>Test</p>')->at('p')
181 ->child_nodes->first->append(' 123')->root;
183 =head2 append_content
185 $dom = $dom->append_content('<p>I ♥ DOM::Tiny!</p>');
187 Append HTML/XML fragment (for C<root> and C<tag> nodes) or raw content to this
190 # "<div><h1>Test123</h1></div>"
191 $dom->parse('<div><h1>Test</h1></div>')
192 ->at('h1')->append_content('123')->root;
194 # "<!-- Test 123 --><br>"
195 $dom->parse('<!-- Test --><br>')
196 ->child_nodes->first->append_content('123 ')->root;
198 # "<p>Test<i>123</i></p>"
199 $dom->parse('<p>Test</p>')->at('p')->append_content('<i>123</i>')->root;
203 my $result = $dom->at('div ~ p');
205 Find first descendant element of this element matching the CSS selector and
206 return it as a L<DOM::Tiny> object or return C<undef> if none could be found.
207 All selectors listed in L</"SELECTORS"> are supported.
209 # Find first element with "svg" namespace definition
210 my $namespace = $dom->at('[xmlns\:svg]')->{'xmlns:svg'};
214 my $hash = $dom->attr;
215 my $foo = $dom->attr('foo');
216 $dom = $dom->attr({foo => 'bar'});
217 $dom = $dom->attr(foo => 'bar');
219 This element's attributes.
221 # Remove an attribute
222 delete $dom->attr->{id};
224 # Attribute without value
225 $dom->attr(selected => undef);
228 say $dom->find('*')->map(attr => 'id')->compact->join("\n");
232 my $collection = $dom->child_nodes;
234 Return a L<collection|/"COLLECTION METHODS"> containing all child nodes of this
235 element as L<DOM::Tiny> objects.
237 # "<p><b>123</b></p>"
238 $dom->parse('<p>Test<b>123</b></p>')->at('p')->child_nodes->first->remove;
241 $dom->parse('<!DOCTYPE html><b>123</b>')->child_nodes->first;
244 $dom->parse('<b>123</b><!-- Test -->')->child_nodes->last->content;
248 my $collection = $dom->children;
249 my $collection = $dom->children('div ~ p');
251 Find all child elements of this element matching the CSS selector and return a
252 L<collection|/"COLLECTION METHODS"> containing these elements as L<DOM::Tiny>
253 objects. All selectors listed in L</"SELECTORS"> are supported.
255 # Show tag name of random child element
256 say $dom->children->shuffle->first->tag;
260 my $str = $dom->content;
261 $dom = $dom->content('<p>I ♥ DOM::Tiny!</p>');
263 Return this node's content or replace it with HTML/XML fragment (for C<root>
264 and C<tag> nodes) or raw content.
267 $dom->parse('<div><b>Test</b></div>')->at('div')->content;
269 # "<div><h1>123</h1></div>"
270 $dom->parse('<div><h1>Test</h1></div>')->at('h1')->content('123')->root;
272 # "<p><i>123</i></p>"
273 $dom->parse('<p>Test</p>')->at('p')->content('<i>123</i>')->root;
275 # "<div><h1></h1></div>"
276 $dom->parse('<div><h1>Test</h1></div>')->at('h1')->content('')->root;
279 $dom->parse('<!-- Test --><br>')->child_nodes->first->content;
281 # "<div><!-- 123 -->456</div>"
282 $dom->parse('<div><!-- Test -->456</div>')
283 ->at('div')->child_nodes->first->content(' 123 ')->root;
285 =head2 descendant_nodes
287 my $collection = $dom->descendant_nodes;
289 Return a L<collection|/"COLLECTION METHODS"> containing all descendant nodes of
290 this element as L<DOM::Tiny> objects.
292 # "<p><b>123</b></p>"
293 $dom->parse('<p><!-- Test --><b>123<!-- 456 --></b></p>')
294 ->descendant_nodes->grep(sub { $_->type eq 'comment' })
295 ->map('remove')->first;
297 # "<p><b>test</b>test</p>"
298 $dom->parse('<p><b>123</b>456</p>')
299 ->at('p')->descendant_nodes->grep(sub { $_->type eq 'text' })
300 ->map(content => 'test')->first->root;
304 my $collection = $dom->find('div ~ p');
306 Find all descendant elements of this element matching the CSS selector and
307 return a L<collection|/"COLLECTION METHODS"> containing these elements as
308 L<DOM::Tiny> objects. All selectors listed in L</"SELECTORS"> are supported.
310 # Find a specific element and extract information
311 my $id = $dom->find('div')->[23]{id};
313 # Extract information from multiple elements
314 my @headers = $dom->find('h1, h2, h3')->map('text')->each;
316 # Count all the different tags
317 my $hash = $dom->find('*')->reduce(sub { $a->{$b->tag}++; $a }, {});
319 # Find elements with a class that contains dots
320 my @divs = $dom->find('div.foo\.bar')->each;
324 my $collection = $dom->following;
325 my $collection = $dom->following('div ~ p');
327 Find all sibling elements after this node matching the CSS selector and return
328 a L<collection|/"COLLECTION METHODS"> containing these elements as L<DOM::Tiny>
329 objects. All selectors listen in L</"SELECTORS"> are supported.
331 # List tags of sibling elements after this node
332 say $dom->following->map('tag')->join("\n");
334 =head2 following_nodes
336 my $collection = $dom->following_nodes;
338 Return a L<collection|/"COLLECTION METHODS"> containing all sibling nodes after
339 this node as L<DOM::Tiny> objects.
342 $dom->parse('<p>A</p><!-- B -->C')->at('p')->following_nodes->last->content;
346 my $bool = $dom->matches('div ~ p');
348 Check if this element matches the CSS selector. All selectors listed in
349 L</"SELECTORS"> are supported.
352 $dom->parse('<p class="a">A</p>')->at('p')->matches('.a');
353 $dom->parse('<p class="a">A</p>')->at('p')->matches('p[class]');
356 $dom->parse('<p class="a">A</p>')->at('p')->matches('.b');
357 $dom->parse('<p class="a">A</p>')->at('p')->matches('p[id]');
361 my $namespace = $dom->namespace;
363 Find this element's namespace or return C<undef> if none could be found.
365 # Find namespace for an element with namespace prefix
366 my $namespace = $dom->at('svg > svg\:circle')->namespace;
368 # Find namespace for an element that may or may not have a namespace prefix
369 my $namespace = $dom->at('svg > circle')->namespace;
373 my $sibling = $dom->next;
375 Return L<DOM::Tiny> object for next sibling element or C<undef> if there are no
379 $dom->parse('<div><h1>Test</h1><h2>123</h2></div>')->at('h1')->next;
383 my $sibling = $dom->next_node;
385 Return L<DOM::Tiny> object for next sibling node or C<undef> if there are no
389 $dom->parse('<p><b>123</b><!-- Test -->456</p>')
390 ->at('b')->next_node->next_node;
393 $dom->parse('<p><b>123</b><!-- Test -->456</p>')
394 ->at('b')->next_node->content;
398 my $parent = $dom->parent;
400 Return L<DOM::Tiny> object for parent of this node or C<undef> if this node has
405 $dom = $dom->parse('<foo bar="baz">I ♥ DOM::Tiny!</foo>');
407 Parse HTML/XML fragment.
410 my $dom = DOM::Tiny->new->xml(1)->parse($xml);
414 my $collection = $dom->preceding;
415 my $collection = $dom->preceding('div ~ p');
417 Find all sibling elements before this node matching the CSS selector and return
418 a L<collection|/"COLLECTION METHODS"> containing these elements as L<DOM::Tiny>
419 objects. All selectors listed in L</"SELECTORS"> are supported.
421 # List tags of sibling elements before this node
422 say $dom->preceding->map('tag')->join("\n");
424 =head2 preceding_nodes
426 my $collection = $dom->preceding_nodes;
428 Return a L<collection|/"COLLECTION METHODS"> containing all sibling nodes
429 before this node as L<DOM::Tiny> objects.
432 $dom->parse('A<!-- B --><p>C</p>')->at('p')->preceding_nodes->first->content;
436 $dom = $dom->prepend('<p>I ♥ DOM::Tiny!</p>');
438 Prepend HTML/XML fragment to this node.
440 # "<div><h1>Test</h1><h2>123</h2></div>"
441 $dom->parse('<div><h2>123</h2></div>')
442 ->at('h2')->prepend('<h1>Test</h1>')->root;
445 $dom->parse('<p>123</p>')
446 ->at('p')->child_nodes->first->prepend('Test ')->root;
448 =head2 prepend_content
450 $dom = $dom->prepend_content('<p>I ♥ DOM::Tiny!</p>');
452 Prepend HTML/XML fragment (for C<root> and C<tag> nodes) or raw content to this
455 # "<div><h2>Test123</h2></div>"
456 $dom->parse('<div><h2>123</h2></div>')
457 ->at('h2')->prepend_content('Test')->root;
459 # "<!-- Test 123 --><br>"
460 $dom->parse('<!-- 123 --><br>')
461 ->child_nodes->first->prepend_content(' Test')->root;
463 # "<p><i>123</i>Test</p>"
464 $dom->parse('<p>Test</p>')->at('p')->prepend_content('<i>123</i>')->root;
468 my $sibling = $dom->previous;
470 Return L<DOM::Tiny> object for previous sibling element or C<undef> if there
471 are no more siblings.
474 $dom->parse('<div><h1>Test</h1><h2>123</h2></div>')->at('h2')->previous;
478 my $sibling = $dom->previous_node;
480 Return L<DOM::Tiny> object for previous sibling node or C<undef> if there are
484 $dom->parse('<p>123<!-- Test --><b>456</b></p>')
485 ->at('b')->previous_node->previous_node;
488 $dom->parse('<p>123<!-- Test --><b>456</b></p>')
489 ->at('b')->previous_node->content;
493 my $parent = $dom->remove;
495 Remove this node and return L</"root"> (for C<root> nodes) or L</"parent">.
498 $dom->parse('<div><h1>Test</h1></div>')->at('h1')->remove;
500 # "<p><b>456</b></p>"
501 $dom->parse('<p>123<b>456</b></p>')
502 ->at('p')->child_nodes->first->remove->root;
506 my $parent = $dom->replace('<div>I ♥ DOM::Tiny!</div>');
508 Replace this node with HTML/XML fragment and return L</"root"> (for C<root>
509 nodes) or L</"parent">.
511 # "<div><h2>123</h2></div>"
512 $dom->parse('<div><h1>Test</h1></div>')->at('h1')->replace('<h2>123</h2>');
514 # "<p><b>123</b></p>"
515 $dom->parse('<p>Test</p>')
516 ->at('p')->child_nodes->[0]->replace('<b>123</b>')->root;
520 my $root = $dom->root;
522 Return L<DOM::Tiny> object for C<root> node.
526 my $parent = $dom->strip;
528 Remove this element while preserving its content and return L</"parent">.
531 $dom->parse('<div><h1>Test</h1></div>')->at('h1')->strip;
536 $dom = $dom->tag('div');
538 This element's tag name.
540 # List tag names of child elements
541 say $dom->children->map('tag')->join("\n");
545 $dom = $dom->tap(sub {...});
547 Equivalent to L<Mojo::Base/"tap">.
551 my $trimmed = $dom->text;
552 my $untrimmed = $dom->text(0);
554 Extract text content from this element only (not including child elements),
555 smart whitespace trimming is enabled by default.
558 $dom->parse("<div>foo\n<p>bar</p>baz\n</div>")->at('div')->text;
561 $dom->parse("<div>foo\n<p>bar</p>baz\n</div>")->at('div')->text(0);
565 my $str = $dom->to_string;
567 Render this node and its content to HTML/XML.
570 $dom->parse('<div><b>Test</b></div>')->at('div b')->to_string;
574 my $tree = $dom->tree;
575 $dom = $dom->tree(['root']);
577 Document Object Model. Note that this structure should only be used very
578 carefully since it is very dynamic.
582 my $type = $dom->type;
584 This node's type, usually C<cdata>, C<comment>, C<doctype>, C<pi>, C<raw>,
585 C<root>, C<tag> or C<text>.
588 $dom->parse('<![CDATA[Test]]>')->child_nodes->first->type;
591 $dom->parse('<!-- Test -->')->child_nodes->first->type;
594 $dom->parse('<!DOCTYPE html>')->child_nodes->first->type;
597 $dom->parse('<?xml version="1.0"?>')->child_nodes->first->type;
600 $dom->parse('<title>Test</title>')->at('title')->child_nodes->first->type;
603 $dom->parse('<p>Test</p>')->type;
606 $dom->parse('<p>Test</p>')->at('p')->type;
609 $dom->parse('<p>Test</p>')->at('p')->child_nodes->first->type;
613 my $value = $dom->val;
615 Extract value from form element (such as C<button>, C<input>, C<option>,
616 C<select> and C<textarea>) or return C<undef> if this element has no value. In
617 the case of C<select> with C<multiple> attribute, find C<option> elements with
618 C<selected> attribute and return an array reference with all values or C<undef>
619 if none could be found.
622 $dom->parse('<input name="test" value="a">')->at('input')->val;
625 $dom->parse('<textarea>b</textarea>')->at('textarea')->val;
628 $dom->parse('<option value="c">Test</option>')->at('option')->val;
631 $dom->parse('<select><option selected>d</option></select>')
635 $dom->parse('<select multiple><option selected>e</option></select>')
636 ->at('select')->val->[0];
640 $dom = $dom->wrap('<div></div>');
642 Wrap HTML/XML fragment around this node, placing it as the last child of the
643 first innermost element.
645 # "<p>123<b>Test</b></p>"
646 $dom->parse('<b>Test</b>')->at('b')->wrap('<p>123</p>')->root;
648 # "<div><p><b>Test</b></p>123</div>"
649 $dom->parse('<b>Test</b>')->at('b')->wrap('<div><p></p>123</div>')->root;
651 # "<p><b>Test</b></p><p>123</p>"
652 $dom->parse('<b>Test</b>')->at('b')->wrap('<p></p><p>123</p>')->root;
654 # "<p><b>Test</b></p>"
655 $dom->parse('<p>Test</p>')->at('p')->child_nodes->first->wrap('<b>')->root;
659 $dom = $dom->wrap_content('<div></div>');
661 Wrap HTML/XML fragment around this node's content, placing it as the last
662 children of the first innermost element.
664 # "<p><b>123Test</b></p>"
665 $dom->parse('<p>Test<p>')->at('p')->wrap_content('<b>123</b>')->root;
667 # "<p><b>Test</b></p><p>123</p>"
668 $dom->parse('<b>Test</b>')->wrap_content('<p></p><p>123</p>');
672 my $bool = $dom->xml;
673 $dom = $dom->xml($bool);
675 Disable HTML semantics in parser and activate case-sensitivity, defaults to
676 auto detection based on processing instructions.
678 =head1 COLLECTION METHODS
680 Some L<DOM::Tiny> methods return an array-based collection object based on
681 L<Mojo::Collection>, which can either be accessed directly as an array
682 reference, or with the following methods.
685 $collection->map(sub { ucfirst })->shuffle->each(sub {
686 my ($word, $num) = @_;
690 # Access array directly to manipulate collection
691 $collection->[23] += 100;
692 say for @$collection;
696 my $new = $collection->compact;
698 Create a new collection with all elements that are defined and not an empty
701 # $collection contains (0, 1, undef, 2, '', 3)
702 $collection->compact->join(', '); # "0, 1, 2, 3"
706 my @elements = $collection->each;
707 $collection = $collection->each(sub {...});
709 Evaluate callback for each element in collection or return all elements as a
710 list if none has been provided. The element will be the first argument passed
711 to the callback and is also available as C<$_>.
713 # Make a numbered list
714 $collection->each(sub {
721 my $first = $collection->first;
722 my $first = $collection->first(qr/foo/);
723 my $first = $collection->first(sub {...});
724 my $first = $collection->first($method);
725 my $first = $collection->first($method, @args);
727 Evaluate regular expression/callback for, or call method on, each element in
728 collection and return the first one that matched the regular expression, or for
729 which the callback/method returned true. The element will be the first argument
730 passed to the callback and is also available as C<$_>.
733 my $first = $collection->first(sub { $_->$method(@args) });
735 # Find first value that contains the word "dom"
736 my $interesting = $collection->first(qr/dom/i);
738 # Find first value that is greater than 5
739 my $greater = $collection->first(sub { $_ > 5 });
743 my $new = $collection->flatten;
745 Flatten nested collections/arrays recursively and create a new collection with
748 # $collection contains (1, [2, [3, 4], 5, [6]], 7)
749 $collection->flatten->join(', '); # "1, 2, 3, 4, 5, 6, 7"
753 my $new = $collection->grep(qr/foo/);
754 my $new = $collection->grep(sub {...});
755 my $new = $collection->grep($method);
756 my $new = $collection->grep($method, @args);
758 Evaluate regular expression/callback for, or call method on, each element in
759 collection and create a new collection with all elements that matched the
760 regular expression, or for which the callback/method returned true. The element
761 will be the first argument passed to the callback and is also available as
765 my $new = $collection->grep(sub { $_->$method(@args) });
767 # Find all values that contain the word "dom"
768 my $interesting = $collection->grep(qr/dom/i);
770 # Find all values that are greater than 5
771 my $greater = $collection->grep(sub { $_ > 5 });
775 my $stream = $collection->join;
776 my $stream = $collection->join("\n");
778 Turn collection into string.
780 # Join all values with commas
781 $collection->join(', ');
785 my $last = $collection->last;
787 Return the last element in collection.
791 my $new = $collection->map(sub {...});
792 my $new = $collection->map($method);
793 my $new = $collection->map($method, @args);
795 Evaluate callback for, or call method on, each element in collection and create
796 a new collection from the results. The element will be the first argument
797 passed to the callback and is also available as C<$_>.
800 my $new = $collection->map(sub { $_->$method(@args) });
802 # Append the word "dom" to all values
803 my $domified = $collection->map(sub { $_ . 'dom' });
807 my $result = $collection->reduce(sub {...});
808 my $result = $collection->reduce(sub {...}, $initial);
810 Reduce elements in collection with callback, the first element will be used as
811 initial value if none has been provided.
813 # Calculate the sum of all values
814 my $sum = $collection->reduce(sub { $a + $b });
816 # Count how often each value occurs in collection
817 my $hash = $collection->reduce(sub { $a->{$b}++; $a }, {});
821 my $new = $collection->reverse;
823 Create a new collection with all elements in reverse order.
827 my $new = $collection->slice(4 .. 7);
829 Create a new collection with all selected elements.
831 # $collection contains ('A', 'B', 'C', 'D', 'E')
832 $collection->slice(1, 2, 4)->join(' '); # "B C E"
836 my $new = $collection->shuffle;
838 Create a new collection with all elements in random order.
842 my $size = $collection->size;
844 Number of elements in collection.
848 my $new = $collection->sort;
849 my $new = $collection->sort(sub {...});
851 Sort elements based on return value of callback and create a new collection
854 # Sort values case-insensitive
855 my $case_insensitive = $collection->sort(sub { uc($a) cmp uc($b) });
859 $collection = $collection->tap(sub {...});
861 Equivalent to L<Mojo::Base/"tap">.
865 my $array = $collection->to_array;
867 Turn collection into array reference.
871 my $new = $collection->uniq;
872 my $new = $collection->uniq(sub {...});
873 my $new = $collection->uniq($method);
874 my $new = $collection->uniq($method, @args);
876 Create a new collection without duplicate elements, using the string
877 representation of either the elements or the return value of the
881 my $new = $collection->uniq(sub { $_->$method(@args) });
883 # $collection contains ('foo', 'bar', 'bar', 'baz')
884 $collection->uniq->join(' '); # "foo bar baz"
886 # $collection contains ([1, 2], [2, 1], [3, 2])
887 $collection->uniq(sub{ $_->[1] })->to_array; # "[[1, 2], [2, 1]]"
891 L<DOM::Tiny> uses a CSS selector engine based on L<Mojo::DOM::CSS>. All CSS
892 selectors that make sense for a standalone parser are supported.
898 my $all = $dom->find('*');
902 An element of type C<E>.
904 my $title = $dom->at('title');
908 An C<E> element with a C<foo> attribute.
910 my $links = $dom->find('a[href]');
914 An C<E> element whose C<foo> attribute value is exactly equal to C<bar>.
916 my $case_sensitive = $dom->find('input[type="hidden"]');
917 my $case_sensitive = $dom->find('input[type=hidden]');
919 =head2 E[foo="bar" i]
921 An C<E> element whose C<foo> attribute value is exactly equal to any
922 (ASCII-range) case-permutation of C<bar>. Note that this selector is
923 EXPERIMENTAL and might change without warning!
925 my $case_insensitive = $dom->find('input[type="hidden" i]');
926 my $case_insensitive = $dom->find('input[type=hidden i]');
927 my $case_insensitive = $dom->find('input[class~="foo" i]');
929 This selector is part of
930 L<Selectors Level 4|http://dev.w3.org/csswg/selectors-4>, which is still a work
935 An C<E> element whose C<foo> attribute value is a list of whitespace-separated
936 values, one of which is exactly equal to C<bar>.
938 my $foo = $dom->find('input[class~="foo"]');
939 my $foo = $dom->find('input[class~=foo]');
943 An C<E> element whose C<foo> attribute value begins exactly with the string
946 my $begins_with = $dom->find('input[name^="f"]');
947 my $begins_with = $dom->find('input[name^=f]');
951 An C<E> element whose C<foo> attribute value ends exactly with the string
954 my $ends_with = $dom->find('input[name$="o"]');
955 my $ends_with = $dom->find('input[name$=o]');
959 An C<E> element whose C<foo> attribute value contains the substring C<bar>.
961 my $contains = $dom->find('input[name*="fo"]');
962 my $contains = $dom->find('input[name*=fo]');
966 An C<E> element, root of the document.
968 my $root = $dom->at(':root');
970 =head2 E:nth-child(n)
972 An C<E> element, the C<n-th> child of its parent.
974 my $third = $dom->find('div:nth-child(3)');
975 my $odd = $dom->find('div:nth-child(odd)');
976 my $even = $dom->find('div:nth-child(even)');
977 my $top3 = $dom->find('div:nth-child(-n+3)');
979 =head2 E:nth-last-child(n)
981 An C<E> element, the C<n-th> child of its parent, counting from the last one.
983 my $third = $dom->find('div:nth-last-child(3)');
984 my $odd = $dom->find('div:nth-last-child(odd)');
985 my $even = $dom->find('div:nth-last-child(even)');
986 my $bottom3 = $dom->find('div:nth-last-child(-n+3)');
988 =head2 E:nth-of-type(n)
990 An C<E> element, the C<n-th> sibling of its type.
992 my $third = $dom->find('div:nth-of-type(3)');
993 my $odd = $dom->find('div:nth-of-type(odd)');
994 my $even = $dom->find('div:nth-of-type(even)');
995 my $top3 = $dom->find('div:nth-of-type(-n+3)');
997 =head2 E:nth-last-of-type(n)
999 An C<E> element, the C<n-th> sibling of its type, counting from the last one.
1001 my $third = $dom->find('div:nth-last-of-type(3)');
1002 my $odd = $dom->find('div:nth-last-of-type(odd)');
1003 my $even = $dom->find('div:nth-last-of-type(even)');
1004 my $bottom3 = $dom->find('div:nth-last-of-type(-n+3)');
1006 =head2 E:first-child
1008 An C<E> element, first child of its parent.
1010 my $first = $dom->find('div p:first-child');
1014 An C<E> element, last child of its parent.
1016 my $last = $dom->find('div p:last-child');
1018 =head2 E:first-of-type
1020 An C<E> element, first sibling of its type.
1022 my $first = $dom->find('div p:first-of-type');
1024 =head2 E:last-of-type
1026 An C<E> element, last sibling of its type.
1028 my $last = $dom->find('div p:last-of-type');
1032 An C<E> element, only child of its parent.
1034 my $lonely = $dom->find('div p:only-child');
1036 =head2 E:only-of-type
1038 An C<E> element, only sibling of its type.
1040 my $lonely = $dom->find('div p:only-of-type');
1044 An C<E> element that has no children (including text nodes).
1046 my $empty = $dom->find(':empty');
1050 A user interface element C<E> which is checked (for instance a radio-button or
1053 my $input = $dom->find(':checked');
1057 An C<E> element whose class is "warning".
1059 my $warning = $dom->find('div.warning');
1063 An C<E> element with C<ID> equal to "myid".
1065 my $foo = $dom->at('div#foo');
1069 An C<E> element that does not match simple selector C<s>.
1071 my $others = $dom->find('div p:not(:first-child)');
1075 An C<F> element descendant of an C<E> element.
1077 my $headlines = $dom->find('div h1');
1081 An C<F> element child of an C<E> element.
1083 my $headlines = $dom->find('html > body > div > h1');
1087 An C<F> element immediately preceded by an C<E> element.
1089 my $second = $dom->find('h1 + h2');
1093 An C<F> element preceded by an C<E> element.
1095 my $second = $dom->find('h1 ~ h2');
1099 Elements of type C<E>, C<F> and C<G>.
1101 my $headlines = $dom->find('h1, h2, h3');
1103 =head2 E[foo=bar][bar=baz]
1105 An C<E> element whose attributes match all following attribute selectors.
1107 my $links = $dom->find('a[foo^=b][foo$=ar]');
1111 Report any issues on the public bugtracker.
1115 Dan Book <dbook@cpan.org>
1117 =head1 COPYRIGHT AND LICENSE
1119 This software is Copyright (c) 2015 by Dan Book.
1121 This is free software, licensed under:
1123 The Artistic License 2.0 (GPL Compatible)
1127 L<Mojo::DOM>, L<XML::LibXML>, L<XML::Twig>, L<HTML::TreeBuilder>, L<XML::Smart>
1129 =for Pod::Coverage TO_JSON