Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Fix the :only-of-type pseudo class selector #33777

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
Oct 1, 2019
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
33 changes: 30 additions & 3 deletions 33 src/Symfony/Component/CssSelector/Tests/XPath/TranslatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ public function testXmlLang($css, array $elementsId)
$elements = $document->xpath($translator->cssToXPath($css));
$this->assertCount(\count($elementsId), $elements);
foreach ($elements as $element) {
$this->assertTrue(\in_array($element->attributes()->id, $elementsId));
$this->assertContains((string) $element->attributes()->id, $elementsId);
}
}

Expand All @@ -116,7 +116,7 @@ public function testHtmlIds($css, array $elementsId)
$this->assertCount(\count($elementsId), $elementsId);
foreach ($elements as $element) {
if (null !== $element->attributes()->id) {
$this->assertTrue(\in_array($element->attributes()->id, $elementsId));
$this->assertContains((string) $element->attributes()->id, $elementsId);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not strictly related, but it improves an error message shown when the test fails. Before the change, the message would say "expected true, got false". Now it will indicate what's the missing id.

}
}
libxml_clear_errors();
Expand All @@ -137,6 +137,33 @@ public function testHtmlShakespear($css, $count)
$this->assertCount($count, $elements);
}

public function testOnlyOfTypeFindsSingleChildrenOfGivenType()
{
$translator = new Translator();
$translator->registerExtension(new HtmlExtension($translator));
$document = new \DOMDocument();
$document->loadHTML(<<<'HTML'
<html>
<body>
<p>
<span>A</span>
</p>
<p>
<span>B</span>
<span>C</span>
</p>
</body>
</html>
HTML
);

$xpath = new \DOMXPath($document);
$nodeList = $xpath->query($translator->cssToXPath('span:only-of-type'));

$this->assertSame(1, $nodeList->length);
$this->assertSame('A', $nodeList->item(0)->textContent);
}

public function getXpathLiteralTestData()
{
return [
Expand Down Expand Up @@ -175,7 +202,7 @@ public function getCssToXPathTestData()
['e:first-of-type', '*/e[position() = 1]'],
['e:last-of-type', '*/e[position() = last()]'],
['e:only-child', "*/*[(name() = 'e') and (last() = 1)]"],
['e:only-of-type', 'e[last() = 1]'],
['e:only-of-type', 'e[count(preceding-sibling::e)=0 and count(following-sibling::e)=0]'],
['e:empty', 'e[not(*) and not(string-length())]'],
['e:EmPTY', 'e[not(*) and not(string-length())]'],
['e:root', 'e[not(parent::*)]'],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,11 +123,13 @@ public function translateOnlyChild(XPathExpr $xpath)
*/
public function translateOnlyOfType(XPathExpr $xpath)
{
if ('*' === $xpath->getElement()) {
$element = $xpath->getElement();

if ('*' === $element) {
throw new ExpressionErrorException('"*:only-of-type" is not implemented.');
}

return $xpath->addCondition('last() = 1');
return $xpath->addCondition(sprintf('count(preceding-sibling::%s)=0 and count(following-sibling::%s)=0', $element, $element));
}

/**
Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.