File Coverage

blib/lib/WWW/Mechanize/Plugin/Selector.pm
Criterion Covered Total %
statement 9 17 52.9
branch 0 4 0.0
condition 0 5 0.0
subroutine 3 4 75.0
pod 1 1 100.0
total 13 31 41.9


line stmt bran cond sub pod time code
1             package WWW::Mechanize::Plugin::Selector;
2 1     1   14092 use strict;
  1         3  
  1         28  
3 1     1   5 use vars qw($VERSION);
  1         2  
  1         56  
4             $VERSION= '0.16';
5 1     1   416 use HTML::Selector::XPath 'selector_to_xpath';
  1         1977  
  1         144  
6              
7             =head1 SYNOPSIS
8              
9             =head1 NAME
10              
11             WWW::Mechanize::Plugin::Selector - CSS selector method for WWW::Mechanize
12              
13             =head1 DESCRIPTION
14              
15             This is a plugin (or "Role", for some) which supplies the C<< ->selector >>
16             method to your WWW::Mechanize object. It requires that the WWW::Mechanize
17             object implements a corresponding C<< ->xpath >> method, as L
18             and L do.
19              
20             =head1 ADDED METHODS
21              
22             =head2 C<< $mech->selector( $css_selector, %options ) >>
23              
24             my @text = $mech->selector('p.content');
25              
26             Returns all nodes matching the given CSS selector. If
27             C<$css_selector> is an array reference, it returns
28             all nodes matched by any of the CSS selectors in the array.
29              
30             This takes the same options that C<< ->xpath >> does.
31              
32             =cut
33              
34             sub selector {
35 0     0 1   my ($self,$query,%options) = @_;
36 0   0       $options{ user_info } ||= "CSS selector '$query'";
37 0 0 0       if ('ARRAY' ne (ref $query || '')) {
38 0           $query = [$query];
39             };
40 0 0         my $root = $options{ node } ? './' : '';
41 0           my @q = map { selector_to_xpath($_, root => $root) } @$query;
  0            
42 0           $self->xpath(\@q, %options);
43             };
44              
45             1;
46              
47             =head1 USE IN YOUR MODULE
48              
49             If you are not using L, you can import this
50             code in your module via the following:
51              
52             use WWW::Mechanize::Plugin::Selector;
53             {
54             no warnings 'once';
55             *selector = \&WWW::Mechanize::Plugin::Selector::selector;
56             }
57              
58             =head1 REPOSITORY
59              
60             The public repository of this module is
61             L.
62              
63             =head1 SUPPORT
64              
65             The public support forum of this module is
66             L.
67              
68             =head1 BUG TRACKER
69              
70             Please report bugs in this module via the RT CPAN bug queue at
71             L
72             or via mail to L.
73              
74             =head1 AUTHOR
75              
76             Max Maischein C
77              
78             =head1 COPYRIGHT (c)
79              
80             Copyright 2010-2017 by Max Maischein C.
81              
82             =head1 LICENSE
83              
84             This module is released under the same terms as Perl itself.
85              
86             =cut