| 1 | package TPath::Selector::Test; | ||
| 2 | $TPath::Selector::Test::VERSION = '1.007'; | ||
| 3 | # ABSTRACT: role of selectors that apply some test to a node to select it | ||
| 4 | |||
| 5 | |||
| 6 | 14 | ▶ | use v5.10; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | 14 | ▶ | no if $] >= 5.018, warnings => "experimental"; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | |||
| 9 | 14 | ▶ | use Moose::Role; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 10 | 0 | ▶ | use TPath::TypeConstraints; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 11 | use TPath::Test::Node::Complement; | ||
| 12 | |||
| 13 | |||
| 14 | with 'TPath::Selector::Predicated'; | ||
| 15 | |||
| 16 | |||
| 17 | has f => ( is => 'ro', does => 'TPath::Forester', required => 1 ); | ||
| 18 | |||
| 19 | |||
| 20 | has axis => | ||
| 21 | ( is => 'ro', isa => 'Axis', writer => '_axis', default => 'child' ); | ||
| 22 | |||
| 23 | |||
| 24 | has first_sensitive => ( is => 'ro', isa => 'Bool', default => 0 ); | ||
| 25 | |||
| 26 | # axis translated into a forester method | ||
| 27 | has faxis => ( | ||
| 28 | is => 'ro', | ||
| 29 | isa => 'CodeRef', | ||
| 30 | lazy => 1, | ||
| 31 | default => sub { | ||
| 32 | my $self = shift; | ||
| 33 | ( my $v = $self->axis ) =~ tr/-/_/; | ||
| 34 | $self->f->can("axis_$v"); | ||
| 35 | }, | ||
| 36 | ); | ||
| 37 | |||
| 38 | # axis used in a first-sensitive context | ||
| 39 | has sensitive_axis => ( | ||
| 40 | is => 'ro', | ||
| 41 | isa => 'CodeRef', | ||
| 42 | lazy => 1, | ||
| 43 | default => sub { | ||
| 44 | my $self = shift; | ||
| 45 | for ( $self->axis ) { | ||
| 46 | when ('child') { return $self->f->can('axis_self') } | ||
| 47 | when ('descendant') { | ||
| 48 | return $self->f->can('axis_descendant_or_self') | ||
| 49 | } | ||
| 50 | default { return $self->faxis } | ||
| 51 | } | ||
| 52 | }, | ||
| 53 | ); | ||
| 54 | |||
| 55 | |||
| 56 | has is_inverted => | ||
| 57 | ( is => 'ro', isa => 'Bool', default => 0, writer => '_mark_inverted' ); | ||
| 58 | |||
| 59 | sub _stringify_match { | ||
| 60 | my ( $self, $re ) = @_; | ||
| 61 | |||
| 62 | # chop off the "(?-xism:" prefix and ")" suffix | ||
| 63 | if ( $re =~ /^\Q(?-xism:\E/ ) { | ||
| 64 | $re = substr $re, 8, length($re) - 9; | ||
| 65 | } | ||
| 66 | elsif ( $re =~ /^\Q(?^:\E/ ) { | ||
| 67 | $re = substr $re, 4, length($re) - 5; | ||
| 68 | } | ||
| 69 | $re =~ s/~/~~/g; | ||
| 70 | return "~$re~"; | ||
| 71 | } | ||
| 72 | |||
| 73 | |||
| 74 | has node_test => | ||
| 75 | ( is => 'ro', isa => 'TPath::Test::Node', writer => '_node_test' ); | ||
| 76 | |||
| 77 | sub _invert { | ||
| 78 | my $self = shift; | ||
| 79 | $self->_node_test( | ||
| 80 | TPath::Test::Node::Complement->new( nt => $self->node_test ) ); | ||
| 81 | $self->_mark_inverted(1); | ||
| 82 | } | ||
| 83 | |||
| 84 | has _cr1 => ( is => 'rw', isa => 'CodeRef' ); | ||
| 85 | has _cr2 => ( is => 'rw', isa => 'CodeRef' ); | ||
| 86 | |||
| 87 | |||
| 88 | sub candidates { | ||
| 89 | return ( | ||
| 90 | $_[0]->_cr1 // do { | ||
| 91 | my $axis; | ||
| 92 | my $nt = $_[0]->node_test; | ||
| 93 | my $f = $_[0]->f; | ||
| 94 | if ( $_[0]->first_sensitive ) { | ||
| 95 | $axis = $_[0]->sensitive_axis; | ||
| 96 | } | ||
| 97 | else { | ||
| 98 | $axis = $_[0]->faxis; | ||
| 99 | } | ||
| 100 | $_[0]->_cr1( sub { $axis->( $f, $_[0], $nt ) } ); | ||
| 101 | } | ||
| 102 | )->( $_[1] ) if $_[2]; | ||
| 103 | return ( | ||
| 104 | $_[0]->_cr2 // do { | ||
| 105 | my $axis = $_[0]->faxis; | ||
| 106 | my $nt = $_[0]->node_test; | ||
| 107 | my $f = $_[0]->f; | ||
| 108 | $_[0]->_cr2( sub { $axis->( $f, $_[0], $nt ) } ); | ||
| 109 | } | ||
| 110 | )->( $_[1] ); | ||
| 111 | |||
| 112 | # my ( $self, $ctx, $first ) = @_; | ||
| 113 | # my $axis; | ||
| 114 | # if ( $first && $self->first_sensitive ) { | ||
| 115 | # $axis = $self->sensitive_axis; | ||
| 116 | # } | ||
| 117 | # else { | ||
| 118 | # $axis = $self->faxis; | ||
| 119 | # } | ||
| 120 | # return $self->f->$axis( $ctx, $self->node_test ); | ||
| 121 | } | ||
| 122 | |||
| 123 | # implements method required by TPath::Selector | ||
| 124 | sub select { | ||
| 125 | my ( $self, $ctx, $first ) = @_; | ||
| 126 | my @candidates = $self->candidates( $ctx, $first ); | ||
| 127 | return $self->apply_predicates(@candidates); | ||
| 128 | } | ||
| 129 | |||
| 130 | 1; | ||
| 131 | |||
| 132 | __END__ | ||