| 1 | package Pandoc::Selector; | ||||||||||||||||||||||||||||
| 2 | 31 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | 31 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 31 | ▶ | use 5.010001; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 31 | ▶ | use Pandoc::Elements; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 31 | my $IDENTIFIER = qr{[\p{L}\p{N}_-]+}; | |||||||||||||||||||||||||||
| 9 | 31 | my $NAME = qr{[A-Za-z]+}; | |||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | sub new { | ||||||||||||||||||||||||||||
| 12 | 173 | ▶ | my ($class, $selector) = @_; | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 13 | # TODO: compile selector | ||||||||||||||||||||||||||||
| 14 | 173 | bless { selector => $selector }, $class; | |||||||||||||||||||||||||||
| 15 | } | ||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | sub match { | ||||||||||||||||||||||||||||
| 18 | 173 | ▶ | my ($self, $element) = @_; | ||||||||||||||||||||||||||
|
Subroutine
match: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | 173 | foreach my $selector ( split /\|/, $self->{selector} ) { | |||||||||||||||||||||||||||
| 21 | 340 | ▶ | return 1 if _match_expression($selector, $element); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 22 | } | ||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | 90 | return 0; | |||||||||||||||||||||||||||
| 25 | } | ||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | sub _match_expression { | ||||||||||||||||||||||||||||
| 28 | 340 | ▶ | my ( $selector, $elem ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_match_expression: called CC=8 SCAR=20.8
| |||||||||||||||||||||||||||||
| 29 | 340 | $selector =~ s/^\s+|\s+$//g; | |||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | # name | ||||||||||||||||||||||||||||
| 32 | 340 | ▶ | return 0 | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$selector =~ s/^($NAME)\s*//i and lc $1 ne lc $elem->name
MC/DC100%2/2
$selector =~ s/^($NAME)\s*//i and lc $1 ne lc $elem->name
$selector =~ s/^($NAME)\s*//i
lc $1 ne lc $elem->name
Truth table100%3/3
A and B
A= $selector =~ s/^($NAME)\s*//iB= lc $1 ne lc $elem->name
| |||||||||||||||||||||||||||||
| 33 | if $selector =~ s/^($NAME)\s*//i and lc($1) ne lc( $elem->name ); | ||||||||||||||||||||||||||||
| 34 | 90 | ▶ | return 1 if $selector eq ''; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | # type | ||||||||||||||||||||||||||||
| 37 | 17 | ▶ | if ( $selector =~ s/^:(document|block|inline|meta)\s*// ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 38 | 6 | my $method = "is_$1"; | |||||||||||||||||||||||||||
| 39 | 6 | ▶ | return 0 unless $elem->$method; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 40 | 4 | ▶ | return 1 if $selector eq ''; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 41 | } | ||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | # TODO: :method (e.g. :url) | ||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | # TODO: [:level=1] | ||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | # TODO [<number>] | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | # TODO [@<attr>] | ||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | # id and/or classes | ||||||||||||||||||||||||||||
| 52 | 12 | ▶ | return 0 unless $elem->isa('Pandoc::Document::AttributesRole'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 53 | 11 | return _match_attributes($selector, $elem); | |||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | # check #id and .class | ||||||||||||||||||||||||||||
| 57 | sub _match_attributes { | ||||||||||||||||||||||||||||
| 58 | 11 | ▶ | my ( $selector, $elem ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_match_attributes: called CC=6 SCAR=18.0
| |||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | 11 | $selector =~ s/^\s+|\s+$//g; # trim | |||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | 11 | ▶ | while ( $selector ne '' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 63 | 14 | ▶ | if ( $selector =~ s/^#($IDENTIFIER)\s*// ) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 64 | 4 | ▶ | return 0 unless $elem->id eq $1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||
| 66 | elsif ( $selector =~ s/^\.($IDENTIFIER)\s*// ) { | ||||||||||||||||||||||||||||
| 67 | 10 | ▶ | return 0 unless grep { $1 eq $_ } @{ $elem->attr->[1] }; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||
| 69 | else { | ||||||||||||||||||||||||||||
| 70 | 0 | return 0; | |||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | 7 | return 1; | |||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | 31 | 1; | |||||||||||||||||||||||||||
| 78 | __END__ | ||||||||||||||||||||||||||||