Permalink
Browse files

fix xpath "."

  • Loading branch information...
1 parent eaafbe4 commit c93ef5827355335f447a04e14a0f5c9fb5832339 @tomill committed Feb 10, 2010
Showing with 151 additions and 25 deletions.
  1. +16 −4 lib/Template/Semantic.pm
  2. +2 −8 lib/Template/Semantic/Document.pm
  3. +65 −13 t/05_value-type.t
  4. +68 −0 t/11_exp_to_xpath.t
View
@@ -289,13 +289,25 @@ Replace the inner content by another C<process()>-ed result.
=item * selector => { 'selector' => $value, ... }
I<Hash-ref:> Sub query of the part.
-
+
$ts->process($template, {
+ # All <a> tag *in <div class="foo">* disappears
'div.foo' => {
- 'a' => undef, # All <a> tag *in <div class="foo">* disappears
+ 'a' => undef,
},
-
- 'div.foo a' => undef, # same as above
+
+ # same as above
+ 'div.foo a' => undef,
+
+ # xpath '.' = current node (itself)
+ 'a#bar' => {
+ '.' => 'foobar',
+ './@href' => 'foo.html',
+ },
+
+ # same as above
+ 'a#bar' => 'foobar',
+ 'a#bar/@href' => 'foo.html',
});
=back
@@ -59,7 +59,7 @@ sub _exp_to_xpath {
return unless $exp;
my $xpath;
- if ($exp =~ m{^/}) {
+ if ($exp =~ m{^(?:/|\.(?:/|$))}) {
$xpath = $exp;
} elsif ($exp =~ m{^id\(}) {
$xpath = $exp;
@@ -102,15 +102,9 @@ sub _assign_value {
if (not $node->isa('XML::LibXML::Element')) {
croak "Can't assign hashref to " . ref($node);
}
-
- my $fixed_value = { };
- my $prefix_xpath = '/' . $node->nodeName;
- for my $exp (keys %$value) {
- $fixed_value->{ $prefix_xpath . $self->_exp_to_xpath($exp) } = delete $value->{$exp};
- }
my $parted = $self->_to_node($node->serialize);
- $self->_query($parted, $fixed_value);
+ $self->_query($parted, $value);
$node->replaceNode($parted);
}
}
View
@@ -181,36 +181,48 @@ use XML::LibXML;
<div><span>xxx</span></div>
</root>
-=== elem x hashref (xpath)
+=== elem x hashref (css selector)
--- vars
'id("bar")' => {
- '/span' => 'xxx',
+ 'span, .aaa' => 'xxx',
}
--- template
<root>
- <div id="foo"><span>foo</span></div>
- <div id="bar"><span>bar</span></div>
+ <div id="foo">
+ <span>foo</span>
+ <b class="aaa">foo</b>
+ </div>
+ <div id="bar">
+ <span>bar</span>
+ <b class="aaa">bar</b>
+ </div>
</root>
--- expected
<root>
- <div id="foo"><span>foo</span></div>
- <div id="bar"><span>xxx</span></div>
+ <div id="foo">
+ <span>foo</span>
+ <b class="aaa">foo</b>
+ </div>
+ <div id="bar">
+ <span>xxx</span>
+ <b class="aaa">xxx</b>
+ </div>
</root>
-=== elem x hashref (css selector)
+=== elem x hashref (xpath)
--- vars
-'id("bar")' => {
- 'span' => 'xxx',
+'.foo' => {
+ '.' => 'xxx',
+ './@class' => 'yyy',
+ './@href' => 'zzz',
}
--- template
<root>
- <div id="foo"><span>foo</span></div>
- <div id="bar"><span>bar</span></div>
+ <a href="" class="foo"></a>
</root>
--- expected
<root>
- <div id="foo"><span>foo</span></div>
- <div id="bar"><span>xxx</span></div>
+ <a href="zzz" class="yyy">xxx</a>
</root>
=== elem x list (xpath)
@@ -262,6 +274,46 @@ use XML::LibXML;
</table>
</root>
+=== elem x list (nested)
+--- vars
+'ul li' => [
+ {
+ '.foo' => 'A',
+ 'span' => [
+ { '.' => 'aaa' },
+ { '.' => 'aaa' },
+ { '.' => 'aaa' },
+ ],
+ },
+ {
+ '.foo' => 'B',
+ 'span' => [
+ { '.' => 'bbb' },
+ { '.' => 'bbb' },
+ { '.' => 'bbb' },
+ ],
+ },
+]
+--- template
+<ul>
+ <li><div class="foo"></div>
+ <span></span>
+ </li>
+</ul>
+--- expected
+<ul>
+ <li><div class="foo">A</div>
+ <span>aaa</span>
+ <span>aaa</span>
+ <span>aaa</span>
+ </li>
+ <li><div class="foo">B</div>
+ <span>bbb</span>
+ <span>bbb</span>
+ <span>bbb</span>
+ </li>
+</ul>
+
=== elem x sub (return scalar)
--- vars
'//span' => sub { "xxx" }
View
@@ -0,0 +1,68 @@
+use strict;
+use warnings;
+use Test::Base; plan tests => 1 * blocks;
+
+use Template::Semantic;
+
+filters {
+ exp => 'chomp',
+ xpath => 'chomp',
+};
+
+run {
+ my $block = shift;
+ my $xpath = Template::Semantic::Document->_exp_to_xpath($block->exp);
+ is($xpath, $block->xpath, $block->exp);
+};
+
+__DATA__
+===
+--- exp
+/
+--- xpath
+/
+===
+--- exp
+/foo
+--- xpath
+/foo
+===
+--- exp
+//
+--- xpath
+//
+===
+--- exp
+./
+--- xpath
+./
+===
+--- exp
+.
+--- xpath
+.
+===
+--- exp
+.foo
+--- xpath
+//*[contains(concat(' ', @class, ' '), ' foo ')]
+===
+--- exp
+id("foo")
+--- xpath
+//*[@id="foo"]
+===
+--- exp
+foo@attr
+--- xpath
+//foo/@attr
+===
+--- exp
+foo/@attr
+--- xpath
+//foo/@attr
+===
+--- exp
+@attr
+--- xpath
+//@attr

0 comments on commit c93ef58

Please sign in to comment.