Skip to content

Commit f4563c3

Browse files
committed
Add a non-static API for the CssSelector component
1 parent 078f953 commit f4563c3

File tree

9 files changed

+225
-42
lines changed

9 files changed

+225
-42
lines changed

src/Symfony/Component/CssSelector/CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
CHANGELOG
22
=========
33

4+
2.8.0
5+
-----
6+
7+
* Added the ConverterInterface and the Converter implementation as a non-static API for the component.
8+
* Deprecated the `CssSelector` static API of the component.
9+
410
2.1.0
511
-----
612

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\CssSelector;
13+
14+
use Symfony\Component\CssSelector\Parser\Shortcut\ClassParser;
15+
use Symfony\Component\CssSelector\Parser\Shortcut\ElementParser;
16+
use Symfony\Component\CssSelector\Parser\Shortcut\EmptyStringParser;
17+
use Symfony\Component\CssSelector\Parser\Shortcut\HashParser;
18+
use Symfony\Component\CssSelector\XPath\Extension\HtmlExtension;
19+
use Symfony\Component\CssSelector\XPath\Translator;
20+
21+
/**
22+
* @author Christophe Coevoet <stof@notk.org>
23+
*
24+
* @api
25+
*/
26+
class Converter implements ConverterInterface
27+
{
28+
private $translator;
29+
30+
/**
31+
* @param bool $html Whether HTML support should be enabled. Disable it for XML documents.
32+
*/
33+
public function __construct($html = true)
34+
{
35+
$this->translator = new Translator();
36+
37+
if ($html) {
38+
$this->translator->registerExtension(new HtmlExtension($this->translator));
39+
}
40+
41+
$this->translator
42+
->registerParserShortcut(new EmptyStringParser())
43+
->registerParserShortcut(new ElementParser())
44+
->registerParserShortcut(new ClassParser())
45+
->registerParserShortcut(new HashParser())
46+
;
47+
}
48+
49+
/**
50+
* {@inheritdoc}
51+
*/
52+
public function toXPath($cssExpr, $prefix = 'descendant-or-self::')
53+
{
54+
return $this->translator->cssToXPath($cssExpr, $prefix);
55+
}
56+
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\CssSelector;
13+
14+
/**
15+
* ConverterInterface is the main entry point of the component and can convert CSS
16+
* selectors to XPath expressions.
17+
*
18+
* This component is a port of the Python cssselect library,
19+
* which is copyright Ian Bicking, @see https://github.com/SimonSapin/cssselect.
20+
*
21+
* Copyright (c) 2007-2012 Ian Bicking and contributors. See AUTHORS
22+
* for more details.
23+
*
24+
* All rights reserved.
25+
*
26+
* Redistribution and use in source and binary forms, with or without
27+
* modification, are permitted provided that the following conditions are
28+
* met:
29+
*
30+
* 1. Redistributions of source code must retain the above copyright
31+
* notice, this list of conditions and the following disclaimer.
32+
*
33+
* 2. Redistributions in binary form must reproduce the above copyright
34+
* notice, this list of conditions and the following disclaimer in
35+
* the documentation and/or other materials provided with the
36+
* distribution.
37+
*
38+
* 3. Neither the name of Ian Bicking nor the names of its contributors may
39+
* be used to endorse or promote products derived from this software
40+
* without specific prior written permission.
41+
*
42+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
43+
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
44+
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
45+
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL IAN BICKING OR
46+
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
47+
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
48+
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
49+
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
50+
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
51+
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
52+
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
53+
*
54+
* @author Christophe Coevoet <stof@notk.org>
55+
*
56+
* @api
57+
*/
58+
interface ConverterInterface
59+
{
60+
/**
61+
* Translates a CSS expression to its XPath equivalent.
62+
*
63+
* Optionally, a prefix can be added to the resulting XPath
64+
* expression with the $prefix parameter.
65+
*
66+
* @param string $cssExpr The CSS expression.
67+
* @param string $prefix An optional prefix for the XPath expression.
68+
*
69+
* @return string
70+
*
71+
* @api
72+
*/
73+
public function toXPath($cssExpr, $prefix = 'descendant-or-self::');
74+
75+
}

src/Symfony/Component/CssSelector/CssSelector.php

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,7 @@
1111

1212
namespace Symfony\Component\CssSelector;
1313

14-
use Symfony\Component\CssSelector\Parser\Shortcut\ClassParser;
15-
use Symfony\Component\CssSelector\Parser\Shortcut\ElementParser;
16-
use Symfony\Component\CssSelector\Parser\Shortcut\EmptyStringParser;
17-
use Symfony\Component\CssSelector\Parser\Shortcut\HashParser;
18-
use Symfony\Component\CssSelector\XPath\Extension\HtmlExtension;
19-
use Symfony\Component\CssSelector\XPath\Translator;
14+
@trigger_error('The '.__NAMESPACE__.'\CssSelector class is deprecated since version 2.8 and will be removed in 3.0. Use directly the \Symfony\Component\CssSelector\Converter class instead.', E_USER_DEPRECATED);
2015

2116
/**
2217
* CssSelector is the main entry point of the component and can convert CSS
@@ -62,6 +57,8 @@
6257
*
6358
* @author Fabien Potencier <fabien@symfony.com>
6459
*
60+
* @deprecated as of 2.8, will be removed in 3.0. Use the \Symfony\Component\CssSelector\Converter class instead.
61+
*
6562
* @api
6663
*/
6764
class CssSelector
@@ -82,20 +79,9 @@ class CssSelector
8279
*/
8380
public static function toXPath($cssExpr, $prefix = 'descendant-or-self::')
8481
{
85-
$translator = new Translator();
86-
87-
if (self::$html) {
88-
$translator->registerExtension(new HtmlExtension($translator));
89-
}
90-
91-
$translator
92-
->registerParserShortcut(new EmptyStringParser())
93-
->registerParserShortcut(new ElementParser())
94-
->registerParserShortcut(new ClassParser())
95-
->registerParserShortcut(new HashParser())
96-
;
82+
$converter = new Converter(self::$html);
9783

98-
return $translator->cssToXPath($cssExpr, $prefix);
84+
return $converter->toXPath($cssExpr, $prefix);
9985
}
10086

10187
/**
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\CssSelector\Tests;
13+
14+
use Symfony\Component\CssSelector\Converter;
15+
16+
class ConverterTest extends \PHPUnit_Framework_TestCase
17+
{
18+
public function testCssToXPath()
19+
{
20+
$converter = new Converter();
21+
22+
$this->assertEquals('descendant-or-self::*', $converter->toXPath(''));
23+
$this->assertEquals('descendant-or-self::h1', $converter->toXPath('h1'));
24+
$this->assertEquals("descendant-or-self::h1[@id = 'foo']", $converter->toXPath('h1#foo'));
25+
$this->assertEquals("descendant-or-self::h1[@class and contains(concat(' ', normalize-space(@class), ' '), ' foo ')]", $converter->toXPath('h1.foo'));
26+
$this->assertEquals('descendant-or-self::foo:h1', $converter->toXPath('foo|h1'));
27+
$this->assertEquals('descendant-or-self::h1', $converter->toXPath('H1'));
28+
}
29+
30+
public function testCssToXPathXml()
31+
{
32+
$converter = new Converter(false);
33+
34+
$this->assertEquals('descendant-or-self::H1', $converter->toXPath('H1'));
35+
}
36+
}

src/Symfony/Component/CssSelector/Tests/CssSelectorTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313

1414
use Symfony\Component\CssSelector\CssSelector;
1515

16+
/**
17+
* @group legacy
18+
*/
1619
class CssSelectorTest extends \PHPUnit_Framework_TestCase
1720
{
1821
public function testCssToXPath()

0 commit comments

Comments
 (0)