line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Mojo::DOM::Role::Restrict; |
2
|
9
|
|
|
9
|
|
61632
|
use strict; use warnings; our $VERSION = 0.05; |
|
9
|
|
|
9
|
|
22
|
|
|
9
|
|
|
|
|
231
|
|
|
9
|
|
|
|
|
43
|
|
|
9
|
|
|
|
|
14
|
|
|
9
|
|
|
|
|
357
|
|
3
|
9
|
|
|
9
|
|
483
|
use Mojo::Base -role; |
|
9
|
|
|
|
|
166419
|
|
|
9
|
|
|
|
|
64
|
|
4
|
9
|
|
|
9
|
|
4000
|
use Mojo::Util qw(xml_escape); |
|
9
|
|
|
|
|
14
|
|
|
9
|
|
|
|
|
412
|
|
5
|
9
|
|
|
9
|
|
45
|
use File::Spec; |
|
9
|
|
|
|
|
12
|
|
|
9
|
|
|
|
|
7585
|
|
6
|
|
|
|
|
|
|
|
7
|
26
|
100
|
|
26
|
0
|
9438
|
sub to_string { $_[1] ? ${$_[0]}->render : $_[0]->render; } |
|
11
|
|
|
|
|
44
|
|
8
|
|
|
|
|
|
|
|
9
|
15
|
|
|
15
|
0
|
65
|
sub render { _render($_[0]->tree, $_[0]->xml, $_[0]->restrict_spec) } |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
around parse => sub { |
12
|
|
|
|
|
|
|
my ($orig, $self) = (shift, shift); |
13
|
|
|
|
|
|
|
$self->restrict_spec($_[1] || $self->restrict_spec || { |
14
|
|
|
|
|
|
|
'*' => { '*' => 1 } |
15
|
|
|
|
|
|
|
}); |
16
|
|
|
|
|
|
|
return $self->$orig(@_); |
17
|
|
|
|
|
|
|
}; |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
sub restrict_spec { |
20
|
70
|
100
|
|
70
|
1
|
501
|
if ( $_[1] ) { |
21
|
33
|
|
100
|
|
|
48
|
$_[1]->{$_} && ! ref $_[1]->{$_} && do { $_[1]->{$_} = { '*' => 1 } } for (keys %{$_[1]}); |
|
33
|
|
66
|
|
|
296
|
|
|
24
|
|
|
|
|
79
|
|
22
|
33
|
|
|
|
|
57
|
${$_[0]}->{restrict_spec} = $_[1]; |
|
33
|
|
|
|
|
516
|
|
23
|
|
|
|
|
|
|
} |
24
|
70
|
|
|
|
|
254
|
${$_[0]}->{restrict_spec}; |
|
70
|
|
|
|
|
176
|
|
25
|
|
|
|
|
|
|
} |
26
|
|
|
|
|
|
|
|
27
|
9
|
|
|
9
|
1
|
3964
|
sub valid { _valid($_[0]->tree, $_[0]->restrict_spec($_[1])) } |
28
|
|
|
|
|
|
|
|
29
|
12
|
50
|
|
12
|
1
|
8136
|
sub restrict { _restrict($_[0]->tree, $_[0]->restrict_spec($_[1])) && $_[0] } |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
sub diff_module { |
32
|
0
|
0
|
0
|
0
|
1
|
0
|
if ( $_[1] && $_[0]->diff_module_name !~ $_[1]) { |
33
|
0
|
|
|
|
|
0
|
$_[0]->diff_module_name($_[1]); |
34
|
0
|
|
|
|
|
0
|
$_[0]->diff_module_loaded(0); |
35
|
|
|
|
|
|
|
} |
36
|
0
|
0
|
|
|
|
0
|
$_[0]->diff_module_method($_[2]) if $_[2]; |
37
|
0
|
0
|
|
|
|
0
|
$_[0]->diff_module_params($_[3]) if defined $_[3]; |
38
|
|
|
|
|
|
|
return ( |
39
|
0
|
|
|
|
|
0
|
$_[0]->diff_module_name, |
40
|
|
|
|
|
|
|
$_[0]->diff_module_method, |
41
|
|
|
|
|
|
|
$_[0]->diff_module_params |
42
|
|
|
|
|
|
|
); |
43
|
|
|
|
|
|
|
} |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
has diff_module_name => 'Text::Diff'; |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
has diff_module_loaded => 0; |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
has diff_module_method => 'diff'; |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
has diff_module_params => sub { { style => 'Unified' } }; |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
sub diff { |
54
|
0
|
|
|
0
|
1
|
0
|
my ($self, $spec) = ($_[0], (shift)->restrict_spec(shift)); |
55
|
0
|
|
|
|
|
0
|
my ($module, $method, $params) = $self->diff_module(@_); |
56
|
0
|
0
|
|
|
|
0
|
unless ( $self->diff_module_loaded ) { |
57
|
0
|
|
|
|
|
0
|
my @parts = split /::|'/, $module, -1; |
58
|
0
|
0
|
0
|
|
|
0
|
shift @parts if @parts && !$parts[0]; |
59
|
0
|
|
|
|
|
0
|
my $file = File::Spec->catfile( @parts ); |
60
|
|
|
|
|
|
|
LOAD_DIFF_MODULE: { |
61
|
0
|
|
|
|
|
0
|
my $err; |
|
0
|
|
|
|
|
0
|
|
62
|
0
|
|
|
|
|
0
|
for my $flag ( qw[1 0] ) { |
63
|
0
|
0
|
|
|
|
0
|
my $load = $file . ($flag ? '.pm' : ''); |
64
|
0
|
|
|
|
|
0
|
eval { require $load }; |
|
0
|
|
|
|
|
0
|
|
65
|
0
|
0
|
|
|
|
0
|
$@ ? $err .= $@ : last LOAD_DIFF_MODULE; |
66
|
|
|
|
|
|
|
} |
67
|
0
|
0
|
|
|
|
0
|
die $err if $err; |
68
|
|
|
|
|
|
|
} |
69
|
0
|
|
|
|
|
0
|
$self->diff_module_loaded(1) |
70
|
|
|
|
|
|
|
} |
71
|
|
|
|
|
|
|
{ |
72
|
9
|
|
|
9
|
|
71
|
no strict 'refs'; |
|
9
|
|
|
|
|
16
|
|
|
9
|
|
|
|
|
3101
|
|
|
0
|
|
|
|
|
0
|
|
73
|
0
|
|
|
|
|
0
|
return *{"${module}::${method}"}->(\$self->to_string(1), \$self->to_string(), $params); |
|
0
|
|
|
|
|
0
|
|
74
|
|
|
|
|
|
|
} |
75
|
|
|
|
|
|
|
} |
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
# copy, paste and edit via Mojo::DOM::HTML::_render |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
my %EMPTY = map { $_ => 1 } qw(area base br col embed hr img input keygen link menuitem meta param source track wbr); |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
sub _render { |
82
|
143
|
|
|
143
|
|
221
|
my ($tree, $xml, $spec) = @_; |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
# Tag |
85
|
143
|
|
|
|
|
192
|
my $type = $tree->[0]; |
86
|
143
|
100
|
|
|
|
240
|
if ($type eq 'tag') { |
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
# Start tag |
89
|
90
|
|
|
|
|
111
|
my ($tag, $attrs) = _valid_tag($spec, $tree->[1], {%{$tree->[2]}}); |
|
90
|
|
|
|
|
234
|
|
90
|
|
|
|
|
|
|
|
91
|
90
|
100
|
|
|
|
288
|
return '' unless $tag; |
92
|
|
|
|
|
|
|
|
93
|
76
|
|
|
|
|
129
|
my $result = "<$tag"; |
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
# Attributes |
96
|
76
|
|
|
|
|
88
|
for (sort keys %{$attrs}) { |
|
76
|
|
|
|
|
198
|
|
97
|
78
|
|
|
|
|
425
|
my ($key, $value) = _valid_attribute($spec, $tag, $_, $attrs->{$_}); |
98
|
78
|
0
|
|
|
|
360
|
$result .= defined $value |
|
|
50
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
99
|
|
|
|
|
|
|
? qq{ $key="} . xml_escape($value) . '"' |
100
|
|
|
|
|
|
|
: $xml |
101
|
|
|
|
|
|
|
? qq{ $key="$key"} |
102
|
|
|
|
|
|
|
: " $key" |
103
|
|
|
|
|
|
|
if $key; |
104
|
|
|
|
|
|
|
} |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
# No children |
107
|
76
|
0
|
|
|
|
204
|
return $xml ? "$result />" : $EMPTY{$tag} ? "$result>" : "$result>$tag>" unless $tree->[4]; |
|
|
0
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
# Children |
110
|
9
|
|
|
9
|
|
62
|
no warnings 'recursion'; |
|
9
|
|
|
|
|
18
|
|
|
9
|
|
|
|
|
8129
|
|
111
|
76
|
|
|
|
|
146
|
$result .= '>' . join '', map { _render($_, $xml, $spec) } @$tree[4 .. $#$tree]; |
|
113
|
|
|
|
|
375
|
|
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
# End tag |
114
|
76
|
|
|
|
|
490
|
return "$result$tag>"; |
115
|
|
|
|
|
|
|
} |
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
# Text (escaped) |
118
|
53
|
100
|
|
|
|
143
|
return xml_escape $tree->[1] if $type eq 'text'; |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
# Raw text |
121
|
17
|
100
|
|
|
|
39
|
return $tree->[1] if $type eq 'raw'; |
122
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
# Root |
124
|
15
|
50
|
|
|
|
55
|
return join '', map { _render($_, $xml, $spec) } @$tree[1 .. $#$tree] if $type eq 'root'; |
|
15
|
|
|
|
|
73
|
|
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
# DOCTYPE |
127
|
0
|
0
|
|
|
|
0
|
return '[1] . '>' if $type eq 'doctype'; |
128
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
# Comment |
130
|
0
|
0
|
|
|
|
0
|
return '' if $type eq 'comment'; |
131
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
# CDATA |
133
|
0
|
0
|
|
|
|
0
|
return '[1] . ']]>' if $type eq 'cdata'; |
134
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
# Processing instruction |
136
|
0
|
0
|
|
|
|
0
|
return '' . $tree->[1] . '?>' if $type eq 'pi'; |
137
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
# Everything else |
139
|
0
|
|
|
|
|
0
|
return ''; |
140
|
|
|
|
|
|
|
} |
141
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
sub _valid_tag { |
143
|
204
|
|
|
204
|
|
326
|
my ($spec, $tag, $attrs) = @_; |
144
|
204
|
|
100
|
|
|
498
|
my $valid = $spec->{$tag} // $spec->{'*'}; |
145
|
|
|
|
|
|
|
return ref $valid && $valid->{validate_tag} |
146
|
204
|
100
|
100
|
|
|
778
|
? $valid->{validate_tag}($tag, $attrs) |
|
|
100
|
|
|
|
|
|
147
|
|
|
|
|
|
|
: $valid |
148
|
|
|
|
|
|
|
? ($tag, $attrs) |
149
|
|
|
|
|
|
|
: 0; |
150
|
|
|
|
|
|
|
} |
151
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
sub _valid_attribute { |
153
|
154
|
|
|
154
|
|
257
|
my ($spec, $tag, $attr, $value) = @_; |
154
|
154
|
|
100
|
|
|
676
|
my $valid = $spec->{$tag}->{$attr} // $spec->{$tag}->{'*'} // $spec->{'*'}->{$attr} // $spec->{'*'}->{'*'}; |
|
|
|
100
|
|
|
|
|
|
|
|
100
|
|
|
|
|
155
|
154
|
100
|
100
|
|
|
763
|
return ref $valid |
|
|
100
|
|
|
|
|
|
156
|
|
|
|
|
|
|
? $valid->($attr, $value) |
157
|
|
|
|
|
|
|
: ($valid and $valid =~ m/1/ || $value =~ m/$valid/) |
158
|
|
|
|
|
|
|
? ( $attr, $value ) |
159
|
|
|
|
|
|
|
: 0; |
160
|
|
|
|
|
|
|
} |
161
|
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
sub _valid { |
163
|
58
|
|
|
58
|
|
78
|
my ($tree, $spec) = @_; |
164
|
58
|
100
|
|
|
|
107
|
if ($tree->[0] eq 'tag') { |
|
|
100
|
|
|
|
|
|
165
|
38
|
|
|
|
|
49
|
my ($tag, $attrs) = _valid_tag($spec, $tree->[1], {%{$tree->[2]}}); |
|
38
|
|
|
|
|
81
|
|
166
|
38
|
100
|
|
|
|
81
|
return 0 unless $tag; |
167
|
|
|
|
|
|
|
_valid_attribute($spec, $tag, $_, $attrs->{$_}) or return 0 |
168
|
34
|
|
100
|
|
|
37
|
for (sort keys %{$attrs}); |
|
34
|
|
|
|
|
77
|
|
169
|
32
|
50
|
|
|
|
57
|
if ($tree->[4]) { |
170
|
32
|
|
100
|
|
|
94
|
_valid($_, $spec) or return 0 for ( @$tree[4 .. $#$tree] ); |
171
|
|
|
|
|
|
|
} |
172
|
|
|
|
|
|
|
} elsif ($tree->[0] eq 'root') { |
173
|
9
|
|
100
|
|
|
34
|
_valid($_, $spec) or return 0 for ( @$tree[1 .. $#$tree] ); |
174
|
|
|
|
|
|
|
} |
175
|
36
|
|
|
|
|
81
|
return 1; |
176
|
|
|
|
|
|
|
} |
177
|
|
|
|
|
|
|
|
178
|
|
|
|
|
|
|
sub _restrict { |
179
|
120
|
|
|
120
|
|
182
|
my ($tree, $spec) = @_; |
180
|
120
|
100
|
|
|
|
218
|
if ($tree->[0] eq 'tag') { |
|
|
100
|
|
|
|
|
|
181
|
76
|
|
|
|
|
116
|
my ($tag, $attrs) = _valid_tag($spec, $tree->[1], $tree->[2]); |
182
|
76
|
100
|
|
|
|
185
|
return 0 unless $tag; |
183
|
64
|
|
|
|
|
86
|
$tree->[1] = $tag; |
184
|
64
|
|
|
|
|
79
|
for (sort keys %{$attrs}) { |
|
64
|
|
|
|
|
180
|
|
185
|
64
|
|
|
|
|
124
|
my ($key, $value) = _valid_attribute($spec, $tag, $_, delete $attrs->{$_}); |
186
|
64
|
100
|
|
|
|
247
|
$attrs->{$key} = $value if $key; |
187
|
|
|
|
|
|
|
} |
188
|
64
|
50
|
|
|
|
115
|
if ($tree->[4]) { |
189
|
64
|
|
|
|
|
86
|
my $i = 4; |
190
|
12
|
|
|
|
|
50
|
_restrict($_, $spec) ? $i++ : splice(@{$tree}, $i, 1) |
191
|
64
|
100
|
|
|
|
175
|
for ( @$tree[$i .. $#$tree] ); |
192
|
|
|
|
|
|
|
} |
193
|
|
|
|
|
|
|
} elsif ($tree->[0] eq 'root') { |
194
|
12
|
|
|
|
|
20
|
my $i = 1; |
195
|
0
|
|
|
|
|
0
|
_restrict($_, $spec) ? $i++ : splice(@{$tree}, $i, 1) |
196
|
12
|
50
|
|
|
|
54
|
for ( @$tree[$i .. $#$tree] ); |
197
|
|
|
|
|
|
|
} |
198
|
108
|
|
|
|
|
221
|
return 1; |
199
|
|
|
|
|
|
|
} |
200
|
|
|
|
|
|
|
|
201
|
|
|
|
|
|
|
|
202
|
|
|
|
|
|
|
1; |
203
|
|
|
|
|
|
|
|
204
|
|
|
|
|
|
|
# TODO pretty print (for diff) and minmize. |
205
|
|
|
|
|
|
|
|
206
|
|
|
|
|
|
|
__END__ |