Merge branch '2.7' into 2.8
* 2.7: [DomCrawler] Optimize DomCrawler::relativize() [HttpKernel] Fix source links with latests Twig versions [DomCrawler] Allow pipe (|) character in link tags when using Xpath expressions
This commit is contained in:
commit
5b566dae1b
|
@ -990,22 +990,47 @@ class Crawler extends \SplObjectStorage
|
||||||
{
|
{
|
||||||
$expressions = array();
|
$expressions = array();
|
||||||
|
|
||||||
$unionPattern = '/\|(?![^\[]*\])/';
|
|
||||||
// An expression which will never match to replace expressions which cannot match in the crawler
|
// An expression which will never match to replace expressions which cannot match in the crawler
|
||||||
// We cannot simply drop
|
// We cannot simply drop
|
||||||
$nonMatchingExpression = 'a[name() = "b"]';
|
$nonMatchingExpression = 'a[name() = "b"]';
|
||||||
|
|
||||||
// Split any unions into individual expressions.
|
$xpathLen = strlen($xpath);
|
||||||
foreach (preg_split($unionPattern, $xpath) as $expression) {
|
$openedBrackets = 0;
|
||||||
$expression = trim($expression);
|
$startPosition = strspn($xpath, " \t\n\r\0\x0B");
|
||||||
$parenthesis = '';
|
|
||||||
|
|
||||||
// If the union is inside some braces, we need to preserve the opening braces and apply
|
for ($i = $startPosition; $i <= $xpathLen; ++$i) {
|
||||||
// the change only inside it.
|
$i += strcspn($xpath, '"\'[]|', $i);
|
||||||
if (preg_match('/^[\(\s*]+/', $expression, $matches)) {
|
|
||||||
$parenthesis = $matches[0];
|
if ($i < $xpathLen) {
|
||||||
$expression = substr($expression, strlen($parenthesis));
|
switch ($xpath[$i]) {
|
||||||
|
case '"':
|
||||||
|
case "'":
|
||||||
|
if (false === $i = strpos($xpath, $xpath[$i], $i + 1)) {
|
||||||
|
return $xpath; // The XPath expression is invalid
|
||||||
|
}
|
||||||
|
continue 2;
|
||||||
|
case '[':
|
||||||
|
++$openedBrackets;
|
||||||
|
continue 2;
|
||||||
|
case ']':
|
||||||
|
--$openedBrackets;
|
||||||
|
continue 2;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
if ($openedBrackets) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($startPosition < $xpathLen && '(' === $xpath[$startPosition]) {
|
||||||
|
// If the union is inside some braces, we need to preserve the opening braces and apply
|
||||||
|
// the change only inside it.
|
||||||
|
$j = 1 + strspn($xpath, "( \t\n\r\0\x0B", $startPosition + 1);
|
||||||
|
$parenthesis = substr($xpath, $startPosition, $j);
|
||||||
|
$startPosition += $j;
|
||||||
|
} else {
|
||||||
|
$parenthesis = '';
|
||||||
|
}
|
||||||
|
$expression = rtrim(substr($xpath, $startPosition, $i - $startPosition));
|
||||||
|
|
||||||
// BC for Symfony 2.4 and lower were elements were adding in a fake _root parent
|
// BC for Symfony 2.4 and lower were elements were adding in a fake _root parent
|
||||||
if (0 === strpos($expression, '/_root/')) {
|
if (0 === strpos($expression, '/_root/')) {
|
||||||
|
@ -1017,7 +1042,7 @@ class Crawler extends \SplObjectStorage
|
||||||
}
|
}
|
||||||
|
|
||||||
// add prefix before absolute element selector
|
// add prefix before absolute element selector
|
||||||
if (empty($expression)) {
|
if ('' === $expression) {
|
||||||
$expression = $nonMatchingExpression;
|
$expression = $nonMatchingExpression;
|
||||||
} elseif (0 === strpos($expression, '//')) {
|
} elseif (0 === strpos($expression, '//')) {
|
||||||
$expression = 'descendant-or-self::'.substr($expression, 2);
|
$expression = 'descendant-or-self::'.substr($expression, 2);
|
||||||
|
@ -1035,7 +1060,7 @@ class Crawler extends \SplObjectStorage
|
||||||
// '.' is the fake root element in Symfony 2.4 and lower, which is excluded from results
|
// '.' is the fake root element in Symfony 2.4 and lower, which is excluded from results
|
||||||
$expression = $nonMatchingExpression;
|
$expression = $nonMatchingExpression;
|
||||||
} elseif (0 === strpos($expression, 'descendant::')) {
|
} elseif (0 === strpos($expression, 'descendant::')) {
|
||||||
$expression = 'descendant-or-self::'.substr($expression, strlen('descendant::'));
|
$expression = 'descendant-or-self::'.substr($expression, 12);
|
||||||
} elseif (preg_match('/^(ancestor|ancestor-or-self|attribute|following|following-sibling|namespace|parent|preceding|preceding-sibling)::/', $expression)) {
|
} elseif (preg_match('/^(ancestor|ancestor-or-self|attribute|following|following-sibling|namespace|parent|preceding|preceding-sibling)::/', $expression)) {
|
||||||
// the fake root has no parent, preceding or following nodes and also no attributes (even no namespace attributes)
|
// the fake root has no parent, preceding or following nodes and also no attributes (even no namespace attributes)
|
||||||
$expression = $nonMatchingExpression;
|
$expression = $nonMatchingExpression;
|
||||||
|
@ -1043,9 +1068,16 @@ class Crawler extends \SplObjectStorage
|
||||||
$expression = 'self::'.$expression;
|
$expression = 'self::'.$expression;
|
||||||
}
|
}
|
||||||
$expressions[] = $parenthesis.$expression;
|
$expressions[] = $parenthesis.$expression;
|
||||||
|
|
||||||
|
if ($i === $xpathLen) {
|
||||||
|
return implode(' | ', $expressions);
|
||||||
|
}
|
||||||
|
|
||||||
|
$i += strspn($xpath, " \t\n\r\0\x0B", $i + 1);
|
||||||
|
$startPosition = $i + 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
return implode(' | ', $expressions);
|
return $xpath; // The XPath expression is invalid
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -406,6 +406,7 @@ EOF
|
||||||
$this->assertCount(5, $crawler->filterXPath('(//a | //div)//img'));
|
$this->assertCount(5, $crawler->filterXPath('(//a | //div)//img'));
|
||||||
$this->assertCount(7, $crawler->filterXPath('((//a | //div)//img | //ul)'));
|
$this->assertCount(7, $crawler->filterXPath('((//a | //div)//img | //ul)'));
|
||||||
$this->assertCount(7, $crawler->filterXPath('( ( //a | //div )//img | //ul )'));
|
$this->assertCount(7, $crawler->filterXPath('( ( //a | //div )//img | //ul )'));
|
||||||
|
$this->assertCount(1, $crawler->filterXPath("//a[./@href][((./@id = 'Klausi|Claudiu' or normalize-space(string(.)) = 'Klausi|Claudiu' or ./@title = 'Klausi|Claudiu' or ./@rel = 'Klausi|Claudiu') or .//img[./@alt = 'Klausi|Claudiu'])]"));
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testFilterXPath()
|
public function testFilterXPath()
|
||||||
|
@ -582,7 +583,7 @@ EOF
|
||||||
|
|
||||||
$this->assertCount(0, $crawler->filterXPath('self::a'), 'The fake root node has no "real" element name');
|
$this->assertCount(0, $crawler->filterXPath('self::a'), 'The fake root node has no "real" element name');
|
||||||
$this->assertCount(0, $crawler->filterXPath('self::a/img'), 'The fake root node has no "real" element name');
|
$this->assertCount(0, $crawler->filterXPath('self::a/img'), 'The fake root node has no "real" element name');
|
||||||
$this->assertCount(9, $crawler->filterXPath('self::*/a'));
|
$this->assertCount(10, $crawler->filterXPath('self::*/a'));
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testFilter()
|
public function testFilter()
|
||||||
|
@ -1029,6 +1030,8 @@ HTML;
|
||||||
|
|
||||||
<a href="?get=param">GetLink</a>
|
<a href="?get=param">GetLink</a>
|
||||||
|
|
||||||
|
<a href="/example">Klausi|Claudiu</a>
|
||||||
|
|
||||||
<form action="foo" id="FooFormId">
|
<form action="foo" id="FooFormId">
|
||||||
<input type="text" value="TextValue" name="TextName" />
|
<input type="text" value="TextValue" name="TextName" />
|
||||||
<input type="submit" value="FooValue" name="FooName" id="FooId" />
|
<input type="submit" value="FooValue" name="FooName" id="FooId" />
|
||||||
|
|
|
@ -99,11 +99,11 @@ class DumpDataCollector extends DataCollector implements DataDumperInterface
|
||||||
} elseif (isset($trace[$i]['object']) && $trace[$i]['object'] instanceof \Twig_Template) {
|
} elseif (isset($trace[$i]['object']) && $trace[$i]['object'] instanceof \Twig_Template) {
|
||||||
$template = $trace[$i]['object'];
|
$template = $trace[$i]['object'];
|
||||||
$name = $template->getTemplateName();
|
$name = $template->getTemplateName();
|
||||||
$file = method_exists($template, 'getSourceContext') ? $template->getSourceContext()->getPath() : false;
|
|
||||||
$src = method_exists($template, 'getSourceContext') ? $template->getSourceContext()->getCode() : (method_exists($template, 'getSource') ? $template->getSource() : false);
|
$src = method_exists($template, 'getSourceContext') ? $template->getSourceContext()->getCode() : (method_exists($template, 'getSource') ? $template->getSource() : false);
|
||||||
$info = $template->getDebugInfo();
|
$info = $template->getDebugInfo();
|
||||||
if (null !== $src && isset($info[$trace[$i - 1]['line']])) {
|
if (isset($info[$trace[$i - 1]['line']])) {
|
||||||
$line = $info[$trace[$i - 1]['line']];
|
$line = $info[$trace[$i - 1]['line']];
|
||||||
|
$file = method_exists($template, 'getSourceContext') ? $template->getSourceContext()->getPath() : false;
|
||||||
|
|
||||||
if ($src) {
|
if ($src) {
|
||||||
$src = explode("\n", $src);
|
$src = explode("\n", $src);
|
||||||
|
|
Reference in New Issue