Skip to content

Commit

Permalink
Merge pull request #24 from alepeino/any-ns-css-selector
Browse files Browse the repository at this point in the history
FluidXML supports CSS 'any namespace' selector.

Thanks to @alepeino.
  • Loading branch information
daniele-orlando authored Jan 31, 2017
2 parents 3c3d9c1 + 5028c04 commit b6fb8de
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 23 deletions.
7 changes: 7 additions & 0 deletions source/FluidXml/CssTranslator.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,13 @@ class CssTranslator
'\1:',
'NS',
false ],
// *|A
[ '\* # Namespace wildcard
\|
(\w+)',
'*[local-name() = \'\1\']',
'NS',
false ],
// :root
[ ':root\b',
'/*',
Expand Down
123 changes: 100 additions & 23 deletions specs/FluidXml.php
Original file line number Diff line number Diff line change
Expand Up @@ -2826,81 +2826,142 @@ function addchild($parent, $i)

describe('CssTranslator', function () {
describe('.xpath()', function () {
$hml = new FluidXml([ 'html' => [ 'body' => [ 'div' =>
[ 'p' => [ '@class' => 'a', '@id' => '123', [ 'span' ] ],
'h1' => [ '@class' => 'b' ],
'p' => [ '@class' => 'a b' ],
'p' => [ '@class' => 'a' ] ]
]]]);
$hml = new FluidXml([ 'html' => [
'body' => [
'div' => [
[ 'p' => [ '@class' => 'a', '@id' => '123', [ 'span' ] ] ],
[ 'h1' => [ '@class' => 'b' ] ],
[ 'shape' => [ '@class' => 'c' ] ],
[ 'p' => [ '@class' => 'a b' ] ],
[ 'p' => [ '@class' => 'a' ] ],
[ 'span' => [ '@class' => 'b' ] ],
]]]]);

$hml->namespace('svg', 'http://svg.org');
$hml->query('//body')
->add('svg', true)
->add('shape');
->add('svg:svg', true)
->add('svg:shape')
->add('svg:shape');

it('should support the CSS selector A', function () use ($hml) {
$actual = $hml->query('p')->array();
$expected = $hml->query('.//p')->array();
$expected = $hml->query('//p')->array();
\assert($actual === $expected, __($actual, $expected));

$expected = $hml->query('//p')->array();
$actual = $hml->query('p')->size();
$expected = 3;
\assert($actual === $expected, __($actual, $expected));
});

it('should support the CSS selector ns|A', function () use ($hml) {
$actual = $hml->query('svg|shape')->array();
$expected = $hml->query('//svg:shape')->array();
\assert($actual === $expected, __($actual, $expected));

$actual = $hml->query('svg|shape')->size();
$expected = 2;
\assert($actual === $expected, __($actual, $expected));
});

it('should support the CSS selector *|A', function () use ($hml) {
$actual = $hml->query('*|shape')->array();
$expected = $hml->query('[local-name() = "shape"]')->array();
\assert($actual === $expected, __($actual, $expected));

$actual = $hml->query('*|shape')->size();
$expected = 3;
\assert($actual === $expected, __($actual, $expected));
});

it('should support the CSS selector :root', function () use ($hml) {
$actual = $hml->query(':root')->array();
$expected = $hml->query('/*')->array();
\assert($actual === $expected, __($actual, $expected));

$actual = $hml->query(':root')->size();
$expected = 1;
\assert($actual === $expected, __($actual, $expected));
});

it('should support the CSS selector #id', function () use ($hml) {
$actual = $hml->query('#123')->array();
$expected = $hml->query('//*[@id="123"]')->array();
\assert($actual === $expected, __($actual, $expected));

$actual = $hml->query('#123')->size();
$expected = 1;
\assert($actual === $expected, __($actual, $expected));
});

it('should support the CSS selector .class.class', function () use ($hml) {
$actual = $hml->query('.a')->array();
$expected = $hml->query('//p')->array();
\assert($actual === $expected, __($actual, $expected));

$actual = $hml->query('.a')->size();
$expected = 3;
\assert($actual === $expected, __($actual, $expected));

$actual = $hml->query('.a.b')->array();
$expected = $hml->query('//p[2]')->array();
\assert($actual === $expected, __($actual, $expected));

$actual = $hml->query('.a.b')->size();
$expected = 1;
\assert($actual === $expected, __($actual, $expected));

$actual = $hml->query('h1.b')->array();
$expected = $hml->query('//h1')->array();
\assert($actual === $expected, __($actual, $expected));

$actual = $hml->query('h1.b')->size();
$expected = 1;
\assert($actual === $expected, __($actual, $expected));
});

it('should support the CSS selector [attr]', function () use ($hml) {
$actual = $hml->query('[class]')->array();
$expected = $hml->query('//div//*')->array();
$expected = $hml->query('//div/*')->array();
\assert($actual === $expected, __($actual, $expected));

$actual = $hml->query('[class]')->size();
$expected = 6;
\assert($actual === $expected, __($actual, $expected));

$actual = $hml->query('[id]')->array();
$expected = $hml->query('//*[@id]')->array();
\assert($actual === $expected, __($actual, $expected));
});

it('should support the CSS selector [attr="val"]', function () use ($hml) {
$actual = $hml->query('[id="123"]')->array();
$expected = $hml->query('//*[@id]')->array();
$actual = $hml->query('[id]')->size();
$expected = 1;
\assert($actual === $expected, __($actual, $expected));
});

it('should support the CSS selector [attr="val"]', function () use ($hml) {
$actual = $hml->query('p[id="123"]')->array();
$expected = $hml->query('//p[@id]')->array();
\assert($actual === $expected, __($actual, $expected));

$actual = $hml->query('p[id="123"]')->size();
$expected = 1;
\assert($actual === $expected, __($actual, $expected));

$actual = $hml->query('[class="a"]')->array();
$expected = $hml->query('//*[@class="a"]')->array();
\assert($actual === $expected, __($actual, $expected));

$actual = $hml->query('[class="a"]')->size();
$expected = 2;
\assert($actual === $expected, __($actual, $expected));
});

it('should support the CSS selector A B', function () use ($hml) {
$actual = $hml->query('body p')->array();
$expected = $hml->query('//body//p')->array();
$actual = $hml->query('div span')->array();
$expected = $hml->query('//div//span')->array();
\assert($actual === $expected, __($actual, $expected));

$actual = $hml->query('div span')->size();
$expected = 2;
\assert($actual === $expected, __($actual, $expected));
});

Expand All @@ -2909,32 +2970,48 @@ function addchild($parent, $i)
$expected = $hml->query('//div/p')->array();
\assert($actual === $expected, __($actual, $expected));

$actual = $hml->query('body > p')->array();
$expected = $hml->query('//body/p')->array();
$actual = $hml->query('div > p')->size();
$expected = 3;
\assert($actual === $expected, __($actual, $expected));
});

it('should support the CSS selector A, B', function () use ($hml) {
$actual = $hml->query('p, div')->array();
$expected = $hml->query('//p|//div')->array();
\assert($actual === $expected, __($actual, $expected));

$actual = $hml->query('p, div')->size();
$expected = 4;
\assert($actual === $expected, __($actual, $expected));
});

it('should support the CSS selector A + B', function () use ($hml) {
$actual = $hml->query('p + p')->array();
$expected = $hml->query('//p[2]', '//p[3]')->array();
$expected = $hml->query('//p[3]')->array();
\assert($actual === $expected, __($actual, $expected));

$actual = $hml->query('p + p')->size();
$expected = 1;
\assert($actual === $expected, __($actual, $expected));
});

it('should support the CSS selector A ~ B', function () use ($hml) {
$actual = $hml->query('p ~ h1')->array();
$expected = $hml->query('//h1')->array();
$actual = $hml->query('h1 ~ p')->array();
$expected = $hml->query('//p[2]|//p[3]')->array();
\assert($actual === $expected, __($actual, $expected));

$actual = $hml->query('h1 ~ p')->size();
$expected = 2;
\assert($actual === $expected, __($actual, $expected));
});

it('should support mixing CSS selectors :root #123 span, div, :root .a', function () use ($hml) {
$actual = $hml->query(':root #123 span, div, :root .a')->array();
$expected = $hml->query('//span', '//div', '//p')->array();
$expected = $hml->query('//p/span|//div|//*[@class="a"]|//*[@class="a b"]')->array();
\assert($actual === $expected, __($actual, $expected));

$actual = $hml->query(':root #123 span, div, :root .a')->size();
$expected = 5;
\assert($actual === $expected, __($actual, $expected));
});
});
Expand Down

0 comments on commit b6fb8de

Please sign in to comment.