Skip to content

Commit 7550326

Browse files
committed
[String] Fix Notice when argument is empty string
0 parents  commit 7550326

File tree

85 files changed

+6279
-0
lines changed

Some content is hidden

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

85 files changed

+6279
-0
lines changed

.gitattributes

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/Tests export-ignore
2+
/phpunit.xml.dist export-ignore
3+
/.gitattributes export-ignore
4+
/.gitignore export-ignore

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
vendor/
2+
composer.lock
3+
phpunit.xml

CHANGELOG.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
CHANGELOG
2+
=========
3+
4+
4.4.0
5+
-----
6+
7+
* Added support for `*:only-of-type`
8+
9+
2.8.0
10+
-----
11+
12+
* Added the `CssSelectorConverter` class as a non-static API for the component.
13+
* Deprecated the `CssSelector` static API of the component.
14+
15+
2.1.0
16+
-----
17+
18+
* none

CssSelectorConverter.php

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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+
* CssSelectorConverter is the main entry point of the component and can convert CSS
23+
* selectors to XPath expressions.
24+
*
25+
* @author Christophe Coevoet <[email protected]>
26+
*/
27+
class CssSelectorConverter
28+
{
29+
private $translator;
30+
private $cache;
31+
32+
private static $xmlCache = [];
33+
private static $htmlCache = [];
34+
35+
/**
36+
* @param bool $html Whether HTML support should be enabled. Disable it for XML documents
37+
*/
38+
public function __construct(bool $html = true)
39+
{
40+
$this->translator = new Translator();
41+
42+
if ($html) {
43+
$this->translator->registerExtension(new HtmlExtension($this->translator));
44+
$this->cache = &self::$htmlCache;
45+
} else {
46+
$this->cache = &self::$xmlCache;
47+
}
48+
49+
$this->translator
50+
->registerParserShortcut(new EmptyStringParser())
51+
->registerParserShortcut(new ElementParser())
52+
->registerParserShortcut(new ClassParser())
53+
->registerParserShortcut(new HashParser())
54+
;
55+
}
56+
57+
/**
58+
* Translates a CSS expression to its XPath equivalent.
59+
*
60+
* Optionally, a prefix can be added to the resulting XPath
61+
* expression with the $prefix parameter.
62+
*
63+
* @return string
64+
*/
65+
public function toXPath(string $cssExpr, string $prefix = 'descendant-or-self::')
66+
{
67+
return $this->cache[$prefix][$cssExpr] ?? $this->cache[$prefix][$cssExpr] = $this->translator->cssToXPath($cssExpr, $prefix);
68+
}
69+
}

Exception/ExceptionInterface.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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\Exception;
13+
14+
/**
15+
* Interface for exceptions.
16+
*
17+
* This component is a port of the Python cssselect library,
18+
* which is copyright Ian Bicking, @see https://github.com/SimonSapin/cssselect.
19+
*
20+
* @author Jean-François Simon <[email protected]>
21+
*/
22+
interface ExceptionInterface extends \Throwable
23+
{
24+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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\Exception;
13+
14+
/**
15+
* ParseException is thrown when a CSS selector syntax is not valid.
16+
*
17+
* This component is a port of the Python cssselect library,
18+
* which is copyright Ian Bicking, @see https://github.com/SimonSapin/cssselect.
19+
*
20+
* @author Jean-François Simon <[email protected]>
21+
*/
22+
class ExpressionErrorException extends ParseException
23+
{
24+
}

Exception/InternalErrorException.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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\Exception;
13+
14+
/**
15+
* ParseException is thrown when a CSS selector syntax is not valid.
16+
*
17+
* This component is a port of the Python cssselect library,
18+
* which is copyright Ian Bicking, @see https://github.com/SimonSapin/cssselect.
19+
*
20+
* @author Jean-François Simon <[email protected]>
21+
*/
22+
class InternalErrorException extends ParseException
23+
{
24+
}

Exception/ParseException.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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\Exception;
13+
14+
/**
15+
* ParseException is thrown when a CSS selector syntax is not valid.
16+
*
17+
* This component is a port of the Python cssselect library,
18+
* which is copyright Ian Bicking, @see https://github.com/SimonSapin/cssselect.
19+
*
20+
* @author Fabien Potencier <[email protected]>
21+
*/
22+
class ParseException extends \Exception implements ExceptionInterface
23+
{
24+
}

Exception/SyntaxErrorException.php

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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\Exception;
13+
14+
use Symfony\Component\CssSelector\Parser\Token;
15+
16+
/**
17+
* ParseException is thrown when a CSS selector syntax is not valid.
18+
*
19+
* This component is a port of the Python cssselect library,
20+
* which is copyright Ian Bicking, @see https://github.com/SimonSapin/cssselect.
21+
*
22+
* @author Jean-François Simon <[email protected]>
23+
*/
24+
class SyntaxErrorException extends ParseException
25+
{
26+
/**
27+
* @return self
28+
*/
29+
public static function unexpectedToken(string $expectedValue, Token $foundToken)
30+
{
31+
return new self(sprintf('Expected %s, but %s found.', $expectedValue, $foundToken));
32+
}
33+
34+
/**
35+
* @return self
36+
*/
37+
public static function pseudoElementFound(string $pseudoElement, string $unexpectedLocation)
38+
{
39+
return new self(sprintf('Unexpected pseudo-element "::%s" found %s.', $pseudoElement, $unexpectedLocation));
40+
}
41+
42+
/**
43+
* @return self
44+
*/
45+
public static function unclosedString(int $position)
46+
{
47+
return new self(sprintf('Unclosed/invalid string at %s.', $position));
48+
}
49+
50+
/**
51+
* @return self
52+
*/
53+
public static function nestedNot()
54+
{
55+
return new self('Got nested ::not().');
56+
}
57+
58+
/**
59+
* @return self
60+
*/
61+
public static function stringAsFunctionArgument()
62+
{
63+
return new self('String not allowed as function argument.');
64+
}
65+
}

LICENSE

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
Copyright (c) 2004-2020 Fabien Potencier
2+
3+
Permission is hereby granted, free of charge, to any person obtaining a copy
4+
of this software and associated documentation files (the "Software"), to deal
5+
in the Software without restriction, including without limitation the rights
6+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7+
copies of the Software, and to permit persons to whom the Software is furnished
8+
to do so, subject to the following conditions:
9+
10+
The above copyright notice and this permission notice shall be included in all
11+
copies or substantial portions of the Software.
12+
13+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19+
THE SOFTWARE.

0 commit comments

Comments
 (0)