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