function Parser::parseSelectorPartAttribute
Parses an attribute selector part
Return value
Node\Part\Attribute|null
Throws
1 call to Parser::parseSelectorPartAttribute()
- Parser::parseSelectorParts in vendor/
mck89/ peast/ lib/ Peast/ Selector/ Parser.php - Parses a set of selector pats
File
-
vendor/
mck89/ peast/ lib/ Peast/ Selector/ Parser.php, line 205
Class
- Parser
- Selector parser class
Namespace
Peast\SelectorCode
protected function parseSelectorPartAttribute() {
if (!$this->consume("[")) {
return null;
}
$this->consumeWhitespaces();
$part = new Node\Part\Attribute();
if (!($name = $this->consumeWord())) {
throw new Exception("Missing attribute name");
}
$part->addName($name);
while ($this->consume(".")) {
if (!($name = $this->consumeWord())) {
throw new Exception("Missing attribute name after dot");
}
$part->addName($name);
}
$this->consumeWhitespaces();
$operator = $this->consumeAny($this->attrOperatorChars);
if ($operator) {
if (!in_array($operator, $this->attrOperators)) {
throw new Exception("Invalid attribute operator '{$operator}'");
}
$part->setOperator($operator);
$this->consumeWhitespaces();
if (!($value = $this->parseLiteral())) {
throw new Exception("Missing attribute value");
}
$part->setValue($value[0]);
if ($value[1]) {
if ($operator != "=") {
throw new Exception("Only '=' operator is valid for attribute regex match");
}
$part->setRegex(true);
}
$this->consumeWhitespaces();
if ($this->consume("i")) {
if (!is_string($value[0]) || $value[1]) {
throw new Exception("Case insensitive flag can be used only for string values");
}
$part->setCaseInsensitive(true);
$this->consumeWhitespaces();
}
}
if (!$this->consume("]")) {
throw new Exception("Unterminated attribute selector");
}
return $part;
}