Subject: | selector_to_xpath wth css selector using :contains |
Date: | Mon, 22 Aug 2016 14:19:45 +0000 |
To: | "bug-HTML-Selector-XPath [...] rt.cpan.org" <bug-HTML-Selector-XPath [...] rt.cpan.org> |
From: | Michal Kos <kosmichal [...] gmail.com> |
Hi,
There seems to be a problem when converting css selectors to xpath if they
use :contains pseudoclass.
Consider following html:
#####
<label ><span >Title</span>
<span >*</span> </label>
#####
now if I try following css selector (say in Chrome console)
"label:contains('Title')" it returns <label>...</label>
if I use selector_to_xpath("label:contains('Title')") it returns following
xpath:
"//label[text()[contains(string(.),'Title')]]"
trying to use this xpath results in empty result []
however if I modify the xpath and remove text() so the xpath is
"//label[contains(string(.),'Title')]" it will return same element as css
selector would do.
The fix would be to edit HTML/Selector/XPath.pm lines 221 and 223 and
change:
push @parts, qq{[text()[contains(string(.),"$1")]]};
to
push @parts, qq{[contains(string(.),"$1")]};
Could you please apply it?
* Distrubution name: HTML-Selector-XPath 0.20
* Perl version:
$ perl -v
This is perl 5, version 20, subversion 2 (v5.20.2) built for
x86_64-linux-gnu-thread-multi
(with 81 registered patches, see perl -V for more detail)
* OS:
$ uname -a
Linux deb-vm 3.16.0-4-amd64 #1 SMP Debian 3.16.7-ckt25-2 (2016-04-08)
x86_64 GNU/Linux
### test case ###
use strict;
use warnings;
use HTML::Selector::XPath 'selector_to_xpath';
use Test::More tests => 1;
my $css="label:contains('Title')";
my $xpath = selector_to_xpath($css);
#//label[text()[contains(string(.),"Title")]]
my $expected="//label[contains(string(.),'Title')]";
is($xpath,$expected,"selector_to_xpath(label:contains('Title'))");
### test case ###
### html example ###
<!DOCTYPE html>
<html>
<head>
<title>
test
</title>
<script src="/resources/jquery-3.1.0.min.js"></script>
</head>
<body>
<label ><span >Title</span>
<span >*</span> </label>
</body>
</html>
### html example ###
Thank you.