HTML-Parser/lib/Token.php

107 lines
2.7 KiB
PHP
Raw Normal View History

2018-03-21 11:55:32 -04:00
<?php
declare(strict_types=1);
namespace dW\HTML5;
abstract class Token {}
abstract class DataToken extends Token {
public $data;
public function __construct(string $data) {
$this->data = $data;
2018-03-21 11:55:32 -04:00
}
}
abstract class TagToken extends Token {
public $name;
public function __construct(string $name) {
$this->name = $name;
2018-03-21 11:55:32 -04:00
}
}
class EOFToken extends Token {}
class DOCTYPEToken extends Token {
public $forceQuirks = false;
public $public;
public $system;
public function __construct(string $name = null, string $public = '', string $system = '') {
$this->name = $name;
2018-03-21 11:55:32 -04:00
$this->public = $public;
$this->system = $system;
2018-03-21 11:55:32 -04:00
}
}
class CharacterToken extends DataToken {}
class CommentToken extends DataToken {
public function __construct(string $data = '') {
2018-03-21 11:55:32 -04:00
parent::__construct($data);
}
}
class StartTagToken extends TagToken {
public $namespace;
public $selfClosing;
public $attributes = [];
2018-03-21 11:55:32 -04:00
public function __construct(string $name, bool $selfClosing = false, string $namespace = Parser::HTML_NAMESPACE) {
2018-03-21 11:55:32 -04:00
$this->selfClosing = $selfClosing;
$this->namespace = $namespace;
parent::__construct($name);
}
public function getAttribute(string $name) {
$key = $this->_getAttributeKey($name);
return (isset($this->attributes[$key])) ? $this->attributes[$key] : null;
2018-03-21 11:55:32 -04:00
}
public function hasAttribute(string $name): bool {
return (!is_null($this->_getAttributeKey($name)));
2018-03-21 11:55:32 -04:00
}
public function removeAttribute(string $name) {
unset($this->attributes[$this->_getAttributeKey($name)]);
2018-03-21 11:55:32 -04:00
}
public function setAttribute(string $name, string $value, string $namespace = Parser::HTML_NAMESPACE) {
$key = $this->_getAttributeKey($name);
if (is_null($key)) {
$this->attributes[] = new TokenAttr($name, $value, $namespace);
} else {
$attribute = &$this->attributes[$key];
$attribute->name = $name;
$attribute->value = $value;
$attribute->namespace = $namespace;
}
2018-03-21 11:55:32 -04:00
}
private function _getAttributeKey(string $name) {
foreach ($this->attributes as $key => $a) {
if ($a->name === $name) {
return $key;
}
2018-03-21 11:55:32 -04:00
}
return null;
2018-03-21 11:55:32 -04:00
}
}
class EndTagToken extends TagToken {}
class TokenAttr {
public $name;
public $value;
public $namespace;
public function __construct(string $name, string $value, string $namespace = Parser::HTML_NAMESPACE) {
$this->name = $name;
$this->value = $value;
$this->namespace = $namespace;
}
}