Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 47d001e

Browse files
committed
feature #15934 Add a non-static API for the CssSelector component (stof)
This PR was merged into the 2.8 branch. Discussion ---------- Add a non-static API for the CssSelector component | Q | A | ------------- | --- | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | yes | Tests pass? | yes | Fixed tickets | #15850, #8404 | License | MIT | Doc PR | todo This implements a non-static API for the CssSelector component. I decided to keep the static API too, as it is convenient when you just need a one-shot conversion (if you need lots of conversions, keeping a reference to the Converter and all its internal object graph may be faster than releasing it all the time and rebuilding it). I deprecated the global state to choose between HTML and XML conversion. The static API would always enable the HTML extension in 3.0. Dealing with XML would be done by using the Converter class. A second commit also tags all internal classes of the component as ``@internal``, as there is really no reason for a user to deal with them (btw, we already considered them fully internal in the past, as we broke BC on them in a patch release to fix memory performance of the component in the past). TODOs: - [x] Validate whether we keep the static facade to the component - [ ] send a PR on the documentation to document this new API. - [x] handle usage of the deprecated API in the DomCrawler testsuite The DomCrawler component does not use the new API yet. I will do it in a separate PR, as distinguishing between HTML and XML modes for a crawler will be easier once I deprecate the possibility to load multiple documents (which I will do tomorrow). Commits ------- 9e51279 [CssSelector] Tag all internal classes as internal ones f4563c3 Add a non-static API for the CssSelector component
2 parents 8e1af88 + 9e51279 commit 47d001e

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+309
-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 <[email protected]>
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 <[email protected]>
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 <[email protected]>
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 <[email protected]>
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 <[email protected]>
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
/**

src/Symfony/Component/CssSelector/Node/AbstractNode.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
* which is copyright Ian Bicking, @see https://github.com/SimonSapin/cssselect.
1919
*
2020
* @author Jean-François Simon <[email protected]>
21+
*
22+
* @internal
2123
*/
2224
abstract class AbstractNode implements NodeInterface
2325
{

src/Symfony/Component/CssSelector/Node/AttributeNode.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
* which is copyright Ian Bicking, @see https://github.com/SimonSapin/cssselect.
1919
*
2020
* @author Jean-François Simon <[email protected]>
21+
*
22+
* @internal
2123
*/
2224
class AttributeNode extends AbstractNode
2325
{

src/Symfony/Component/CssSelector/Node/ClassNode.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
* which is copyright Ian Bicking, @see https://github.com/SimonSapin/cssselect.
1919
*
2020
* @author Jean-François Simon <[email protected]>
21+
*
22+
* @internal
2123
*/
2224
class ClassNode extends AbstractNode
2325
{

src/Symfony/Component/CssSelector/Node/CombinedSelectorNode.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
* which is copyright Ian Bicking, @see https://github.com/SimonSapin/cssselect.
1919
*
2020
* @author Jean-François Simon <[email protected]>
21+
*
22+
* @internal
2123
*/
2224
class CombinedSelectorNode extends AbstractNode
2325
{

src/Symfony/Component/CssSelector/Node/ElementNode.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
* which is copyright Ian Bicking, @see https://github.com/SimonSapin/cssselect.
1919
*
2020
* @author Jean-François Simon <[email protected]>
21+
*
22+
* @internal
2123
*/
2224
class ElementNode extends AbstractNode
2325
{

src/Symfony/Component/CssSelector/Node/FunctionNode.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
* which is copyright Ian Bicking, @see https://github.com/SimonSapin/cssselect.
2121
*
2222
* @author Jean-François Simon <[email protected]>
23+
*
24+
* @internal
2325
*/
2426
class FunctionNode extends AbstractNode
2527
{

src/Symfony/Component/CssSelector/Node/HashNode.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
* which is copyright Ian Bicking, @see https://github.com/SimonSapin/cssselect.
1919
*
2020
* @author Jean-François Simon <[email protected]>
21+
*
22+
* @internal
2123
*/
2224
class HashNode extends AbstractNode
2325
{

src/Symfony/Component/CssSelector/Node/NegationNode.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
* which is copyright Ian Bicking, @see https://github.com/SimonSapin/cssselect.
1919
*
2020
* @author Jean-François Simon <[email protected]>
21+
*
22+
* @internal
2123
*/
2224
class NegationNode extends AbstractNode
2325
{

src/Symfony/Component/CssSelector/Node/NodeInterface.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
* which is copyright Ian Bicking, @see https://github.com/SimonSapin/cssselect.
1919
*
2020
* @author Jean-François Simon <[email protected]>
21+
*
22+
* @internal
2123
*/
2224
interface NodeInterface
2325
{

src/Symfony/Component/CssSelector/Node/PseudoNode.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
* which is copyright Ian Bicking, @see https://github.com/SimonSapin/cssselect.
1919
*
2020
* @author Jean-François Simon <[email protected]>
21+
*
22+
* @internal
2123
*/
2224
class PseudoNode extends AbstractNode
2325
{

src/Symfony/Component/CssSelector/Node/SelectorNode.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
* which is copyright Ian Bicking, @see https://github.com/SimonSapin/cssselect.
1919
*
2020
* @author Jean-François Simon <[email protected]>
21+
*
22+
* @internal
2123
*/
2224
class SelectorNode extends AbstractNode
2325
{

src/Symfony/Component/CssSelector/Node/Specificity.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
* @see http://www.w3.org/TR/selectors/#specificity
2121
*
2222
* @author Jean-François Simon <[email protected]>
23+
*
24+
* @internal
2325
*/
2426
class Specificity
2527
{

src/Symfony/Component/CssSelector/Parser/Handler/CommentHandler.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
* which is copyright Ian Bicking, @see https://github.com/SimonSapin/cssselect.
2222
*
2323
* @author Jean-François Simon <[email protected]>
24+
*
25+
* @internal
2426
*/
2527
class CommentHandler implements HandlerInterface
2628
{

src/Symfony/Component/CssSelector/Parser/Handler/HandlerInterface.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
* which is copyright Ian Bicking, @see https://github.com/SimonSapin/cssselect.
2222
*
2323
* @author Jean-François Simon <[email protected]>
24+
*
25+
* @internal
2426
*/
2527
interface HandlerInterface
2628
{

src/Symfony/Component/CssSelector/Parser/Handler/HashHandler.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
* which is copyright Ian Bicking, @see https://github.com/SimonSapin/cssselect.
2525
*
2626
* @author Jean-François Simon <[email protected]>
27+
*
28+
* @internal
2729
*/
2830
class HashHandler implements HandlerInterface
2931
{

src/Symfony/Component/CssSelector/Parser/Handler/IdentifierHandler.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
* which is copyright Ian Bicking, @see https://github.com/SimonSapin/cssselect.
2525
*
2626
* @author Jean-François Simon <[email protected]>
27+
*
28+
* @internal
2729
*/
2830
class IdentifierHandler implements HandlerInterface
2931
{

src/Symfony/Component/CssSelector/Parser/Handler/NumberHandler.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
* which is copyright Ian Bicking, @see https://github.com/SimonSapin/cssselect.
2424
*
2525
* @author Jean-François Simon <[email protected]>
26+
*
27+
* @internal
2628
*/
2729
class NumberHandler implements HandlerInterface
2830
{

src/Symfony/Component/CssSelector/Parser/Handler/StringHandler.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
* which is copyright Ian Bicking, @see https://github.com/SimonSapin/cssselect.
2727
*
2828
* @author Jean-François Simon <[email protected]>
29+
*
30+
* @internal
2931
*/
3032
class StringHandler implements HandlerInterface
3133
{

src/Symfony/Component/CssSelector/Parser/Handler/WhitespaceHandler.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
* which is copyright Ian Bicking, @see https://github.com/SimonSapin/cssselect.
2323
*
2424
* @author Jean-François Simon <[email protected]>
25+
*
26+
* @internal
2527
*/
2628
class WhitespaceHandler implements HandlerInterface
2729
{

src/Symfony/Component/CssSelector/Parser/Parser.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
* which is copyright Ian Bicking, @see https://github.com/SimonSapin/cssselect.
2323
*
2424
* @author Jean-François Simon <[email protected]>
25+
*
26+
* @internal
2527
*/
2628
class Parser implements ParserInterface
2729
{

src/Symfony/Component/CssSelector/Parser/ParserInterface.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
* which is copyright Ian Bicking, @see https://github.com/SimonSapin/cssselect.
2121
*
2222
* @author Jean-François Simon <[email protected]>
23+
*
24+
* @internal
2325
*/
2426
interface ParserInterface
2527
{

src/Symfony/Component/CssSelector/Parser/Reader.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
* which is copyright Ian Bicking, @see https://github.com/SimonSapin/cssselect.
1919
*
2020
* @author Jean-François Simon <[email protected]>
21+
*
22+
* @internal
2123
*/
2224
class Reader
2325
{

src/Symfony/Component/CssSelector/Parser/Shortcut/ClassParser.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
* which is copyright Ian Bicking, @see https://github.com/SimonSapin/cssselect.
2424
*
2525
* @author Jean-François Simon <[email protected]>
26+
*
27+
* @internal
2628
*/
2729
class ClassParser implements ParserInterface
2830
{

0 commit comments

Comments
 (0)