Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 25 |
|
0.00% |
0 / 10 |
CRAP | |
0.00% |
0 / 1 |
| TokenSerializer | |
0.00% |
0 / 25 |
|
0.00% |
0 / 10 |
272 | |
0.00% |
0 / 1 |
| getOutput | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getErrors | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| startDocument | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| endDocument | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| error | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| characters | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| startTag | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
12 | |||
| endTag | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| doctype | |
0.00% |
0 / 10 |
|
0.00% |
0 / 1 |
30 | |||
| comment | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | declare( strict_types = 1 ); |
| 3 | |
| 4 | namespace Wikimedia\RemexHtml\Tokenizer; |
| 5 | |
| 6 | /** |
| 7 | * A simple serializer for the token stream, mostly meant for debugging. |
| 8 | * |
| 9 | * The main reason this shouldn't be used in production for modification of |
| 10 | * HTML documents is that element names are only determined by TreeBuilder |
| 11 | * and are not known here. The correct serialization for a text node depends |
| 12 | * on its parent element, including its namespace. |
| 13 | */ |
| 14 | class TokenSerializer implements TokenHandler { |
| 15 | /** @var string */ |
| 16 | private $output; |
| 17 | /** @var array[] */ |
| 18 | private $errors = []; |
| 19 | |
| 20 | /** |
| 21 | * @return string |
| 22 | */ |
| 23 | public function getOutput() { |
| 24 | return $this->output; |
| 25 | } |
| 26 | |
| 27 | /** |
| 28 | * @return array[] |
| 29 | */ |
| 30 | public function getErrors() { |
| 31 | return $this->errors; |
| 32 | } |
| 33 | |
| 34 | /** @inheritDoc */ |
| 35 | public function startDocument( Tokenizer $tokenizer, $fns, $fn ) { |
| 36 | $this->output = ''; |
| 37 | } |
| 38 | |
| 39 | /** @inheritDoc */ |
| 40 | public function endDocument( $pos ) { |
| 41 | } |
| 42 | |
| 43 | /** @inheritDoc */ |
| 44 | public function error( $text, $pos ) { |
| 45 | $this->errors[] = [ $text, $pos ]; |
| 46 | } |
| 47 | |
| 48 | /** @inheritDoc */ |
| 49 | public function characters( $text, $start, $length, $sourceStart, $sourceLength ) { |
| 50 | $this->output .= htmlspecialchars( substr( $text, $start, $length ) ); |
| 51 | } |
| 52 | |
| 53 | /** @inheritDoc */ |
| 54 | public function startTag( $name, Attributes $attrs, $selfClose, $sourceStart, $sourceLength ) { |
| 55 | $attrs = $attrs->getValues(); |
| 56 | $this->output .= "<$name"; |
| 57 | foreach ( $attrs as $name => $value ) { |
| 58 | $this->output .= " $name=\"" . str_replace( '"', '"', $value ) . '"'; |
| 59 | } |
| 60 | if ( $selfClose ) { |
| 61 | $this->output .= ' /'; |
| 62 | } |
| 63 | $this->output .= '>'; |
| 64 | } |
| 65 | |
| 66 | /** @inheritDoc */ |
| 67 | public function endTag( $name, $sourceStart, $sourceLength ) { |
| 68 | $this->output .= "</$name>"; |
| 69 | } |
| 70 | |
| 71 | /** @inheritDoc */ |
| 72 | public function doctype( $name, $public, $system, $quirks, $sourceStart, $sourceLength ) { |
| 73 | $this->output .= "<!DOCTYPE $name"; |
| 74 | if ( strlen( $public ) ) { |
| 75 | $this->output .= " PUBLIC \"$public\""; |
| 76 | if ( strlen( $system ) ) { |
| 77 | $this->output .= " \"$system\""; |
| 78 | } |
| 79 | } elseif ( strlen( $system ) ) { |
| 80 | $this->output .= " SYSTEM \"$system\""; |
| 81 | } |
| 82 | $this->output .= '>'; |
| 83 | if ( $quirks ) { |
| 84 | $this->output .= '<!--quirks-->'; |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | /** @inheritDoc */ |
| 89 | public function comment( $text, $sourceStart, $sourceLength ) { |
| 90 | $this->output .= '<!--' . $text . '-->'; |
| 91 | } |
| 92 | } |