Commit | Line | Data |
a292be34 |
1 | package DOM::Tiny; |
2 | |
3 | use strict; |
4 | use warnings; |
5 | |
d6512b50 |
6 | use overload |
7 | '@{}' => sub { shift->child_nodes }, |
8 | '%{}' => sub { shift->attr }, |
9 | bool => sub {1}, |
10 | '""' => sub { shift->to_string }, |
11 | fallback => 1; |
12 | |
13 | use Carp 'croak'; |
14 | use DOM::Tiny::Collection; |
15 | use DOM::Tiny::CSS; |
16 | use DOM::Tiny::HTML; |
17 | use Scalar::Util qw(blessed weaken); |
18 | |
a292be34 |
19 | our $VERSION = '0.001'; |
20 | |
d6512b50 |
21 | sub all_text { shift->_all_text(1, @_) } |
22 | |
23 | sub ancestors { _select($_[0]->_collect($_[0]->_ancestors), $_[1]) } |
24 | |
25 | sub append { shift->_add(1, @_) } |
26 | sub append_content { shift->_content(1, 0, @_) } |
27 | |
28 | sub at { |
29 | my $self = shift; |
30 | return undef unless my $result = $self->_css->select_one(@_); |
31 | return $self->_build($result, $self->xml); |
32 | } |
33 | |
34 | sub attr { |
35 | my $self = shift; |
36 | |
37 | # Hash |
38 | my $tree = $self->tree; |
39 | my $attrs = $tree->[0] ne 'tag' ? {} : $tree->[2]; |
40 | return $attrs unless @_; |
41 | |
42 | # Get |
43 | return $attrs->{$_[0]} unless @_ > 1 || ref $_[0]; |
44 | |
45 | # Set |
46 | my $values = ref $_[0] ? $_[0] : {@_}; |
47 | @$attrs{keys %$values} = values %$values; |
48 | |
49 | return $self; |
50 | } |
51 | |
52 | sub child_nodes { $_[0]->_collect(_nodes($_[0]->tree)) } |
53 | |
54 | sub children { _select($_[0]->_collect(_nodes($_[0]->tree, 1)), $_[1]) } |
55 | |
56 | sub content { |
57 | my $self = shift; |
58 | |
59 | my $type = $self->type; |
60 | if ($type eq 'root' || $type eq 'tag') { |
61 | return $self->_content(0, 1, @_) if @_; |
62 | my $html = DOM::Tiny::HTML->new(xml => $self->xml); |
63 | return join '', map { $html->tree($_)->render } _nodes($self->tree); |
64 | } |
65 | |
66 | return $self->tree->[1] unless @_; |
67 | $self->tree->[1] = shift; |
68 | return $self; |
69 | } |
70 | |
71 | sub descendant_nodes { $_[0]->_collect(_all(_nodes($_[0]->tree))) } |
72 | |
73 | sub find { $_[0]->_collect(@{$_[0]->_css->select($_[1])}) } |
74 | |
75 | sub following { _select($_[0]->_collect(@{$_[0]->_siblings(1)->[1]}), $_[1]) } |
76 | sub following_nodes { $_[0]->_collect(@{$_[0]->_siblings->[1]}) } |
77 | |
78 | sub matches { shift->_css->matches(@_) } |
79 | |
80 | sub namespace { |
81 | my $self = shift; |
82 | |
83 | return undef if (my $tree = $self->tree)->[0] ne 'tag'; |
84 | |
85 | # Extract namespace prefix and search parents |
86 | my $ns = $tree->[1] =~ /^(.*?):/ ? "xmlns:$1" : undef; |
87 | for my $node ($tree, $self->_ancestors) { |
88 | |
89 | # Namespace for prefix |
90 | my $attrs = $node->[2]; |
91 | if ($ns) { $_ eq $ns and return $attrs->{$_} for keys %$attrs } |
92 | |
93 | # Namespace attribute |
94 | elsif (defined $attrs->{xmlns}) { return $attrs->{xmlns} } |
95 | } |
96 | |
97 | return undef; |
98 | } |
99 | |
100 | sub new { |
101 | my $class = shift; |
102 | my $self = bless \DOM::Tiny::HTML->new, ref $class || $class; |
103 | return @_ ? $self->parse(@_) : $self; |
104 | } |
105 | |
106 | sub next { $_[0]->_maybe($_[0]->_siblings(1, 0)->[1]) } |
107 | sub next_node { $_[0]->_maybe($_[0]->_siblings(0, 0)->[1]) } |
108 | |
109 | sub parent { |
110 | my $self = shift; |
111 | return undef if $self->tree->[0] eq 'root'; |
112 | return $self->_build($self->_parent, $self->xml); |
113 | } |
114 | |
115 | sub parse { shift->_delegate(parse => @_) } |
116 | |
117 | sub preceding { _select($_[0]->_collect(@{$_[0]->_siblings(1)->[0]}), $_[1]) } |
118 | sub preceding_nodes { $_[0]->_collect(@{$_[0]->_siblings->[0]}) } |
119 | |
120 | sub prepend { shift->_add(0, @_) } |
121 | sub prepend_content { shift->_content(0, 0, @_) } |
122 | |
123 | sub previous { $_[0]->_maybe($_[0]->_siblings(1, -1)->[0]) } |
124 | sub previous_node { $_[0]->_maybe($_[0]->_siblings(0, -1)->[0]) } |
125 | |
126 | sub remove { shift->replace('') } |
127 | |
128 | sub 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))); |
132 | } |
133 | |
134 | sub root { |
135 | my $self = shift; |
136 | return $self unless my $tree = $self->_ancestors(1); |
137 | return $self->_build($tree, $self->xml); |
138 | } |
139 | |
140 | sub strip { |
141 | my $self = shift; |
142 | return $self if (my $tree = $self->tree)->[0] ne 'tag'; |
143 | return $self->_replace($tree->[3], $tree, _nodes($tree)); |
144 | } |
145 | |
146 | sub tag { |
147 | my ($self, $tag) = @_; |
148 | return undef if (my $tree = $self->tree)->[0] ne 'tag'; |
149 | return $tree->[1] unless $tag; |
150 | $tree->[1] = $tag; |
151 | return $self; |
152 | } |
153 | |
154 | sub tap { shift->DOM::Tiny::Collection::tap(@_) } |
155 | |
156 | sub text { shift->_all_text(0, @_) } |
157 | |
158 | sub to_string { shift->_delegate('render') } |
159 | |
160 | sub tree { shift->_delegate(tree => @_) } |
161 | |
162 | sub type { shift->tree->[0] } |
163 | |
164 | sub val { |
165 | my $self = shift; |
166 | |
927f1351 |
167 | my $tag = $self->tag; |
168 | |
d6512b50 |
169 | # "option" |
927f1351 |
170 | return defined $self->{value} ? $self->{value} : $self->text if $tag eq 'option'; |
d6512b50 |
171 | |
172 | # "textarea", "input" or "button" |
173 | return $tag eq 'textarea' ? $self->text : $self->{value} if $tag ne 'select'; |
174 | |
175 | # "select" |
176 | my $v = $self->find('option:checked')->map('val'); |
177 | return exists $self->{multiple} ? $v->size ? $v->to_array : undef : $v->last; |
178 | } |
179 | |
180 | sub wrap { shift->_wrap(0, @_) } |
181 | sub wrap_content { shift->_wrap(1, @_) } |
182 | |
183 | sub xml { shift->_delegate(xml => @_) } |
184 | |
185 | sub _add { |
186 | my ($self, $offset, $new) = @_; |
187 | |
188 | return $self if (my $tree = $self->tree)->[0] eq 'root'; |
189 | |
190 | my $parent = $self->_parent; |
191 | splice @$parent, _offset($parent, $tree) + $offset, 0, |
192 | _link($parent, _nodes($self->_parse($new))); |
193 | |
194 | return $self; |
195 | } |
196 | |
197 | sub _all { |
198 | map { $_->[0] eq 'tag' ? ($_, _all(_nodes($_))) : ($_) } @_; |
199 | } |
200 | |
201 | sub _all_text { |
202 | my ($self, $recurse, $trim) = @_; |
203 | |
204 | # Detect "pre" tag |
205 | my $tree = $self->tree; |
206 | $trim = 1 unless defined $trim; |
207 | map { $_->[1] eq 'pre' and $trim = 0 } $self->_ancestors, $tree |
208 | if $trim && $tree->[0] ne 'root'; |
209 | |
210 | return _text([_nodes($tree)], $recurse, $trim); |
211 | } |
212 | |
213 | sub _ancestors { |
214 | my ($self, $root) = @_; |
215 | |
216 | return unless my $tree = $self->_parent; |
217 | my @ancestors; |
218 | do { push @ancestors, $tree } |
219 | while ($tree->[0] eq 'tag') && ($tree = $tree->[3]); |
220 | return $root ? $ancestors[-1] : @ancestors[0 .. $#ancestors - 1]; |
221 | } |
222 | |
223 | sub _build { shift->new->tree(shift)->xml(shift) } |
224 | |
225 | sub _collect { |
226 | my $self = shift; |
227 | my $xml = $self->xml; |
228 | return DOM::Tiny::Collection->new(map { $self->_build($_, $xml) } @_); |
229 | } |
230 | |
231 | sub _content { |
232 | my ($self, $start, $offset, $new) = @_; |
233 | |
234 | my $tree = $self->tree; |
235 | unless ($tree->[0] eq 'root' || $tree->[0] eq 'tag') { |
236 | my $old = $self->content; |
237 | return $self->content($start ? "$old$new" : "$new$old"); |
238 | } |
239 | |
240 | $start = $start ? ($#$tree + 1) : _start($tree); |
241 | $offset = $offset ? $#$tree : 0; |
242 | splice @$tree, $start, $offset, _link($tree, _nodes($self->_parse($new))); |
243 | |
244 | return $self; |
245 | } |
246 | |
247 | sub _css { DOM::Tiny::CSS->new(tree => shift->tree) } |
248 | |
249 | sub _delegate { |
250 | my ($self, $method) = (shift, shift); |
251 | return $$self->$method unless @_; |
252 | $$self->$method(@_); |
253 | return $self; |
254 | } |
255 | |
256 | sub _link { |
257 | my ($parent, @children) = @_; |
258 | |
259 | # Link parent to children |
260 | for my $node (@children) { |
261 | my $offset = $node->[0] eq 'tag' ? 3 : 2; |
262 | $node->[$offset] = $parent; |
263 | weaken $node->[$offset]; |
264 | } |
265 | |
266 | return @children; |
267 | } |
268 | |
269 | sub _maybe { $_[1] ? $_[0]->_build($_[1], $_[0]->xml) : undef } |
270 | |
271 | sub _nodes { |
272 | return unless my $tree = shift; |
273 | my @nodes = @$tree[_start($tree) .. $#$tree]; |
274 | return shift() ? grep { $_->[0] eq 'tag' } @nodes : @nodes; |
275 | } |
276 | |
277 | sub _offset { |
278 | my ($parent, $child) = @_; |
279 | my $i = _start($parent); |
280 | $_ eq $child ? last : $i++ for @$parent[$i .. $#$parent]; |
281 | return $i; |
282 | } |
283 | |
284 | sub _parent { $_[0]->tree->[$_[0]->type eq 'tag' ? 3 : 2] } |
285 | |
286 | sub _parse { DOM::Tiny::HTML->new(xml => shift->xml)->parse(shift)->tree } |
287 | |
288 | sub _replace { |
927f1351 |
289 | my ($self, $parent, $child, @nodes) = @_; |
290 | splice @$parent, _offset($parent, $child), 1, _link($parent, @nodes); |
d6512b50 |
291 | return $self->parent; |
292 | } |
293 | |
294 | sub _select { |
295 | my ($collection, $selector) = @_; |
296 | return $collection unless $selector; |
297 | return $collection->new(grep { $_->matches($selector) } @$collection); |
298 | } |
299 | |
300 | sub _siblings { |
301 | my ($self, $tags, $i) = @_; |
302 | |
303 | return [] unless my $parent = $self->parent; |
304 | |
305 | my $tree = $self->tree; |
306 | my (@before, @after, $match); |
307 | for my $node (_nodes($parent->tree)) { |
308 | ++$match and next if !$match && $node eq $tree; |
309 | next if $tags && $node->[0] ne 'tag'; |
310 | $match ? push @after, $node : push @before, $node; |
311 | } |
312 | |
313 | return defined $i ? [$before[$i], $after[$i]] : [\@before, \@after]; |
314 | } |
315 | |
316 | sub _squish { |
317 | my $str = shift; |
318 | $str =~ s/^\s+//; |
319 | $str =~ s/\s+$//; |
320 | $str =~ s/\s+/ /g; |
321 | return $str; |
322 | } |
323 | |
324 | sub _start { $_[0][0] eq 'root' ? 1 : 4 } |
325 | |
326 | sub _text { |
327 | my ($nodes, $recurse, $trim) = @_; |
328 | |
329 | # Merge successive text nodes |
330 | my $i = 0; |
331 | while (my $next = $nodes->[$i + 1]) { |
332 | ++$i and next unless $nodes->[$i][0] eq 'text' && $next->[0] eq 'text'; |
333 | splice @$nodes, $i, 2, ['text', $nodes->[$i][1] . $next->[1]]; |
334 | } |
335 | |
336 | my $text = ''; |
337 | for my $node (@$nodes) { |
338 | my $type = $node->[0]; |
339 | |
340 | # Text |
341 | my $chunk = ''; |
342 | if ($type eq 'text') { $chunk = $trim ? _squish $node->[1] : $node->[1] } |
343 | |
344 | # CDATA or raw text |
345 | elsif ($type eq 'cdata' || $type eq 'raw') { $chunk = $node->[1] } |
346 | |
347 | # Nested tag |
348 | elsif ($type eq 'tag' && $recurse) { |
349 | no warnings 'recursion'; |
350 | $chunk = _text([_nodes($node)], 1, $node->[1] eq 'pre' ? 0 : $trim); |
351 | } |
352 | |
353 | # Add leading whitespace if punctuation allows it |
354 | $chunk = " $chunk" if $text =~ /\S\z/ && $chunk =~ /^[^.!?,;:\s]+/; |
355 | |
356 | # Trim whitespace blocks |
357 | $text .= $chunk if $chunk =~ /\S+/ || !$trim; |
358 | } |
359 | |
360 | return $text; |
361 | } |
362 | |
363 | sub _wrap { |
364 | my ($self, $content, $new) = @_; |
365 | |
366 | $content = 1 if (my $tree = $self->tree)->[0] eq 'root'; |
367 | $content = 0 if $tree->[0] ne 'root' && $tree->[0] ne 'tag'; |
368 | |
369 | # Find innermost tag |
370 | my $current; |
371 | my $first = $new = $self->_parse($new); |
372 | $current = $first while $first = (_nodes($first, 1))[0]; |
373 | return $self unless $current; |
374 | |
375 | # Wrap content |
376 | if ($content) { |
377 | push @$current, _link($current, _nodes($tree)); |
378 | splice @$tree, _start($tree), $#$tree, _link($tree, _nodes($new)); |
379 | return $self; |
380 | } |
381 | |
382 | # Wrap element |
383 | $self->_replace($self->_parent, $tree, _nodes($new)); |
384 | push @$current, _link($current, $tree); |
385 | return $self; |
386 | } |
387 | |
a292be34 |
388 | 1; |
389 | |
d6512b50 |
390 | =encoding utf8 |
391 | |
a292be34 |
392 | =head1 NAME |
393 | |
d6512b50 |
394 | DOM::Tiny - Minimalistic HTML/XML DOM parser with CSS selectors |
a292be34 |
395 | |
396 | =head1 SYNOPSIS |
397 | |
d6512b50 |
398 | use DOM::Tiny; |
399 | |
400 | # Parse |
401 | my $dom = DOM::Tiny->new('<div><p id="a">Test</p><p id="b">123</p></div>'); |
402 | |
403 | # Find |
404 | say $dom->at('#b')->text; |
405 | say $dom->find('p')->map('text')->join("\n"); |
406 | say $dom->find('[id]')->map(attr => 'id')->join("\n"); |
407 | |
408 | # Iterate |
409 | $dom->find('p[id]')->reverse->each(sub { say $_->{id} }); |
410 | |
411 | # Loop |
412 | for my $e ($dom->find('p[id]')->each) { |
413 | say $e->{id}, ':', $e->text; |
414 | } |
415 | |
416 | # Modify |
417 | $dom->find('div p')->last->append('<p id="c">456</p>'); |
418 | $dom->find(':not(p)')->map('strip'); |
419 | |
420 | # Render |
421 | say "$dom"; |
422 | |
a292be34 |
423 | =head1 DESCRIPTION |
424 | |
d6512b50 |
425 | L<DOM::Tiny> is a minimalistic and relaxed HTML/XML DOM parser with CSS |
426 | selector support based on L<Mojo::DOM>. It will even try to interpret broken |
427 | HTML and XML, so you should not use it for validation. |
428 | |
429 | =head1 NODES AND ELEMENTS |
430 | |
431 | When we parse an HTML/XML fragment, it gets turned into a tree of nodes. |
432 | |
433 | <!DOCTYPE html> |
434 | <html> |
435 | <head><title>Hello</title></head> |
436 | <body>World!</body> |
437 | </html> |
438 | |
439 | There are currently eight different kinds of nodes, C<cdata>, C<comment>, |
440 | C<doctype>, C<pi>, C<raw>, C<root>, C<tag> and C<text>. Elements are nodes of |
441 | the type C<tag>. |
442 | |
443 | root |
444 | |- doctype (html) |
445 | +- tag (html) |
446 | |- tag (head) |
447 | | +- tag (title) |
448 | | +- raw (Hello) |
449 | +- tag (body) |
450 | +- text (World!) |
451 | |
452 | While all node types are represented as L<DOM::Tiny> objects, some methods like |
453 | L</"attr"> and L</"namespace"> only apply to elements. |
454 | |
455 | =head1 CASE-SENSITIVITY |
456 | |
457 | L<DOM::Tiny> defaults to HTML semantics, that means all tags and attribute |
458 | names are lowercased and selectors need to be lowercase as well. |
459 | |
460 | # HTML semantics |
461 | my $dom = DOM::Tiny->new('<P ID="greeting">Hi!</P>'); |
462 | say $dom->at('p[id]')->text; |
463 | |
464 | If XML processing instructions are found, the parser will automatically switch |
465 | into XML mode and everything becomes case-sensitive. |
466 | |
467 | # XML semantics |
468 | my $dom = DOM::Tiny->new('<?xml version="1.0"?><P ID="greeting">Hi!</P>'); |
469 | say $dom->at('P[ID]')->text; |
470 | |
471 | XML detection can also be disabled with the L</"xml"> method. |
472 | |
473 | # Force XML semantics |
474 | my $dom = DOM::Tiny->new->xml(1)->parse('<P ID="greeting">Hi!</P>'); |
475 | say $dom->at('P[ID]')->text; |
476 | |
477 | # Force HTML semantics |
478 | my $dom = DOM::Tiny->new->xml(0)->parse('<P ID="greeting">Hi!</P>'); |
479 | say $dom->at('p[id]')->text; |
480 | |
481 | =head1 METHODS |
482 | |
483 | L<DOM::Tiny> implements the following methods. |
484 | |
485 | =head2 all_text |
486 | |
487 | my $trimmed = $dom->all_text; |
488 | my $untrimmed = $dom->all_text(0); |
489 | |
490 | Extract text content from all descendant nodes of this element, smart |
491 | whitespace trimming is enabled by default. |
492 | |
493 | # "foo bar baz" |
494 | $dom->parse("<div>foo\n<p>bar</p>baz\n</div>")->at('div')->all_text; |
495 | |
496 | # "foo\nbarbaz\n" |
497 | $dom->parse("<div>foo\n<p>bar</p>baz\n</div>")->at('div')->all_text(0); |
498 | |
499 | =head2 ancestors |
500 | |
501 | my $collection = $dom->ancestors; |
502 | my $collection = $dom->ancestors('div ~ p'); |
503 | |
504 | Find all ancestor elements of this node matching the CSS selector and return a |
505 | L<DOM::Tiny::Collection> object containing these elements as L<DOM::Tiny> |
506 | objects. All selectors from L<DOM::Tiny::CSS/"SELECTORS"> are supported. |
507 | |
508 | # List tag names of ancestor elements |
509 | say $dom->ancestors->map('tag')->join("\n"); |
510 | |
511 | =head2 append |
512 | |
513 | $dom = $dom->append('<p>I ♥ DOM::Tiny!</p>'); |
514 | |
515 | Append HTML/XML fragment to this node. |
516 | |
517 | # "<div><h1>Test</h1><h2>123</h2></div>" |
518 | $dom->parse('<div><h1>Test</h1></div>') |
519 | ->at('h1')->append('<h2>123</h2>')->root; |
520 | |
521 | # "<p>Test 123</p>" |
522 | $dom->parse('<p>Test</p>')->at('p') |
523 | ->child_nodes->first->append(' 123')->root; |
524 | |
525 | =head2 append_content |
526 | |
527 | $dom = $dom->append_content('<p>I ♥ DOM::Tiny!</p>'); |
528 | |
529 | Append HTML/XML fragment (for C<root> and C<tag> nodes) or raw content to this |
530 | node's content. |
531 | |
532 | # "<div><h1>Test123</h1></div>" |
533 | $dom->parse('<div><h1>Test</h1></div>') |
534 | ->at('h1')->append_content('123')->root; |
535 | |
536 | # "<!-- Test 123 --><br>" |
537 | $dom->parse('<!-- Test --><br>') |
538 | ->child_nodes->first->append_content('123 ')->root; |
539 | |
540 | # "<p>Test<i>123</i></p>" |
541 | $dom->parse('<p>Test</p>')->at('p')->append_content('<i>123</i>')->root; |
542 | |
543 | =head2 at |
544 | |
545 | my $result = $dom->at('div ~ p'); |
546 | |
547 | Find first descendant element of this element matching the CSS selector and |
548 | return it as a L<DOM::Tiny> object or return C<undef> if none could be found. |
549 | All selectors from L<DOM::Tiny::CSS/"SELECTORS"> are supported. |
550 | |
551 | # Find first element with "svg" namespace definition |
552 | my $namespace = $dom->at('[xmlns\:svg]')->{'xmlns:svg'}; |
553 | |
554 | =head2 attr |
555 | |
556 | my $hash = $dom->attr; |
557 | my $foo = $dom->attr('foo'); |
558 | $dom = $dom->attr({foo => 'bar'}); |
559 | $dom = $dom->attr(foo => 'bar'); |
560 | |
561 | This element's attributes. |
562 | |
563 | # Remove an attribute |
564 | delete $dom->attr->{id}; |
565 | |
566 | # Attribute without value |
567 | $dom->attr(selected => undef); |
568 | |
569 | # List id attributes |
570 | say $dom->find('*')->map(attr => 'id')->compact->join("\n"); |
571 | |
572 | =head2 child_nodes |
573 | |
574 | my $collection = $dom->child_nodes; |
575 | |
576 | Return a L<DOM::Tiny::Collection> object containing all child nodes of this |
577 | element as L<DOM::Tiny> objects. |
578 | |
579 | # "<p><b>123</b></p>" |
580 | $dom->parse('<p>Test<b>123</b></p>')->at('p')->child_nodes->first->remove; |
581 | |
582 | # "<!DOCTYPE html>" |
583 | $dom->parse('<!DOCTYPE html><b>123</b>')->child_nodes->first; |
584 | |
585 | # " Test " |
586 | $dom->parse('<b>123</b><!-- Test -->')->child_nodes->last->content; |
587 | |
588 | =head2 children |
589 | |
590 | my $collection = $dom->children; |
591 | my $collection = $dom->children('div ~ p'); |
592 | |
593 | Find all child elements of this element matching the CSS selector and return a |
594 | L<DOM::Tiny::Collection> object containing these elements as L<DOM::Tiny> |
595 | objects. All selectors from L<DOM::Tiny::CSS/"SELECTORS"> are supported. |
596 | |
597 | # Show tag name of random child element |
598 | say $dom->children->shuffle->first->tag; |
599 | |
600 | =head2 content |
601 | |
602 | my $str = $dom->content; |
603 | $dom = $dom->content('<p>I ♥ DOM::Tiny!</p>'); |
604 | |
605 | Return this node's content or replace it with HTML/XML fragment (for C<root> |
606 | and C<tag> nodes) or raw content. |
607 | |
608 | # "<b>Test</b>" |
609 | $dom->parse('<div><b>Test</b></div>')->at('div')->content; |
610 | |
611 | # "<div><h1>123</h1></div>" |
612 | $dom->parse('<div><h1>Test</h1></div>')->at('h1')->content('123')->root; |
613 | |
614 | # "<p><i>123</i></p>" |
615 | $dom->parse('<p>Test</p>')->at('p')->content('<i>123</i>')->root; |
616 | |
617 | # "<div><h1></h1></div>" |
618 | $dom->parse('<div><h1>Test</h1></div>')->at('h1')->content('')->root; |
619 | |
620 | # " Test " |
621 | $dom->parse('<!-- Test --><br>')->child_nodes->first->content; |
622 | |
623 | # "<div><!-- 123 -->456</div>" |
624 | $dom->parse('<div><!-- Test -->456</div>') |
625 | ->at('div')->child_nodes->first->content(' 123 ')->root; |
626 | |
627 | =head2 descendant_nodes |
628 | |
629 | my $collection = $dom->descendant_nodes; |
630 | |
631 | Return a L<DOM::Tiny::Collection> object containing all descendant nodes of |
632 | this element as L<DOM::Tiny> objects. |
633 | |
634 | # "<p><b>123</b></p>" |
635 | $dom->parse('<p><!-- Test --><b>123<!-- 456 --></b></p>') |
636 | ->descendant_nodes->grep(sub { $_->type eq 'comment' }) |
637 | ->map('remove')->first; |
638 | |
639 | # "<p><b>test</b>test</p>" |
640 | $dom->parse('<p><b>123</b>456</p>') |
641 | ->at('p')->descendant_nodes->grep(sub { $_->type eq 'text' }) |
642 | ->map(content => 'test')->first->root; |
643 | |
644 | =head2 find |
645 | |
646 | my $collection = $dom->find('div ~ p'); |
647 | |
648 | Find all descendant elements of this element matching the CSS selector and |
649 | return a L<DOM::Tiny::Collection> object containing these elements as |
650 | L<DOM::Tiny> objects. All selectors from L<DOM::Tiny::CSS/"SELECTORS"> are |
651 | supported. |
652 | |
653 | # Find a specific element and extract information |
654 | my $id = $dom->find('div')->[23]{id}; |
655 | |
656 | # Extract information from multiple elements |
657 | my @headers = $dom->find('h1, h2, h3')->map('text')->each; |
658 | |
659 | # Count all the different tags |
660 | my $hash = $dom->find('*')->reduce(sub { $a->{$b->tag}++; $a }, {}); |
661 | |
662 | # Find elements with a class that contains dots |
663 | my @divs = $dom->find('div.foo\.bar')->each; |
664 | |
665 | =head2 following |
666 | |
667 | my $collection = $dom->following; |
668 | my $collection = $dom->following('div ~ p'); |
669 | |
670 | Find all sibling elements after this node matching the CSS selector and return |
671 | a L<DOM::Tiny::Collection> object containing these elements as L<DOM::Tiny> |
672 | objects. All selectors from L<DOM::Tiny::CSS/"SELECTORS"> are supported. |
673 | |
674 | # List tags of sibling elements after this node |
675 | say $dom->following->map('tag')->join("\n"); |
676 | |
677 | =head2 following_nodes |
678 | |
679 | my $collection = $dom->following_nodes; |
680 | |
681 | Return a L<DOM::Tiny::Collection> object containing all sibling nodes after |
682 | this node as L<DOM::Tiny> objects. |
683 | |
684 | # "C" |
685 | $dom->parse('<p>A</p><!-- B -->C')->at('p')->following_nodes->last->content; |
686 | |
687 | =head2 matches |
688 | |
689 | my $bool = $dom->matches('div ~ p'); |
690 | |
691 | Check if this element matches the CSS selector. All selectors from |
692 | L<DOM::Tiny::CSS/"SELECTORS"> are supported. |
693 | |
694 | # True |
695 | $dom->parse('<p class="a">A</p>')->at('p')->matches('.a'); |
696 | $dom->parse('<p class="a">A</p>')->at('p')->matches('p[class]'); |
697 | |
698 | # False |
699 | $dom->parse('<p class="a">A</p>')->at('p')->matches('.b'); |
700 | $dom->parse('<p class="a">A</p>')->at('p')->matches('p[id]'); |
701 | |
702 | =head2 namespace |
703 | |
704 | my $namespace = $dom->namespace; |
705 | |
706 | Find this element's namespace or return C<undef> if none could be found. |
707 | |
708 | # Find namespace for an element with namespace prefix |
709 | my $namespace = $dom->at('svg > svg\:circle')->namespace; |
710 | |
711 | # Find namespace for an element that may or may not have a namespace prefix |
712 | my $namespace = $dom->at('svg > circle')->namespace; |
713 | |
714 | =head2 new |
715 | |
716 | my $dom = DOM::Tiny->new; |
717 | my $dom = DOM::Tiny->new('<foo bar="baz">I ♥ DOM::Tiny!</foo>'); |
718 | |
719 | Construct a new scalar-based L<DOM::Tiny> object and L</"parse"> HTML/XML |
720 | fragment if necessary. |
721 | |
722 | =head2 next |
723 | |
724 | my $sibling = $dom->next; |
725 | |
726 | Return L<DOM::Tiny> object for next sibling element or C<undef> if there are no |
727 | more siblings. |
728 | |
729 | # "<h2>123</h2>" |
730 | $dom->parse('<div><h1>Test</h1><h2>123</h2></div>')->at('h1')->next; |
731 | |
732 | =head2 next_node |
733 | |
734 | my $sibling = $dom->next_node; |
735 | |
736 | Return L<DOM::Tiny> object for next sibling node or C<undef> if there are no |
737 | more siblings. |
738 | |
739 | # "456" |
740 | $dom->parse('<p><b>123</b><!-- Test -->456</p>') |
741 | ->at('b')->next_node->next_node; |
742 | |
743 | # " Test " |
744 | $dom->parse('<p><b>123</b><!-- Test -->456</p>') |
745 | ->at('b')->next_node->content; |
746 | |
747 | =head2 parent |
748 | |
749 | my $parent = $dom->parent; |
750 | |
751 | Return L<DOM::Tiny> object for parent of this node or C<undef> if this node has |
752 | no parent. |
753 | |
754 | =head2 parse |
755 | |
756 | $dom = $dom->parse('<foo bar="baz">I ♥ DOM::Tiny!</foo>'); |
757 | |
758 | Parse HTML/XML fragment with L<DOM::Tiny::HTML>. |
759 | |
760 | # Parse XML |
761 | my $dom = DOM::Tiny->new->xml(1)->parse($xml); |
762 | |
763 | =head2 preceding |
764 | |
765 | my $collection = $dom->preceding; |
766 | my $collection = $dom->preceding('div ~ p'); |
767 | |
768 | Find all sibling elements before this node matching the CSS selector and return |
769 | a L<DOM::Tiny::Collection> object containing these elements as L<DOM::Tiny> |
770 | objects. All selectors from L<DOM::Tiny::CSS/"SELECTORS"> are supported. |
771 | |
772 | # List tags of sibling elements before this node |
773 | say $dom->preceding->map('tag')->join("\n"); |
774 | |
775 | =head2 preceding_nodes |
776 | |
777 | my $collection = $dom->preceding_nodes; |
778 | |
779 | Return a L<DOM::Tiny::Collection> object containing all sibling nodes before |
780 | this node as L<DOM::Tiny> objects. |
781 | |
782 | # "A" |
783 | $dom->parse('A<!-- B --><p>C</p>')->at('p')->preceding_nodes->first->content; |
784 | |
785 | =head2 prepend |
786 | |
787 | $dom = $dom->prepend('<p>I ♥ DOM::Tiny!</p>'); |
788 | |
789 | Prepend HTML/XML fragment to this node. |
790 | |
791 | # "<div><h1>Test</h1><h2>123</h2></div>" |
792 | $dom->parse('<div><h2>123</h2></div>') |
793 | ->at('h2')->prepend('<h1>Test</h1>')->root; |
794 | |
795 | # "<p>Test 123</p>" |
796 | $dom->parse('<p>123</p>') |
797 | ->at('p')->child_nodes->first->prepend('Test ')->root; |
798 | |
799 | =head2 prepend_content |
800 | |
801 | $dom = $dom->prepend_content('<p>I ♥ DOM::Tiny!</p>'); |
802 | |
803 | Prepend HTML/XML fragment (for C<root> and C<tag> nodes) or raw content to this |
804 | node's content. |
805 | |
806 | # "<div><h2>Test123</h2></div>" |
807 | $dom->parse('<div><h2>123</h2></div>') |
808 | ->at('h2')->prepend_content('Test')->root; |
809 | |
810 | # "<!-- Test 123 --><br>" |
811 | $dom->parse('<!-- 123 --><br>') |
812 | ->child_nodes->first->prepend_content(' Test')->root; |
813 | |
814 | # "<p><i>123</i>Test</p>" |
815 | $dom->parse('<p>Test</p>')->at('p')->prepend_content('<i>123</i>')->root; |
816 | |
817 | =head2 previous |
818 | |
819 | my $sibling = $dom->previous; |
820 | |
821 | Return L<DOM::Tiny> object for previous sibling element or C<undef> if there |
822 | are no more siblings. |
823 | |
824 | # "<h1>Test</h1>" |
825 | $dom->parse('<div><h1>Test</h1><h2>123</h2></div>')->at('h2')->previous; |
826 | |
827 | =head2 previous_node |
828 | |
829 | my $sibling = $dom->previous_node; |
830 | |
831 | Return L<DOM::Tiny> object for previous sibling node or C<undef> if there are |
832 | no more siblings. |
833 | |
834 | # "123" |
835 | $dom->parse('<p>123<!-- Test --><b>456</b></p>') |
836 | ->at('b')->previous_node->previous_node; |
837 | |
838 | # " Test " |
839 | $dom->parse('<p>123<!-- Test --><b>456</b></p>') |
840 | ->at('b')->previous_node->content; |
841 | |
842 | =head2 remove |
843 | |
844 | my $parent = $dom->remove; |
845 | |
846 | Remove this node and return L</"root"> (for C<root> nodes) or L</"parent">. |
847 | |
848 | # "<div></div>" |
849 | $dom->parse('<div><h1>Test</h1></div>')->at('h1')->remove; |
850 | |
851 | # "<p><b>456</b></p>" |
852 | $dom->parse('<p>123<b>456</b></p>') |
853 | ->at('p')->child_nodes->first->remove->root; |
854 | |
855 | =head2 replace |
856 | |
857 | my $parent = $dom->replace('<div>I ♥ DOM::Tiny!</div>'); |
858 | |
859 | Replace this node with HTML/XML fragment and return L</"root"> (for C<root> |
860 | nodes) or L</"parent">. |
861 | |
862 | # "<div><h2>123</h2></div>" |
863 | $dom->parse('<div><h1>Test</h1></div>')->at('h1')->replace('<h2>123</h2>'); |
864 | |
865 | # "<p><b>123</b></p>" |
866 | $dom->parse('<p>Test</p>') |
867 | ->at('p')->child_nodes->[0]->replace('<b>123</b>')->root; |
868 | |
869 | =head2 root |
870 | |
871 | my $root = $dom->root; |
872 | |
873 | Return L<DOM::Tiny> object for C<root> node. |
874 | |
875 | =head2 strip |
876 | |
877 | my $parent = $dom->strip; |
878 | |
879 | Remove this element while preserving its content and return L</"parent">. |
880 | |
881 | # "<div>Test</div>" |
882 | $dom->parse('<div><h1>Test</h1></div>')->at('h1')->strip; |
883 | |
884 | =head2 tag |
885 | |
886 | my $tag = $dom->tag; |
887 | $dom = $dom->tag('div'); |
888 | |
889 | This element's tag name. |
890 | |
891 | # List tag names of child elements |
892 | say $dom->children->map('tag')->join("\n"); |
893 | |
894 | =head2 tap |
895 | |
896 | $dom = $dom->tap(sub {...}); |
897 | |
898 | Alias for L<Mojo::Base/"tap">. |
899 | |
900 | =head2 text |
901 | |
902 | my $trimmed = $dom->text; |
903 | my $untrimmed = $dom->text(0); |
904 | |
905 | Extract text content from this element only (not including child elements), |
906 | smart whitespace trimming is enabled by default. |
907 | |
908 | # "foo baz" |
909 | $dom->parse("<div>foo\n<p>bar</p>baz\n</div>")->at('div')->text; |
910 | |
911 | # "foo\nbaz\n" |
912 | $dom->parse("<div>foo\n<p>bar</p>baz\n</div>")->at('div')->text(0); |
913 | |
914 | =head2 to_string |
915 | |
916 | my $str = $dom->to_string; |
917 | |
918 | Render this node and its content to HTML/XML. |
919 | |
920 | # "<b>Test</b>" |
921 | $dom->parse('<div><b>Test</b></div>')->at('div b')->to_string; |
922 | |
923 | =head2 tree |
924 | |
925 | my $tree = $dom->tree; |
926 | $dom = $dom->tree(['root']); |
927 | |
928 | Document Object Model. Note that this structure should only be used very |
929 | carefully since it is very dynamic. |
930 | |
931 | =head2 type |
932 | |
933 | my $type = $dom->type; |
934 | |
935 | This node's type, usually C<cdata>, C<comment>, C<doctype>, C<pi>, C<raw>, |
936 | C<root>, C<tag> or C<text>. |
937 | |
938 | # "cdata" |
939 | $dom->parse('<![CDATA[Test]]>')->child_nodes->first->type; |
940 | |
941 | # "comment" |
942 | $dom->parse('<!-- Test -->')->child_nodes->first->type; |
943 | |
944 | # "doctype" |
945 | $dom->parse('<!DOCTYPE html>')->child_nodes->first->type; |
946 | |
947 | # "pi" |
948 | $dom->parse('<?xml version="1.0"?>')->child_nodes->first->type; |
949 | |
950 | # "raw" |
951 | $dom->parse('<title>Test</title>')->at('title')->child_nodes->first->type; |
952 | |
953 | # "root" |
954 | $dom->parse('<p>Test</p>')->type; |
955 | |
956 | # "tag" |
957 | $dom->parse('<p>Test</p>')->at('p')->type; |
958 | |
959 | # "text" |
960 | $dom->parse('<p>Test</p>')->at('p')->child_nodes->first->type; |
961 | |
962 | =head2 val |
963 | |
964 | my $value = $dom->val; |
965 | |
966 | Extract value from form element (such as C<button>, C<input>, C<option>, |
967 | C<select> and C<textarea>) or return C<undef> if this element has no value. In |
968 | the case of C<select> with C<multiple> attribute, find C<option> elements with |
969 | C<selected> attribute and return an array reference with all values or C<undef> |
970 | if none could be found. |
971 | |
972 | # "a" |
973 | $dom->parse('<input name="test" value="a">')->at('input')->val; |
974 | |
975 | # "b" |
976 | $dom->parse('<textarea>b</textarea>')->at('textarea')->val; |
977 | |
978 | # "c" |
979 | $dom->parse('<option value="c">Test</option>')->at('option')->val; |
980 | |
981 | # "d" |
982 | $dom->parse('<select><option selected>d</option></select>') |
983 | ->at('select')->val; |
984 | |
985 | # "e" |
986 | $dom->parse('<select multiple><option selected>e</option></select>') |
987 | ->at('select')->val->[0]; |
988 | |
989 | =head2 wrap |
990 | |
991 | $dom = $dom->wrap('<div></div>'); |
992 | |
993 | Wrap HTML/XML fragment around this node, placing it as the last child of the |
994 | first innermost element. |
995 | |
996 | # "<p>123<b>Test</b></p>" |
997 | $dom->parse('<b>Test</b>')->at('b')->wrap('<p>123</p>')->root; |
998 | |
999 | # "<div><p><b>Test</b></p>123</div>" |
1000 | $dom->parse('<b>Test</b>')->at('b')->wrap('<div><p></p>123</div>')->root; |
1001 | |
1002 | # "<p><b>Test</b></p><p>123</p>" |
1003 | $dom->parse('<b>Test</b>')->at('b')->wrap('<p></p><p>123</p>')->root; |
1004 | |
1005 | # "<p><b>Test</b></p>" |
1006 | $dom->parse('<p>Test</p>')->at('p')->child_nodes->first->wrap('<b>')->root; |
1007 | |
1008 | =head2 wrap_content |
1009 | |
1010 | $dom = $dom->wrap_content('<div></div>'); |
1011 | |
1012 | Wrap HTML/XML fragment around this node's content, placing it as the last |
1013 | children of the first innermost element. |
1014 | |
1015 | # "<p><b>123Test</b></p>" |
1016 | $dom->parse('<p>Test<p>')->at('p')->wrap_content('<b>123</b>')->root; |
1017 | |
1018 | # "<p><b>Test</b></p><p>123</p>" |
1019 | $dom->parse('<b>Test</b>')->wrap_content('<p></p><p>123</p>'); |
1020 | |
1021 | =head2 xml |
1022 | |
1023 | my $bool = $dom->xml; |
1024 | $dom = $dom->xml($bool); |
1025 | |
1026 | Disable HTML semantics in parser and activate case-sensitivity, defaults to |
1027 | auto detection based on processing instructions. |
1028 | |
1029 | =head1 OPERATORS |
1030 | |
1031 | L<DOM::Tiny> overloads the following operators. |
1032 | |
1033 | =head2 array |
1034 | |
1035 | my @nodes = @$dom; |
1036 | |
1037 | Alias for L</"child_nodes">. |
1038 | |
1039 | # "<!-- Test -->" |
1040 | $dom->parse('<!-- Test --><b>123</b>')->[0]; |
1041 | |
1042 | =head2 bool |
1043 | |
1044 | my $bool = !!$dom; |
1045 | |
1046 | Always true. |
1047 | |
1048 | =head2 hash |
1049 | |
1050 | my %attrs = %$dom; |
1051 | |
1052 | Alias for L</"attr">. |
1053 | |
1054 | # "test" |
1055 | $dom->parse('<div id="test">Test</div>')->at('div')->{id}; |
1056 | |
1057 | =head2 stringify |
1058 | |
1059 | my $str = "$dom"; |
1060 | |
1061 | Alias for L</"to_string">. |
1062 | |
a292be34 |
1063 | =head1 BUGS |
1064 | |
1065 | Report any issues on the public bugtracker. |
1066 | |
1067 | =head1 AUTHOR |
1068 | |
1069 | Dan Book <dbook@cpan.org> |
1070 | |
1071 | =head1 COPYRIGHT AND LICENSE |
1072 | |
1073 | This software is Copyright (c) 2015 by Dan Book. |
1074 | |
1075 | This is free software, licensed under: |
1076 | |
1077 | The Artistic License 2.0 (GPL Compatible) |
1078 | |
1079 | =head1 SEE ALSO |
1080 | |
d6512b50 |
1081 | L<Mojo::DOM>, L<XML::LibXML>, L<XML::Twig>, L<HTML::TreeBuilder>, L<XML::Smart> |