MediaWiki REL1_39
RemexCompatMunger.php
Go to the documentation of this file.
1<?php
2
3namespace MediaWiki\Tidy;
4
5use Wikimedia\RemexHtml\HTMLData;
6use Wikimedia\RemexHtml\Serializer\Serializer;
7use Wikimedia\RemexHtml\Serializer\SerializerNode;
8use Wikimedia\RemexHtml\Tokenizer\Attributes;
9use Wikimedia\RemexHtml\Tokenizer\PlainAttributes;
10use Wikimedia\RemexHtml\TreeBuilder\Element;
11use Wikimedia\RemexHtml\TreeBuilder\TreeBuilder;
12use Wikimedia\RemexHtml\TreeBuilder\TreeHandler;
13
17class RemexCompatMunger implements TreeHandler {
18 private static $onlyInlineElements = [
19 "a" => true,
20 "abbr" => true,
21 "acronym" => true,
22 "applet" => true,
23 "b" => true,
24 "basefont" => true,
25 "bdo" => true,
26 "big" => true,
27 "br" => true,
28 "button" => true,
29 "cite" => true,
30 "code" => true,
31 "del" => true,
32 "dfn" => true,
33 "em" => true,
34 "font" => true,
35 "i" => true,
36 "iframe" => true,
37 "img" => true,
38 "input" => true,
39 "ins" => true,
40 "kbd" => true,
41 "label" => true,
42 "legend" => true,
43 "map" => true,
44 "object" => true,
45 "param" => true,
46 "q" => true,
47 "rb" => true,
48 "rbc" => true,
49 "rp" => true,
50 "rt" => true,
51 "rtc" => true,
52 "ruby" => true,
53 "s" => true,
54 "samp" => true,
55 "select" => true,
56 "small" => true,
57 "span" => true,
58 "strike" => true,
59 "strong" => true,
60 "sub" => true,
61 "sup" => true,
62 "textarea" => true,
63 "tt" => true,
64 "u" => true,
65 "var" => true,
66 // Those defined in tidy.conf
67 "video" => true,
68 "audio" => true,
69 "bdi" => true,
70 "data" => true,
71 "time" => true,
72 "mark" => true,
73 ];
74
83 private static $metadataElements = [
84 'style' => true,
85 'script' => true,
86 'link' => true,
87 'meta' => true,
88 ];
89
90 private static $formattingElements = [
91 'a' => true,
92 'b' => true,
93 'big' => true,
94 'code' => true,
95 'em' => true,
96 'font' => true,
97 'i' => true,
98 'nobr' => true,
99 's' => true,
100 'small' => true,
101 'strike' => true,
102 'strong' => true,
103 'tt' => true,
104 'u' => true,
105 ];
106
108 private $serializer;
109
111 private $trace;
112
117 public function __construct( Serializer $serializer, $trace = false ) {
118 $this->serializer = $serializer;
119 $this->trace = $trace;
120 }
121
122 public function startDocument( $fragmentNamespace, $fragmentName ) {
123 $this->serializer->startDocument( $fragmentNamespace, $fragmentName );
124 $root = $this->serializer->getRootNode();
125 $root->snData = new RemexMungerData;
126 $root->snData->needsPWrapping = true;
127 }
128
129 public function endDocument( $pos ) {
130 $this->serializer->endDocument( $pos );
131 }
132
133 private function getParentForInsert( $preposition, $refElement ) {
134 if ( $preposition === TreeBuilder::ROOT ) {
135 return [ $this->serializer->getRootNode(), null ];
136 } elseif ( $preposition === TreeBuilder::BEFORE ) {
137 $refNode = $refElement->userData;
138 return [ $this->serializer->getParentNode( $refNode ), $refNode ];
139 } else {
140 $refNode = $refElement->userData;
141 $refData = $refNode->snData;
142 if ( $refData->currentCloneElement ) {
143 // Follow a chain of clone links if necessary
144 $origRefData = $refData;
145 while ( $refData->currentCloneElement ) {
146 $refElement = $refData->currentCloneElement;
147 $refNode = $refElement->userData;
148 $refData = $refNode->snData;
149 }
150 // Cache the end of the chain in the requested element
151 $origRefData->currentCloneElement = $refElement;
152 } elseif ( $refData->childPElement ) {
153 $refElement = $refData->childPElement;
154 $refNode = $refElement->userData;
155 }
156 return [ $refNode, $refNode ];
157 }
158 }
159
167 private function insertPWrapper( SerializerNode $parent, $sourceStart ) {
168 $pWrap = new Element( HTMLData::NS_HTML, 'mw:p-wrap', new PlainAttributes );
169 $this->serializer->insertElement( TreeBuilder::UNDER, $parent, $pWrap, false,
170 $sourceStart, 0 );
171 $data = new RemexMungerData;
172 $data->isPWrapper = true;
173 $data->wrapBaseNode = $parent;
174 $pWrap->userData->snData = $data;
175 $parent->snData->childPElement = $pWrap;
176 return $pWrap->userData;
177 }
178
179 public function characters( $preposition, $refElement, $text, $start, $length,
180 $sourceStart, $sourceLength
181 ) {
182 $isBlank = strspn( $text, "\t\n\f\r ", $start, $length ) === $length;
183
184 list( $parent, $refNode ) = $this->getParentForInsert( $preposition, $refElement );
185 $parentData = $parent->snData;
186
187 if ( $preposition === TreeBuilder::UNDER ) {
188 if ( $parentData->needsPWrapping && !$isBlank ) {
189 // Add a p-wrapper for bare text under body/blockquote
190 $refNode = $this->insertPWrapper( $refNode, $sourceStart );
191 $parent = $refNode;
192 $parentData = $parent->snData;
193 } elseif ( $parentData->isSplittable && !$parentData->ancestorPNode ) {
194 // The parent is splittable and in block mode, so split the tag stack
195 $refNode = $this->splitTagStack( $refNode, true, $sourceStart );
196 $parent = $refNode;
197 $parentData = $parent->snData;
198 }
199 }
200
201 if ( !$isBlank ) {
202 // Non-whitespace characters detected
203 $parentData->nonblankNodeCount++;
204 }
205 $this->serializer->characters( $preposition, $refNode, $text, $start,
206 $length, $sourceStart, $sourceLength );
207 }
208
209 private function trace( $msg ) {
210 if ( $this->trace ) {
211 wfDebug( "[RCM] $msg" );
212 }
213 }
214
268 public function insertElement( $preposition, $refElement, Element $element, $void,
269 $sourceStart, $sourceLength
270 ) {
271 list( $parent, $newRef ) = $this->getParentForInsert( $preposition, $refElement );
272 $parentData = $parent->snData;
273 $elementName = $element->htmlName;
274
275 $inline = isset( self::$onlyInlineElements[$elementName] );
276 $under = $preposition === TreeBuilder::UNDER;
277
278 if ( isset( self::$metadataElements[$elementName] ) ) {
279 // The element is a metadata element, that we allow to appear in
280 // both inline and block contexts.
281 $this->trace( 'insert metadata' );
282 } elseif ( $under && $parentData->isPWrapper && !$inline ) {
283 // [B/b] The element is non-inline and the parent is a p-wrapper,
284 // close the parent and insert into its parent instead
285 $this->trace( 'insert B/b' );
286 $newParent = $this->serializer->getParentNode( $parent );
287 $parent = $newParent;
288 $parentData = $parent->snData;
289 $parentData->childPElement = null;
290 $newRef = $refElement->userData;
291 } elseif ( $under && $parentData->isSplittable
292 && (bool)$parentData->ancestorPNode !== $inline
293 ) {
294 // [CS/b, DS/i] The parent is splittable and the current element is
295 // inline in block context, or if the current element is a block
296 // under a p-wrapper, split the tag stack.
297 $this->trace( $inline ? 'insert DS/i' : 'insert CS/b' );
298 $newRef = $this->splitTagStack( $newRef, $inline, $sourceStart );
299 $parent = $newRef;
300 $parentData = $parent->snData;
301 } elseif ( $under && $parentData->needsPWrapping && $inline ) {
302 // [A/i] If the element is inline and we are in body/blockquote,
303 // we need to create a p-wrapper
304 $this->trace( 'insert A/i' );
305 $newRef = $this->insertPWrapper( $newRef, $sourceStart );
306 $parent = $newRef;
307 $parentData = $parent->snData;
308 } elseif ( $parentData->ancestorPNode && !$inline ) {
309 // [CU/b] If the element is non-inline and (despite attempting to
310 // split above) there is still an ancestor p-wrap, disable that
311 // p-wrap
312 $this->trace( 'insert CU/b' );
313 $this->disablePWrapper( $parent, $sourceStart );
314 } else {
315 // [A/b, B/i, C/i, D/b, DU/i] insert as normal
316 $this->trace( 'insert normal' );
317 }
318
319 // An element with element children is a non-blank element
320 $parentData->nonblankNodeCount++;
321
322 // Insert the element downstream and so initialise its userData
323 $this->serializer->insertElement( $preposition, $newRef,
324 $element, $void, $sourceStart, $sourceLength );
325
326 // Initialise snData
327 if ( !$element->userData->snData ) {
328 $elementData = $element->userData->snData = new RemexMungerData;
329 } else {
330 $elementData = $element->userData->snData;
331 }
332 if ( ( $parentData->isPWrapper || $parentData->isSplittable )
333 && isset( self::$formattingElements[$elementName] )
334 ) {
335 $elementData->isSplittable = true;
336 }
337 if ( $parentData->isPWrapper ) {
338 $elementData->ancestorPNode = $parent;
339 } elseif ( $parentData->ancestorPNode ) {
340 $elementData->ancestorPNode = $parentData->ancestorPNode;
341 }
342 if ( $parentData->wrapBaseNode ) {
343 $elementData->wrapBaseNode = $parentData->wrapBaseNode;
344 } elseif ( $parentData->needsPWrapping ) {
345 $elementData->wrapBaseNode = $parent;
346 }
347 if ( $elementName === 'body'
348 || $elementName === 'blockquote'
349 || $elementName === 'html'
350 ) {
351 $elementData->needsPWrapping = true;
352 }
353 }
354
363 private function splitTagStack( SerializerNode $parentNode, $inline, $pos ) {
364 $parentData = $parentNode->snData;
365 $wrapBase = $parentData->wrapBaseNode;
366 $pWrap = $parentData->ancestorPNode;
367 if ( !$pWrap ) {
368 $cloneEnd = $wrapBase;
369 } else {
370 $cloneEnd = $parentData->ancestorPNode;
371 }
372
373 $serializer = $this->serializer;
374 $node = $parentNode;
375 $root = $serializer->getRootNode();
376 $nodes = [];
377 $removableNodes = [];
378 while ( $node !== $cloneEnd ) {
379 $nextParent = $serializer->getParentNode( $node );
380 if ( $nextParent === $root ) {
381 throw new \Exception( 'Did not find end of clone range' );
382 }
383 $nodes[] = $node;
384 if ( $node->snData->nonblankNodeCount === 0 ) {
385 $removableNodes[] = $node;
386 $nextParent->snData->nonblankNodeCount--;
387 }
388 $node = $nextParent;
389 }
390
391 if ( $inline ) {
392 $pWrap = $this->insertPWrapper( $wrapBase, $pos );
393 $node = $pWrap;
394 } else {
395 if ( $pWrap ) {
396 // End the p-wrap which was open, cancel the diversion
397 $wrapBase->snData->childPElement = null;
398 }
399 $pWrap = null;
400 $node = $wrapBase;
401 }
402
403 for ( $i = count( $nodes ) - 1; $i >= 0; $i-- ) {
404 $oldNode = $nodes[$i];
405 $oldData = $oldNode->snData;
406 $nodeParent = $node;
407 $element = new Element( $oldNode->namespace, $oldNode->name, $oldNode->attrs );
408 $this->serializer->insertElement( TreeBuilder::UNDER, $nodeParent,
409 $element, false, $pos, 0 );
410 $oldData->currentCloneElement = $element;
411
412 $newNode = $element->userData;
413 $newData = $newNode->snData = new RemexMungerData;
414 if ( $pWrap ) {
415 $newData->ancestorPNode = $pWrap;
416 }
417 $newData->isSplittable = true;
418 $newData->wrapBaseNode = $wrapBase;
419 $newData->isPWrapper = $oldData->isPWrapper;
420
421 $nodeParent->snData->nonblankNodeCount++;
422
423 $node = $newNode;
424 }
425 foreach ( $removableNodes as $rNode ) {
426 $fakeElement = new Element( $rNode->namespace, $rNode->name, $rNode->attrs );
427 $fakeElement->userData = $rNode;
428 $this->serializer->removeNode( $fakeElement, $pos );
429 }
430 // @phan-suppress-next-line PhanTypeMismatchReturnNullable False positive
431 return $node;
432 }
433
440 private function disablePWrapper( SerializerNode $node, $sourceStart ) {
441 $nodeData = $node->snData;
442 $pWrapNode = $nodeData->ancestorPNode;
443 $newParent = $this->serializer->getParentNode( $pWrapNode );
444 if ( $pWrapNode !== $this->serializer->getLastChild( $newParent ) ) {
445 // Fostering or something? Abort!
446 return;
447 }
448
449 $nextParent = $node;
450 do {
451 $victim = $nextParent;
452 $victim->snData->ancestorPNode = null;
453 $nextParent = $this->serializer->getParentNode( $victim );
454 } while ( $nextParent !== $pWrapNode );
455
456 // Make a fake Element to use in a reparenting operation
457 $victimElement = new Element( $victim->namespace, $victim->name, $victim->attrs );
458 $victimElement->userData = $victim;
459
460 // Reparent
461 $this->serializer->insertElement( TreeBuilder::UNDER, $newParent, $victimElement,
462 false, $sourceStart, 0 );
463
464 // Decrement nonblank node count
465 $pWrapNode->snData->nonblankNodeCount--;
466
467 // Cancel the diversion so that no more elements are inserted under this p-wrap
468 $newParent->snData->childPElement = null;
469 }
470
471 public function endTag( Element $element, $sourceStart, $sourceLength ) {
472 $data = $element->userData->snData;
473 if ( $data->childPElement ) {
474 $this->endTag( $data->childPElement, $sourceStart, 0 );
475 }
476 $this->serializer->endTag( $element, $sourceStart, $sourceLength );
477 $element->userData->snData = null;
478 $element->userData = null;
479 }
480
481 public function doctype( $name, $public, $system, $quirks, $sourceStart, $sourceLength ) {
482 $this->serializer->doctype( $name, $public, $system, $quirks,
483 $sourceStart, $sourceLength );
484 }
485
486 public function comment( $preposition, $refElement, $text, $sourceStart, $sourceLength ) {
487 list( , $refNode ) = $this->getParentForInsert( $preposition, $refElement );
488 $this->serializer->comment( $preposition, $refNode, $text, $sourceStart, $sourceLength );
489 }
490
491 public function error( $text, $pos ) {
492 $this->serializer->error( $text, $pos );
493 }
494
495 public function mergeAttributes( Element $element, Attributes $attrs, $sourceStart ) {
496 $this->serializer->mergeAttributes( $element, $attrs, $sourceStart );
497 }
498
499 public function removeNode( Element $element, $sourceStart ) {
500 $this->serializer->removeNode( $element, $sourceStart );
501 }
502
503 public function reparentChildren( Element $element, Element $newParent, $sourceStart ) {
504 $self = $element->userData;
505 if ( $self->snData->childPElement ) {
506 // Reparent under the p-wrapper instead, so that e.g.
507 // <blockquote><mw:p-wrap>...</mw:p-wrap></blockquote>
508 // becomes
509 // <blockquote><mw:p-wrap><i>...</i></mw:p-wrap></blockquote>
510
511 // The formatting element should not be the parent of the p-wrap.
512 // Without this special case, the insertElement() of the <i> below
513 // would be diverted into the p-wrapper, causing infinite recursion
514 // (T178632)
515 $this->reparentChildren( $self->snData->childPElement, $newParent, $sourceStart );
516 return;
517 }
518
519 $children = $self->children;
520 $self->children = [];
521 $this->insertElement( TreeBuilder::UNDER, $element, $newParent, false, $sourceStart, 0 );
522 $newParentNode = $newParent->userData;
523 $newParentId = $newParentNode->id;
524 foreach ( $children as $child ) {
525 if ( is_object( $child ) ) {
526 $this->trace( "reparent <{$child->name}>" );
527 $child->parentId = $newParentId;
528 }
529 }
530 $newParentNode->children = $children;
531 }
532}
wfDebug( $text, $dest='all', array $context=[])
Sends a line to the debug log if enabled or, optionally, to a comment in output.
characters( $preposition, $refElement, $text, $start, $length, $sourceStart, $sourceLength)
mergeAttributes(Element $element, Attributes $attrs, $sourceStart)
__construct(Serializer $serializer, $trace=false)
doctype( $name, $public, $system, $quirks, $sourceStart, $sourceLength)
insertElement( $preposition, $refElement, Element $element, $void, $sourceStart, $sourceLength)
Insert or reparent an element.
removeNode(Element $element, $sourceStart)
comment( $preposition, $refElement, $text, $sourceStart, $sourceLength)
startDocument( $fragmentNamespace, $fragmentName)
reparentChildren(Element $element, Element $newParent, $sourceStart)
endTag(Element $element, $sourceStart, $sourceLength)