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 = [
64 // Those defined in tidy.conf
73 private static $formattingElements = [
91 * @param Serializer $serializer
93 public function __construct( Serializer
$serializer ) {
94 $this->serializer
= $serializer;
97 public function startDocument( $fragmentNamespace, $fragmentName ) {
98 $this->serializer
->startDocument( $fragmentNamespace, $fragmentName );
99 $root = $this->serializer
->getRootNode();
100 $root->snData
= new RemexMungerData
;
101 $root->snData
->needsPWrapping
= true;
104 public function endDocument( $pos ) {
105 $this->serializer
->endDocument( $pos );
108 private function getParentForInsert( $preposition, $refElement ) {
109 if ( $preposition === TreeBuilder
::ROOT
) {
110 return [ $this->serializer
->getRootNode(), null ];
111 } elseif ( $preposition === TreeBuilder
::BEFORE
) {
112 $refNode = $refElement->userData
;
113 return [ $this->serializer
->getParentNode( $refNode ), $refNode ];
115 $refNode = $refElement->userData
;
116 $refData = $refNode->snData
;
117 if ( $refData->currentCloneElement
) {
118 // Follow a chain of clone links if necessary
119 $origRefData = $refData;
120 while ( $refData->currentCloneElement
) {
121 $refElement = $refData->currentCloneElement
;
122 $refNode = $refElement->userData
;
123 $refData = $refNode->snData
;
125 // Cache the end of the chain in the requested element
126 $origRefData->currentCloneElement
= $refElement;
127 } elseif ( $refData->childPElement
) {
128 $refElement = $refData->childPElement
;
129 $refNode = $refElement->userData
;
131 return [ $refNode, $refNode ];
138 * @param SerializerNode $parent
139 * @param int $sourceStart
140 * @return SerializerNode
142 private function insertPWrapper( SerializerNode
$parent, $sourceStart ) {
143 $pWrap = new Element( HTMLData
::NS_HTML
, 'mw:p-wrap', new PlainAttributes
);
144 $this->serializer
->insertElement( TreeBuilder
::UNDER
, $parent, $pWrap, false,
146 $data = new RemexMungerData
;
147 $data->isPWrapper
= true;
148 $data->wrapBaseNode
= $parent;
149 $pWrap->userData
->snData
= $data;
150 $parent->snData
->childPElement
= $pWrap;
151 return $pWrap->userData
;
154 public function characters( $preposition, $refElement, $text, $start, $length,
155 $sourceStart, $sourceLength
157 $isBlank = strspn( $text, "\t\n\f\r ", $start, $length ) === $length;
159 list( $parent, $refNode ) = $this->getParentForInsert( $preposition, $refElement );
160 $parentData = $parent->snData
;
162 if ( $preposition === TreeBuilder
::UNDER
) {
163 if ( $parentData->needsPWrapping
&& !$isBlank ) {
164 // Add a p-wrapper for bare text under body/blockquote
165 $refNode = $this->insertPWrapper( $refNode, $sourceStart );
167 $parentData = $parent->snData
;
168 } elseif ( $parentData->isSplittable
&& !$parentData->ancestorPNode
) {
169 // The parent is splittable and in block mode, so split the tag stack
170 $refNode = $this->splitTagStack( $refNode, true, $sourceStart );
172 $parentData = $parent->snData
;
177 // Non-whitespace characters detected
178 $parentData->nonblankNodeCount++
;
180 $this->serializer
->characters( $preposition, $refNode, $text, $start,
181 $length, $sourceStart, $sourceLength );
184 private function trace( $msg ) {
185 // echo "[RCM] $msg\n";
189 * Insert or reparent an element. Create p-wrappers or split the tag stack
192 * Consider the following insertion locations. The parent may be:
194 * - A: A body or blockquote (!!needsPWrapping)
195 * - B: A p-wrapper (!!isPWrapper)
196 * - C: A descendant of a p-wrapper (!!ancestorPNode)
197 * - CS: With splittable formatting elements in the stack region up to
199 * - CU: With one or more unsplittable elements in the stack region up
201 * - D: Not a descendant of a p-wrapper (!ancestorNode)
202 * - DS: With splittable formatting elements in the stack region up to
203 * the body or blockquote
204 * - DU: With one or more unsplittable elements in the stack region up
205 * to the body or blockquote
207 * And consider that we may insert two types of element:
211 * We handle the insertion as follows:
213 * - A/i: Create a p-wrapper, insert under it
214 * - A/b: Insert as normal
215 * - B/i: Insert as normal
216 * - B/b: Close the p-wrapper, insert under the body/blockquote (wrap
218 * - C/i: Insert as normal
219 * - CS/b: Split the tag stack, insert the block under cloned formatting
220 * elements which have the wrap base (the parent of the p-wrap) as
221 * their ultimate parent.
222 * - CU/b: Disable the p-wrap, by reparenting the currently open child
223 * of the p-wrap under the p-wrap's parent. Then insert the block as
225 * - D/b: Insert as normal
226 * - DS/i: Split the tag stack, creating a new p-wrapper as the ultimate
227 * parent of the formatting elements thus cloned. The parent of the
228 * p-wrapper is the body or blockquote.
229 * - DU/i: Insert as normal
231 * FIXME: fostering ($preposition == BEFORE) is mostly done by inserting as
232 * normal, the full algorithm is not followed.
234 * @param int $preposition
235 * @param Element|SerializerNode|null $refElement
236 * @param Element $element
238 * @param int $sourceStart
239 * @param int $sourceLength
241 public function insertElement( $preposition, $refElement, Element
$element, $void,
242 $sourceStart, $sourceLength
244 list( $parent, $newRef ) = $this->getParentForInsert( $preposition, $refElement );
245 $parentData = $parent->snData
;
246 $parentNs = $parent->namespace;
247 $parentName = $parent->name
;
248 $elementName = $element->htmlName
;
250 $inline = isset( self
::$onlyInlineElements[$elementName] );
251 $under = $preposition === TreeBuilder
::UNDER
;
253 if ( $under && $parentData->isPWrapper
&& !$inline ) {
254 // [B/b] The element is non-inline and the parent is a p-wrapper,
255 // close the parent and insert into its parent instead
256 $this->trace( 'insert B/b' );
257 $newParent = $this->serializer
->getParentNode( $parent );
258 $parent = $newParent;
259 $parentData = $parent->snData
;
260 $pElement = $parentData->childPElement
;
261 $parentData->childPElement
= null;
262 $newRef = $refElement->userData
;
263 $this->endTag( $pElement, $sourceStart, 0 );
264 } elseif ( $under && $parentData->isSplittable
265 && (bool)$parentData->ancestorPNode
!== $inline
267 // [CS/b, DS/i] The parent is splittable and the current element is
268 // inline in block context, or if the current element is a block
269 // under a p-wrapper, split the tag stack.
270 $this->trace( $inline ?
'insert DS/i' : 'insert CS/b' );
271 $newRef = $this->splitTagStack( $newRef, $inline, $sourceStart );
273 $parentData = $parent->snData
;
274 } elseif ( $under && $parentData->needsPWrapping
&& $inline ) {
275 // [A/i] If the element is inline and we are in body/blockquote,
276 // we need to create a p-wrapper
277 $this->trace( 'insert A/i' );
278 $newRef = $this->insertPWrapper( $newRef, $sourceStart );
280 $parentData = $parent->snData
;
281 } elseif ( $parentData->ancestorPNode
&& !$inline ) {
282 // [CU/b] If the element is non-inline and (despite attempting to
283 // split above) there is still an ancestor p-wrap, disable that
285 $this->trace( 'insert CU/b' );
286 $this->disablePWrapper( $parent, $sourceStart );
288 // [A/b, B/i, C/i, D/b, DU/i] insert as normal
289 $this->trace( 'insert normal' );
292 // An element with element children is a non-blank element
293 $parentData->nonblankNodeCount++
;
295 // Insert the element downstream and so initialise its userData
296 $this->serializer
->insertElement( $preposition, $newRef,
297 $element, $void, $sourceStart, $sourceLength );
300 if ( !$element->userData
->snData
) {
301 $elementData = $element->userData
->snData
= new RemexMungerData
;
303 $elementData = $element->userData
->snData
;
305 if ( ( $parentData->isPWrapper ||
$parentData->isSplittable
)
306 && isset( self
::$formattingElements[$elementName] )
308 $elementData->isSplittable
= true;
310 if ( $parentData->isPWrapper
) {
311 $elementData->ancestorPNode
= $parent;
312 } elseif ( $parentData->ancestorPNode
) {
313 $elementData->ancestorPNode
= $parentData->ancestorPNode
;
315 if ( $parentData->wrapBaseNode
) {
316 $elementData->wrapBaseNode
= $parentData->wrapBaseNode
;
317 } elseif ( $parentData->needsPWrapping
) {
318 $elementData->wrapBaseNode
= $parent;
320 if ( $elementName === 'body'
321 ||
$elementName === 'blockquote'
322 ||
$elementName === 'html'
324 $elementData->needsPWrapping
= true;
329 * Clone nodes in a stack range and return the new parent
331 * @param SerializerNode $parentNode
332 * @param bool $inline
333 * @param int $pos The source position
334 * @return SerializerNode
336 private function splitTagStack( SerializerNode
$parentNode, $inline, $pos ) {
337 $parentData = $parentNode->snData
;
338 $wrapBase = $parentData->wrapBaseNode
;
339 $pWrap = $parentData->ancestorPNode
;
341 $cloneEnd = $wrapBase;
343 $cloneEnd = $parentData->ancestorPNode
;
346 $serializer = $this->serializer
;
348 $root = $serializer->getRootNode();
350 $removableNodes = [];
351 $haveContent = false;
352 while ( $node !== $cloneEnd ) {
353 $nextParent = $serializer->getParentNode( $node );
354 if ( $nextParent === $root ) {
355 throw new \
Exception( 'Did not find end of clone range' );
358 if ( $node->snData
->nonblankNodeCount
=== 0 ) {
359 $removableNodes[] = $node;
360 $nextParent->snData
->nonblankNodeCount
--;
366 $pWrap = $this->insertPWrapper( $wrapBase, $pos );
370 // End the p-wrap which was open, cancel the diversion
371 $wrapBase->snData
->childPElement
= null;
377 for ( $i = count( $nodes ) - 1; $i >= 0; $i-- ) {
378 $oldNode = $nodes[$i];
379 $oldData = $oldNode->snData
;
381 $element = new Element( $oldNode->namespace, $oldNode->name
, $oldNode->attrs
);
382 $this->serializer
->insertElement( TreeBuilder
::UNDER
, $nodeParent,
383 $element, false, $pos, 0 );
384 $oldData->currentCloneElement
= $element;
386 $newNode = $element->userData
;
387 $newData = $newNode->snData
= new RemexMungerData
;
389 $newData->ancestorPNode
= $pWrap;
391 $newData->isSplittable
= true;
392 $newData->wrapBaseNode
= $wrapBase;
393 $newData->isPWrapper
= $oldData->isPWrapper
;
395 $nodeParent->snData
->nonblankNodeCount++
;
399 foreach ( $removableNodes as $rNode ) {
400 $fakeElement = new Element( $rNode->namespace, $rNode->name
, $rNode->attrs
);
401 $fakeElement->userData
= $rNode;
402 $this->serializer
->removeNode( $fakeElement, $pos );
408 * Find the ancestor of $node which is a child of a p-wrapper, and
409 * reparent that node so that it is placed after the end of the p-wrapper
411 private function disablePWrapper( SerializerNode
$node, $sourceStart ) {
412 $nodeData = $node->snData
;
413 $pWrapNode = $nodeData->ancestorPNode
;
414 $newParent = $this->serializer
->getParentNode( $pWrapNode );
415 if ( $pWrapNode !== $this->serializer
->getLastChild( $newParent ) ) {
416 // Fostering or something? Abort!
422 $victim = $nextParent;
423 $victim->snData
->ancestorPNode
= null;
424 $nextParent = $this->serializer
->getParentNode( $victim );
425 } while ( $nextParent !== $pWrapNode );
427 // Make a fake Element to use in a reparenting operation
428 $victimElement = new Element( $victim->namespace, $victim->name
, $victim->attrs
);
429 $victimElement->userData
= $victim;
432 $this->serializer
->insertElement( TreeBuilder
::UNDER
, $newParent, $victimElement,
433 false, $sourceStart, 0 );
435 // Decrement nonblank node count
436 $pWrapNode->snData
->nonblankNodeCount
--;
438 // Cancel the diversion so that no more elements are inserted under this p-wrap
439 $newParent->snData
->childPElement
= null;
442 public function endTag( Element
$element, $sourceStart, $sourceLength ) {
443 $data = $element->userData
->snData
;
444 if ( $data->childPElement
) {
445 $this->endTag( $data->childPElement
, $sourceStart, 0 );
447 $this->serializer
->endTag( $element, $sourceStart, $sourceLength );
448 $element->userData
->snData
= null;
449 $element->userData
= null;
452 public function doctype( $name, $public, $system, $quirks, $sourceStart, $sourceLength ) {
453 $this->serializer
->doctype( $name, $public, $system, $quirks,
454 $sourceStart, $sourceLength );
457 public function comment( $preposition, $refElement, $text, $sourceStart, $sourceLength ) {
458 list( $parent, $refNode ) = $this->getParentForInsert( $preposition, $refElement );
459 $this->serializer
->comment( $preposition, $refNode, $text,
460 $sourceStart, $sourceLength );
463 public function error( $text, $pos ) {
464 $this->serializer
->error( $text, $pos );
467 public function mergeAttributes( Element
$element, Attributes
$attrs, $sourceStart ) {
468 $this->serializer
->mergeAttributes( $element, $attrs, $sourceStart );
471 public function removeNode( Element
$element, $sourceStart ) {
472 $this->serializer
->removeNode( $element, $sourceStart );
475 public function reparentChildren( Element
$element, Element
$newParent, $sourceStart ) {
476 $self = $element->userData
;
477 if ( $self->snData
->childPElement
) {
478 // Reparent under the p-wrapper instead, so that e.g.
479 // <blockquote><mw:p-wrap>...</mw:p-wrap></blockquote>
481 // <blockquote><mw:p-wrap><i>...</i></mw:p-wrap></blockquote>
483 // The formatting element should not be the parent of the p-wrap.
484 // Without this special case, the insertElement() of the <i> below
485 // would be diverted into the p-wrapper, causing infinite recursion
487 $this->reparentChildren( $self->snData
->childPElement
, $newParent, $sourceStart );
491 $children = $self->children
;
492 $self->children
= [];
493 $this->insertElement( TreeBuilder
::UNDER
, $element, $newParent, false, $sourceStart, 0 );
494 $newParentNode = $newParent->userData
;
495 $newParentId = $newParentNode->id
;
496 foreach ( $children as $child ) {
497 if ( is_object( $child ) ) {
498 $this->trace( "reparent <{$child->name}>" );
499 $child->parentId
= $newParentId;
502 $newParentNode->children
= $children;