You are here

protected function Tokenizer::attribute in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 vendor/masterminds/html5/src/HTML5/Parser/Tokenizer.php \Masterminds\HTML5\Parser\Tokenizer::attribute()

Parse attributes from inside of a tag.

1 call to Tokenizer::attribute()
Tokenizer::tagName in vendor/masterminds/html5/src/HTML5/Parser/Tokenizer.php
Consume a tag name and body. 8.2.4.10

File

vendor/masterminds/html5/src/HTML5/Parser/Tokenizer.php, line 431

Class

Tokenizer
The HTML5 tokenizer.

Namespace

Masterminds\HTML5\Parser

Code

protected function attribute(&$attributes) {
  $tok = $this->scanner
    ->current();
  if ($tok == '/' || $tok == '>' || $tok === false) {
    return false;
  }
  if ($tok == '<') {
    $this
      ->parseError("Unexepcted '<' inside of attributes list.");

    // Push the < back onto the stack.
    $this->scanner
      ->unconsume();

    // Let the caller figure out how to handle this.
    throw new ParseError("Start tag inside of attribute.");
  }
  $name = strtolower($this->scanner
    ->charsUntil("/>=\n\f\t "));
  if (strlen($name) == 0) {
    $this
      ->parseError("Expected an attribute name, got %s.", $this->scanner
      ->current());

    // Really, only '=' can be the char here. Everything else gets absorbed
    // under one rule or another.
    $name = $this->scanner
      ->current();
    $this->scanner
      ->next();
  }
  $isValidAttribute = true;

  // Attribute names can contain most Unicode characters for HTML5.
  // But method "DOMElement::setAttribute" is throwing exception
  // because of it's own internal restriction so these have to be filtered.
  // see issue #23: https://github.com/Masterminds/html5-php/issues/23
  // and http://www.w3.org/TR/2011/WD-html5-20110525/syntax.html#syntax-attribute-name
  if (preg_match("/[\1-,\\/;-@[-^`{-]/u", $name)) {
    $this
      ->parseError("Unexpected characters in attribute name: %s", $name);
    $isValidAttribute = false;
  }
  else {
    if (preg_match("/^[0-9.-]/u", $name)) {
      $this
        ->parseError("Unexpected character at the begining of attribute name: %s", $name);
      $isValidAttribute = false;
    }
  }

  // 8.1.2.3
  $this->scanner
    ->whitespace();
  $val = $this
    ->attributeValue();
  if ($isValidAttribute) {
    $attributes[$name] = $val;
  }
  return true;
}