| 1 | package Mojo::DOM58; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 3 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 4 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 6 | use overload | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 7 | 4 | ▶ | '@{}' => sub { shift->child_nodes }, | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 8 | 103 | ▶ | '%{}' => sub { shift->attr }, | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 9 | 33 | ▶ | bool => sub {1}, | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 10 | 161 | ▶ | '""' => sub { shift->to_string }, | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 11 | 2 | fallback => 1; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 13 | 2 | ▶ | use Exporter 'import'; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 14 | 2 | ▶ | use Mojo::DOM58::_Collection; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 15 | 2 | ▶ | use Mojo::DOM58::_CSS; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 16 | 2 | ▶ | use Mojo::DOM58::_HTML 'tag_to_html'; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 17 | 2 | ▶ | use Scalar::Util qw(blessed weaken); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 18 | 2 | ▶ | use Storable 'dclone'; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 20 | 2 | our $VERSION = '3.003'; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 22 | 2 | our @EXPORT_OK = 'tag_to_html'; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 24 | 2348 | ▶ | sub new { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 25 | 2348 | my $class = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 26 | 2348 | ▶ | my $self = bless \Mojo::DOM58::_HTML->new, ref $class || $class; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
ref $class || $class
MC/DC0%0/2
ref $class || $class
ref $class
$class
Truth table66%2/3
A || B
A= ref $classB= $class
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 27 | 2348 | ▶ | return @_ ? $self->parse(@_) : $self; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 28 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 30 | 12 | ▶ | sub new_tag { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new_tag: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 31 | 12 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 32 | 12 | my $new = $self->new; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 33 | 12 | $$new->tag(@_); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 34 | 12 | ▶ | $$new->xml($$self->xml) if ref $self; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 35 | 12 | return $new; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 36 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 38 | 1 | ▶ | sub TO_JSON { ${shift()}->render } | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
TO_JSON: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 40 | 29 | ▶ | sub all_text { _text(_nodes($_[0]->tree), $_[0]->xml, 1) } | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
all_text: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 42 | 15 | ▶ | sub ancestors { _select($_[0]->_collect([_ancestors($_[0]->tree)]), $_[1]) } | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
ancestors: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 44 | 9 | ▶ | sub append { shift->_add(1, @_) } | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
append: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 45 | 16 | ▶ | sub append_content { shift->_content(1, 0, @_) } | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
append_content: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 47 | 712 | ▶ | sub at { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
at: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 48 | 712 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 49 | 712 | ▶ | return undef unless my $result = $self->_css->select_one(@_); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 50 | 648 | return $self->_build($result, $self->xml); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 53 | 173 | ▶ | sub attr { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
attr: called CC=6 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 54 | 173 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 56 | # Hash | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 57 | 173 | my $tree = $self->tree; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 58 | 173 | ▶ | my $attrs = $tree->[0] ne 'tag' ? {} : $tree->[2]; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 59 | 173 | ▶ | return $attrs unless @_; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 61 | # Get | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 62 | 37 | ▶ | return $attrs->{$_[0]} unless @_ > 1 || ref $_[0]; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
@_ > 1 or ref $_[0]
MC/DC100%2/2
@_ > 1 or ref $_[0]
@_ > 1
ref $_[0]
Truth table100%3/3
A or B
A= @_ > 1B= ref $_[0]
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 64 | # Set | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 65 | 4 | ▶ | my $values = ref $_[0] ? $_[0] : {@_}; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 66 | 4 | @$attrs{keys %$values} = values %$values; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 68 | 4 | return $self; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 71 | 72 | ▶ | sub child_nodes { $_[0]->_collect(_nodes($_[0]->tree)) } | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
child_nodes: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 73 | 14 | ▶ | sub children { _select($_[0]->_collect(_nodes($_[0]->tree, 1)), $_[1]) } | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
children: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 75 | 61 | ▶ | sub content { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
content: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 76 | 61 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 78 | 61 | my $type = $self->type; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 79 | 61 | ▶ | if ($type eq 'root' || $type eq 'tag') { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$type eq "root" or $type eq "tag"
MC/DC100%2/2
$type eq "root" or $type eq "tag"
$type eq "root"
$type eq "tag"
Truth table100%3/3
A or B
A= $type eq "root"B= $type eq "tag"
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 80 | 24 | ▶ | return $self->_content(0, 1, @_) if @_; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 81 | 7 | my $html = Mojo::DOM58::_HTML->new(xml => $self->xml); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 82 | 7 | return join '', map { $html->tree($_)->render } @{_nodes($self->tree)}; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 85 | 37 | ▶ | return $self->tree->[1] unless @_; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 86 | 3 | $self->tree->[1] = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 87 | 3 | return $self; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 90 | 13 | ▶ | sub descendant_nodes { $_[0]->_collect(_all(_nodes($_[0]->tree))) } | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
descendant_nodes: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 92 | 445 | ▶ | sub find { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
find: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 93 | 445 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 94 | 445 | return $self->_collect($self->_css->select(@_)); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 97 | 8 | ▶ | sub following { _select($_[0]->_collect(_siblings($_[0]->tree, 1, 1)), $_[1]) } | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
following: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 98 | 7 | ▶ | sub following_nodes { $_[0]->_collect(_siblings($_[0]->tree, 0, 1)) } | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
following_nodes: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 100 | 51 | ▶ | sub matches { shift->_css->matches(@_) } | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
matches: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 102 | 18 | ▶ | sub namespace { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
namespace: called CC=8 SCAR=20.8
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 103 | 18 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 105 | 18 | ▶ | return undef if (my $tree = $self->tree)->[0] ne 'tag'; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 107 | # Extract namespace prefix and search parents | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 108 | 16 | ▶ | my $ns = $tree->[1] =~ /^(.*?):/ ? "xmlns:$1" : undef; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 109 | 16 | for my $node ($tree, _ancestors($tree)) { | |||||||||||||||||||||||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 111 | # Namespace for prefix | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 112 | 35 | my $attrs = $node->[2]; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 113 | 35 | ▶ | if ($ns) { $_ eq $ns and return $attrs->{$_} for keys %$attrs } | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 115 | # Namespace attribute | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 116 | 10 | elsif (defined $attrs->{xmlns}) { return $attrs->{xmlns} } | |||||||||||||||||||||||||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 119 | 1 | return undef; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 122 | 13 | ▶ | sub next { $_[0]->_maybe(_siblings($_[0]->tree, 1, 1, 0)) } | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
next: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 123 | 5 | ▶ | sub next_node { $_[0]->_maybe(_siblings($_[0]->tree, 0, 1, 0)) } | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
next_node: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 125 | 54 | ▶ | sub parent { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
parent: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 126 | 54 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 127 | 54 | ▶ | return undef if (my $tree = $self->tree)->[0] eq 'root'; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 128 | 54 | return $self->_build(_parent($tree), $self->xml); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 129 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 131 | 241 | ▶ | sub parse { ${$_[0]}->parse($_[1]) and return $_[0] } | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
parse: called CC=2 SCAR=7.2
Pod
documented
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 133 | 5 | ▶ | sub preceding { _select($_[0]->_collect(_siblings($_[0]->tree, 1, 0)), $_[1]) } | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
preceding: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 134 | 7 | ▶ | sub preceding_nodes { $_[0]->_collect(_siblings($_[0]->tree, 0)) } | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
preceding_nodes: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 136 | 11 | ▶ | sub prepend { shift->_add(0, @_) } | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
prepend: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 137 | 6 | ▶ | sub prepend_content { shift->_content(0, 0, @_) } | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
prepend_content: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 139 | 7 | ▶ | sub previous { $_[0]->_maybe(_siblings($_[0]->tree, 1, 0, -1)) } | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
previous: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 140 | 5 | ▶ | sub previous_node { $_[0]->_maybe(_siblings($_[0]->tree, 0, 0, -1)) } | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
previous_node: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 142 | 11 | ▶ | sub remove { shift->replace('') } | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
remove: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 144 | 29 | ▶ | sub replace { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
replace: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 145 | 29 | my ($self, $new) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 146 | 29 | ▶ | return $self->parse($new) if (my $tree = $self->tree)->[0] eq 'root'; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 147 | 21 | return $self->_replace(_parent($tree), $tree, _nodes($self->_parse($new))); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 148 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 150 | 12 | ▶ | sub root { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
root: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 151 | 12 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 152 | 12 | ▶ | return $self unless my $tree = _ancestors($self->tree, 1); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 153 | 9 | return $self->_build($tree, $self->xml); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 154 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 156 | 13 | ▶ | sub selector { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
selector: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 157 | 13 | ▶ | return undef unless (my $tree = shift->tree)->[0] eq 'tag'; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 158 | return join ' > ', | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 159 | 11 | reverse map { $_->[1] . ':nth-child(' . (@{_siblings($_, 1)} + 1) . ')' } | |||||||||||||||||||||||||||||||||||||||||||||||||
| 160 | $tree, _ancestors($tree); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 161 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 163 | 10 | ▶ | sub strip { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
strip: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 164 | 10 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 165 | 10 | ▶ | return $self if (my $tree = $self->tree)->[0] ne 'tag'; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 166 | 8 | return $self->_replace($tree->[3], $tree, _nodes($tree)); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 167 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 169 | 97 | ▶ | sub tag { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
tag: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 170 | 97 | my ($self, $tag) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 171 | 97 | ▶ | return undef if (my $tree = $self->tree)->[0] ne 'tag'; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 172 | 95 | ▶ | return $tree->[1] unless $tag; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 173 | 1 | $tree->[1] = $tag; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 174 | 1 | return $self; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 175 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 177 | 1 | ▶ | sub tap { Mojo::DOM58::_Collection::tap(@_) } | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
tap: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 179 | 792 | ▶ | sub text { _text(_nodes(shift->tree), 0, 0) } | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
text: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 181 | 171 | ▶ | sub to_string { ${shift()}->render } | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
to_string: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 183 | 4994 | ▶ | sub tree { @_ > 1 ? (${$_[0]}->tree($_[1]) and return $_[0]) : ${$_[0]}->tree } | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
tree: called CC=3 SCAR=11.1
Pod
documented
Branch: 1 branch
Condition50%1/2
${$_[0];}->tree($_[1]) and return $_[0]
MC/DC0%0/1
${$_[0];}->tree($_[1]) and return $_[0]
${$_[0];}->tree($_[1])
Truth table50%1/2
A
A= ${$_[0];}->tree($_[1])
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 185 | 89 | ▶ | sub type { shift->tree->[0] } | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
type: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 187 | 27 | ▶ | sub val { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
val: called CC=12 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 188 | 27 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 190 | # "option" | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 191 | 27 | ▶ | return defined($self->{value}) ? $self->{value} : $self->text | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 192 | if (my $tag = $self->tag) eq 'option'; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 194 | # "input" ("type=checkbox" and "type=radio") | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 195 | 17 | ▶ | my $type = $self->{type} || ''; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$self->{'type'} || ''
MC/DC100%1/1
$self->{'type'} || ''
$self->{'type'}
Truth table100%2/2
A
A= $self->{'type'}
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 196 | 17 | ▶ | return defined $self->{value} ? $self->{value} : 'on' | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition100%6/6
$tag eq "input" and $type eq "radio" || $type eq "checkbox"
$type eq "radio" or $type eq "checkbox"
MC/DC100%3/3
$tag eq "input" and $type eq "radio" || $type eq "checkbox"
$tag eq "input"
$type eq "radio"
$type eq "checkbox"
Truth table100%4/4
A and B or C
A= $tag eq "input"B= $type eq "radio"C= $type eq "checkbox"
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 197 | if $tag eq 'input' && ($type eq 'radio' || $type eq 'checkbox'); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 199 | # "textarea", "input" or "button" | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 200 | 12 | ▶ | return $tag eq 'textarea' ? $self->text : $self->{value} if $tag ne 'select'; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 202 | # "select" | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 203 | my $v = $self->find('option:checked:not([disabled])') | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 204 | 5 | ▶ | ->grep(sub { !$_->ancestors('optgroup[disabled]')->size })->map('val'); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 205 | 5 | ▶ | return exists $self->{multiple} ? $v->size ? $v->to_array : undef : $v->last; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 206 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 208 | 1 | ▶ | sub with_roles { Mojo::DOM58::_Collection::with_roles(@_) } | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
with_roles: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 210 | 9 | ▶ | sub wrap { shift->_wrap(0, @_) } | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
wrap: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 211 | 7 | ▶ | sub wrap_content { shift->_wrap(1, @_) } | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
wrap_content: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 212 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 213 | 3569 | ▶ | sub xml { @_ > 1 ? (${$_[0]}->xml($_[1]) and return $_[0]) : ${$_[0]}->xml } | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
xml: called CC=3 SCAR=11.1
Pod
documented
Branch: 1 branch
Condition50%1/2
${$_[0];}->xml($_[1]) and return $_[0]
MC/DC0%0/1
${$_[0];}->xml($_[1]) and return $_[0]
${$_[0];}->xml($_[1])
Truth table50%1/2
A
A= ${$_[0];}->xml($_[1])
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 214 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 215 | 20 | ▶ | sub _add { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_add: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 216 | 20 | my ($self, $offset, $new) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 217 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 218 | 20 | ▶ | return $self if (my $tree = $self->tree)->[0] eq 'root'; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 219 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 220 | 16 | my $parent = _parent($tree); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 221 | splice @$parent, _offset($parent, $tree) + $offset, 0, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 222 | 16 | @{_link($parent, _nodes($self->_parse($new)))}; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 223 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 224 | 16 | return $self; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 225 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 226 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 227 | 21 | ▶ | sub _all { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_all: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 228 | 21 | my $nodes = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 229 | 21 | ▶ | @$nodes = map { $_->[0] eq 'tag' ? ($_, @{_all(_nodes($_))}) : ($_) } @$nodes; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 230 | 21 | return $nodes; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 231 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 232 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 233 | 54 | ▶ | sub _ancestors { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_ancestors: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 234 | 54 | my ($tree, $root) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 235 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 236 | 54 | ▶ | return () unless $tree = _parent($tree); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 237 | 51 | my @ancestors; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 238 | 51 | ▶ | do { push @ancestors, $tree } | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$tree->[0] eq "tag" and $tree = $tree->[3]
MC/DC50%1/2
$tree->[0] eq "tag" and $tree = $tree->[3]
$tree->[0] eq "tag"
$tree = $tree->[3]
Truth table66%2/3
A and B
A= $tree->[0] eq "tag"B= $tree = $tree->[3]
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 239 | while ($tree->[0] eq 'tag') && ($tree = $tree->[3]); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 240 | 51 | ▶ | return $root ? $ancestors[-1] : @ancestors[0 .. $#ancestors - 1]; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 241 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 242 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 243 | 2099 | ▶ | sub _build { shift->new->tree(shift)->xml(shift) } | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_build: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 244 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 245 | 585 | ▶ | sub _collect { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_collect: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 246 | 585 | ▶ | my ($self, $nodes) = (shift, shift || []); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
shift() || []
MC/DC0%0/1
shift() || []
shift()
Truth table50%1/2
A
A= shift()
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 247 | 585 | my $xml = $self->xml; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 248 | 585 | return Mojo::DOM58::_Collection->new(map { $self->_build($_, $xml) } @$nodes); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 249 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 250 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 251 | 39 | ▶ | sub _content { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_content: called CC=6 SCAR=17.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 252 | 39 | my ($self, $start, $offset, $new) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 253 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 254 | 39 | my $tree = $self->tree; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 255 | 39 | ▶ | unless ($tree->[0] eq 'root' || $tree->[0] eq 'tag') { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$tree->[0] eq "root" or $tree->[0] eq "tag"
MC/DC100%2/2
$tree->[0] eq "root" or $tree->[0] eq "tag"
$tree->[0] eq "root"
$tree->[0] eq "tag"
Truth table100%3/3
A or B
A= $tree->[0] eq "root"B= $tree->[0] eq "tag"
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 256 | 2 | my $old = $self->content; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 257 | 2 | ▶ | return $self->content($start ? $old . $new : $new . $old); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 258 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 259 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 260 | 37 | ▶ | $start = $start ? ($#$tree + 1) : _start($tree); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 261 | 37 | ▶ | $offset = $offset ? $#$tree : 0; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 262 | 37 | splice @$tree, $start, $offset, @{_link($tree, _nodes($self->_parse($new)))}; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 263 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 264 | 37 | return $self; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 265 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 266 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 267 | 1208 | ▶ | sub _css { Mojo::DOM58::_CSS->new(tree => shift->tree) } | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_css: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 268 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 269 | 3 | ▶ | sub _fragment { _link(my $r = ['root', @_], [@_], 1); $r } | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_fragment: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 270 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 271 | 109 | ▶ | sub _link { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_link: called CC=8 SCAR=20.8
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 272 | 109 | my ($parent, $children, $fragment) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 273 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 274 | # Link parent to children (raw text is only allowd in script and style elements) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 275 | 109 | ▶ | my $raw = $fragment || $parent->[0] eq 'tag' && ($parent->[1] eq 'script' || $parent->[1] eq 'style'); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%9/9
$fragment || $parent->[0] eq 'tag' && ($parent->[1] eq 'script' || $parent->[1] eq 'style')
$parent->[0] eq "tag" and $parent->[1] eq "script" || $parent->[1] eq "style"
$parent->[1] eq "script" or $parent->[1] eq "style"
MC/DC100%4/4
$fragment || $parent->[0] eq 'tag' && ($parent->[1] eq 'script' || $parent->[1] eq 'style')
$fragment
$parent->[0] eq "tag"
$parent->[1] eq "script"
$parent->[1] eq "style"
Truth table100%5/5
A || B and C or D
A= $fragmentB= $parent->[0] eq "tag"C= $parent->[1] eq "script"D= $parent->[1] eq "style"
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 276 | 109 | for my $node (@$children) { | |||||||||||||||||||||||||||||||||||||||||||||||||
| 277 | 108 | ▶ | $node->[0] = 'text' if $node->[0] eq 'raw' && !$raw; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$node->[0] eq "raw" and not $raw
MC/DC100%2/2
$node->[0] eq "raw" and not $raw
$node->[0] eq "raw"
not $raw
Truth table100%3/3
A and not(B)
A= $node->[0] eq "raw"B= not $raw
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 278 | 108 | ▶ | my $offset = $node->[0] eq 'tag' ? 3 : 2; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 279 | 108 | $node->[$offset] = $parent; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 280 | 108 | weaken $node->[$offset]; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 281 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 282 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 283 | 109 | return $children; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 284 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 285 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 286 | 30 | ▶ | sub _maybe { $_[1] ? $_[0]->_build($_[1], $_[0]->xml) : undef } | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_maybe: called CC=2 SCAR=6.9
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 287 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 288 | 1293 | ▶ | sub _nodes { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_nodes: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 289 | 1293 | ▶ | return () unless my $tree = shift; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 290 | 1293 | my @nodes = @$tree[_start($tree) .. $#$tree]; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 291 | 1293 | ▶ | return shift() ? [grep { $_->[0] eq 'tag' } @nodes] : \@nodes; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 292 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 293 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 294 | 52 | ▶ | sub _offset { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_offset: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 295 | 52 | my ($parent, $child) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 296 | 52 | my $i = _start($parent); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 297 | 52 | ▶ | $_ eq $child ? last : $i++ for @$parent[$i .. $#$parent]; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 298 | 52 | return $i; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 299 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 300 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 301 | 234 | ▶ | sub _parent { $_[0]->[$_[0][0] eq 'tag' ? 3 : 2] } | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_parent: called CC=2 SCAR=6.9
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 302 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 303 | 88 | ▶ | sub _parse { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_parse: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 304 | 88 | my ($self, $input) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 305 | 88 | ▶ | return Mojo::DOM58::_HTML->new(xml => $self->xml)->parse($input)->tree | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
builtin::blessed($input) and $input->isa("Mojo::DOM58")
MC/DC50%1/2
builtin::blessed($input) and $input->isa("Mojo::DOM58")
builtin::blessed($input)
$input->isa("Mojo::DOM58")
Truth table66%2/3
A and B
A= builtin::blessed($input)B= $input->isa("Mojo::DOM58")
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 306 | unless blessed $input && $input->isa('Mojo::DOM58'); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 307 | 24 | my $tree = dclone $input->tree; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 308 | 24 | ▶ | return $tree->[0] eq 'root' ? $tree : _fragment($tree); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 309 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 310 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 311 | 36 | ▶ | sub _replace { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_replace: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 312 | 36 | my ($self, $parent, $child, $nodes) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 313 | 36 | splice @$parent, _offset($parent, $child), 1, @{_link($parent, $nodes)}; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 314 | 36 | return $self->parent; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 315 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 316 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 317 | 42 | ▶ | sub _select { $_[1] ? $_[0]->grep(matches => $_[1]) : $_[0] } | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_select: called CC=2 SCAR=6.9
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 318 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 319 | 88 | ▶ | sub _siblings { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_siblings: called CC=11 SCAR=24.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 320 | 88 | my ($tree, $tags, $tail, $i) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 321 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 322 | 88 | ▶ | return defined $i ? undef : [] if $tree->[0] eq 'root'; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 323 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 324 | 82 | my $nodes = _nodes(_parent($tree)); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 325 | 82 | my $match = -1; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 326 | 82 | ▶ | defined($match++) and $_ eq $tree and last for @$nodes; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $match++ and $_ eq $tree
MC/DC50%1/2
defined $match++ and $_ eq $tree
defined $match++
$_ eq $tree
Truth table66%2/3
A and B
A= defined $match++B= $_ eq $tree
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 327 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 328 | 82 | ▶ | if ($tail) { splice @$nodes, 0, $match + 1 } | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 329 | 52 | else { splice @$nodes, $match, ($#$nodes + 1) - $match } | |||||||||||||||||||||||||||||||||||||||||||||||||
| 330 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 331 | 82 | ▶ | @$nodes = grep { $_->[0] eq 'tag' } @$nodes if $tags; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 332 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 333 | 82 | ▶ | return defined $i ? $i == -1 && !@$nodes ? undef : $nodes->[$i] : $nodes; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition100%3/3
$i == -1 and not @$nodes
MC/DC100%2/2
$i == -1 and not @$nodes
$i == -1
not @$nodes
Truth table100%3/3
A and not(B)
A= $i == -1B= not @$nodes
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 334 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 335 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 336 | 1372 | ▶ | sub _start { $_[0][0] eq 'root' ? 1 : 4 } | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_start: called CC=2 SCAR=6.9
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 337 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 338 | 821 | ▶ | sub _text { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_text: called CC=10 SCAR=23.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 339 | 821 | my ($nodes, $xml, $all) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 340 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 341 | 821 | my $text = ''; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 342 | 821 | ▶ | while (my $node = shift @$nodes) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 343 | 1189 | my $type = $node->[0]; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 344 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 345 | # Text | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 346 | 1189 | ▶ | if ($type eq 'text' || $type eq 'cdata' || $type eq 'raw') { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition100%6/6
$type eq "text" or $type eq "cdata" or $type eq "raw"
$type eq "text" or $type eq "cdata"
MC/DC100%3/3
$type eq "text" or $type eq "cdata" or $type eq "raw"
$type eq "text"
$type eq "cdata"
$type eq "raw"
Truth table100%4/4
A or B or C
A= $type eq "text"B= $type eq "cdata"C= $type eq "raw"
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 347 | 972 | $text .= $node->[1]; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 348 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 349 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 350 | # Nested tag | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 351 | ▶ | elsif ($type eq 'tag' && $all) { | |||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%3/3
$type eq "tag" and $all
MC/DC100%2/2
$type eq "tag" and $all
$type eq "tag"
$all
Truth table100%3/3
A and B
A= $type eq "tag"B= $all
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 352 | 157 | ▶ | unshift @$nodes, @{_nodes($node)} if $xml || ($node->[1] ne 'script' && $node->[1] ne 'style'); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%6/6
$xml or $node->[1] ne "script" and $node->[1] ne "style"
$node->[1] ne "script" and $node->[1] ne "style"
MC/DC100%3/3
$xml or $node->[1] ne "script" and $node->[1] ne "style"
$xml
$node->[1] ne "script"
$node->[1] ne "style"
Truth table100%4/4
A or B and C
A= $xmlB= $node->[1] ne "script"C= $node->[1] ne "style"
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 353 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 354 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 355 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 356 | 821 | return $text; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 357 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 358 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 359 | 16 | ▶ | sub _wrap { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_wrap: called CC=9 SCAR=22.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 360 | 16 | my ($self, $content, $new) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 361 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 362 | 16 | ▶ | return $self if (my $tree = $self->tree)->[0] eq 'root' && !$content; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
+(my $tree = $self->tree)->[0] eq "root" and not $content
MC/DC100%2/2
+(my $tree = $self->tree)->[0] eq "root" and not $content
+(my $tree = $self->tree)->[0] eq "root"
not $content
Truth table100%3/3
A and not(B)
A= +(my $tree = $self->tree)->[0] eq "root"B= not $content
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 363 | 15 | ▶ | return $self if $tree->[0] ne 'root' && $tree->[0] ne 'tag' && $content; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%6/6
$tree->[0] ne "root" and $tree->[0] ne "tag" and $content
$tree->[0] ne "root" and $tree->[0] ne "tag"
MC/DC100%3/3
$tree->[0] ne "root" and $tree->[0] ne "tag" and $content
$tree->[0] ne "root"
$tree->[0] ne "tag"
$content
Truth table100%4/4
A and B and C
A= $tree->[0] ne "root"B= $tree->[0] ne "tag"C= $content
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 364 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 365 | # Find innermost tag | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 366 | 14 | my $current; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 367 | 14 | my $first = $new = $self->_parse($new); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 368 | 14 | ▶ | $current = $first while $first = _nodes($first, 1)->[0]; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 369 | 14 | ▶ | return $self unless $current; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 370 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 371 | # Wrap content | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 372 | 12 | ▶ | if ($content) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 373 | 5 | push @$current, @{_link($current, _nodes($tree))}; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 374 | 5 | splice @$tree, _start($tree), $#$tree, @{_link($tree, _nodes($new))}; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 375 | 5 | return $self; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 376 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 377 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 378 | # Wrap element | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 379 | 7 | $self->_replace(_parent($tree), $tree, _nodes($new)); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 380 | 7 | push @$current, @{_link($current, [$tree])}; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 381 | 7 | return $self; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 382 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 383 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 384 | 2 | 1; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 385 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 386 | =encoding utf8 | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 387 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 388 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 389 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 390 | Mojo::DOM58 - Minimalistic HTML/XML DOM parser with CSS selectors | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 391 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 392 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 393 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 394 | use Mojo::DOM58; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 395 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 396 | # Parse | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 397 | my $dom = Mojo::DOM58->new('<div><p id="a">Test</p><p id="b">123</p></div>'); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 398 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 399 | # Find | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 400 | say $dom->at('#b')->text; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 401 | say $dom->find('p')->map('text')->join("\n"); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 402 | say $dom->find('[id]')->map(attr => 'id')->join("\n"); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 403 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 404 | # Iterate | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 405 | $dom->find('p[id]')->reverse->each(sub { say $_->{id} }); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 406 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 407 | # Loop | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 408 | for my $e ($dom->find('p[id]')->each) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 409 | say $e->{id}, ':', $e->text; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 410 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 411 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 412 | # Modify | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 413 | $dom->find('div p')->last->append('<p id="c">456</p>'); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 414 | $dom->at('#c')->prepend($dom->new_tag('p', id => 'd', '789')); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 415 | $dom->find(':not(p)')->map('strip'); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 416 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 417 | # Render | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 418 | say "$dom"; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 419 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 420 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 421 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 422 | L<Mojo::DOM58> is a minimalistic and relaxed pure-perl HTML/XML DOM parser based | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 423 | on L<Mojo::DOM>. It supports the L<HTML Living Standard|https://html.spec.whatwg.org/> | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 424 | and L<Extensible Markup Language (XML) 1.0|https://www.w3.org/TR/xml/>, and | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 425 | matching based on L<CSS3 selectors|https://www.w3.org/TR/selectors/>. It will | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 426 | even try to interpret broken HTML and XML, so you should not use it for | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 427 | validation or sanitization. Its interpretation of broken markup can differ from | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 428 | that of a web browser. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 429 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 430 | =head1 FORK INFO | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 431 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 432 | L<Mojo::DOM58> is a fork of L<Mojo::DOM> and tracks features and fixes to stay | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 433 | closely compatible with upstream. It differs only in the standalone format and | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 434 | compatibility with Perl 5.8. Any bugs or patches not related to these changes | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 435 | should be reported directly to the L<Mojolicious> issue tracker. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 436 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 437 | This release of L<Mojo::DOM58> is up to date with version C<9.53> of | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 438 | L<Mojolicious>. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 439 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 440 | =head1 NODES AND ELEMENTS | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 441 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 442 | When we parse an HTML/XML fragment, it gets turned into a tree of nodes. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 443 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 444 | <!DOCTYPE html> | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 445 | <html> | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 446 | <head><title>Hello</title></head> | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 447 | <body>World!</body> | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 448 | </html> | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 449 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 450 | There are currently eight different kinds of nodes, C<cdata>, C<comment>, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 451 | C<doctype>, C<pi>, C<raw>, C<root>, C<tag> and C<text>. Elements are nodes of | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 452 | the type C<tag>. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 453 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 454 | root | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 455 | |- doctype (html) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 456 | +- tag (html) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 457 | |- tag (head) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 458 | | +- tag (title) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 459 | | +- raw (Hello) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 460 | +- tag (body) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 461 | +- text (World!) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 462 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 463 | While all node types are represented as L<Mojo::DOM58> objects, some methods like | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 464 | L</"attr"> and L</"namespace"> only apply to elements. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 465 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 466 | =head1 HTML AND XML | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 467 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 468 | L<Mojo::DOM58> defaults to HTML semantics, that means all tags and attribute | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 469 | names are lowercased and selectors need to be lowercase as well. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 470 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 471 | # HTML semantics | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 472 | my $dom = Mojo::DOM58->new('<P ID="greeting">Hi!</P>'); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 473 | say $dom->at('p[id]')->text; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 474 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 475 | If the document starts with an XML declaration, the parser will automatically | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 476 | switch into XML mode and everything becomes case-sensitive. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 477 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 478 | # XML semantics | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 479 | my $dom = Mojo::DOM58->new('<?xml version="1.0"?><P ID="greeting">Hi!</P>'); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 480 | say $dom->at('P[ID]')->text; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 481 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 482 | HTML or XML semantics can also be forced with the L</"xml"> method. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 483 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 484 | # Force HTML semantics | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 485 | my $dom = Mojo::DOM58->new->xml(0)->parse('<P ID="greeting">Hi!</P>'); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 486 | say $dom->at('p[id]')->text; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 487 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 488 | # Force XML semantics | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 489 | my $dom = Mojo::DOM58->new->xml(1)->parse('<P ID="greeting">Hi!</P>'); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 490 | say $dom->at('P[ID]')->text; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 491 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 492 | =head1 SELECTORS | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 493 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 494 | L<Mojo::DOM58> uses a CSS selector engine based on L<Mojo::DOM::CSS>. All CSS | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 495 | selectors that make sense for a standalone parser are supported. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 496 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 497 | =over | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 498 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 499 | =item Z<>* | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 500 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 501 | Any element. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 502 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 503 | my $all = $dom->find('*'); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 504 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 505 | =item E | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 506 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 507 | An element of type C<E>. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 508 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 509 | my $title = $dom->at('title'); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 510 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 511 | =item E[foo] | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 512 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 513 | An C<E> element with a C<foo> attribute. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 514 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 515 | my $links = $dom->find('a[href]'); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 516 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 517 | =item E[foo="bar"] | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 518 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 519 | An C<E> element whose C<foo> attribute value is exactly equal to C<bar>. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 520 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 521 | my $case_sensitive = $dom->find('input[type="hidden"]'); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 522 | my $case_sensitive = $dom->find('input[type=hidden]'); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 523 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 524 | =item E[foo="bar" i] | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 525 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 526 | An C<E> element whose C<foo> attribute value is exactly equal to any | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 527 | (ASCII-range) case-permutation of C<bar>. Note that this selector is | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 528 | B<EXPERIMENTAL> and might change without warning! | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 529 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 530 | my $case_insensitive = $dom->find('input[type="hidden" i]'); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 531 | my $case_insensitive = $dom->find('input[type=hidden i]'); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 532 | my $case_insensitive = $dom->find('input[class~="foo" i]'); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 533 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 534 | This selector is part of | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 535 | L<Selectors Level 4|https://dev.w3.org/csswg/selectors-4>, which is still a work | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 536 | in progress. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 537 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 538 | =item E[foo="bar" s] | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 539 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 540 | An C<E> element whose C<foo> attribute value is exactly and case-sensitively | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 541 | equal to C<bar>. Note that this selector is B<EXPERIMENTAL> and might change | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 542 | without warning! | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 543 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 544 | my $case_sensitive = $dom->find('input[type="hidden" s]'); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 545 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 546 | This selector is part of | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 547 | L<Selectors Level 4|https://dev.w3.org/csswg/selectors-4>, which is still a work | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 548 | in progress. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 549 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 550 | =item E[foo~="bar"] | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 551 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 552 | An C<E> element whose C<foo> attribute value is a list of whitespace-separated | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 553 | values, one of which is exactly equal to C<bar>. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 554 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 555 | my $foo = $dom->find('input[class~="foo"]'); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 556 | my $foo = $dom->find('input[class~=foo]'); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 557 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 558 | =item E[foo^="bar"] | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 559 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 560 | An C<E> element whose C<foo> attribute value begins exactly with the string | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 561 | C<bar>. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 562 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 563 | my $begins_with = $dom->find('input[name^="f"]'); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 564 | my $begins_with = $dom->find('input[name^=f]'); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 565 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 566 | =item E[foo$="bar"] | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 567 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 568 | An C<E> element whose C<foo> attribute value ends exactly with the string | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 569 | C<bar>. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 570 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 571 | my $ends_with = $dom->find('input[name$="o"]'); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 572 | my $ends_with = $dom->find('input[name$=o]'); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 573 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 574 | =item E[foo*="bar"] | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 575 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 576 | An C<E> element whose C<foo> attribute value contains the substring C<bar>. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 577 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 578 | my $contains = $dom->find('input[name*="fo"]'); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 579 | my $contains = $dom->find('input[name*=fo]'); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 580 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 581 | =item E[foo|="en"] | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 582 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 583 | An C<E> element whose C<foo> attribute has a hyphen-separated list of values | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 584 | beginning (from the left) with C<en>. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 585 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 586 | my $english = $dom->find('link[hreflang|=en]'); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 587 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 588 | =item E:root | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 589 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 590 | An C<E> element, root of the document. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 591 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 592 | my $root = $dom->at(':root'); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 593 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 594 | =item E:nth-child(n) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 595 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 596 | An C<E> element, the C<n-th> child of its parent. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 597 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 598 | my $third = $dom->find('div:nth-child(3)'); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 599 | my $odd = $dom->find('div:nth-child(odd)'); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 600 | my $even = $dom->find('div:nth-child(even)'); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 601 | my $top3 = $dom->find('div:nth-child(-n+3)'); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 602 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 603 | =item E:nth-last-child(n) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 604 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 605 | An C<E> element, the C<n-th> child of its parent, counting from the last one. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 606 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 607 | my $third = $dom->find('div:nth-last-child(3)'); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 608 | my $odd = $dom->find('div:nth-last-child(odd)'); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 609 | my $even = $dom->find('div:nth-last-child(even)'); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 610 | my $bottom3 = $dom->find('div:nth-last-child(-n+3)'); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 611 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 612 | =item E:nth-of-type(n) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 613 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 614 | An C<E> element, the C<n-th> sibling of its type. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 615 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 616 | my $third = $dom->find('div:nth-of-type(3)'); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 617 | my $odd = $dom->find('div:nth-of-type(odd)'); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 618 | my $even = $dom->find('div:nth-of-type(even)'); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 619 | my $top3 = $dom->find('div:nth-of-type(-n+3)'); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 620 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 621 | =item E:nth-last-of-type(n) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 622 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 623 | An C<E> element, the C<n-th> sibling of its type, counting from the last one. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 624 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 625 | my $third = $dom->find('div:nth-last-of-type(3)'); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 626 | my $odd = $dom->find('div:nth-last-of-type(odd)'); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 627 | my $even = $dom->find('div:nth-last-of-type(even)'); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 628 | my $bottom3 = $dom->find('div:nth-last-of-type(-n+3)'); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 629 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 630 | =item E:first-child | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 631 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 632 | An C<E> element, first child of its parent. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 633 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 634 | my $first = $dom->find('div p:first-child'); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 635 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 636 | =item E:last-child | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 637 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 638 | An C<E> element, last child of its parent. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 639 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 640 | my $last = $dom->find('div p:last-child'); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 641 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 642 | =item E:first-of-type | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 643 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 644 | An C<E> element, first sibling of its type. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 645 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 646 | my $first = $dom->find('div p:first-of-type'); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 647 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 648 | =item E:last-of-type | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 649 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 650 | An C<E> element, last sibling of its type. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 651 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 652 | my $last = $dom->find('div p:last-of-type'); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 653 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 654 | =item E:only-child | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 655 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 656 | An C<E> element, only child of its parent. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 657 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 658 | my $lonely = $dom->find('div p:only-child'); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 659 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 660 | =item E:only-of-type | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 661 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 662 | An C<E> element, only sibling of its type. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 663 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 664 | my $lonely = $dom->find('div p:only-of-type'); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 665 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 666 | =item E:empty | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 667 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 668 | An C<E> element that has no children (including text nodes). | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 669 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 670 | my $empty = $dom->find(':empty'); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 671 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 672 | =item E:any-link | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 673 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 674 | Alias for L</"E:link">. Note that this selector is B<EXPERIMENTAL> and might | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 675 | change without warning! This selector is part of | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 676 | L<Selectors Level 4|https://dev.w3.org/csswg/selectors-4>, which is still a | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 677 | work in progress. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 678 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 679 | =item E:link | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 680 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 681 | An C<E> element being the source anchor of a hyperlink of which the target is | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 682 | not yet visited (C<:link>) or already visited (C<:visited>). Note that | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 683 | L<Mojo::DOM58> is not stateful, therefore C<:any-link>, C<:link> and | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 684 | C<:visited> yield exactly the same results. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 685 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 686 | my $links = $dom->find(':any-link'); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 687 | my $links = $dom->find(':link'); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 688 | my $links = $dom->find(':visited'); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 689 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 690 | =item E:visited | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 691 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 692 | Alias for L</"E:link">. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 693 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 694 | =item E:scope | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 695 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 696 | An C<E> element being a designated reference element. Note that this selector is B<EXPERIMENTAL> and might change | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 697 | without warning! | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 698 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 699 | my $scoped = $dom->find('a:not(:scope > a)'); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 700 | my $scoped = $dom->find('div :scope p'); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 701 | my $scoped = $dom->find('~ p'); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 702 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 703 | This selector is part of L<Selectors Level 4|https://dev.w3.org/csswg/selectors-4>, which is still a work in progress. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 704 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 705 | =item E:checked | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 706 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 707 | A user interface element C<E> which is checked (for instance a radio-button or | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 708 | checkbox). | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 709 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 710 | my $input = $dom->find(':checked'); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 711 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 712 | =item E.warning | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 713 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 714 | An C<E> element whose class is "warning". | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 715 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 716 | my $warning = $dom->find('div.warning'); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 717 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 718 | =item E#myid | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 719 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 720 | An C<E> element with C<ID> equal to "myid". | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 721 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 722 | my $foo = $dom->at('div#foo'); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 723 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 724 | =item E:not(s1, s2) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 725 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 726 | An C<E> element that does not match either compound selector C<s1> or compound | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 727 | selector C<s2>. Note that support for compound selectors is B<EXPERIMENTAL> and | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 728 | might change without warning! | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 729 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 730 | my $others = $dom->find('div p:not(:first-child, :last-child)'); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 731 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 732 | Support for compound selectors was added as part of | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 733 | L<Selectors Level 4|https://dev.w3.org/csswg/selectors-4>, which is still a work | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 734 | in progress. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 735 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 736 | =item E:is(s1, s2) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 737 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 738 | An C<E> element that matches compound selector C<s1> and/or compound selector | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 739 | C<s2>. Note that this selector is B<EXPERIMENTAL> and might change without warning! | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 740 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 741 | my $headers = $dom->find(':is(section, article, aside, nav) h1'); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 742 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 743 | This selector is part of | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 744 | L<Selectors Level 4|https://dev.w3.org/csswg/selectors-4>, which is still a work | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 745 | in progress. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 746 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 747 | =item E:has(rs1, rs2) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 748 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 749 | An C<E> element, if either of the relative selectors C<rs1> or C<rs2>, when evaluated with C<E> as the :scope elements, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 750 | match an element. Note that this selector is B<EXPERIMENTAL> and might change without warning! | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 751 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 752 | my $link = $dom->find('a:has(> img)'); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 753 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 754 | This selector is part of L<Selectors Level 4|https://dev.w3.org/csswg/selectors-4>, which is still a work in progress. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 755 | Also be aware that this feature is currently marked C<at-risk>, so there is a high chance that it will get removed | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 756 | completely. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 757 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 758 | =item E:text(string_or_regex) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 759 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 760 | An C<E> element containing text content that substring matches C<string_or_regex> case-insensitively or that regex | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 761 | matches C<string_or_regex>. For regular expressions use the format C<:text(/.../)>. Note that this selector is | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 762 | B<EXPERIMENTAL> and might change without warning! | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 763 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 764 | # Substring match | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 765 | my $login = $dom->find(':text(Log in)'); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 766 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 767 | # Regex match | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 768 | my $login = $dom->find(':text(/Log ?in/)'); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 769 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 770 | # Regex match (case-insensitive) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 771 | my $login = $dom->find(':text(/(?i:Log ?in)/)'); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 772 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 773 | This is a custom selector for L<Mojo::DOM58> and not part of any spec. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 774 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 775 | =item A|E | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 776 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 777 | An C<E> element that belongs to the namespace alias C<A> from | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 778 | L<CSS Namespaces Module Level 3|https://www.w3.org/TR/css-namespaces-3/>. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 779 | Key/value pairs passed to selector methods are used to declare namespace | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 780 | aliases. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 781 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 782 | my $elem = $dom->find('lq|elem', lq => 'http://example.com/q-markup'); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 783 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 784 | Using an empty alias searches for an element that belongs to no namespace. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 785 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 786 | my $div = $dom->find('|div'); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 787 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 788 | =item E F | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 789 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 790 | An C<F> element descendant of an C<E> element. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 791 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 792 | my $headlines = $dom->find('div h1'); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 793 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 794 | =item E E<gt> F | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 795 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 796 | An C<F> element child of an C<E> element. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 797 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 798 | my $headlines = $dom->find('html > body > div > h1'); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 799 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 800 | =item E + F | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 801 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 802 | An C<F> element immediately preceded by an C<E> element. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 803 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 804 | my $second = $dom->find('h1 + h2'); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 805 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 806 | =item E ~ F | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 807 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 808 | An C<F> element preceded by an C<E> element. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 809 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 810 | my $second = $dom->find('h1 ~ h2'); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 811 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 812 | =item E, F, G | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 813 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 814 | Elements of type C<E>, C<F> and C<G>. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 815 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 816 | my $headlines = $dom->find('h1, h2, h3'); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 817 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 818 | =item E[foo=bar][bar=baz] | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 819 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 820 | An C<E> element whose attributes match all following attribute selectors. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 821 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 822 | my $links = $dom->find('a[foo^=b][foo$=ar]'); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 823 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 824 | =back | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 825 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 826 | =head1 OPERATORS | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 827 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 828 | L<Mojo::DOM58> overloads the following operators. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 829 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 830 | =head2 array | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 831 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 832 | my @nodes = @$dom; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 833 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 834 | Alias for L</"child_nodes">. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 835 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 836 | # "<!-- Test -->" | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 837 | $dom->parse('<!-- Test --><b>123</b>')->[0]; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 838 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 839 | =head2 bool | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 840 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 841 | my $bool = !!$dom; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 842 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 843 | Always true. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 844 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 845 | =head2 hash | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 846 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 847 | my %attrs = %$dom; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 848 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 849 | Alias for L</"attr">. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 850 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 851 | # "test" | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 852 | $dom->parse('<div id="test">Test</div>')->at('div')->{id}; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 853 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 854 | =head2 stringify | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 855 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 856 | my $str = "$dom"; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 857 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 858 | Alias for L</"to_string">. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 859 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 860 | =head1 FUNCTIONS | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 861 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 862 | L<Mojo::DOM58> implements the following functions, which can be imported | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 863 | individually. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 864 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 865 | =head2 tag_to_html | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 866 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 867 | my $str = tag_to_html 'div', id => 'foo', 'safe content'; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 868 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 869 | Generate HTML/XML tag and render it right away. This is a significantly faster | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 870 | alternative to L</"new_tag"> for template systems that have to generate a lot | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 871 | of tags. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 872 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 873 | =head1 METHODS | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 874 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 875 | L<Mojo::DOM58> implements the following methods. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 876 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 877 | =head2 new | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 878 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 879 | my $dom = Mojo::DOM58->new; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 880 | my $dom = Mojo::DOM58->new('<foo bar="baz">I ♥ Mojo::DOM58!</foo>'); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 881 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 882 | Construct a new scalar-based L<Mojo::DOM58> object and L</"parse"> HTML/XML | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 883 | fragment if necessary. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 884 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 885 | =head2 new_tag | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 886 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 887 | my $tag = Mojo::DOM58->new_tag('div'); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 888 | my $tag = $dom->new_tag('div'); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 889 | my $tag = $dom->new_tag('div', id => 'foo', hidden => undef); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 890 | my $tag = $dom->new_tag('div', 'safe content'); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 891 | my $tag = $dom->new_tag('div', id => 'foo', 'safe content'); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 892 | my $tag = $dom->new_tag('div', data => {mojo => 'rocks'}, 'safe content'); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 893 | my $tag = $dom->new_tag('div', id => 'foo', sub { 'unsafe content' }); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 894 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 895 | Construct a new L<Mojo::DOM58> object for an HTML/XML tag with or without | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 896 | attributes and content. The C<data> attribute may contain a hash reference with | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 897 | key/value pairs to generate attributes from. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 898 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 899 | # "<br>" | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 900 | $dom->new_tag('br'); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 901 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 902 | # "<div></div>" | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 903 | $dom->new_tag('div'); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 904 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 905 | # "<div id="foo" hidden></div>" | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 906 | $dom->new_tag('div', id => 'foo', hidden => undef); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 907 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 908 | # "<div>test & 123</div>" | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 909 | $dom->new_tag('div', 'test & 123'); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 910 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 911 | # "<div id="foo">test & 123</div>" | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 912 | $dom->new_tag('div', id => 'foo', 'test & 123'); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 913 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 914 | # "<div data-foo="1" data-bar="test">test & 123</div>"" | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 915 | $dom->new_tag('div', data => {foo => 1, Bar => 'test'}, 'test & 123'); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 916 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 917 | # "<div id="foo">test & 123</div>" | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 918 | $dom->new_tag('div', id => 'foo', sub { 'test & 123' }); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 919 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 920 | # "<div>Hello<b>Mojo!</b></div>" | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 921 | $dom->parse('<div>Hello</div>')->at('div') | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 922 | ->append_content($dom->new_tag('b', 'Mojo!'))->root; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 923 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 924 | =head2 all_text | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 925 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 926 | my $text = $dom->all_text; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 927 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 928 | Extract text content from all descendant nodes of this element. For HTML documents C<script> and C<style> elements are | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 929 | excluded. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 930 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 931 | # "foo\nbarbaz\n" | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 932 | $dom->parse("<div>foo\n<p>bar</p>baz\n</div>")->at('div')->all_text; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 933 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 934 | =head2 ancestors | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 935 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 936 | my $collection = $dom->ancestors; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 937 | my $collection = $dom->ancestors('div ~ p'); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 938 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 939 | Find all ancestor elements of this node matching the CSS selector and return a | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 940 | L<collection|/"COLLECTION METHODS"> containing these elements as L<Mojo::DOM58> | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 941 | objects. All selectors listed in L</"SELECTORS"> are supported. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 942 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 943 | # List tag names of ancestor elements | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 944 | say $dom->ancestors->map('tag')->join("\n"); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 945 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 946 | =head2 append | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 947 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 948 | $dom = $dom->append('<p>I ♥ Mojo::DOM58!</p>'); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 949 | $dom = $dom->append(Mojo::DOM58->new); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 950 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 951 | Append HTML/XML fragment to this node (for all node types other than C<root>). | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 952 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 953 | # "<div><h1>Test</h1><h2>123</h2></div>" | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 954 | $dom->parse('<div><h1>Test</h1></div>') | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 955 | ->at('h1')->append('<h2>123</h2>')->root; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 956 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 957 | # "<p>Test 123</p>" | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 958 | $dom->parse('<p>Test</p>')->at('p') | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 959 | ->child_nodes->first->append(' 123')->root; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 960 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 961 | =head2 append_content | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 962 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 963 | $dom = $dom->append_content('<p>I ♥ Mojo::DOM58!</p>'); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 964 | $dom = $dom->append_content(Mojo::DOM58->new); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 965 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 966 | Append HTML/XML fragment (for C<root> and C<tag> nodes) or raw content to this | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 967 | node's content. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 968 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 969 | # "<div><h1>Test123</h1></div>" | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 970 | $dom->parse('<div><h1>Test</h1></div>') | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 971 | ->at('h1')->append_content('123')->root; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 972 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 973 | # "<!-- Test 123 --><br>" | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 974 | $dom->parse('<!-- Test --><br>') | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 975 | ->child_nodes->first->append_content('123 ')->root; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 976 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 977 | # "<p>Test<i>123</i></p>" | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 978 | $dom->parse('<p>Test</p>')->at('p')->append_content('<i>123</i>')->root; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 979 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 980 | =head2 at | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 981 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 982 | my $result = $dom->at('div ~ p'); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 983 | my $result = $dom->at('svg|line', svg => 'http://www.w3.org/2000/svg'); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 984 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 985 | Find first descendant element of this element matching the CSS selector and | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 986 | return it as a L<Mojo::DOM58> object, or C<undef> if none could be found. All | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 987 | selectors listed in L</"SELECTORS"> are supported. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 988 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 989 | # Find first element with "svg" namespace definition | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 990 | my $namespace = $dom->at('[xmlns\:svg]')->{'xmlns:svg'}; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 991 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 992 | Trailing key/value pairs can be used to declare xml namespace aliases. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 993 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 994 | # "<rect />" | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 995 | $dom->parse('<svg xmlns="http://www.w3.org/2000/svg"><rect /></svg>') | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 996 | ->at('svg|rect', svg => 'http://www.w3.org/2000/svg'); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 997 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 998 | =head2 attr | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 999 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1000 | my $hash = $dom->attr; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1001 | my $foo = $dom->attr('foo'); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1002 | $dom = $dom->attr({foo => 'bar'}); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1003 | $dom = $dom->attr(foo => 'bar'); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1004 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1005 | This element's attributes. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1006 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1007 | # Remove an attribute | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1008 | delete $dom->attr->{id}; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1009 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1010 | # Attribute without value | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1011 | $dom->attr(selected => undef); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1012 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1013 | # List id attributes | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1014 | say $dom->find('*')->map(attr => 'id')->compact->join("\n"); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1015 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1016 | =head2 child_nodes | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1017 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1018 | my $collection = $dom->child_nodes; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1019 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1020 | Return a L<collection|/"COLLECTION METHODS"> containing all child nodes of this | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1021 | element as L<Mojo::DOM58> objects. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1022 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1023 | # "<p><b>123</b></p>" | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1024 | $dom->parse('<p>Test<b>123</b></p>')->at('p')->child_nodes->first->remove; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1025 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1026 | # "<!DOCTYPE html>" | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1027 | $dom->parse('<!DOCTYPE html><b>123</b>')->child_nodes->first; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1028 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1029 | # " Test " | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1030 | $dom->parse('<b>123</b><!-- Test -->')->child_nodes->last->content; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1031 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1032 | =head2 children | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1033 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1034 | my $collection = $dom->children; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1035 | my $collection = $dom->children('div ~ p'); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1036 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1037 | Find all child elements of this element matching the CSS selector and return a | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1038 | L<collection|/"COLLECTION METHODS"> containing these elements as L<Mojo::DOM58> | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1039 | objects. All selectors listed in L</"SELECTORS"> are supported. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1040 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1041 | # Show tag name of random child element | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1042 | say $dom->children->shuffle->first->tag; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1043 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1044 | =head2 content | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1045 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1046 | my $str = $dom->content; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1047 | $dom = $dom->content('<p>I ♥ Mojo::DOM58!</p>'); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1048 | $dom = $dom->content(Mojo::DOM58->new); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1049 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1050 | Return this node's content or replace it with HTML/XML fragment (for C<root> | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1051 | and C<tag> nodes) or raw content. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1052 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1053 | # "<b>Test</b>" | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1054 | $dom->parse('<div><b>Test</b></div>')->at('div')->content; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1055 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1056 | # "<div><h1>123</h1></div>" | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1057 | $dom->parse('<div><h1>Test</h1></div>')->at('h1')->content('123')->root; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1058 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1059 | # "<p><i>123</i></p>" | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1060 | $dom->parse('<p>Test</p>')->at('p')->content('<i>123</i>')->root; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1061 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1062 | # "<div><h1></h1></div>" | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1063 | $dom->parse('<div><h1>Test</h1></div>')->at('h1')->content('')->root; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1064 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1065 | # " Test " | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1066 | $dom->parse('<!-- Test --><br>')->child_nodes->first->content; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1067 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1068 | # "<div><!-- 123 -->456</div>" | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1069 | $dom->parse('<div><!-- Test -->456</div>') | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1070 | ->at('div')->child_nodes->first->content(' 123 ')->root; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1071 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1072 | =head2 descendant_nodes | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1073 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1074 | my $collection = $dom->descendant_nodes; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1075 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1076 | Return a L<collection|/"COLLECTION METHODS"> containing all descendant nodes of | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1077 | this element as L<Mojo::DOM58> objects. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1078 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1079 | # "<p><b>123</b></p>" | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1080 | $dom->parse('<p><!-- Test --><b>123<!-- 456 --></b></p>') | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1081 | ->descendant_nodes->grep(sub { $_->type eq 'comment' }) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1082 | ->map('remove')->first; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1083 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1084 | # "<p><b>test</b>test</p>" | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1085 | $dom->parse('<p><b>123</b>456</p>') | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1086 | ->at('p')->descendant_nodes->grep(sub { $_->type eq 'text' }) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1087 | ->map(content => 'test')->first->root; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1088 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1089 | =head2 find | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1090 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1091 | my $collection = $dom->find('div ~ p'); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1092 | my $collection = $dom->find('svg|line', svg => 'http://www.w3.org/2000/svg'); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1093 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1094 | Find all descendant elements of this element matching the CSS selector and | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1095 | return a L<collection|/"COLLECTION METHODS"> containing these elements as | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1096 | L<Mojo::DOM58> objects. All selectors listed in L</"SELECTORS"> are supported. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1097 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1098 | # Find a specific element and extract information | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1099 | my $id = $dom->find('div')->[23]{id}; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1100 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1101 | # Extract information from multiple elements | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1102 | my @headers = $dom->find('h1, h2, h3')->map('text')->each; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1103 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1104 | # Count all the different tags | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1105 | my $hash = $dom->find('*')->reduce(sub { $a->{$b->tag}++; $a }, {}); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1106 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1107 | # Find elements with a class that contains dots | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1108 | my @divs = $dom->find('div.foo\.bar')->each; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1109 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1110 | Trailing key/value pairs can be used to declare xml namespace aliases. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1111 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1112 | # "<rect />" | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1113 | $dom->parse('<svg xmlns="http://www.w3.org/2000/svg"><rect /></svg>') | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1114 | ->find('svg|rect', svg => 'http://www.w3.org/2000/svg')->first; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1115 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1116 | =head2 following | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1117 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1118 | my $collection = $dom->following; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1119 | my $collection = $dom->following('div ~ p'); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1120 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1121 | Find all sibling elements after this node matching the CSS selector and return | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1122 | a L<collection|/"COLLECTION METHODS"> containing these elements as | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1123 | L<Mojo::DOM58> objects. All selectors listed in L</"SELECTORS"> are supported. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1124 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1125 | # List tags of sibling elements after this node | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1126 | say $dom->following->map('tag')->join("\n"); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1127 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1128 | =head2 following_nodes | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1129 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1130 | my $collection = $dom->following_nodes; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1131 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1132 | Return a L<collection|/"COLLECTION METHODS"> containing all sibling nodes after | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1133 | this node as L<Mojo::DOM58> objects. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1134 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1135 | # "C" | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1136 | $dom->parse('<p>A</p><!-- B -->C')->at('p')->following_nodes->last->content; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1137 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1138 | =head2 matches | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1139 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1140 | my $bool = $dom->matches('div ~ p'); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1141 | my $bool = $dom->matches('svg|line', svg => 'http://www.w3.org/2000/svg'); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1142 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1143 | Check if this element matches the CSS selector. All selectors listed in | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1144 | L</"SELECTORS"> are supported. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1145 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1146 | # True | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1147 | $dom->parse('<p class="a">A</p>')->at('p')->matches('.a'); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1148 | $dom->parse('<p class="a">A</p>')->at('p')->matches('p[class]'); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1149 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1150 | # False | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1151 | $dom->parse('<p class="a">A</p>')->at('p')->matches('.b'); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1152 | $dom->parse('<p class="a">A</p>')->at('p')->matches('p[id]'); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1153 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1154 | Trailing key/value pairs can be used to declare xml namespace aliases. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1155 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1156 | # True | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1157 | $dom->parse('<svg xmlns="http://www.w3.org/2000/svg"><rect /></svg>') | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1158 | ->matches('svg|rect', svg => 'http://www.w3.org/2000/svg'); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1159 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1160 | =head2 namespace | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1161 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1162 | my $namespace = $dom->namespace; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1163 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1164 | Find this element's namespace, or return C<undef> if none could be found. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1165 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1166 | # "http://www.w3.org/2000/svg" | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1167 | Mojo::DOM58->new('<svg xmlns:svg="http://www.w3.org/2000/svg"><svg:circle>3.14</svg:circle></svg>')->at('svg\:circle')->namespace; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1168 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1169 | # Find namespace for an element with namespace prefix | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1170 | my $namespace = $dom->at('svg > svg\:circle')->namespace; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1171 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1172 | # Find namespace for an element that may or may not have a namespace prefix | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1173 | my $namespace = $dom->at('svg > circle')->namespace; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1174 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1175 | =head2 next | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1176 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1177 | my $sibling = $dom->next; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1178 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1179 | Return L<Mojo::DOM58> object for next sibling element, or C<undef> if there are | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1180 | no more siblings. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1181 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1182 | # "<h2>123</h2>" | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1183 | $dom->parse('<div><h1>Test</h1><h2>123</h2></div>')->at('h1')->next; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1184 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1185 | =head2 next_node | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1186 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1187 | my $sibling = $dom->next_node; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1188 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1189 | Return L<Mojo::DOM58> object for next sibling node, or C<undef> if there are no | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1190 | more siblings. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1191 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1192 | # "456" | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1193 | $dom->parse('<p><b>123</b><!-- Test -->456</p>') | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1194 | ->at('b')->next_node->next_node; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1195 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1196 | # " Test " | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1197 | $dom->parse('<p><b>123</b><!-- Test -->456</p>') | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1198 | ->at('b')->next_node->content; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1199 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1200 | =head2 parent | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1201 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1202 | my $parent = $dom->parent; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1203 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1204 | Return L<Mojo::DOM58> object for parent of this node, or C<undef> if this node | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1205 | has no parent. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1206 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1207 | # "<b><i>Test</i></b>" | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1208 | $dom->parse('<p><b><i>Test</i></b></p>')->at('i')->parent; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1209 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1210 | =head2 parse | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1211 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1212 | $dom = $dom->parse('<foo bar="baz">I ♥ Mojo::DOM58!</foo>'); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1213 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1214 | Parse HTML/XML fragment. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1215 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1216 | # Parse XML | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1217 | my $dom = Mojo::DOM58->new->xml(1)->parse('<foo>I ♥ Mojo::DOM58!</foo>'); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1218 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1219 | =head2 preceding | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1220 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1221 | my $collection = $dom->preceding; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1222 | my $collection = $dom->preceding('div ~ p'); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1223 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1224 | Find all sibling elements before this node matching the CSS selector and return | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1225 | a L<collection|/"COLLECTION METHODS"> containing these elements as | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1226 | L<Mojo::DOM58> objects. All selectors listed in L</"SELECTORS"> are supported. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1227 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1228 | # List tags of sibling elements before this node | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1229 | say $dom->preceding->map('tag')->join("\n"); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1230 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1231 | =head2 preceding_nodes | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1232 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1233 | my $collection = $dom->preceding_nodes; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1234 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1235 | Return a L<collection|/"COLLECTION METHODS"> containing all sibling nodes | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1236 | before this node as L<Mojo::DOM58> objects. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1237 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1238 | # "A" | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1239 | $dom->parse('A<!-- B --><p>C</p>')->at('p')->preceding_nodes->first->content; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1240 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1241 | =head2 prepend | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1242 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1243 | $dom = $dom->prepend('<p>I ♥ Mojo::DOM58!</p>'); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1244 | $dom = $dom->prepend(Mojo::DOM58->new); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1245 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1246 | Prepend HTML/XML fragment to this node (for all node types other than C<root>). | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1247 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1248 | # "<div><h1>Test</h1><h2>123</h2></div>" | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1249 | $dom->parse('<div><h2>123</h2></div>') | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1250 | ->at('h2')->prepend('<h1>Test</h1>')->root; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1251 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1252 | # "<p>Test 123</p>" | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1253 | $dom->parse('<p>123</p>') | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1254 | ->at('p')->child_nodes->first->prepend('Test ')->root; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1255 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1256 | =head2 prepend_content | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1257 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1258 | $dom = $dom->prepend_content('<p>I ♥ Mojo::DOM58!</p>'); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1259 | $dom = $dom->prepend_content(Mojo::DOM58->new); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1260 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1261 | Prepend HTML/XML fragment (for C<root> and C<tag> nodes) or raw content to this | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1262 | node's content. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1263 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1264 | # "<div><h2>Test123</h2></div>" | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1265 | $dom->parse('<div><h2>123</h2></div>') | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1266 | ->at('h2')->prepend_content('Test')->root; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1267 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1268 | # "<!-- Test 123 --><br>" | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1269 | $dom->parse('<!-- 123 --><br>') | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1270 | ->child_nodes->first->prepend_content(' Test')->root; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1271 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1272 | # "<p><i>123</i>Test</p>" | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1273 | $dom->parse('<p>Test</p>')->at('p')->prepend_content('<i>123</i>')->root; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1274 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1275 | =head2 previous | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1276 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1277 | my $sibling = $dom->previous; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1278 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1279 | Return L<Mojo::DOM58> object for previous sibling element, or C<undef> if there | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1280 | are no more siblings. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1281 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1282 | # "<h1>Test</h1>" | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1283 | $dom->parse('<div><h1>Test</h1><h2>123</h2></div>')->at('h2')->previous; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1284 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1285 | =head2 previous_node | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1286 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1287 | my $sibling = $dom->previous_node; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1288 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1289 | Return L<Mojo::DOM58> object for previous sibling node, or C<undef> if there are | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1290 | no more siblings. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1291 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1292 | # "123" | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1293 | $dom->parse('<p>123<!-- Test --><b>456</b></p>') | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1294 | ->at('b')->previous_node->previous_node; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1295 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1296 | # " Test " | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1297 | $dom->parse('<p>123<!-- Test --><b>456</b></p>') | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1298 | ->at('b')->previous_node->content; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1299 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1300 | =head2 remove | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1301 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1302 | my $parent = $dom->remove; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1303 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1304 | Remove this node and return L</"root"> (for C<root> nodes) or L</"parent">. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1305 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1306 | # "<div></div>" | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1307 | $dom->parse('<div><h1>Test</h1></div>')->at('h1')->remove; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1308 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1309 | # "<p><b>456</b></p>" | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1310 | $dom->parse('<p>123<b>456</b></p>') | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1311 | ->at('p')->child_nodes->first->remove->root; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1312 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1313 | =head2 replace | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1314 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1315 | my $parent = $dom->replace('<div>I ♥ Mojo::DOM58!</div>'); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1316 | my $parent = $dom->replace(Mojo::DOM58->new); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1317 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1318 | Replace this node with HTML/XML fragment and return L</"root"> (for C<root> | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1319 | nodes) or L</"parent">. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1320 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1321 | # "<div><h2>123</h2></div>" | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1322 | $dom->parse('<div><h1>Test</h1></div>')->at('h1')->replace('<h2>123</h2>'); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1323 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1324 | # "<p><b>123</b></p>" | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1325 | $dom->parse('<p>Test</p>') | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1326 | ->at('p')->child_nodes->[0]->replace('<b>123</b>')->root; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1327 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1328 | =head2 root | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1329 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1330 | my $root = $dom->root; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1331 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1332 | Return L<Mojo::DOM58> object for C<root> node. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1333 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1334 | =head2 selector | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1335 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1336 | my $selector = $dom->selector; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1337 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1338 | Get a unique CSS selector for this element. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1339 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1340 | # "ul:nth-child(1) > li:nth-child(2)" | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1341 | $dom->parse('<ul><li>Test</li><li>123</li></ul>')->find('li')->last->selector; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1342 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1343 | # "p:nth-child(1) > b:nth-child(1) > i:nth-child(1)" | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1344 | $dom->parse('<p><b><i>Test</i></b></p>')->at('i')->selector; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1345 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1346 | =head2 strip | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1347 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1348 | my $parent = $dom->strip; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1349 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1350 | Remove this element while preserving its content and return L</"parent">. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1351 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1352 | # "<div>Test</div>" | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1353 | $dom->parse('<div><h1>Test</h1></div>')->at('h1')->strip; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1354 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1355 | =head2 tag | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1356 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1357 | my $tag = $dom->tag; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1358 | $dom = $dom->tag('div'); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1359 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1360 | This element's tag name. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1361 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1362 | # List tag names of child elements | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1363 | say $dom->children->map('tag')->join("\n"); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1364 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1365 | =head2 tap | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1366 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1367 | $dom = $dom->tap(sub {...}); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1368 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1369 | Equivalent to L<Mojo::Base/"tap">. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1370 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1371 | =head2 text | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1372 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1373 | my $text = $dom->text; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1374 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1375 | Extract text content from this element only (not including child elements). | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1376 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1377 | # "bar" | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1378 | $dom->parse("<div>foo<p>bar</p>baz</div>")->at('p')->text; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1379 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1380 | # "foo\nbaz\n" | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1381 | $dom->parse("<div>foo\n<p>bar</p>baz\n</div>")->at('div')->text; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1382 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1383 | =head2 to_string | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1384 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1385 | my $str = $dom->to_string; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1386 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1387 | Render this node and its content to HTML/XML. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1388 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1389 | # "<b>Test</b>" | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1390 | $dom->parse('<div><b>Test</b></div>')->at('div b')->to_string; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1391 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1392 | To extract text content from all descendant nodes, see L</"all_text">. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1393 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1394 | =head2 tree | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1395 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1396 | my $tree = $dom->tree; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1397 | $dom = $dom->tree(['root']); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1398 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1399 | Document Object Model. Note that this structure should only be used very | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1400 | carefully since it is very dynamic. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1401 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1402 | =head2 type | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1403 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1404 | my $type = $dom->type; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1405 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1406 | This node's type, usually C<cdata>, C<comment>, C<doctype>, C<pi>, C<raw>, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1407 | C<root>, C<tag> or C<text>. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1408 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1409 | # "cdata" | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1410 | $dom->parse('<![CDATA[Test]]>')->child_nodes->first->type; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1411 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1412 | # "comment" | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1413 | $dom->parse('<!-- Test -->')->child_nodes->first->type; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1414 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1415 | # "doctype" | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1416 | $dom->parse('<!DOCTYPE html>')->child_nodes->first->type; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1417 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1418 | # "pi" | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1419 | $dom->parse('<?xml version="1.0"?>')->child_nodes->first->type; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1420 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1421 | # "raw" | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1422 | $dom->parse('<title>Test</title>')->at('title')->child_nodes->first->type; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1423 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1424 | # "root" | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1425 | $dom->parse('<p>Test</p>')->type; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1426 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1427 | # "tag" | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1428 | $dom->parse('<p>Test</p>')->at('p')->type; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1429 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1430 | # "text" | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1431 | $dom->parse('<p>Test</p>')->at('p')->child_nodes->first->type; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1432 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1433 | =head2 val | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1434 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1435 | my $value = $dom->val; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1436 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1437 | Extract value from form element (such as C<button>, C<input>, C<option>, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1438 | C<select> and C<textarea>), or return C<undef> if this element has no value. In | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1439 | the case of C<select> with C<multiple> attribute, find C<option> elements with | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1440 | C<selected> attribute and return an array reference with all values, or | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1441 | C<undef> if none could be found. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1442 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1443 | # "a" | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1444 | $dom->parse('<input name=test value=a>')->at('input')->val; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1445 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1446 | # "b" | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1447 | $dom->parse('<textarea>b</textarea>')->at('textarea')->val; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1448 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1449 | # "c" | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1450 | $dom->parse('<option value="c">Test</option>')->at('option')->val; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1451 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1452 | # "d" | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1453 | $dom->parse('<select><option selected>d</option></select>') | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1454 | ->at('select')->val; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1455 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1456 | # "e" | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1457 | $dom->parse('<select multiple><option selected>e</option></select>') | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1458 | ->at('select')->val->[0]; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1459 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1460 | # "on" | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1461 | $dom->parse('<input name=test type=checkbox>')->at('input')->val; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1462 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1463 | =head2 with_roles | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1464 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1465 | my $new_class = Mojo::DOM58->with_roles('Mojo::DOM58::Role::One'); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1466 | my $new_class = Mojo::DOM58->with_roles('+One', '+Two'); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1467 | $dom = $dom->with_roles('+One', '+Two'); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1468 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1469 | Equivalent to L<Mojo::Base/"with_roles">. Note that role support depends on | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1470 | L<Role::Tiny> (2.000001+). | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1471 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1472 | =head2 wrap | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1473 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1474 | $dom = $dom->wrap('<div></div>'); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1475 | $dom = $dom->wrap(Mojo::DOM58->new); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1476 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1477 | Wrap HTML/XML fragment around this node (for all node types other than C<root>), | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1478 | placing it as the last child of the first innermost element. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1479 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1480 | # "<p>123<b>Test</b></p>" | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1481 | $dom->parse('<b>Test</b>')->at('b')->wrap('<p>123</p>')->root; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1482 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1483 | # "<div><p><b>Test</b></p>123</div>" | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1484 | $dom->parse('<b>Test</b>')->at('b')->wrap('<div><p></p>123</div>')->root; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1485 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1486 | # "<p><b>Test</b></p><p>123</p>" | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1487 | $dom->parse('<b>Test</b>')->at('b')->wrap('<p></p><p>123</p>')->root; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1488 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1489 | # "<p><b>Test</b></p>" | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1490 | $dom->parse('<p>Test</p>')->at('p')->child_nodes->first->wrap('<b>')->root; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1491 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1492 | =head2 wrap_content | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1493 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1494 | $dom = $dom->wrap_content('<div></div>'); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1495 | $dom = $dom->wrap_content(Mojo::DOM58->new); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1496 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1497 | Wrap HTML/XML fragment around this node's content (for C<root> and C<tag> | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1498 | nodes), placing it as the last children of the first innermost element. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1499 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1500 | # "<p><b>123Test</b></p>" | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1501 | $dom->parse('<p>Test<p>')->at('p')->wrap_content('<b>123</b>')->root; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1502 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1503 | # "<p><b>Test</b></p><p>123</p>" | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1504 | $dom->parse('<b>Test</b>')->wrap_content('<p></p><p>123</p>'); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1505 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1506 | =head2 xml | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1507 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1508 | my $bool = $dom->xml; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1509 | $dom = $dom->xml($bool); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1510 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1511 | Disable HTML semantics in parser and activate case-sensitivity, defaults to | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1512 | auto detection based on XML declarations. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1513 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1514 | =head1 COLLECTION METHODS | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1515 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1516 | Some L<Mojo::DOM58> methods return an array-based collection object based on | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1517 | L<Mojo::Collection>, which can either be accessed directly as an array | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1518 | reference, or with the following methods. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1519 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1520 | # Chain methods | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1521 | $collection->map(sub { ucfirst })->shuffle->each(sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1522 | my ($word, $num) = @_; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1523 | say "$num: $word"; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1524 | }); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1525 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1526 | # Access array directly to manipulate collection | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1527 | $collection->[23] += 100; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1528 | say for @$collection; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1529 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1530 | =head2 compact | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1531 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1532 | my $new = $collection->compact; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1533 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1534 | Create a new L<collection|/"COLLECTION METHODS"> with all elements that are | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1535 | defined and not an empty string. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1536 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1537 | # $collection contains (0, 1, undef, 2, '', 3) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1538 | $collection->compact->join(', '); # "0, 1, 2, 3" | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1539 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1540 | =head2 each | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1541 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1542 | my @elements = $collection->each; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1543 | $collection = $collection->each(sub {...}); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1544 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1545 | Evaluate callback for each element in collection or return all elements as a | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1546 | list if none has been provided. The element will be the first argument passed | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1547 | to the callback and is also available as C<$_>. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1548 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1549 | # Make a numbered list | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1550 | $collection->each(sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1551 | my ($e, $num) = @_; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1552 | say "$num: $e"; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1553 | }); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1554 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1555 | =head2 first | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1556 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1557 | my $first = $collection->first; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1558 | my $first = $collection->first(qr/foo/); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1559 | my $first = $collection->first(sub {...}); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1560 | my $first = $collection->first($method); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1561 | my $first = $collection->first($method, @args); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1562 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1563 | Evaluate regular expression/callback for, or call method on, each element in | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1564 | collection and return the first one that matched the regular expression, or for | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1565 | which the callback/method returned true. The element will be the first argument | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1566 | passed to the callback and is also available as C<$_>. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1567 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1568 | # Longer version | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1569 | my $first = $collection->first(sub { $_->$method(@args) }); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1570 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1571 | # Find first value that contains the word "mojo" | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1572 | my $interesting = $collection->first(qr/mojo/i); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1573 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1574 | # Find first value that is greater than 5 | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1575 | my $greater = $collection->first(sub { $_ > 5 }); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1576 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1577 | =head2 flatten | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1578 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1579 | my $new = $collection->flatten; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1580 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1581 | Flatten nested collections/arrays recursively and create a new | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1582 | L<collection|/"COLLECTION METHODS"> with all elements. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1583 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1584 | # $collection contains (1, [2, [3, 4], 5, [6]], 7) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1585 | $collection->flatten->join(', '); # "1, 2, 3, 4, 5, 6, 7" | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1586 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1587 | =head2 grep | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1588 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1589 | my $new = $collection->grep(qr/foo/); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1590 | my $new = $collection->grep(sub {...}); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1591 | my $new = $collection->grep($method); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1592 | my $new = $collection->grep($method, @args); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1593 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1594 | Evaluate regular expression/callback for, or call method on, each element in | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1595 | collection and create a new L<collection|/"COLLECTION METHODS"> with all | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1596 | elements that matched the regular expression, or for which the callback/method | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1597 | returned true. The element will be the first argument passed to the callback | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1598 | and is also available as C<$_>. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1599 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1600 | # Longer version | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1601 | my $new = $collection->grep(sub { $_->$method(@args) }); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1602 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1603 | # Find all values that contain the word "mojo" | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1604 | my $interesting = $collection->grep(qr/mojo/i); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1605 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1606 | # Find all values that are greater than 5 | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1607 | my $greater = $collection->grep(sub { $_ > 5 }); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1608 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1609 | =head2 head | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1610 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1611 | my $new = $collection->head(4); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1612 | my $new = $collection->head(-2); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1613 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1614 | Create a new L<collection|/"COLLECTION METHODS"> with up to the specified | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1615 | number of elements from the beginning of the collection. A negative number will | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1616 | count from the end. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1617 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1618 | # $collection contains ('A', 'B', 'C', 'D', 'E') | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1619 | $collection->head(3)->join(' '); # "A B C" | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1620 | $collection->head(-3)->join(' '); # "A B" | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1621 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1622 | =head2 join | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1623 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1624 | my $stream = $collection->join; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1625 | my $stream = $collection->join("\n"); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1626 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1627 | Turn collection into string. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1628 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1629 | # Join all values with commas | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1630 | $collection->join(', '); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1631 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1632 | =head2 last | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1633 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1634 | my $last = $collection->last; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1635 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1636 | Return the last element in collection. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1637 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1638 | =head2 map | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1639 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1640 | my $new = $collection->map(sub {...}); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1641 | my $new = $collection->map($method); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1642 | my $new = $collection->map($method, @args); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1643 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1644 | Evaluate callback for, or call method on, each element in collection and create | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1645 | a new L<collection|/"COLLECTION METHODS"> from the results. The element will be | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1646 | the first argument passed to the callback and is also available as C<$_>. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1647 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1648 | # Longer version | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1649 | my $new = $collection->map(sub { $_->$method(@args) }); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1650 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1651 | # Append the word "mojo" to all values | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1652 | my $domified = $collection->map(sub { $_ . 'mojo' }); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1653 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1654 | =head2 reduce | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1655 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1656 | my $result = $collection->reduce(sub {...}); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1657 | my $result = $collection->reduce(sub {...}, $initial); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1658 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1659 | Reduce elements in collection with callback, the first element will be used as | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1660 | initial value if none has been provided. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1661 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1662 | # Calculate the sum of all values | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1663 | my $sum = $collection->reduce(sub { $a + $b }); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1664 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1665 | # Count how often each value occurs in collection | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1666 | my $hash = $collection->reduce(sub { $a->{$b}++; $a }, {}); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1667 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1668 | =head2 reverse | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1669 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1670 | my $new = $collection->reverse; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1671 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1672 | Create a new L<collection|/"COLLECTION METHODS"> with all elements in reverse | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1673 | order. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1674 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1675 | =head2 slice | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1676 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1677 | my $new = $collection->slice(4 .. 7); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1678 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1679 | Create a new L<collection|/"COLLECTION METHODS"> with all selected elements. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1680 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1681 | # $collection contains ('A', 'B', 'C', 'D', 'E') | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1682 | $collection->slice(1, 2, 4)->join(' '); # "B C E" | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1683 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1684 | =head2 shuffle | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1685 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1686 | my $new = $collection->shuffle; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1687 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1688 | Create a new L<collection|/"COLLECTION METHODS"> with all elements in random | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1689 | order. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1690 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1691 | =head2 size | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1692 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1693 | my $size = $collection->size; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1694 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1695 | Number of elements in collection. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1696 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1697 | =head2 sort | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1698 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1699 | my $new = $collection->sort; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1700 | my $new = $collection->sort(sub {...}); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1701 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1702 | Sort elements based on return value of callback and create a new | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1703 | L<collection|/"COLLECTION METHODS"> from the results. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1704 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1705 | # Sort values case-insensitive | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1706 | my $case_insensitive = $collection->sort(sub { uc($a) cmp uc($b) }); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1707 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1708 | =head2 tail | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1709 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1710 | my $new = $collection->tail(4); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1711 | my $new = $collection->tail(-2); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1712 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1713 | Create a new L<collection|/"COLLECTION METHODS"> with up to the specified | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1714 | number of elements from the end of the collection. A negative number will count | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1715 | from the beginning. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1716 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1717 | # $collection contains ('A', 'B', 'C', 'D', 'E') | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1718 | $collection->tail(3)->join(' '); # "C D E" | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1719 | $collection->tail(-3)->join(' '); # "D E" | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1720 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1721 | =head2 tap | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1722 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1723 | $collection = $collection->tap(sub {...}); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1724 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1725 | Equivalent to L<Mojo::Base/"tap">. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1726 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1727 | =head2 to_array | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1728 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1729 | my $array = $collection->to_array; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1730 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1731 | Turn collection into array reference. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1732 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1733 | =head2 uniq | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1734 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1735 | my $new = $collection->uniq; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1736 | my $new = $collection->uniq(sub {...}); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1737 | my $new = $collection->uniq($method); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1738 | my $new = $collection->uniq($method, @args); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1739 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1740 | Create a new L<collection|/"COLLECTION METHODS"> without duplicate elements, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1741 | using the string representation of either the elements or the return value of | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1742 | the callback/method to decide uniqueness. Note that C<undef> and empty string | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1743 | are treated the same. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1744 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1745 | # Longer version | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1746 | my $new = $collection->uniq(sub { $_->$method(@args) }); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1747 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1748 | # $collection contains ('foo', 'bar', 'bar', 'baz') | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1749 | $collection->uniq->join(' '); # "foo bar baz" | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1750 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1751 | # $collection contains ([1, 2], [2, 1], [3, 2]) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1752 | $collection->uniq(sub{ $_->[1] })->to_array; # "[[1, 2], [2, 1]]" | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1753 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1754 | =head2 with_roles | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1755 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1756 | $collection = $collection->with_roles('Mojo::Collection::Role::One'); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1757 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1758 | Equivalent to L<Mojo::Base/"with_roles">. Note that role support depends on | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1759 | L<Role::Tiny> (2.000001+). | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1760 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1761 | =head1 DEBUGGING | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1762 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1763 | You can set the C<MOJO_DOM58_CSS_DEBUG> environment variable to get some advanced diagnostics information printed to | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1764 | C<STDERR>. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1765 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1766 | MOJO_DOM58_CSS_DEBUG=1 | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1767 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1768 | =head1 BUGS | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1769 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1770 | Report issues related to the format of this distribution or Perl 5.8 support to | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1771 | the public bugtracker. Any other issues should be reported directly to the | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1772 | upstream L<Mojolicious> issue tracker. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1773 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1774 | =head1 AUTHOR | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1775 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1776 | Dan Book <book.d.wit@proton.me> | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1777 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1778 | Code and tests adapted from L<Mojo::DOM>, a lightweight DOM parser by the L<Mojolicious> team. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1779 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1780 | =head1 CONTRIBUTORS | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1781 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1782 | =over | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1783 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1784 | =item Matt S Trout (mst) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1785 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1786 | =back | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1787 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1788 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1789 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1790 | Copyright (c) 2008-2016 Sebastian Riedel and others. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1791 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1792 | Copyright (c) 2016 L</"AUTHOR"> and L</"CONTRIBUTORS"> for adaptation to standalone format. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1793 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1794 | This is free software, licensed under: | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1795 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1796 | The Artistic License 2.0 (GPL Compatible) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1797 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1798 | =head1 SEE ALSO | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1799 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1800 | L<Mojo::DOM>, L<HTML::TreeBuilder>, L<XML::LibXML>, L<XML::Twig>, L<XML::Smart> | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1801 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1802 | =for Pod::Coverage TO_JSON | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 1803 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 1804 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||