Skip to content

Commit f4b3ff6

Browse files
Merge branch '3.4' into 4.3
* 3.4: Sync Twig templateExists behaviors Fix the :only-of-type pseudo class selector [Serializer] Add CsvEncoder tests for PHP 7.4 Copy phpunit.xsd to a predictable path [Security/Http] fix parsing X509 emailAddress [Serializer] fix denormalization of string-arrays with only one element #33731 [Cache] fix known tag versions ttl check
2 parents c6e5e2a + f819f71 commit f4b3ff6

File tree

2 files changed

+34
-5
lines changed

2 files changed

+34
-5
lines changed

Diff for: Tests/XPath/TranslatorTest.php

+30-3
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::*)]'],

Diff for: XPath/Extension/PseudoClassExtension.php

+4-2
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)