Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 37 |
|
0.00% |
0 / 14 |
CRAP | |
0.00% |
0 / 1 |
| TraceFormatter | |
0.00% |
0 / 37 |
|
0.00% |
0 / 14 |
380 | |
0.00% |
0 / 1 |
| getDebugTag | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
20 | |||
| excerpt | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
| getPrepositionName | |
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 | |||
| characters | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
2 | |||
| insertElement | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
6 | |||
| endTag | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| doctype | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
| comment | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
2 | |||
| error | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| mergeAttributes | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| removeNode | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| reparentChildren | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | declare( strict_types = 1 ); |
| 3 | |
| 4 | namespace Wikimedia\RemexHtml\TreeBuilder; |
| 5 | |
| 6 | use Wikimedia\RemexHtml\Serializer\SerializerNode; |
| 7 | use Wikimedia\RemexHtml\Tokenizer\Attributes; |
| 8 | |
| 9 | class TraceFormatter { |
| 10 | |
| 11 | private const PREPOSITION_NAME = [ |
| 12 | TreeBuilder::BEFORE => 'before', |
| 13 | TreeBuilder::UNDER => 'under', |
| 14 | TreeBuilder::ROOT => 'under root', |
| 15 | ]; |
| 16 | |
| 17 | private const QUIRKS_TYPES = [ |
| 18 | TreeBuilder::QUIRKS => 'quirks', |
| 19 | TreeBuilder::NO_QUIRKS => 'no-quirks', |
| 20 | TreeBuilder::LIMITED_QUIRKS => 'limited-quirks', |
| 21 | ]; |
| 22 | |
| 23 | /** |
| 24 | * Get a debug tag for an element or null |
| 25 | * |
| 26 | * @param Element|SerializerNode|null $element |
| 27 | * @return string |
| 28 | */ |
| 29 | public static function getDebugTag( $element ) { |
| 30 | if ( !$element ) { |
| 31 | return ''; |
| 32 | } elseif ( $element instanceof Element || $element instanceof SerializerNode ) { |
| 33 | return $element->getDebugTag(); |
| 34 | } else { |
| 35 | return get_class( $element ) . '#' . substr( md5( spl_object_hash( $element ) ), 0, 8 ); |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | /** |
| 40 | * Get a short excerpt of some text |
| 41 | * |
| 42 | * @param string $text |
| 43 | * @return string |
| 44 | */ |
| 45 | public static function excerpt( $text ) { |
| 46 | if ( strlen( $text ) > 20 ) { |
| 47 | $text = substr( $text, 0, 20 ) . '...'; |
| 48 | } |
| 49 | return str_replace( "\n", "\\n", $text ); |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * Get a readable version of the TreeBuilder preposition constants |
| 54 | * @param int $prep |
| 55 | * @return string |
| 56 | */ |
| 57 | public static function getPrepositionName( $prep ) { |
| 58 | return self::PREPOSITION_NAME[$prep] ?? '???'; |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * @param string|null $fns |
| 63 | * @param string|null $fn |
| 64 | * @return string |
| 65 | */ |
| 66 | public static function startDocument( $fns, $fn ) { |
| 67 | return "startDocument"; |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * @param int $pos |
| 72 | * @return string |
| 73 | */ |
| 74 | public static function endDocument( $pos ) { |
| 75 | return "endDocument pos=$pos"; |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * @param int $preposition |
| 80 | * @param Element|null $refNode |
| 81 | * @param string $text |
| 82 | * @param int $start |
| 83 | * @param int $length |
| 84 | * @param int $sourceStart |
| 85 | * @param int $sourceLength |
| 86 | * @return string |
| 87 | */ |
| 88 | public static function characters( $preposition, $refNode, $text, $start, $length, |
| 89 | $sourceStart, $sourceLength |
| 90 | ) { |
| 91 | $excerpt = self::excerpt( substr( $text, $start, $length ) ); |
| 92 | $prepName = self::getPrepositionName( $preposition ); |
| 93 | $refTag = self::getDebugTag( $refNode ); |
| 94 | |
| 95 | return "characters \"$excerpt\", $prepName $refTag, pos=$sourceStart, len=$sourceLength"; |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * @param int $preposition |
| 100 | * @param Element|null $refNode |
| 101 | * @param Element $element |
| 102 | * @param bool $void |
| 103 | * @param int $sourceStart |
| 104 | * @param int $sourceLength |
| 105 | * @return string |
| 106 | */ |
| 107 | public static function insertElement( $preposition, $refNode, Element $element, $void, |
| 108 | $sourceStart, $sourceLength |
| 109 | ) { |
| 110 | $prepName = self::getPrepositionName( $preposition ); |
| 111 | $refTag = self::getDebugTag( $refNode ); |
| 112 | $elementTag = self::getDebugTag( $element ); |
| 113 | $voidMsg = $void ? 'void' : ''; |
| 114 | return "insert $elementTag $voidMsg, $prepName $refTag, pos=$sourceStart, len=$sourceLength"; |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * @param Element $element |
| 119 | * @param int $sourceStart |
| 120 | * @param int $sourceLength |
| 121 | * @return string |
| 122 | */ |
| 123 | public static function endTag( Element $element, $sourceStart, $sourceLength ) { |
| 124 | $elementTag = self::getDebugTag( $element ); |
| 125 | return "end $elementTag, pos=$sourceStart, len=$sourceLength"; |
| 126 | } |
| 127 | |
| 128 | /** |
| 129 | * @param string $name |
| 130 | * @param string $public |
| 131 | * @param string $system |
| 132 | * @param int $quirks |
| 133 | * @param int $sourceStart |
| 134 | * @param int $sourceLength |
| 135 | * @return string |
| 136 | */ |
| 137 | public static function doctype( $name, $public, $system, $quirks, $sourceStart, $sourceLength |
| 138 | ) { |
| 139 | $quirksMsg = self::QUIRKS_TYPES[$quirks]; |
| 140 | return "doctype $name, public=\"$public\", system=\"$system\", " . |
| 141 | "$quirksMsg, pos=$sourceStart, len=$sourceLength"; |
| 142 | } |
| 143 | |
| 144 | /** |
| 145 | * @param int $preposition |
| 146 | * @param Element|null $refNode |
| 147 | * @param string $text |
| 148 | * @param int $sourceStart |
| 149 | * @param int $sourceLength |
| 150 | * @return string |
| 151 | */ |
| 152 | public static function comment( $preposition, $refNode, $text, $sourceStart, $sourceLength ) { |
| 153 | $prepName = self::getPrepositionName( $preposition ); |
| 154 | $refTag = self::getDebugTag( $refNode ); |
| 155 | $excerpt = self::excerpt( $text ); |
| 156 | |
| 157 | return "comment \"$excerpt\", $prepName $refTag, pos=$sourceStart, len=$sourceLength"; |
| 158 | } |
| 159 | |
| 160 | /** |
| 161 | * @param string $text |
| 162 | * @param int $pos |
| 163 | * @return string |
| 164 | */ |
| 165 | public static function error( $text, $pos ) { |
| 166 | return "error \"$text\", pos=$pos"; |
| 167 | } |
| 168 | |
| 169 | /** |
| 170 | * @param Element $element |
| 171 | * @param Attributes $attrs |
| 172 | * @param int $sourceStart |
| 173 | * @return string |
| 174 | */ |
| 175 | public static function mergeAttributes( Element $element, Attributes $attrs, $sourceStart ) { |
| 176 | $elementTag = self::getDebugTag( $element ); |
| 177 | return "merge $elementTag, pos=$sourceStart"; |
| 178 | } |
| 179 | |
| 180 | /** |
| 181 | * @param Element $element |
| 182 | * @param int $sourceStart |
| 183 | * @return string |
| 184 | */ |
| 185 | public static function removeNode( Element $element, $sourceStart ) { |
| 186 | $elementTag = self::getDebugTag( $element ); |
| 187 | return "remove $elementTag, pos=$sourceStart"; |
| 188 | } |
| 189 | |
| 190 | /** |
| 191 | * @param Element $element |
| 192 | * @param Element $newParent |
| 193 | * @param int $sourceStart |
| 194 | * @return string |
| 195 | */ |
| 196 | public static function reparentChildren( Element $element, Element $newParent, $sourceStart ) { |
| 197 | $elementTag = self::getDebugTag( $element ); |
| 198 | $newParentTag = self::getDebugTag( $newParent ); |
| 199 | return "reparent children of $elementTag under $newParentTag, pos=$sourceStart"; |
| 200 | } |
| 201 | } |