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

Commit c2a9bf0

Browse filesBrowse files
jakzalfabpot
authored andcommitted
Fix the :only-of-type pseudo class selector
1 parent a2cd56c commit c2a9bf0
Copy full SHA for c2a9bf0

File tree

Expand file treeCollapse file tree

2 files changed

+34
-5
lines changed
Filter options
Expand file treeCollapse file tree

2 files changed

+34
-5
lines changed

‎src/Symfony/Component/CssSelector/Tests/XPath/TranslatorTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/CssSelector/Tests/XPath/TranslatorTest.php
+30-3Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ public function testXmlLang($css, array $elementsId)
9898
$elements = $document->xpath($translator->cssToXPath($css));
9999
$this->assertCount(\count($elementsId), $elements);
100100
foreach ($elements as $element) {
101-
$this->assertTrue(\in_array($element->attributes()->id, $elementsId));
101+
$this->assertContains((string) $element->attributes()->id, $elementsId);
102102
}
103103
}
104104

@@ -116,7 +116,7 @@ public function testHtmlIds($css, array $elementsId)
116116
$this->assertCount(\count($elementsId), $elementsId);
117117
foreach ($elements as $element) {
118118
if (null !== $element->attributes()->id) {
119-
$this->assertTrue(\in_array($element->attributes()->id, $elementsId));
119+
$this->assertContains((string) $element->attributes()->id, $elementsId);
120120
}
121121
}
122122
libxml_clear_errors();
@@ -137,6 +137,33 @@ public function testHtmlShakespear($css, $count)
137137
$this->assertCount($count, $elements);
138138
}
139139

140+
public function testOnlyOfTypeFindsSingleChildrenOfGivenType()
141+
{
142+
$translator = new Translator();
143+
$translator->registerExtension(new HtmlExtension($translator));
144+
$document = new \DOMDocument();
145+
$document->loadHTML(<<<'HTML'
146+
<html>
147+
<body>
148+
<p>
149+
<span>A</span>
150+
</p>
151+
<p>
152+
<span>B</span>
153+
<span>C</span>
154+
</p>
155+
</body>
156+
</html>
157+
HTML
158+
);
159+
160+
$xpath = new \DOMXPath($document);
161+
$nodeList = $xpath->query($translator->cssToXPath('span:only-of-type'));
162+
163+
$this->assertSame(1, $nodeList->length);
164+
$this->assertSame('A', $nodeList->item(0)->textContent);
165+
}
166+
140167
public function getXpathLiteralTestData()
141168
{
142169
return [
@@ -175,7 +202,7 @@ public function getCssToXPathTestData()
175202
['e:first-of-type', '*/e[position() = 1]'],
176203
['e:last-of-type', '*/e[position() = last()]'],
177204
['e:only-child', "*/*[(name() = 'e') and (last() = 1)]"],
178-
['e:only-of-type', 'e[last() = 1]'],
205+
['e:only-of-type', 'e[count(preceding-sibling::e)=0 and count(following-sibling::e)=0]'],
179206
['e:empty', 'e[not(*) and not(string-length())]'],
180207
['e:EmPTY', 'e[not(*) and not(string-length())]'],
181208
['e:root', 'e[not(parent::*)]'],

‎src/Symfony/Component/CssSelector/XPath/Extension/PseudoClassExtension.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/CssSelector/XPath/Extension/PseudoClassExtension.php
+4-2Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,11 +123,13 @@ public function translateOnlyChild(XPathExpr $xpath)
123123
*/
124124
public function translateOnlyOfType(XPathExpr $xpath)
125125
{
126-
if ('*' === $xpath->getElement()) {
126+
$element = $xpath->getElement();
127+
128+
if ('*' === $element) {
127129
throw new ExpressionErrorException('"*:only-of-type" is not implemented.');
128130
}
129131

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

133135
/**

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.