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;
|
|
|
|
|
2018-08-01 17:40:03 -04:00
|
|
|
public function __construct(string $data) {
|
|
|
|
$this->data = $data;
|
2018-03-21 11:55:32 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
abstract class TagToken extends Token {
|
|
|
|
public $name;
|
|
|
|
|
2018-08-01 17:40:03 -04:00
|
|
|
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;
|
|
|
|
|
2018-07-26 17:30:29 -04:00
|
|
|
public function __construct(string $name = null, string $public = '', string $system = '') {
|
|
|
|
$this->name = $name;
|
2018-03-21 11:55:32 -04:00
|
|
|
|
2018-07-26 17:30:29 -04:00
|
|
|
$this->public = $public;
|
|
|
|
$this->system = $system;
|
2018-03-21 11:55:32 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class CharacterToken extends DataToken {}
|
|
|
|
|
|
|
|
class CommentToken extends DataToken {
|
2018-08-01 17:40:03 -04:00
|
|
|
public function __construct(string $data = '') {
|
2018-03-21 11:55:32 -04:00
|
|
|
parent::__construct($data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class StartTagToken extends TagToken {
|
|
|
|
public $namespace;
|
|
|
|
public $selfClosing;
|
2018-07-26 17:30:29 -04:00
|
|
|
public $attributes = [];
|
2018-03-21 11:55:32 -04:00
|
|
|
|
2018-08-01 17:40:03 -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);
|
|
|
|
}
|
|
|
|
|
2018-07-25 10:57:27 -04:00
|
|
|
public function getAttribute(string $name) {
|
2018-08-20 15:00:27 -04:00
|
|
|
$key = $this->_getAttributeKey($name);
|
2018-07-25 10:57:27 -04:00
|
|
|
|
|
|
|
return (isset($this->attributes[$key])) ? $this->attributes[$key] : null;
|
2018-03-21 11:55:32 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
public function hasAttribute(string $name): bool {
|
2018-07-25 10:57:27 -04:00
|
|
|
return (!is_null($this->_getAttributeKey($name)));
|
2018-03-21 11:55:32 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
public function removeAttribute(string $name) {
|
2018-08-20 15:00:27 -04:00
|
|
|
unset($this->attributes[$this->_getAttributeKey($name)]);
|
2018-03-21 11:55:32 -04:00
|
|
|
}
|
|
|
|
|
2018-08-01 17:40:03 -04:00
|
|
|
public function setAttribute(string $name, string $value, string $namespace = Parser::HTML_NAMESPACE) {
|
2018-07-25 10:57:27 -04:00
|
|
|
$key = $this->_getAttributeKey($name);
|
|
|
|
|
|
|
|
if (is_null($key)) {
|
2018-08-20 15:00:27 -04:00
|
|
|
$this->attributes[] = new TokenAttr($name, $value, $namespace);
|
2018-07-25 10:57:27 -04:00
|
|
|
} else {
|
2018-08-20 15:00:27 -04:00
|
|
|
$attribute = &$this->attributes[$key];
|
|
|
|
$attribute->name = $name;
|
|
|
|
$attribute->value = $value;
|
|
|
|
$attribute->namespace = $namespace;
|
2018-07-25 10:57:27 -04:00
|
|
|
}
|
2018-03-21 11:55:32 -04:00
|
|
|
}
|
|
|
|
|
2018-08-01 17:40:03 -04:00
|
|
|
private function _getAttributeKey(string $name) {
|
2018-07-25 10:57:27 -04:00
|
|
|
foreach ($this->attributes as $key => $a) {
|
|
|
|
if ($a->name === $name) {
|
2018-08-20 15:00:27 -04:00
|
|
|
return $key;
|
2018-07-25 10:57:27 -04:00
|
|
|
}
|
2018-03-21 11:55:32 -04:00
|
|
|
}
|
|
|
|
|
2018-08-20 15:00:27 -04:00
|
|
|
return null;
|
2018-03-21 11:55:32 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-25 10:57:27 -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;
|
|
|
|
}
|
|
|
|
}
|