Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
95.74% |
45 / 47 |
|
85.71% |
12 / 14 |
CRAP | |
0.00% |
0 / 1 |
TraceFormatter | |
95.74% |
45 / 47 |
|
85.71% |
12 / 14 |
19 | |
0.00% |
0 / 1 |
getDebugTag | |
80.00% |
4 / 5 |
|
0.00% |
0 / 1 |
4.13 | |||
excerpt | |
66.67% |
2 / 3 |
|
0.00% |
0 / 1 |
2.15 | |||
getPrepositionName | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
1 | |||
startDocument | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
endDocument | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
characters | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
1 | |||
insertElement | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
2 | |||
endTag | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
doctype | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
1 | |||
comment | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
1 | |||
error | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
mergeAttributes | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
removeNode | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
reparentChildren | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 |
1 | <?php |
2 | |
3 | namespace Wikimedia\RemexHtml\TreeBuilder; |
4 | |
5 | use Wikimedia\RemexHtml\Serializer\SerializerNode; |
6 | use Wikimedia\RemexHtml\Tokenizer\Attributes; |
7 | |
8 | class TraceFormatter { |
9 | /** |
10 | * Get a debug tag for an element or null |
11 | * |
12 | * @param Element|SerializerNode|null $element |
13 | * @return string |
14 | */ |
15 | public static function getDebugTag( $element ) { |
16 | if ( !$element ) { |
17 | return ''; |
18 | } elseif ( $element instanceof Element || $element instanceof SerializerNode ) { |
19 | return $element->getDebugTag(); |
20 | } else { |
21 | return get_class( $element ) . '#' . substr( md5( spl_object_hash( $element ) ), 0, 8 ); |
22 | } |
23 | } |
24 | |
25 | /** |
26 | * Get a short excerpt of some text |
27 | * |
28 | * @param string $text |
29 | * @return string |
30 | */ |
31 | public static function excerpt( $text ) { |
32 | if ( strlen( $text ) > 20 ) { |
33 | $text = substr( $text, 0, 20 ) . '...'; |
34 | } |
35 | return str_replace( "\n", "\\n", $text ); |
36 | } |
37 | |
38 | /** |
39 | * Get a readable version of the TreeBuilder preposition constants |
40 | * @param int $prep |
41 | * @return string |
42 | */ |
43 | public static function getPrepositionName( $prep ) { |
44 | $names = [ |
45 | TreeBuilder::BEFORE => 'before', |
46 | TreeBuilder::UNDER => 'under', |
47 | TreeBuilder::ROOT => 'under root' |
48 | ]; |
49 | return $names[$prep] ?? '???'; |
50 | } |
51 | |
52 | public static function startDocument( $fns, $fn ) { |
53 | return "startDocument"; |
54 | } |
55 | |
56 | public static function endDocument( $pos ) { |
57 | return "endDocument pos=$pos"; |
58 | } |
59 | |
60 | public static function characters( $preposition, $refNode, $text, $start, $length, |
61 | $sourceStart, $sourceLength |
62 | ) { |
63 | $excerpt = self::excerpt( substr( $text, $start, $length ) ); |
64 | $prepName = self::getPrepositionName( $preposition ); |
65 | $refTag = self::getDebugTag( $refNode ); |
66 | |
67 | return "characters \"$excerpt\", $prepName $refTag, pos=$sourceStart, len=$sourceLength"; |
68 | } |
69 | |
70 | public static function insertElement( $preposition, $refNode, Element $element, $void, |
71 | $sourceStart, $sourceLength |
72 | ) { |
73 | $prepName = self::getPrepositionName( $preposition ); |
74 | $refTag = self::getDebugTag( $refNode ); |
75 | $elementTag = self::getDebugTag( $element ); |
76 | $voidMsg = $void ? 'void' : ''; |
77 | return "insert $elementTag $voidMsg, $prepName $refTag, pos=$sourceStart, len=$sourceLength"; |
78 | } |
79 | |
80 | public static function endTag( Element $element, $sourceStart, $sourceLength ) { |
81 | $elementTag = self::getDebugTag( $element ); |
82 | return "end $elementTag, pos=$sourceStart, len=$sourceLength"; |
83 | } |
84 | |
85 | public static function doctype( $name, $public, $system, $quirks, $sourceStart, $sourceLength |
86 | ) { |
87 | $quirksTypes = [ |
88 | TreeBuilder::QUIRKS => 'quirks', |
89 | TreeBuilder::NO_QUIRKS => 'no-quirks', |
90 | TreeBuilder::LIMITED_QUIRKS => 'limited-quirks' |
91 | ]; |
92 | $quirksMsg = $quirksTypes[$quirks]; |
93 | return "doctype $name, public=\"$public\", system=\"$system\", " . |
94 | "$quirksMsg, pos=$sourceStart, len=$sourceLength"; |
95 | } |
96 | |
97 | public static function comment( $preposition, $refNode, $text, $sourceStart, $sourceLength ) { |
98 | $prepName = self::getPrepositionName( $preposition ); |
99 | $refTag = self::getDebugTag( $refNode ); |
100 | $excerpt = self::excerpt( $text ); |
101 | |
102 | return "comment \"$excerpt\", $prepName $refTag, pos=$sourceStart, len=$sourceLength"; |
103 | } |
104 | |
105 | public static function error( $text, $pos ) { |
106 | return "error \"$text\", pos=$pos"; |
107 | } |
108 | |
109 | public static function mergeAttributes( Element $element, Attributes $attrs, $sourceStart ) { |
110 | $elementTag = self::getDebugTag( $element ); |
111 | return "merge $elementTag, pos=$sourceStart"; |
112 | } |
113 | |
114 | public static function removeNode( Element $element, $sourceStart ) { |
115 | $elementTag = self::getDebugTag( $element ); |
116 | return "remove $elementTag, pos=$sourceStart"; |
117 | } |
118 | |
119 | public static function reparentChildren( Element $element, Element $newParent, $sourceStart ) { |
120 | $elementTag = self::getDebugTag( $element ); |
121 | $newParentTag = self::getDebugTag( $newParent ); |
122 | return "reparent children of $elementTag under $newParentTag, pos=$sourceStart"; |
123 | } |
124 | } |