| 1 |
<?php |
| 2 |
|
| 3 |
namespace Wikimedia\RemexHtml\Tokenizer; |
| 4 |
|
| 5 |
/** |
| 6 |
* A TokenHandler which collects events from the Tokenizer and generates an |
| 7 |
* array compatible with the html5lib tokenizer tests. |
| 8 |
*/ |
| 9 |
class TestTokenHandler implements TokenHandler { |
| 10 |
/** @var array<string|array> */ |
| 11 |
private $tokens = []; |
| 12 |
|
| 13 |
/** @return array<string|array> */ |
| 14 |
public function getTokens() { |
| 15 |
return $this->tokens; |
| 16 |
} |
| 17 |
|
| 18 |
/** @inheritDoc */ |
| 19 |
public function startDocument( Tokenizer $tokenizer, $fns, $fn ) { |
| 20 |
} |
| 21 |
|
| 22 |
/** @inheritDoc */ |
| 23 |
public function endDocument( $pos ) { |
| 24 |
} |
| 25 |
|
| 26 |
/** @inheritDoc */ |
| 27 |
public function error( $text, $pos ) { |
| 28 |
$this->tokens[] = 'ParseError'; |
| 29 |
} |
| 30 |
|
| 31 |
/** @inheritDoc */ |
| 32 |
public function characters( $text, $start, $length, $sourceStart, $sourceLength ) { |
| 33 |
$this->tokens[] = [ 'Character', substr( $text, $start, $length ) ]; |
| 34 |
} |
| 35 |
|
| 36 |
/** @inheritDoc */ |
| 37 |
public function startTag( $name, Attributes $attrs, $selfClose, $sourceStart, $sourceLength ) { |
| 38 |
$attrArray = $attrs->getValues(); |
| 39 |
if ( $selfClose ) { |
| 40 |
$this->tokens[] = [ 'StartTag', $name, $attrArray, $selfClose ]; |
| 41 |
} else { |
| 42 |
$this->tokens[] = [ 'StartTag', $name, $attrArray ]; |
| 43 |
} |
| 44 |
} |
| 45 |
|
| 46 |
/** @inheritDoc */ |
| 47 |
public function endTag( $name, $sourceStart, $sourceLength ) { |
| 48 |
$this->tokens[] = [ 'EndTag', $name ]; |
| 49 |
} |
| 50 |
|
| 51 |
/** @inheritDoc */ |
| 52 |
public function doctype( $name, $public, $system, $quirks, $sourceStart, $sourceLength ) { |
| 53 |
$this->tokens[] = [ 'DOCTYPE', $name, $public, $system, !$quirks ]; |
| 54 |
} |
| 55 |
|
| 56 |
/** @inheritDoc */ |
| 57 |
public function comment( $text, $sourceStart, $sourceLength ) { |
| 58 |
$this->tokens[] = [ 'Comment', $text ]; |
| 59 |
} |
| 60 |
} |