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 private static $formattingElements = [
86 * @param Serializer $serializer
88 public function __construct( Serializer
$serializer ) {
89 $this->serializer
= $serializer;
92 public function startDocument( $fragmentNamespace, $fragmentName ) {
93 $this->serializer
->startDocument( $fragmentNamespace, $fragmentName );
94 $root = $this->serializer
->getRootNode();
95 $root->snData
= new RemexMungerData
;
96 $root->snData
->needsPWrapping
= true;
99 public function endDocument( $pos ) {
100 $this->serializer
->endDocument( $pos );
103 private function getParentForInsert( $preposition, $refElement ) {
104 if ( $preposition === TreeBuilder
::ROOT
) {
105 return [ $this->serializer
->getRootNode(), null ];
106 } elseif ( $preposition === TreeBuilder
::BEFORE
) {
107 $refNode = $refElement->userData
;
108 return [ $this->serializer
->getParentNode( $refNode ), $refNode ];
110 $refNode = $refElement->userData
;
111 $refData = $refNode->snData
;
112 if ( $refData->currentCloneElement
) {
113 // Follow a chain of clone links if necessary
114 $origRefData = $refData;
115 while ( $refData->currentCloneElement
) {
116 $refElement = $refData->currentCloneElement
;
117 $refNode = $refElement->userData
;
118 $refData = $refNode->snData
;
120 // Cache the end of the chain in the requested element
121 $origRefData->currentCloneElement
= $refElement;
122 } elseif ( $refData->childPElement
) {
123 $refElement = $refData->childPElement
;
124 $refNode = $refElement->userData
;
126 return [ $refNode, $refNode ];
133 * @param SerializerNode $parent
134 * @param integer $sourceStart
135 * @return SerializerNode
137 private function insertPWrapper( SerializerNode
$parent, $sourceStart ) {
138 $pWrap = new Element( HTMLData
::NS_HTML
, 'mw:p-wrap', new PlainAttributes
);
139 $this->serializer
->insertElement( TreeBuilder
::UNDER
, $parent, $pWrap, false,
141 $data = new RemexMungerData
;
142 $data->isPWrapper
= true;
143 $data->wrapBaseNode
= $parent;
144 $pWrap->userData
->snData
= $data;
145 $parent->snData
->childPElement
= $pWrap;
146 return $pWrap->userData
;
149 public function characters( $preposition, $refElement, $text, $start, $length,
150 $sourceStart, $sourceLength
152 $isBlank = strspn( $text, "\t\n\f\r ", $start, $length ) === $length;
154 list( $parent, $refNode ) = $this->getParentForInsert( $preposition, $refElement );
155 $parentData = $parent->snData
;
157 if ( $preposition === TreeBuilder
::UNDER
) {
158 if ( $parentData->needsPWrapping
&& !$isBlank ) {
159 // Add a p-wrapper for bare text under body/blockquote
160 $refNode = $this->insertPWrapper( $refNode, $sourceStart );
162 $parentData = $parent->snData
;
163 } elseif ( $parentData->isSplittable
&& !$parentData->ancestorPNode
) {
164 // The parent is splittable and in block mode, so split the tag stack
165 $refNode = $this->splitTagStack( $refNode, true, $sourceStart );
167 $parentData = $parent->snData
;
172 // Non-whitespace characters detected
173 $parentData->nonblankNodeCount++
;
175 $this->serializer
->characters( $preposition, $refNode, $text, $start,
176 $length, $sourceStart, $sourceLength );
180 * Insert or reparent an element. Create p-wrappers or split the tag stack
183 * Consider the following insertion locations. The parent may be:
185 * - A: A body or blockquote (!!needsPWrapping)
186 * - B: A p-wrapper (!!isPWrapper)
187 * - C: A descendant of a p-wrapper (!!ancestorPNode)
188 * - CS: With splittable formatting elements in the stack region up to
190 * - CU: With one or more unsplittable elements in the stack region up
192 * - D: Not a descendant of a p-wrapper (!ancestorNode)
193 * - DS: With splittable formatting elements in the stack region up to
194 * the body or blockquote
195 * - DU: With one or more unsplittable elements in the stack region up
196 * to the body or blockquote
198 * And consider that we may insert two types of element:
202 * We handle the insertion as follows:
204 * - A/i: Create a p-wrapper, insert under it
205 * - A/b: Insert as normal
206 * - B/i: Insert as normal
207 * - B/b: Close the p-wrapper, insert under the body/blockquote (wrap
209 * - C/i: Insert as normal
210 * - CS/b: Split the tag stack, insert the block under cloned formatting
211 * elements which have the wrap base (the parent of the p-wrap) as
212 * their ultimate parent.
213 * - CU/b: Disable the p-wrap, by reparenting the currently open child
214 * of the p-wrap under the p-wrap's parent. Then insert the block as
216 * - D/b: Insert as normal
217 * - DS/i: Split the tag stack, creating a new p-wrapper as the ultimate
218 * parent of the formatting elements thus cloned. The parent of the
219 * p-wrapper is the body or blockquote.
220 * - DU/i: Insert as normal
222 * FIXME: fostering ($preposition == BEFORE) is mostly done by inserting as
223 * normal, the full algorithm is not followed.
225 * @param integer $preposition
226 * @param Element|SerializerNode|null $refElement
227 * @param Element $element
229 * @param integer $sourceStart
230 * @param integer $sourceLength
233 public function insertElement( $preposition, $refElement, Element
$element, $void,
234 $sourceStart, $sourceLength
236 list( $parent, $newRef ) = $this->getParentForInsert( $preposition, $refElement );
237 $parentData = $parent->snData
;
238 $parentNs = $parent->namespace;
239 $parentName = $parent->name
;
240 $elementName = $element->htmlName
;
242 $inline = isset( self
::$onlyInlineElements[$elementName] );
243 $under = $preposition === TreeBuilder
::UNDER
;
245 if ( $under && $parentData->isPWrapper
&& !$inline ) {
246 // [B/b] The element is non-inline and the parent is a p-wrapper,
247 // close the parent and insert into its parent instead
248 $newParent = $this->serializer
->getParentNode( $parent );
249 $parent = $newParent;
250 $parentData = $parent->snData
;
251 $parentData->childPElement
= null;
252 $newRef = $refElement->userData
;
253 // FIXME cannot call endTag() since we don't have an Element
254 } elseif ( $under && $parentData->isSplittable
255 && (bool)$parentData->ancestorPNode
!== $inline
257 // [CS/b, DS/i] The parent is splittable and the current element is
258 // inline in block context, or if the current element is a block
259 // under a p-wrapper, split the tag stack.
260 $newRef = $this->splitTagStack( $newRef, $inline, $sourceStart );
262 $parentData = $parent->snData
;
263 } elseif ( $under && $parentData->needsPWrapping
&& $inline ) {
264 // [A/i] If the element is inline and we are in body/blockquote,
265 // we need to create a p-wrapper
266 $newRef = $this->insertPWrapper( $newRef, $sourceStart );
268 $parentData = $parent->snData
;
269 } elseif ( $parentData->ancestorPNode
&& !$inline ) {
270 // [CU/b] If the element is non-inline and (despite attempting to
271 // split above) there is still an ancestor p-wrap, disable that
273 $this->disablePWrapper( $parent, $sourceStart );
275 // else [A/b, B/i, C/i, D/b, DU/i] insert as normal
277 // An element with element children is a non-blank element
278 $parentData->nonblankNodeCount++
;
280 // Insert the element downstream and so initialise its userData
281 $this->serializer
->insertElement( $preposition, $newRef,
282 $element, $void, $sourceStart, $sourceLength );
285 if ( !$element->userData
->snData
) {
286 $elementData = $element->userData
->snData
= new RemexMungerData
;
288 $elementData = $element->userData
->snData
;
290 if ( ( $parentData->isPWrapper ||
$parentData->isSplittable
)
291 && isset( self
::$formattingElements[$elementName] )
293 $elementData->isSplittable
= true;
295 if ( $parentData->isPWrapper
) {
296 $elementData->ancestorPNode
= $parent;
297 } elseif ( $parentData->ancestorPNode
) {
298 $elementData->ancestorPNode
= $parentData->ancestorPNode
;
300 if ( $parentData->wrapBaseNode
) {
301 $elementData->wrapBaseNode
= $parentData->wrapBaseNode
;
302 } elseif ( $parentData->needsPWrapping
) {
303 $elementData->wrapBaseNode
= $parent;
305 if ( $elementName === 'body'
306 ||
$elementName === 'blockquote'
307 ||
$elementName === 'html'
309 $elementData->needsPWrapping
= true;
314 * Clone nodes in a stack range and return the new parent
316 * @param SerializerNode $parentNode
317 * @param bool $inline
318 * @param integer $pos The source position
319 * @return SerializerNode
321 private function splitTagStack( SerializerNode
$parentNode, $inline, $pos ) {
322 $parentData = $parentNode->snData
;
323 $wrapBase = $parentData->wrapBaseNode
;
324 $pWrap = $parentData->ancestorPNode
;
326 $cloneEnd = $wrapBase;
328 $cloneEnd = $parentData->ancestorPNode
;
331 $serializer = $this->serializer
;
333 $root = $serializer->getRootNode();
335 $removableNodes = [];
336 $haveContent = false;
337 while ( $node !== $cloneEnd ) {
338 $nextParent = $serializer->getParentNode( $node );
339 if ( $nextParent === $root ) {
340 throw new \
Exception( 'Did not find end of clone range' );
343 if ( $node->snData
->nonblankNodeCount
=== 0 ) {
344 $removableNodes[] = $node;
345 $nextParent->snData
->nonblankNodeCount
--;
351 $pWrap = $this->insertPWrapper( $wrapBase, $pos );
355 // End the p-wrap which was open, cancel the diversion
356 $wrapBase->snData
->childPElement
= null;
362 for ( $i = count( $nodes ) - 1; $i >= 0; $i-- ) {
363 $oldNode = $nodes[$i];
364 $oldData = $oldNode->snData
;
366 $element = new Element( $oldNode->namespace, $oldNode->name
, $oldNode->attrs
);
367 $this->serializer
->insertElement( TreeBuilder
::UNDER
, $nodeParent,
368 $element, false, $pos, 0 );
369 $oldData->currentCloneElement
= $element;
371 $newNode = $element->userData
;
372 $newData = $newNode->snData
= new RemexMungerData
;
374 $newData->ancestorPNode
= $pWrap;
376 $newData->isSplittable
= true;
377 $newData->wrapBaseNode
= $wrapBase;
378 $newData->isPWrapper
= $oldData->isPWrapper
;
380 $nodeParent->snData
->nonblankNodeCount++
;
384 foreach ( $removableNodes as $rNode ) {
385 $fakeElement = new Element( $rNode->namespace, $rNode->name
, $rNode->attrs
);
386 $fakeElement->userData
= $rNode;
387 $this->serializer
->removeNode( $fakeElement, $pos );
393 * Find the ancestor of $node which is a child of a p-wrapper, and
394 * reparent that node so that it is placed after the end of the p-wrapper
396 private function disablePWrapper( SerializerNode
$node, $sourceStart ) {
397 $nodeData = $node->snData
;
398 $pWrapNode = $nodeData->ancestorPNode
;
399 $newParent = $this->serializer
->getParentNode( $pWrapNode );
400 if ( $pWrapNode !== $this->serializer
->getLastChild( $newParent ) ) {
401 // Fostering or something? Abort!
407 $victim = $nextParent;
408 $victim->snData
->ancestorPNode
= null;
409 $nextParent = $this->serializer
->getParentNode( $victim );
410 } while ( $nextParent !== $pWrapNode );
412 // Make a fake Element to use in a reparenting operation
413 $victimElement = new Element( $victim->namespace, $victim->name
, $victim->attrs
);
414 $victimElement->userData
= $victim;
417 $this->serializer
->insertElement( TreeBuilder
::UNDER
, $newParent, $victimElement,
418 false, $sourceStart, 0 );
420 // Decrement nonblank node count
421 $pWrapNode->snData
->nonblankNodeCount
--;
423 // Cancel the diversion so that no more elements are inserted under this p-wrap
424 $newParent->snData
->childPElement
= null;
427 public function endTag( Element
$element, $sourceStart, $sourceLength ) {
428 $this->serializer
->endTag( $element, $sourceStart, $sourceLength );
431 public function doctype( $name, $public, $system, $quirks, $sourceStart, $sourceLength ) {
432 $this->serializer
->doctype( $name, $public, $system, $quirks,
433 $sourceStart, $sourceLength );
436 public function comment( $preposition, $refElement, $text, $sourceStart, $sourceLength ) {
437 list( $parent, $refNode ) = $this->getParentForInsert( $preposition, $refElement );
438 $this->serializer
->comment( $preposition, $refNode, $text,
439 $sourceStart, $sourceLength );
442 public function error( $text, $pos ) {
443 $this->serializer
->error( $text, $pos );
446 public function mergeAttributes( Element
$element, Attributes
$attrs, $sourceStart ) {
447 $this->serializer
->mergeAttributes( $element, $attrs, $sourceStart );
450 public function removeNode( Element
$element, $sourceStart ) {
451 $this->serializer
->removeNode( $element, $sourceStart );
454 public function reparentChildren( Element
$element, Element
$newParent, $sourceStart ) {
455 $self = $element->userData
;
456 $children = $self->children
;
457 $self->children
= [];
458 $this->insertElement( TreeBuilder
::UNDER
, $element, $newParent, false, $sourceStart, 0 );
459 $newParentNode = $newParent->userData
;
460 $newParentId = $newParentNode->id
;
461 foreach ( $children as $child ) {
462 if ( is_object( $child ) ) {
463 $child->parentId
= $newParentId;
466 $newParentNode->children
= $children;