| @@ 7-43 (lines=37) @@ | ||
| 4 | ||
| 5 | use Kevintweber\HtmlTokenizer\Exceptions\ParseException; |
|
| 6 | ||
| 7 | class CData extends AbstractToken |
|
| 8 | { |
|
| 9 | public function __construct(Token $parent = null, bool $throwOnError = true) |
|
| 10 | { |
|
| 11 | parent::__construct(Token::CDATA, $parent, $throwOnError); |
|
| 12 | } |
|
| 13 | ||
| 14 | public function parse(string $html) : string |
|
| 15 | { |
|
| 16 | $html = ltrim($html); |
|
| 17 | $this->setTokenPosition($html); |
|
| 18 | ||
| 19 | // Parse token. |
|
| 20 | $posOfEndOfCData = mb_strpos($html, ']]>'); |
|
| 21 | if ($posOfEndOfCData === false) { |
|
| 22 | if ($this->getThrowOnError()) { |
|
| 23 | throw new ParseException('Invalid CDATA.'); |
|
| 24 | } |
|
| 25 | ||
| 26 | return ''; |
|
| 27 | } |
|
| 28 | ||
| 29 | $this->value = trim(mb_substr($html, 9, $posOfEndOfCData - 9)); |
|
| 30 | ||
| 31 | return mb_substr($html, $posOfEndOfCData + 3); |
|
| 32 | } |
|
| 33 | ||
| 34 | public function toArray() : array |
|
| 35 | { |
|
| 36 | return array( |
|
| 37 | 'type' => 'cdata', |
|
| 38 | 'value' => $this->value, |
|
| 39 | 'line' => $this->getLine(), |
|
| 40 | 'position' => $this->getPosition() |
|
| 41 | ); |
|
| 42 | } |
|
| 43 | } |
|
| 44 | ||
| @@ 7-43 (lines=37) @@ | ||
| 4 | ||
| 5 | use Kevintweber\HtmlTokenizer\Exceptions\ParseException; |
|
| 6 | ||
| 7 | class Comment extends AbstractToken |
|
| 8 | { |
|
| 9 | public function __construct(Token $parent = null, bool $throwOnError = true) |
|
| 10 | { |
|
| 11 | parent::__construct(Token::COMMENT, $parent, $throwOnError); |
|
| 12 | } |
|
| 13 | ||
| 14 | public function parse(string $html) : string |
|
| 15 | { |
|
| 16 | $html = ltrim($html); |
|
| 17 | $this->setTokenPosition($html); |
|
| 18 | ||
| 19 | // Parse token. |
|
| 20 | $posOfEndOfComment = mb_strpos($html, '-->'); |
|
| 21 | if ($posOfEndOfComment === false) { |
|
| 22 | if ($this->getThrowOnError()) { |
|
| 23 | throw new ParseException('Invalid comment.'); |
|
| 24 | } |
|
| 25 | ||
| 26 | return ''; |
|
| 27 | } |
|
| 28 | ||
| 29 | $this->value = trim(mb_substr($html, 4, $posOfEndOfComment - 4)); |
|
| 30 | ||
| 31 | return mb_substr($html, $posOfEndOfComment + 3); |
|
| 32 | } |
|
| 33 | ||
| 34 | public function toArray() : array |
|
| 35 | { |
|
| 36 | return array( |
|
| 37 | 'type' => 'comment', |
|
| 38 | 'value' => $this->value, |
|
| 39 | 'line' => $this->getLine(), |
|
| 40 | 'position' => $this->getPosition() |
|
| 41 | ); |
|
| 42 | } |
|
| 43 | } |
|
| 44 | ||
| @@ 7-43 (lines=37) @@ | ||
| 4 | ||
| 5 | use Kevintweber\HtmlTokenizer\Exceptions\ParseException; |
|
| 6 | ||
| 7 | class DocType extends AbstractToken |
|
| 8 | { |
|
| 9 | public function __construct(Token $parent = null, bool $throwOnError = true) |
|
| 10 | { |
|
| 11 | parent::__construct(Token::DOCTYPE, $parent, $throwOnError); |
|
| 12 | } |
|
| 13 | ||
| 14 | public function parse(string $html) : string |
|
| 15 | { |
|
| 16 | $html = ltrim($html); |
|
| 17 | $this->setTokenPosition($html); |
|
| 18 | ||
| 19 | // Parse token. |
|
| 20 | $posOfEndOfDocType = mb_strpos($html, '>'); |
|
| 21 | if ($posOfEndOfDocType === false) { |
|
| 22 | if ($this->getThrowOnError()) { |
|
| 23 | throw new ParseException('Invalid DOCTYPE.'); |
|
| 24 | } |
|
| 25 | ||
| 26 | return ''; |
|
| 27 | } |
|
| 28 | ||
| 29 | $this->value = trim(mb_substr($html, 10, $posOfEndOfDocType - 10)); |
|
| 30 | ||
| 31 | return mb_substr($html, $posOfEndOfDocType + 1); |
|
| 32 | } |
|
| 33 | ||
| 34 | public function toArray() : array |
|
| 35 | { |
|
| 36 | return array( |
|
| 37 | 'type' => 'doctype', |
|
| 38 | 'value' => $this->value, |
|
| 39 | 'line' => $this->getLine(), |
|
| 40 | 'position' => $this->getPosition() |
|
| 41 | ); |
|
| 42 | } |
|
| 43 | } |
|
| 44 | ||