Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[CssSelector] Fix :nth-last-child() translation #8073

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 19, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 23 additions & 20 deletions src/Symfony/Component/CssSelector/Node/FunctionNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,43 +100,46 @@ protected function _xpath_nth_child($xpath, $expr, $last = false, $addNameTest =
$xpath->addStarPrefix();
if ($a == 0) {
if ($last) {
$b = sprintf('last() - %s', $b);
$b = sprintf('last() - %s', $b - 1);
}
$xpath->addCondition(sprintf('position() = %s', $b));

return $xpath;
}

if ($last) {
// FIXME: I'm not sure if this is right
$a = -$a;
$b = -$b;
}
if ($a < 0) {
if ($b < 1) {
$xpath->addCondition('false()');

if ($b > 0) {
$bNeg = -$b;
return $xpath;
}

$sign = '<=';
} else {
$bNeg = sprintf('+%s', -$b);
$sign = '>=';
}

if ($a != 1) {
$expr = array(sprintf('(position() %s) mod %s = 0', $bNeg, $a));
} else {
$expr = array();
$expr = 'position()';

if ($last) {
$expr = 'last() - '.$expr;
$b--;
}

if ($b >= 0) {
$expr[] = sprintf('position() >= %s', $b);
} elseif ($b < 0 && $last) {
$expr[] = sprintf('position() < (last() %s)', $b);
if (0 !== $b) {
$expr .= ' - '.$b;
}
$expr = implode($expr, ' and ');

$conditions = array(sprintf('%s %s 0', $expr, $sign));

if ($expr) {
$xpath->addCondition($expr);
if (1 !== $a && -1 !== $a) {
$conditions[] = sprintf('(%s) mod %d = 0', $expr, $a);
}

$xpath->addCondition(implode(' and ', $conditions));

return $xpath;

/* FIXME: handle an+b, odd, even
an+b means every-a, plus b, e.g., 2n+1 means odd
0n+b means b
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public function getCssSelectors()
array('h1', "h1"),
array('foo|h1', "foo:h1"),
array('h1, h2, h3', "h1 | h2 | h3"),
array('h1:nth-child(3n+1)', "*/*[name() = 'h1' and ((position() -1) mod 3 = 0 and position() >= 1)]"),
array('h1:nth-child(3n+1)', "*/*[name() = 'h1' and (position() - 1 >= 0 and (position() - 1) mod 3 = 0)]"),
array('h1 > p', "h1/p"),
array('h1#foo', "h1[@id = 'foo']"),
array('h1.foo', "h1[contains(concat(' ', normalize-space(@class), ' '), ' foo ')]"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,12 @@ public function testToXpath()
// h1:nth-child(odd)
$element2 = new ElementNode('*', new Token('Symbol', 'odd', -1));
$function = new FunctionNode($element, ':', 'nth-child', $element2);
$this->assertEquals("*/*[name() = 'h1' and ((position() -1) mod 2 = 0 and position() >= 1)]", (string) $function->toXpath(), '->toXpath() returns the xpath representation of the node');
$this->assertEquals("*/*[name() = 'h1' and (position() - 1 >= 0 and (position() - 1) mod 2 = 0)]", (string) $function->toXpath(), '->toXpath() returns the xpath representation of the node');

// h1:nth-child(even)
$element2 = new ElementNode('*', new Token('Symbol', 'even', -1));
$function = new FunctionNode($element, ':', 'nth-child', $element2);
$this->assertEquals("*/*[name() = 'h1' and ((position() +0) mod 2 = 0 and position() >= 0)]", (string) $function->toXpath(), '->toXpath() returns the xpath representation of the node');
$this->assertEquals("*/*[name() = 'h1' and (position() >= 0 and (position()) mod 2 = 0)]", (string) $function->toXpath(), '->toXpath() returns the xpath representation of the node');

// h1:nth-child(n)
$element2 = new ElementNode('*', new Token('Symbol', 'n', -1));
Expand All @@ -51,12 +51,12 @@ public function testToXpath()
// h1:nth-child(3n+1)
$element2 = new ElementNode('*', new Token('Symbol', '3n+1', -1));
$function = new FunctionNode($element, ':', 'nth-child', $element2);
$this->assertEquals("*/*[name() = 'h1' and ((position() -1) mod 3 = 0 and position() >= 1)]", (string) $function->toXpath(), '->toXpath() returns the xpath representation of the node');
$this->assertEquals("*/*[name() = 'h1' and (position() - 1 >= 0 and (position() - 1) mod 3 = 0)]", (string) $function->toXpath(), '->toXpath() returns the xpath representation of the node');

// h1:nth-child(n+1)
$element2 = new ElementNode('*', new Token('Symbol', 'n+1', -1));
$function = new FunctionNode($element, ':', 'nth-child', $element2);
$this->assertEquals("*/*[name() = 'h1' and (position() >= 1)]", (string) $function->toXpath(), '->toXpath() returns the xpath representation of the node');
$this->assertEquals("*/*[name() = 'h1' and (position() - 1 >= 0)]", (string) $function->toXpath(), '->toXpath() returns the xpath representation of the node');

// h1:nth-child(1)
$element2 = new ElementNode('*', new Token('Symbol', '2', -1));
Expand All @@ -66,24 +66,24 @@ public function testToXpath()
// h1:nth-child(2n)
$element2 = new ElementNode('*', new Token('Symbol', '2n', -1));
$function = new FunctionNode($element, ':', 'nth-child', $element2);
$this->assertEquals("*/*[name() = 'h1' and ((position() +0) mod 2 = 0 and position() >= 0)]", (string) $function->toXpath(), '->toXpath() returns the xpath representation of the node');
$this->assertEquals("*/*[name() = 'h1' and (position() >= 0 and (position()) mod 2 = 0)]", (string) $function->toXpath(), '->toXpath() returns the xpath representation of the node');

// h1:nth-child(-n)
$element2 = new ElementNode('*', new Token('Symbol', '-n', -1));
$function = new FunctionNode($element, ':', 'nth-child', $element2);
$this->assertEquals("*/*[name() = 'h1' and ((position() +0) mod -1 = 0 and position() >= 0)]", (string) $function->toXpath(), '->toXpath() returns the xpath representation of the node');
$this->assertEquals("*/*[name() = 'h1' and (false())]", (string) $function->toXpath(), '->toXpath() returns the xpath representation of the node');

// h1:nth-last-child(2)
$function = new FunctionNode($element, ':', 'nth-last-child', 2);
$this->assertEquals("*/*[name() = 'h1' and (position() = last() - 2)]", (string) $function->toXpath(), '->toXpath() returns the xpath representation of the node');
$this->assertEquals("*/*[name() = 'h1' and (position() = last() - 1)]", (string) $function->toXpath(), '->toXpath() returns the xpath representation of the node');

// h1:nth-of-type(2)
$function = new FunctionNode($element, ':', 'nth-of-type', 2);
$this->assertEquals("*/h1[position() = 2]", (string) $function->toXpath(), '->toXpath() returns the xpath representation of the node');

// h1:nth-last-of-type(2)
$function = new FunctionNode($element, ':', 'nth-last-of-type', 2);
$this->assertEquals("*/h1[position() = last() - 2]", (string) $function->toXpath(), '->toXpath() returns the xpath representation of the node');
$this->assertEquals("*/h1[position() = last() - 1]", (string) $function->toXpath(), '->toXpath() returns the xpath representation of the node');

/*
// h1:not(p)
Expand Down