-
-
Notifications
You must be signed in to change notification settings - Fork 9.5k
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
[CssSelector] Replace "name()" by "local-name()" to make it works in Firefox (XPath) #21011
Conversation
BR0kEN-
commented
Dec 21, 2016
Q | A |
---|---|
Branch? | 2.7 |
Bug fix? | yes |
BC breaks? | no |
Deprecations? | no |
Tests pass? | yes |
Fixed tickets | #21004 |
License | MIT |
This needs to be check in detail to ensure it does not break support for namespaced names. |
@stof, I've added tests for that. |
Do we really need the attribute check? What about using |
@xabbuh, it is interesting for me as well. What do you think? |
@BR0kEN- I don't know. We should investigate why it was implemented this way initially in the Python lib the CssSelector is a port of. |
@xabbuh What I'm proposing now is to change the |
We will still have to investigate why |
@xabbuh I've not found any usage of |
@xabbuh your comment is the wrong way. We use |
@stof, is it this one https://github.com/scrapy/cssselect? |
If so, then this may be valuable: scrapy/cssselect#18 (comment) |
@BR0kEN- yes it is this one |
We can decide to continue with |
I agree once we are sure that this does not break any edge cases we did not consider yet. |
@xabbuh I think we should work on updating our component to the newer version of cssselect for 3.3. This would fix a bunch of other cases too (much better support for pseudo elements for instance) |
Given this is staling, should we consider it for master? Or how can we get confidence here? |
I did some experiments with your PR and it seems your patch doesn't provide a real fix. The unit test you've added to perform a translated XPath query from a CSS expression against an XML document that includes namespaces is a false positive. See the code below of the unit test: class TranslatorTest extends TestCase
{
/** @dataProvider getXmlNamespaceTestData */
public function testXmlNamespace($css, $value, $xpath, $namespace)
{
$translator = new Translator();
$document = new \SimpleXMLElement(file_get_contents(__DIR__.'/Fixtures/namespace.xml'));
$this->assertSame($xpath, $translator->cssToXPath($css, ''));
foreach ($document->xpath($xpath) as $element) {
$this->assertSame($namespace, $element->getNamespaces());
$this->assertSame($value, (string) $element);
}
}
public function getXmlNamespaceTestData()
{
return array(
array('#table1 td:nth-child(2)', 'Bananas 1', "*[@id = 'table1']/descendant-or-self::*/*[local-name() = 'td' and (position() = 2)]", array('' => 'http://www.w3.org/TR/html4')),
array('#table2 > tr > td:nth-child(1)', 'Apples 2', "*[@id = 'table2']/tr/*[local-name() = 'td' and (position() = 1)]", array('fruits' => 'http://www.w3schools.com/fruits')),
);
}
} And the XML document your try to parse: <?xml version="1.0" encoding="UTF-8"?>
<root xmlns:fruits="http://www.w3schools.com/fruits">
<table id="table1" xmlns="http://www.w3.org/TR/html4">
<tr>
<td>Apples 1</td>
<td>Bananas 1</td>
</tr>
</table>
<fruits:table id="table2">
<fruits:tr>
<fruits:td>Apples 2</fruits:td>
<fruits:td>Bananas 2</fruits:td>
</fruits:tr>
</fruits:table>
</root> When the unit tests runs, it successfully validates all assertions because the XPath expression successfully matches a non namespaced element. However, the unit test doesn't really pass with the second data set because it only validates the first assertion before the To me, it seems the main problem is that the |
I tried the following expressions:
|
@hhamon, impressive findings. Thanks. Didn't know that namespaced queries aren't supported at all. For second case XPath should look something like this |
Closing as this PR cannot be merged as is. Issue is still open if someone wants to take over. |
Closing as this PR cannot be merged as is. Issue is still open if someone wants to take over. |