Debian Codegraph @

vendor/wikimedia/remex-html/src/TreeBuilder/TraceFormatter.php

raw
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 /** @inheritDoc */
53 public static function startDocument( $fns, $fn ) {
54 return "startDocument";
55 }
56
57 /** @inheritDoc */
58 public static function endDocument( $pos ) {
59 return "endDocument pos=$pos";
60 }
61
62 /** @inheritDoc */
63 public static function characters( $preposition, $refNode, $text, $start, $length,
64 $sourceStart, $sourceLength
65 ) {
66 $excerpt = self::excerpt( substr( $text, $start, $length ) );
67 $prepName = self::getPrepositionName( $preposition );
68 $refTag = self::getDebugTag( $refNode );
69
70 return "characters \"$excerpt\", $prepName $refTag, pos=$sourceStart, len=$sourceLength";
71 }
72
73 /** @inheritDoc */
74 public static function insertElement( $preposition, $refNode, Element $element, $void,
75 $sourceStart, $sourceLength
76 ) {
77 $prepName = self::getPrepositionName( $preposition );
78 $refTag = self::getDebugTag( $refNode );
79 $elementTag = self::getDebugTag( $element );
80 $voidMsg = $void ? 'void' : '';
81 return "insert $elementTag $voidMsg, $prepName $refTag, pos=$sourceStart, len=$sourceLength";
82 }
83
84 /** @inheritDoc */
85 public static function endTag( Element $element, $sourceStart, $sourceLength ) {
86 $elementTag = self::getDebugTag( $element );
87 return "end $elementTag, pos=$sourceStart, len=$sourceLength";
88 }
89
90 /** @inheritDoc */
91 public static function doctype( $name, $public, $system, $quirks, $sourceStart, $sourceLength
92 ) {
93 $quirksTypes = [
94 TreeBuilder::QUIRKS => 'quirks',
95 TreeBuilder::NO_QUIRKS => 'no-quirks',
96 TreeBuilder::LIMITED_QUIRKS => 'limited-quirks'
97 ];
98 $quirksMsg = $quirksTypes[$quirks];
99 return "doctype $name, public=\"$public\", system=\"$system\", " .
100 "$quirksMsg, pos=$sourceStart, len=$sourceLength";
101 }
102
103 /** @inheritDoc */
104 public static function comment( $preposition, $refNode, $text, $sourceStart, $sourceLength ) {
105 $prepName = self::getPrepositionName( $preposition );
106 $refTag = self::getDebugTag( $refNode );
107 $excerpt = self::excerpt( $text );
108
109 return "comment \"$excerpt\", $prepName $refTag, pos=$sourceStart, len=$sourceLength";
110 }
111
112 /** @inheritDoc */
113 public static function error( $text, $pos ) {
114 return "error \"$text\", pos=$pos";
115 }
116
117 /** @inheritDoc */
118 public static function mergeAttributes( Element $element, Attributes $attrs, $sourceStart ) {
119 $elementTag = self::getDebugTag( $element );
120 return "merge $elementTag, pos=$sourceStart";
121 }
122
123 /** @inheritDoc */
124 public static function removeNode( Element $element, $sourceStart ) {
125 $elementTag = self::getDebugTag( $element );
126 return "remove $elementTag, pos=$sourceStart";
127 }
128
129 /** @inheritDoc */
130 public static function reparentChildren( Element $element, Element $newParent, $sourceStart ) {
131 $elementTag = self::getDebugTag( $element );
132 $newParentTag = self::getDebugTag( $newParent );
133 return "reparent children of $elementTag under $newParentTag, pos=$sourceStart";
134 }
135 }