This queue is for tickets about the HTML-Selector-XPath CPAN distribution.

Report information
The Basics
Id:
57603
Status:
resolved
Priority:
Low/Low

People
Owner:
Nobody in particular
Requestors:
JZOBEL [...] cpan.org
Cc:
dwheeler [...] cpan.org
AdminCc:

BugTracker
Severity:
Wishlist
Broken in:
(no value)
Fixed in:
(no value)



Subject: An option to create xpath expressions that start at the context node
Please add an option to create xpath expressions that start at the context node instead of root. e.g.: my $selector = HTML::Selector::XPath->new("li#main"); $selector->to_xpath(local => 1); # ./li[@id='main'] my $xpath = selector_to_xpath('div.foo', local => 1); # ./div[@class='foo'] Thanks, Joachim
Subject: Re: [rt.cpan.org #57603] An option to create xpath expressions that start at the context node
Date: Tue, 18 May 2010 13:28:33 -0700
To: bug-HTML-Selector-XPath@rt.cpan.org
From: Tatsuhiko Miyagawa <miyagawa@gmail.com>
Sounds like a good option. Patches welcome. On Tue, May 18, 2010 at 1:12 PM, Joachim Zobel via RT <bug-HTML-Selector-XPath@rt.cpan.org> wrote:
Show quoted text
> Tue May 18 16:12:42 2010: Request 57603 was acted upon. > Transaction: Ticket created by JZOBEL >       Queue: HTML-Selector-XPath >     Subject: An option to create xpath expressions that start at the context node >   Broken in: (no value) >    Severity: Wishlist >       Owner: Nobody >  Requestors: JZOBEL@cpan.org >      Status: new >  Ticket <URL: https://rt.cpan.org/Ticket/Display.html?id=57603 > > > > Please add an option to create xpath expressions that start at the > context node instead of root. e.g.: > > my $selector = HTML::Selector::XPath->new("li#main"); > $selector->to_xpath(local => 1); # ./li[@id='main'] > > my $xpath = selector_to_xpath('div.foo', local => 1); > # ./div[@class='foo'] > > Thanks, > Joachim > >
-- Tatsuhiko Miyagawa
Hi. By now I would suggest adding a root parameter. The following should do, if I did not miss anything. --- /usr/share/perl5/HTML/Selector/XPath.pm 2008-03-02 17:02:02.000000000 +0100 +++ lib/HTML/Selector//XPath.pm 2010-05-21 11:31:01.000000000 +0200 @@ -10,7 +10,7 @@ use Carp; sub selector_to_xpath { - __PACKAGE__->new(shift)->to_xpath; + __PACKAGE__->new(shift)->to_xpath(@_); } my $reg = { @@ -42,10 +42,12 @@ sub to_xpath { my $self = shift; + my %parms = @_; + my $root = $parms{root} || '/'; my $rule = $self->{expression} or return; my $index = 1; - my @parts = ("//", "*"); + my @parts = ("$root/", "*"); my $last_rule = ''; my @next_parts; @@ -141,7 +143,7 @@ # Match commas if ($rule =~ s/$reg->{comma}//) { - push @parts, " | ", "//", "*"; # ending one rule and beginning another + push @parts, " | ", "$root/", "*"; # ending one rule and beginning another $index = @parts - 1; } }
Subject: Re: [rt.cpan.org #57603] An option to create xpath expressions that start at the context node
Date: Fri, 21 May 2010 02:55:08 -0700
To: "bug-HTML-Selector-XPath@rt.cpan.org" <bug-HTML-Selector-XPath@rt.cpan.org>
From: Tatsuhiko Miyagawa <miyagawa@gmail.com>
Can you add a unit test for this as well? On Friday, May 21, 2010, Joachim Zobel via RT <bug-HTML-Selector-XPath@rt.cpan.org> wrote:
Show quoted text
>       Queue: HTML-Selector-XPath >  Ticket <URL: https://rt.cpan.org/Ticket/Display.html?id=57603 > > > Hi. > > By now I would suggest adding a root parameter. The following should do, > if I did not miss anything. > > --- /usr/share/perl5/HTML/Selector/XPath.pm     2008-03-02 > 17:02:02.000000000 +0100 > +++ lib/HTML/Selector//XPath.pm 2010-05-21 11:31:01.000000000 +0200 > @@ -10,7 +10,7 @@ >  use Carp; > >  sub selector_to_xpath { > -    __PACKAGE__->new(shift)->to_xpath; > +    __PACKAGE__->new(shift)->to_xpath(@_); >  } > >  my $reg = { > @@ -42,10 +42,12 @@ > >  sub to_xpath { >     my $self = shift; > +    my %parms = @_; > +    my $root = $parms{root} || '/'; >     my $rule = $self->{expression} or return; > >     my $index = 1; > -    my @parts = ("//", "*"); > +    my @parts = ("$root/", "*"); >     my $last_rule = ''; >     my @next_parts; > > @@ -141,7 +143,7 @@ > >         # Match commas >         if ($rule =~ s/$reg->{comma}//) { > -            push @parts, " | ", "//", "*"; # ending one rule and > beginning another > +            push @parts, " | ", "$root/", "*"; # ending one rule and > beginning another >             $index = @parts - 1; >         } >     } > >
-- Tatsuhiko Miyagawa
I have attached the complete patch including tests now as a file since copy & paste causes trouble.
Subject: JZOBEL.patch
diff -rupN HTML-Selector-XPath-0.04/lib/HTML/Selector/XPath.pm HTML-Selector-XPath-0.04-JZOBEL/lib/HTML/Selector/XPath.pm --- HTML-Selector-XPath-0.04/lib/HTML/Selector/XPath.pm 2010-02-27 10:44:04.000000000 +0100 +++ HTML-Selector-XPath-0.04-JZOBEL/lib/HTML/Selector/XPath.pm 2010-05-22 14:39:02.000000000 +0200 @@ -11,7 +11,7 @@ our @EXPORT_OK = qw(selector_to_xpath); use Carp; sub selector_to_xpath { - __PACKAGE__->new(shift)->to_xpath; + __PACKAGE__->new(shift)->to_xpath(@_); } my $reg = { @@ -44,9 +44,11 @@ sub selector { sub to_xpath { my $self = shift; my $rule = $self->{expression} or return; + my %parms = @_; + my $root = $parms{root} || '/'; my $index = 1; - my @parts = ("//", "*"); + my @parts = ("$root/", "*"); my $last_rule = ''; my @next_parts; @@ -144,7 +146,7 @@ sub to_xpath { # Match commas if ($rule =~ s/$reg->{comma}//) { - push @parts, " | ", "//", "*"; # ending one rule and beginning another + push @parts, " | ", "$root/", "*"; # ending one rule and beginning another $index = @parts - 1; } } diff -rupN HTML-Selector-XPath-0.04/t/03_xpath_root.t HTML-Selector-XPath-0.04-JZOBEL/t/03_xpath_root.t --- HTML-Selector-XPath-0.04/t/03_xpath_root.t 1970-01-01 01:00:00.000000000 +0100 +++ HTML-Selector-XPath-0.04-JZOBEL/t/03_xpath_root.t 2010-05-22 15:07:31.000000000 +0200 @@ -0,0 +1,68 @@ +use strict; +use Test::Base; +use HTML::Selector::XPath qw(selector_to_xpath); + +plan tests => 1 * blocks; +filters { selector => 'chomp', xpath => 'chomp' }; + +run { + my $block = shift; + is selector_to_xpath($block->selector, root=> '/R/'), $block->xpath, $block->selector; +} + +__END__ +=== +--- selector +* +--- xpath +/R//* + +=== +--- selector +E +--- xpath +/R//E + +=== +--- selector +E F +--- xpath +/R//E//F + +=== +--- selector +E > F +--- xpath +/R//E/F + +=== +--- selector +E + F +--- xpath +/R//E/following-sibling::*[1]/self::F + +=== +--- selector +E[foo] +--- xpath +/R//E[@foo] + +=== +--- selector +E[foo="warning"] +--- xpath +/R//E[@foo='warning'] + +=== +--- selector +E#myid +--- xpath +/R//E[@id='myid'] + +=== +--- selector +foo.bar, bar +--- xpath +/R//foo[contains(concat(' ', @class, ' '), ' bar ')] | /R//bar + +
On Sat May 22 09:12:10 2010, JZOBEL wrote:
Show quoted text
> I have attached the complete patch including tests now as a file since > copy & paste causes trouble.
I've rebased this patch and pushed it to my GitHub fork: https://github.com/theory/HTML-Selector-XPath/tree/context-node The tests fail, however, because of the new behavior of `*` introduced in 0.05, and which I don't understand, alas. Also needs documentation.
RT-Send-CC: miyagawa@gmail.com
Am Do 17. Feb 2011, 13:31:24, DWHEELER schrieb:
Show quoted text
> On Sat May 22 09:12:10 2010, JZOBEL wrote:
> > I have attached the complete patch including tests now as a file
> since
> > copy & paste causes trouble.
> > I've rebased this patch and pushed it to my GitHub fork: > > https://github.com/theory/HTML-Selector-XPath/tree/context-node > > The tests fail, however, because of the new behavior of `*` introduced > in 0.05, and which I don't > understand, alas. Also needs documentation.
Merged and fixed by me, in _my_ github branch at https://github.com/Corion/HTML-Selector-XPath which I hope is likely to become 0.06 as soon as Miagawa-sama looks at it... The documentation is a bit scant, so improvements there are very welcome, especially as I'm not a native speaker!


This service runs on Request Tracker, is sponsored by The Perl Foundation, and maintained by Best Practical Solutions.

Please report any issues with rt.cpan.org to rt-cpan-admin@bestpractical.com.