@@ -98,7 +98,7 @@ public function testXmlLang($css, array $elementsId)
98
98
$ elements = $ document ->xpath ($ translator ->cssToXPath ($ css ));
99
99
$ this ->assertCount (\count ($ elementsId ), $ elements );
100
100
foreach ($ elements as $ element ) {
101
- $ this ->assertTrue ( \in_array ( $ element ->attributes ()->id , $ elementsId) );
101
+ $ this ->assertContains (( string ) $ element ->attributes ()->id , $ elementsId );
102
102
}
103
103
}
104
104
@@ -116,7 +116,7 @@ public function testHtmlIds($css, array $elementsId)
116
116
$ this ->assertCount (\count ($ elementsId ), $ elementsId );
117
117
foreach ($ elements as $ element ) {
118
118
if (null !== $ element ->attributes ()->id ) {
119
- $ this ->assertTrue ( \in_array ( $ element ->attributes ()->id , $ elementsId) );
119
+ $ this ->assertContains (( string ) $ element ->attributes ()->id , $ elementsId );
120
120
}
121
121
}
122
122
libxml_clear_errors ();
@@ -137,6 +137,33 @@ public function testHtmlShakespear($css, $count)
137
137
$ this ->assertCount ($ count , $ elements );
138
138
}
139
139
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
+
140
167
public function getXpathLiteralTestData ()
141
168
{
142
169
return [
@@ -175,7 +202,7 @@ public function getCssToXPathTestData()
175
202
['e:first-of-type ' , '*/e[position() = 1] ' ],
176
203
['e:last-of-type ' , '*/e[position() = last()] ' ],
177
204
['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 ] ' ],
179
206
['e:empty ' , 'e[not(*) and not(string-length())] ' ],
180
207
['e:EmPTY ' , 'e[not(*) and not(string-length())] ' ],
181
208
['e:root ' , 'e[not(parent::*)] ' ],
0 commit comments