line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package HTML::Linear::Element; |
2
|
|
|
|
|
|
|
# ABSTRACT: represent elements to populate HTML::Linear |
3
|
64
|
|
|
64
|
|
416
|
use strict; |
|
64
|
|
|
|
|
129
|
|
|
64
|
|
|
|
|
6511
|
|
4
|
64
|
|
|
64
|
|
532
|
use utf8; |
|
64
|
|
|
|
|
288
|
|
|
64
|
|
|
|
|
740
|
|
5
|
64
|
|
|
64
|
|
2556
|
use warnings qw(all); |
|
64
|
|
|
|
|
225
|
|
|
64
|
|
|
|
|
5428
|
|
6
|
|
|
|
|
|
|
|
7
|
64
|
|
|
64
|
|
406
|
use Digest::SHA; |
|
64
|
|
|
|
|
129
|
|
|
64
|
|
|
|
|
2720
|
|
8
|
64
|
|
|
64
|
|
3613
|
use Encode; |
|
64
|
|
|
|
|
34905
|
|
|
64
|
|
|
|
|
7069
|
|
9
|
64
|
|
|
64
|
|
389
|
use List::Util qw(sum); |
|
64
|
|
|
|
|
158
|
|
|
64
|
|
|
|
|
4922
|
|
10
|
64
|
|
|
64
|
|
725
|
use Moo; |
|
64
|
|
|
|
|
131
|
|
|
64
|
|
|
|
|
642
|
|
11
|
64
|
|
|
64
|
|
24411
|
use MooX::Types::MooseLike::Base qw(:all); |
|
64
|
|
|
|
|
124
|
|
|
64
|
|
|
|
|
34014
|
|
12
|
|
|
|
|
|
|
|
13
|
64
|
|
|
64
|
|
43625
|
use HTML::Linear::Path; |
|
64
|
|
|
|
|
225
|
|
|
64
|
|
|
|
|
38453
|
|
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
## no critic (ProtectPrivateSubs) |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
our $VERSION = '0.019'; # VERSION |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
has attributes => (is => 'rw', isa => HashRef[Str], default => sub { {} }); |
21
|
|
|
|
|
|
|
has content => (is => 'rw', isa => Str, default => sub { '' }); |
22
|
|
|
|
|
|
|
has depth => (is => 'ro', isa => Int, required => 1); |
23
|
|
|
|
|
|
|
has index => (is => 'rw', isa => Int, default => sub { 0 }); |
24
|
|
|
|
|
|
|
has index_map => (is => 'rw', isa => HashRef[Str], default => sub { {} }); |
25
|
|
|
|
|
|
|
has key => (is => 'rw', isa => Str, default => sub { '' }); |
26
|
|
|
|
|
|
|
has path => (is => 'ro', isa => ArrayRef[InstanceOf('HTML::Linear::Path')], required => 1); |
27
|
|
|
|
|
|
|
has sha => (is => 'ro', isa => InstanceOf('Digest::SHA'), default => sub { Digest::SHA->new(256) }, lazy => 1 ); |
28
|
|
|
|
|
|
|
has strict => (is => 'ro', isa => Bool, default => sub { 0 }); |
29
|
|
|
|
|
|
|
has trim_at => (is => 'rw', isa => Int, default => sub { 0 }); |
30
|
|
|
|
|
|
|
|
31
|
64
|
|
|
64
|
|
807
|
use overload '""' => \&as_string, fallback => 1; |
|
64
|
|
|
|
|
326
|
|
|
64
|
|
|
|
|
933
|
|
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
sub BUILD { |
35
|
6296
|
|
|
6296
|
0
|
297475
|
my ($self) = @_; |
36
|
6296
|
|
|
|
|
8394
|
$self->attributes({%{$self->path->[-1]->attributes}}); |
|
6296
|
|
|
|
|
212080
|
|
37
|
6296
|
|
|
|
|
702629
|
return; |
38
|
|
|
|
|
|
|
} |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
sub as_string { |
42
|
38248
|
|
|
38248
|
1
|
1855269
|
my ($self) = @_; |
43
|
38248
|
100
|
|
|
|
1028714
|
return $self->key if $self->key; |
44
|
|
|
|
|
|
|
|
45
|
4781
|
|
|
|
|
172815
|
my $content = $self->content; |
46
|
4781
|
|
|
|
|
58717
|
Encode::_utf8_off($content); |
47
|
4781
|
|
|
|
|
132025
|
$self->sha->add($content); |
48
|
|
|
|
|
|
|
|
49
|
4781
|
|
|
|
|
559086
|
$self->sha->add($self->index); |
50
|
4781
|
|
|
|
|
268213
|
$self->sha->add(join ',', $self->path); |
51
|
|
|
|
|
|
|
|
52
|
4781
|
|
|
|
|
152295
|
return $self->key($self->sha->b64digest); |
53
|
|
|
|
|
|
|
} |
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
sub as_xpath { |
57
|
28386
|
|
|
28386
|
1
|
39462
|
my ($self) = @_; |
58
|
202824
|
|
100
|
|
|
6371202
|
my @xpath = map { |
59
|
28386
|
|
|
|
|
74237
|
$_->as_xpath . ($self->index_map->{$_->address} // '') |
60
|
28386
|
|
|
|
|
687238
|
} @{$self->path} [$self->trim_at .. $#{$self->path}]; |
|
28386
|
|
|
|
|
270152
|
|
61
|
28386
|
100
|
|
|
|
1667555
|
$self->trim_at and unshift @xpath, HTML::Linear::Path::_wrap(separator => '/'); |
62
|
|
|
|
|
|
|
return wantarray |
63
|
|
|
|
|
|
|
? @xpath |
64
|
28386
|
100
|
|
|
|
478697
|
: join '', @xpath; |
65
|
|
|
|
|
|
|
} |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
sub as_hash { |
69
|
6296
|
|
|
6296
|
1
|
44912
|
my ($self) = @_; |
70
|
6296
|
|
|
|
|
10400
|
my $hash = {}; |
71
|
6296
|
|
|
|
|
13842
|
my $xpath = $self->as_xpath . HTML::Linear::Path::_wrap(separator => '/'); |
72
|
|
|
|
|
|
|
|
73
|
6296
|
|
|
|
|
11847
|
for my $key (sort keys %{$self->attributes}) { |
|
6296
|
|
|
|
|
155539
|
|
74
|
|
|
|
|
|
|
$hash->{ |
75
|
9329
|
100
|
100
|
|
|
229377
|
$xpath |
76
|
|
|
|
|
|
|
. HTML::Linear::Path::_wrap(sigil => '@') |
77
|
|
|
|
|
|
|
. HTML::Linear::Path::_wrap(attribute => $key) |
78
|
|
|
|
|
|
|
} = $self->attributes->{$key} |
79
|
|
|
|
|
|
|
if |
80
|
|
|
|
|
|
|
$self->strict |
81
|
|
|
|
|
|
|
or not HTML::Linear::Path::_isgroup($self->path->[-1]->tag, $key); |
82
|
|
|
|
|
|
|
} |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
$hash->{ |
85
|
6296
|
100
|
|
|
|
168317
|
$xpath |
86
|
|
|
|
|
|
|
. HTML::Linear::Path::_wrap(attribute => 'text()') |
87
|
|
|
|
|
|
|
} = $self->content |
88
|
|
|
|
|
|
|
unless $self->content =~ m{^\s*$}sx; |
89
|
|
|
|
|
|
|
|
90
|
6296
|
|
|
|
|
68589
|
return $hash; |
91
|
|
|
|
|
|
|
} |
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
sub weight { |
95
|
24
|
|
|
24
|
1
|
119
|
my ($self) = @_; |
96
|
24
|
|
|
|
|
49
|
return sum map { $_->weight } @{$self->path}; |
|
72
|
|
|
|
|
221
|
|
|
24
|
|
|
|
|
101
|
|
97
|
|
|
|
|
|
|
} |
98
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
1; |
100
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
__END__ |