3 namespace MediaWiki\Tidy
;
5 use RemexHtml\HTMLData
;
6 use RemexHtml\Serializer\Serializer
;
7 use RemexHtml\Serializer\SerializerNode
;
8 use RemexHtml\Tokenizer\Attributes
;
9 use RemexHtml\Tokenizer\PlainAttributes
;
10 use RemexHtml\TreeBuilder\TreeBuilder
;
11 use RemexHtml\TreeBuilder\TreeHandler
;
12 use RemexHtml\TreeBuilder\Element
;
17 class RemexCompatMunger
implements TreeHandler
{
18 private static $onlyInlineElements = [
66 // Those defined in tidy.conf
75 private static $formattingElements = [
93 * @param Serializer $serializer
95 public function __construct( Serializer
$serializer ) {
96 $this->serializer
= $serializer;
99 public function startDocument( $fragmentNamespace, $fragmentName ) {
100 $this->serializer
->startDocument( $fragmentNamespace, $fragmentName );
101 $root = $this->serializer
->getRootNode();
102 $root->snData
= new RemexMungerData
;
103 $root->snData
->needsPWrapping
= true;
106 public function endDocument( $pos ) {
107 $this->serializer
->endDocument( $pos );
110 private function getParentForInsert( $preposition, $refElement ) {
111 if ( $preposition === TreeBuilder
::ROOT
) {
112 return [ $this->serializer
->getRootNode(), null ];
113 } elseif ( $preposition === TreeBuilder
::BEFORE
) {
114 $refNode = $refElement->userData
;
115 return [ $this->serializer
->getParentNode( $refNode ), $refNode ];
117 $refNode = $refElement->userData
;
118 $refData = $refNode->snData
;
119 if ( $refData->currentCloneElement
) {
120 // Follow a chain of clone links if necessary
121 $origRefData = $refData;
122 while ( $refData->currentCloneElement
) {
123 $refElement = $refData->currentCloneElement
;
124 $refNode = $refElement->userData
;
125 $refData = $refNode->snData
;
127 // Cache the end of the chain in the requested element
128 $origRefData->currentCloneElement
= $refElement;
129 } elseif ( $refData->childPElement
) {
130 $refElement = $refData->childPElement
;
131 $refNode = $refElement->userData
;
133 return [ $refNode, $refNode ];
140 * @param SerializerNode $parent
141 * @param int $sourceStart
142 * @return SerializerNode
144 private function insertPWrapper( SerializerNode
$parent, $sourceStart ) {
145 $pWrap = new Element( HTMLData
::NS_HTML
, 'mw:p-wrap', new PlainAttributes
);
146 $this->serializer
->insertElement( TreeBuilder
::UNDER
, $parent, $pWrap, false,
148 $data = new RemexMungerData
;
149 $data->isPWrapper
= true;
150 $data->wrapBaseNode
= $parent;
151 $pWrap->userData
->snData
= $data;
152 $parent->snData
->childPElement
= $pWrap;
153 return $pWrap->userData
;
156 public function characters( $preposition, $refElement, $text, $start, $length,
157 $sourceStart, $sourceLength
159 $isBlank = strspn( $text, "\t\n\f\r ", $start, $length ) === $length;
161 list( $parent, $refNode ) = $this->getParentForInsert( $preposition, $refElement );
162 $parentData = $parent->snData
;
164 if ( $preposition === TreeBuilder
::UNDER
) {
165 if ( $parentData->needsPWrapping
&& !$isBlank ) {
166 // Add a p-wrapper for bare text under body/blockquote
167 $refNode = $this->insertPWrapper( $refNode, $sourceStart );
169 $parentData = $parent->snData
;
170 } elseif ( $parentData->isSplittable
&& !$parentData->ancestorPNode
) {
171 // The parent is splittable and in block mode, so split the tag stack
172 $refNode = $this->splitTagStack( $refNode, true, $sourceStart );
174 $parentData = $parent->snData
;
179 // Non-whitespace characters detected
180 $parentData->nonblankNodeCount++
;
182 $this->serializer
->characters( $preposition, $refNode, $text, $start,
183 $length, $sourceStart, $sourceLength );
186 private function trace( $msg ) {
187 // echo "[RCM] $msg\n";
191 * Insert or reparent an element. Create p-wrappers or split the tag stack
194 * Consider the following insertion locations. The parent may be:
196 * - A: A body or blockquote (!!needsPWrapping)
197 * - B: A p-wrapper (!!isPWrapper)
198 * - C: A descendant of a p-wrapper (!!ancestorPNode)
199 * - CS: With splittable formatting elements in the stack region up to
201 * - CU: With one or more unsplittable elements in the stack region up
203 * - D: Not a descendant of a p-wrapper (!ancestorNode)
204 * - DS: With splittable formatting elements in the stack region up to
205 * the body or blockquote
206 * - DU: With one or more unsplittable elements in the stack region up
207 * to the body or blockquote
209 * And consider that we may insert two types of element:
213 * We handle the insertion as follows:
215 * - A/i: Create a p-wrapper, insert under it
216 * - A/b: Insert as normal
217 * - B/i: Insert as normal
218 * - B/b: Close the p-wrapper, insert under the body/blockquote (wrap
220 * - C/i: Insert as normal
221 * - CS/b: Split the tag stack, insert the block under cloned formatting
222 * elements which have the wrap base (the parent of the p-wrap) as
223 * their ultimate parent.
224 * - CU/b: Disable the p-wrap, by reparenting the currently open child
225 * of the p-wrap under the p-wrap's parent. Then insert the block as
227 * - D/b: Insert as normal
228 * - DS/i: Split the tag stack, creating a new p-wrapper as the ultimate
229 * parent of the formatting elements thus cloned. The parent of the
230 * p-wrapper is the body or blockquote.
231 * - DU/i: Insert as normal
233 * FIXME: fostering ($preposition == BEFORE) is mostly done by inserting as
234 * normal, the full algorithm is not followed.
236 * @param int $preposition
237 * @param Element|SerializerNode|null $refElement
238 * @param Element $element
240 * @param int $sourceStart
241 * @param int $sourceLength
243 public function insertElement( $preposition, $refElement, Element
$element, $void,
244 $sourceStart, $sourceLength
246 list( $parent, $newRef ) = $this->getParentForInsert( $preposition, $refElement );
247 $parentData = $parent->snData
;
248 $parentNs = $parent->namespace;
249 $parentName = $parent->name
;
250 $elementName = $element->htmlName
;
252 $inline = isset( self
::$onlyInlineElements[$elementName] );
253 $under = $preposition === TreeBuilder
::UNDER
;
255 if ( $under && $parentData->isPWrapper
&& !$inline ) {
256 // [B/b] The element is non-inline and the parent is a p-wrapper,
257 // close the parent and insert into its parent instead
258 $this->trace( 'insert B/b' );
259 $newParent = $this->serializer
->getParentNode( $parent );
260 $parent = $newParent;
261 $parentData = $parent->snData
;
262 $pElement = $parentData->childPElement
;
263 $parentData->childPElement
= null;
264 $newRef = $refElement->userData
;
265 $this->endTag( $pElement, $sourceStart, 0 );
266 } elseif ( $under && $parentData->isSplittable
267 && (bool)$parentData->ancestorPNode
!== $inline
269 // [CS/b, DS/i] The parent is splittable and the current element is
270 // inline in block context, or if the current element is a block
271 // under a p-wrapper, split the tag stack.
272 $this->trace( $inline ?
'insert DS/i' : 'insert CS/b' );
273 $newRef = $this->splitTagStack( $newRef, $inline, $sourceStart );
275 $parentData = $parent->snData
;
276 } elseif ( $under && $parentData->needsPWrapping
&& $inline ) {
277 // [A/i] If the element is inline and we are in body/blockquote,
278 // we need to create a p-wrapper
279 $this->trace( 'insert A/i' );
280 $newRef = $this->insertPWrapper( $newRef, $sourceStart );
282 $parentData = $parent->snData
;
283 } elseif ( $parentData->ancestorPNode
&& !$inline ) {
284 // [CU/b] If the element is non-inline and (despite attempting to
285 // split above) there is still an ancestor p-wrap, disable that
287 $this->trace( 'insert CU/b' );
288 $this->disablePWrapper( $parent, $sourceStart );
290 // [A/b, B/i, C/i, D/b, DU/i] insert as normal
291 $this->trace( 'insert normal' );
294 // An element with element children is a non-blank element
295 $parentData->nonblankNodeCount++
;
297 // Insert the element downstream and so initialise its userData
298 $this->serializer
->insertElement( $preposition, $newRef,
299 $element, $void, $sourceStart, $sourceLength );
302 if ( !$element->userData
->snData
) {
303 $elementData = $element->userData
->snData
= new RemexMungerData
;
305 $elementData = $element->userData
->snData
;
307 if ( ( $parentData->isPWrapper ||
$parentData->isSplittable
)
308 && isset( self
::$formattingElements[$elementName] )
310 $elementData->isSplittable
= true;
312 if ( $parentData->isPWrapper
) {
313 $elementData->ancestorPNode
= $parent;
314 } elseif ( $parentData->ancestorPNode
) {
315 $elementData->ancestorPNode
= $parentData->ancestorPNode
;
317 if ( $parentData->wrapBaseNode
) {
318 $elementData->wrapBaseNode
= $parentData->wrapBaseNode
;
319 } elseif ( $parentData->needsPWrapping
) {
320 $elementData->wrapBaseNode
= $parent;
322 if ( $elementName === 'body'
323 ||
$elementName === 'blockquote'
324 ||
$elementName === 'html'
326 $elementData->needsPWrapping
= true;
331 * Clone nodes in a stack range and return the new parent
333 * @param SerializerNode $parentNode
334 * @param bool $inline
335 * @param int $pos The source position
336 * @return SerializerNode
338 private function splitTagStack( SerializerNode
$parentNode, $inline, $pos ) {
339 $parentData = $parentNode->snData
;
340 $wrapBase = $parentData->wrapBaseNode
;
341 $pWrap = $parentData->ancestorPNode
;
343 $cloneEnd = $wrapBase;
345 $cloneEnd = $parentData->ancestorPNode
;
348 $serializer = $this->serializer
;
350 $root = $serializer->getRootNode();
352 $removableNodes = [];
353 $haveContent = false;
354 while ( $node !== $cloneEnd ) {
355 $nextParent = $serializer->getParentNode( $node );
356 if ( $nextParent === $root ) {
357 throw new \
Exception( 'Did not find end of clone range' );
360 if ( $node->snData
->nonblankNodeCount
=== 0 ) {
361 $removableNodes[] = $node;
362 $nextParent->snData
->nonblankNodeCount
--;
368 $pWrap = $this->insertPWrapper( $wrapBase, $pos );
372 // End the p-wrap which was open, cancel the diversion
373 $wrapBase->snData
->childPElement
= null;
379 for ( $i = count( $nodes ) - 1; $i >= 0; $i-- ) {
380 $oldNode = $nodes[$i];
381 $oldData = $oldNode->snData
;
383 $element = new Element( $oldNode->namespace, $oldNode->name
, $oldNode->attrs
);
384 $this->serializer
->insertElement( TreeBuilder
::UNDER
, $nodeParent,
385 $element, false, $pos, 0 );
386 $oldData->currentCloneElement
= $element;
388 $newNode = $element->userData
;
389 $newData = $newNode->snData
= new RemexMungerData
;
391 $newData->ancestorPNode
= $pWrap;
393 $newData->isSplittable
= true;
394 $newData->wrapBaseNode
= $wrapBase;
395 $newData->isPWrapper
= $oldData->isPWrapper
;
397 $nodeParent->snData
->nonblankNodeCount++
;
401 foreach ( $removableNodes as $rNode ) {
402 $fakeElement = new Element( $rNode->namespace, $rNode->name
, $rNode->attrs
);
403 $fakeElement->userData
= $rNode;
404 $this->serializer
->removeNode( $fakeElement, $pos );
410 * Find the ancestor of $node which is a child of a p-wrapper, and
411 * reparent that node so that it is placed after the end of the p-wrapper
413 private function disablePWrapper( SerializerNode
$node, $sourceStart ) {
414 $nodeData = $node->snData
;
415 $pWrapNode = $nodeData->ancestorPNode
;
416 $newParent = $this->serializer
->getParentNode( $pWrapNode );
417 if ( $pWrapNode !== $this->serializer
->getLastChild( $newParent ) ) {
418 // Fostering or something? Abort!
424 $victim = $nextParent;
425 $victim->snData
->ancestorPNode
= null;
426 $nextParent = $this->serializer
->getParentNode( $victim );
427 } while ( $nextParent !== $pWrapNode );
429 // Make a fake Element to use in a reparenting operation
430 $victimElement = new Element( $victim->namespace, $victim->name
, $victim->attrs
);
431 $victimElement->userData
= $victim;
434 $this->serializer
->insertElement( TreeBuilder
::UNDER
, $newParent, $victimElement,
435 false, $sourceStart, 0 );
437 // Decrement nonblank node count
438 $pWrapNode->snData
->nonblankNodeCount
--;
440 // Cancel the diversion so that no more elements are inserted under this p-wrap
441 $newParent->snData
->childPElement
= null;
444 public function endTag( Element
$element, $sourceStart, $sourceLength ) {
445 $data = $element->userData
->snData
;
446 if ( $data->childPElement
) {
447 $this->endTag( $data->childPElement
, $sourceStart, 0 );
449 $this->serializer
->endTag( $element, $sourceStart, $sourceLength );
450 $element->userData
->snData
= null;
451 $element->userData
= null;
454 public function doctype( $name, $public, $system, $quirks, $sourceStart, $sourceLength ) {
455 $this->serializer
->doctype( $name, $public, $system, $quirks,
456 $sourceStart, $sourceLength );
459 public function comment( $preposition, $refElement, $text, $sourceStart, $sourceLength ) {
460 list( $parent, $refNode ) = $this->getParentForInsert( $preposition, $refElement );
461 $this->serializer
->comment( $preposition, $refNode, $text,
462 $sourceStart, $sourceLength );
465 public function error( $text, $pos ) {
466 $this->serializer
->error( $text, $pos );
469 public function mergeAttributes( Element
$element, Attributes
$attrs, $sourceStart ) {
470 $this->serializer
->mergeAttributes( $element, $attrs, $sourceStart );
473 public function removeNode( Element
$element, $sourceStart ) {
474 $this->serializer
->removeNode( $element, $sourceStart );
477 public function reparentChildren( Element
$element, Element
$newParent, $sourceStart ) {
478 $self = $element->userData
;
479 if ( $self->snData
->childPElement
) {
480 // Reparent under the p-wrapper instead, so that e.g.
481 // <blockquote><mw:p-wrap>...</mw:p-wrap></blockquote>
483 // <blockquote><mw:p-wrap><i>...</i></mw:p-wrap></blockquote>
485 // The formatting element should not be the parent of the p-wrap.
486 // Without this special case, the insertElement() of the <i> below
487 // would be diverted into the p-wrapper, causing infinite recursion
489 $this->reparentChildren( $self->snData
->childPElement
, $newParent, $sourceStart );
493 $children = $self->children
;
494 $self->children
= [];
495 $this->insertElement( TreeBuilder
::UNDER
, $element, $newParent, false, $sourceStart, 0 );
496 $newParentNode = $newParent->userData
;
497 $newParentId = $newParentNode->id
;
498 foreach ( $children as $child ) {
499 if ( is_object( $child ) ) {
500 $this->trace( "reparent <{$child->name}>" );
501 $child->parentId
= $newParentId;
504 $newParentNode->children
= $children;