Skip to content

Commit a33c9b2

Browse files
committed
allow all kinds of chars in selector, fixes #4
1 parent 5139c77 commit a33c9b2

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

src/Rct567/DomQuery/CssToXpath.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -164,12 +164,12 @@ function ($matches) {
164164
$segment->attribute_filters[] = $matches[2];
165165
}
166166

167-
while (preg_match('/(.*)\.([a-z]+[a-z0-9\-\_]*)$/i', $segment->selector, $matches)) { // class selector
167+
while (preg_match('/(.*)\.([^\.\#]+)$/i', $segment->selector, $matches)) { // class selector
168168
$segment->selector = $matches[1];
169169
$segment->attribute_filters[] = 'class~="'.$matches[2].'"';
170170
}
171171

172-
while (preg_match('/(.*)\#([a-z]+[a-z0-9\-\_]*)$/i', $segment->selector, $matches)) { // id selector
172+
while (preg_match('/(.*)\#([^\.\#]+)$/i', $segment->selector, $matches)) { // id selector
173173
$segment->selector = $matches[1];
174174
$segment->attribute_filters[] = 'id="'.$matches[2].'"';
175175
}

tests/Rct567/DomQuery/Tests/DomQuerySelectorsTest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ public function testCssToXpath()
4040
'div :header' => '//div//*[self::h1 or self::h2 or self::h3 or self::h5 or self::h5 or self::h6]',
4141
':odd' => '//*[position() mod 2 = 0]',
4242
'.h' => '//*[contains(concat(\' \', normalize-space(@class), \' \'), \' h \')]',
43+
'.😾-_😾' => '//*[contains(concat(\' \', normalize-space(@class), \' \'), \' 😾-_😾 \')]',
4344
'.hidden' => '//*[contains(concat(\' \', normalize-space(@class), \' \'), \' hidden \')]',
4445
'.hidden-something' => '//*[contains(concat(\' \', normalize-space(@class), \' \'), \' hidden-something \')]',
4546
'a.hidden[href]' => '//a[contains(concat(\' \', normalize-space(@class), \' \'), \' hidden \')][@href]',
@@ -127,6 +128,16 @@ public function testIdSelector()
127128
$this->assertEquals(1, $dom->find('#here')->length);
128129
}
129130

131+
/*
132+
* Test emoji as id selector
133+
*/
134+
public function testEmojiIdSelector()
135+
{
136+
$dom = new DomQuery('<div><a>1</a><b id="🐝">2</b></div><a >3</a>');
137+
$this->assertEquals('2', $dom->find('#🐝')->text());
138+
$this->assertEquals(1, $dom->find('#🐝')->length);
139+
}
140+
130141
/*
131142
* Test id selector with meta character
132143
*/
@@ -228,6 +239,16 @@ public function testClassSelectorWithUnderscore()
228239
$this->assertEquals('3', $dom->find('.monkey-moon')->text());
229240
}
230241

242+
/*
243+
* Test emoji as class selector
244+
*/
245+
public function testEmojiClassSelector()
246+
{
247+
$dom = new DomQuery('<div><a>1</a><b class="🐝">2</b></div><a>3</a>');
248+
$this->assertEquals('2', $dom->find('.🐝')->text());
249+
$this->assertEquals(1, $dom->find('b.🐝')->length);
250+
}
251+
231252
/*
232253
* Test not filter selector
233254
*/

0 commit comments

Comments
 (0)