Skip to content

Commit 0d70858

Browse files
committed
DomQuery: searchs from current node
1 parent c5eff68 commit 0d70858

File tree

3 files changed

+33
-29
lines changed

3 files changed

+33
-29
lines changed

src/Framework/DomQuery.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ public function has(string $selector): bool
7878
*/
7979
public static function css2xpath(string $css): string
8080
{
81-
$xpath = '//*';
81+
$xpath = './/*';
8282
preg_match_all('/
8383
([#.:]?)([a-z][a-z0-9_-]*)| # id, class, pseudoclass (1,2)
8484
\[

tests/Framework/DomQuery.css2Xpath.phpt

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -8,73 +8,73 @@ use Tester\DomQuery;
88
require __DIR__ . '/../bootstrap.php';
99

1010
test('type selectors', function () {
11-
Assert::same('//*', DomQuery::css2xpath('*'));
12-
Assert::same('//foo', DomQuery::css2xpath('foo'));
11+
Assert::same('.//*', DomQuery::css2xpath('*'));
12+
Assert::same('.//foo', DomQuery::css2xpath('foo'));
1313
});
1414

1515

1616
test('#ID', function () {
17-
Assert::same("//*[@id='foo']", DomQuery::css2xpath('#foo'));
18-
Assert::same("//*[@id='id']", DomQuery::css2xpath('*#id'));
17+
Assert::same(".//*[@id='foo']", DomQuery::css2xpath('#foo'));
18+
Assert::same(".//*[@id='id']", DomQuery::css2xpath('*#id'));
1919
});
2020

2121

2222
test('class', function () {
23-
Assert::same("//*[contains(concat(' ', normalize-space(@class), ' '), ' foo ')]", DomQuery::css2xpath('.foo'));
24-
Assert::same("//*[contains(concat(' ', normalize-space(@class), ' '), ' foo ')]", DomQuery::css2xpath('*.foo'));
25-
Assert::same("//*[contains(concat(' ', normalize-space(@class), ' '), ' foo ')][contains(concat(' ', normalize-space(@class), ' '), ' bar ')]", DomQuery::css2xpath('.foo.bar'));
23+
Assert::same(".//*[contains(concat(' ', normalize-space(@class), ' '), ' foo ')]", DomQuery::css2xpath('.foo'));
24+
Assert::same(".//*[contains(concat(' ', normalize-space(@class), ' '), ' foo ')]", DomQuery::css2xpath('*.foo'));
25+
Assert::same(".//*[contains(concat(' ', normalize-space(@class), ' '), ' foo ')][contains(concat(' ', normalize-space(@class), ' '), ' bar ')]", DomQuery::css2xpath('.foo.bar'));
2626
});
2727

2828

2929
test('attribute selectors', function () {
30-
Assert::same('//div[@foo]', DomQuery::css2xpath('div[foo]'));
31-
Assert::same("//div[@foo='bar']", DomQuery::css2xpath('div[foo=bar]'));
32-
Assert::same("//*[@foo='bar']", DomQuery::css2xpath('[foo="bar"]'));
33-
Assert::same("//div[@foo='bar']", DomQuery::css2xpath('div[foo="bar"]'));
34-
Assert::same("//div[@foo='bar']", DomQuery::css2xpath("div[foo='bar']"));
35-
Assert::same("//div[@foo='bar']", DomQuery::css2xpath('div[Foo="bar"]'));
36-
Assert::same("//div[contains(concat(' ', normalize-space(@foo), ' '), ' bar ')]", DomQuery::css2xpath('div[foo~="bar"]'));
37-
Assert::same("//div[contains(@foo, 'bar')]", DomQuery::css2xpath('div[foo*="bar"]'));
38-
Assert::same("//div[starts-with(@foo, 'bar')]", DomQuery::css2xpath('div[foo^="bar"]'));
39-
Assert::same("//div[substring(@foo, string-length(@foo)-0)='bar']", DomQuery::css2xpath('div[foo$="bar"]'));
40-
Assert::same("//div[@foo='bar[]']", DomQuery::css2xpath("div[foo='bar[]']"));
41-
Assert::same("//div[@foo='bar[]']", DomQuery::css2xpath('div[foo="bar[]"]'));
30+
Assert::same('.//div[@foo]', DomQuery::css2xpath('div[foo]'));
31+
Assert::same(".//div[@foo='bar']", DomQuery::css2xpath('div[foo=bar]'));
32+
Assert::same(".//*[@foo='bar']", DomQuery::css2xpath('[foo="bar"]'));
33+
Assert::same(".//div[@foo='bar']", DomQuery::css2xpath('div[foo="bar"]'));
34+
Assert::same(".//div[@foo='bar']", DomQuery::css2xpath("div[foo='bar']"));
35+
Assert::same(".//div[@foo='bar']", DomQuery::css2xpath('div[Foo="bar"]'));
36+
Assert::same(".//div[contains(concat(' ', normalize-space(@foo), ' '), ' bar ')]", DomQuery::css2xpath('div[foo~="bar"]'));
37+
Assert::same(".//div[contains(@foo, 'bar')]", DomQuery::css2xpath('div[foo*="bar"]'));
38+
Assert::same(".//div[starts-with(@foo, 'bar')]", DomQuery::css2xpath('div[foo^="bar"]'));
39+
Assert::same(".//div[substring(@foo, string-length(@foo)-0)='bar']", DomQuery::css2xpath('div[foo$="bar"]'));
40+
Assert::same(".//div[@foo='bar[]']", DomQuery::css2xpath("div[foo='bar[]']"));
41+
Assert::same(".//div[@foo='bar[]']", DomQuery::css2xpath('div[foo="bar[]"]'));
4242
});
4343

4444

4545
test('variants', function () {
46-
Assert::same("//*[@id='foo']|//*[@id='bar']", DomQuery::css2xpath('#foo, #bar'));
47-
Assert::same("//*[@id='foo']|//*[@id='bar']", DomQuery::css2xpath('#foo,#bar'));
48-
Assert::same("//*[@id='foo']|//*[@id='bar']", DomQuery::css2xpath('#foo ,#bar'));
46+
Assert::same(".//*[@id='foo']|//*[@id='bar']", DomQuery::css2xpath('#foo, #bar'));
47+
Assert::same(".//*[@id='foo']|//*[@id='bar']", DomQuery::css2xpath('#foo,#bar'));
48+
Assert::same(".//*[@id='foo']|//*[@id='bar']", DomQuery::css2xpath('#foo ,#bar'));
4949
});
5050

5151

5252
test('descendant combinator', function () {
5353
Assert::same(
54-
"//div[@id='foo']//*[contains(concat(' ', normalize-space(@class), ' '), ' bar ')]",
54+
".//div[@id='foo']//*[contains(concat(' ', normalize-space(@class), ' '), ' bar ')]",
5555
DomQuery::css2xpath('div#foo .bar'),
5656
);
5757
Assert::same(
58-
'//div//*//p',
58+
'.//div//*//p',
5959
DomQuery::css2xpath('div * p'),
6060
);
6161
});
6262

6363

6464
test('child combinator', function () {
65-
Assert::same("//div[@id='foo']/span", DomQuery::css2xpath('div#foo>span'));
66-
Assert::same("//div[@id='foo']/span", DomQuery::css2xpath('div#foo > span'));
65+
Assert::same(".//div[@id='foo']/span", DomQuery::css2xpath('div#foo>span'));
66+
Assert::same(".//div[@id='foo']/span", DomQuery::css2xpath('div#foo > span'));
6767
});
6868

6969

7070
test('general sibling combinator', function () {
71-
Assert::same('//div/following-sibling::span', DomQuery::css2xpath('div ~ span'));
71+
Assert::same('.//div/following-sibling::span', DomQuery::css2xpath('div ~ span'));
7272
});
7373

7474

7575
test('complex', function () {
7676
Assert::same(
77-
"//div[@id='foo']//span[contains(concat(' ', normalize-space(@class), ' '), ' bar ')]"
77+
".//div[@id='foo']//span[contains(concat(' ', normalize-space(@class), ' '), ' bar ')]"
7878
. "|//*[@id='bar']//li[contains(concat(' ', normalize-space(@class), ' '), ' baz ')]//a",
7979
DomQuery::css2xpath('div#foo span.bar, #bar li.baz a'),
8080
);

tests/Framework/DomQuery.fromHtml.phpt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ Assert::false($q->has('track footer'));
2626
$q = DomQuery::fromHtml('<audio controls><source src="horse.mp3" type="audio/mpeg"></audio>');
2727
Assert::true($q->has('source'));
2828

29+
Assert::count(1, $q->find('audio'));
30+
Assert::count(1, $q->find('audio')[0]->find('source'));
31+
Assert::count(0, $q->find('audio')[0]->find('audio'));
32+
2933

3034
$q = DomQuery::fromHtml("<script>\nvar s = '</div>';</script> <br> <script type='text/javascript'>var s = '</div>';\n</script>");
3135
Assert::true($q->has('script'));

0 commit comments

Comments
 (0)