[CssSelector] Optimize regexs matching simple selectors

This commit is contained in:
Christophe Coevoet 2015-09-21 01:59:10 +02:00
parent da29357d49
commit d5abe0b114
3 changed files with 20 additions and 23 deletions

View File

@ -33,15 +33,14 @@ class ClassParser implements ParserInterface
{ {
// Matches an optional namespace, optional element, and required class // Matches an optional namespace, optional element, and required class
// $source = 'test|input.ab6bd_field'; // $source = 'test|input.ab6bd_field';
// $matches = array (size=5) // $matches = array (size=4)
// 0 => string 'test:input.ab6bd_field' (length=22) // 0 => string 'test|input.ab6bd_field' (length=22)
// 1 => string 'test:' (length=5) // 1 => string 'test' (length=4)
// 2 => string 'test' (length=4) // 2 => string 'input' (length=5)
// 3 => string 'input' (length=5) // 3 => string 'ab6bd_field' (length=11)
// 4 => string 'ab6bd_field' (length=11) if (preg_match('/^(?:([a-z]++)\|)?+([\w-]++|\*)?+\.([\w-]++)$/i', trim($source), $matches)) {
if (preg_match('/^(([a-z]+)\|)?([\w-]+|\*)?\.([\w-]+)$/i', trim($source), $matches)) {
return array( return array(
new SelectorNode(new ClassNode(new ElementNode($matches[2] ?: null, $matches[3] ?: null), $matches[4])), new SelectorNode(new ClassNode(new ElementNode($matches[1] ?: null, $matches[2] ?: null), $matches[3])),
); );
} }

View File

@ -32,13 +32,12 @@ class ElementParser implements ParserInterface
{ {
// Matches an optional namespace, required element or `*` // Matches an optional namespace, required element or `*`
// $source = 'testns|testel'; // $source = 'testns|testel';
// $matches = array (size=4) // $matches = array (size=3)
// 0 => string 'testns:testel' (length=13) // 0 => string 'testns|testel' (length=13)
// 1 => string 'testns:' (length=7) // 1 => string 'testns' (length=6)
// 2 => string 'testns' (length=6) // 2 => string 'testel' (length=6)
// 3 => string 'testel' (length=6) if (preg_match('/^(?:([a-z]++)\|)?([\w-]++|\*)$/i', trim($source), $matches)) {
if (preg_match('/^(([a-z]+)\|)?([\w-]+|\*)$/i', trim($source), $matches)) { return array(new SelectorNode(new ElementNode($matches[1] ?: null, $matches[2])));
return array(new SelectorNode(new ElementNode($matches[2] ?: null, $matches[3])));
} }
return array(); return array();

View File

@ -33,15 +33,14 @@ class HashParser implements ParserInterface
{ {
// Matches an optional namespace, optional element, and required id // Matches an optional namespace, optional element, and required id
// $source = 'test|input#ab6bd_field'; // $source = 'test|input#ab6bd_field';
// $matches = array (size=5) // $matches = array (size=4)
// 0 => string 'test:input#ab6bd_field' (length=22) // 0 => string 'test|input#ab6bd_field' (length=22)
// 1 => string 'test:' (length=5) // 1 => string 'test' (length=4)
// 2 => string 'test' (length=4) // 2 => string 'input' (length=5)
// 3 => string 'input' (length=5) // 3 => string 'ab6bd_field' (length=11)
// 4 => string 'ab6bd_field' (length=11) if (preg_match('/^(?:([a-z]++)\|)?+([\w-]++|\*)?+#([\w-]++)$/i', trim($source), $matches)) {
if (preg_match('/^(([a-z]+)\|)?([\w-]+|\*)?#([\w-]+)$/i', trim($source), $matches)) {
return array( return array(
new SelectorNode(new HashNode(new ElementNode($matches[2] ?: null, $matches[3] ?: null), $matches[4])), new SelectorNode(new HashNode(new ElementNode($matches[1] ?: null, $matches[2] ?: null), $matches[3])),
); );
} }