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 ];
140 $refNode = $refElement->userData;
141 $refData = $refNode->snData;
142 if ( $refData->currentCloneElement ) {
144 $origRefData = $refData;
145 while ( $refData->currentCloneElement ) {
146 $refElement = $refData->currentCloneElement;
147 $refNode = $refElement->userData;
148 $refData = $refNode->snData;
151 $origRefData->currentCloneElement = $refElement;
152 } elseif ( $refData->childPElement ) {
153 $refElement = $refData->childPElement;
154 $refNode = $refElement->userData;
156 return [ $refNode, $refNode ];
168 $pWrap =
new Element( HTMLData::NS_HTML,
'mw:p-wrap',
new PlainAttributes );
169 $this->serializer->insertElement( TreeBuilder::UNDER, $parent, $pWrap,
false,
172 $data->isPWrapper =
true;
173 $data->wrapBaseNode = $parent;
174 $pWrap->userData->snData = $data;
175 $parent->snData->childPElement = $pWrap;
176 return $pWrap->userData;
179 public function characters( $preposition, $refElement, $text, $start, $length,
180 $sourceStart, $sourceLength
182 $isBlank = strspn( $text,
"\t\n\f\r ", $start, $length ) === $length;
185 $parentData = $parent->snData;
187 if ( $preposition === TreeBuilder::UNDER ) {
188 if ( $parentData->needsPWrapping && !$isBlank ) {
192 $parentData = $parent->snData;
193 } elseif ( $parentData->isSplittable && !$parentData->ancestorPNode ) {
195 $refNode = $this->
splitTagStack( $refNode,
true, $sourceStart );
197 $parentData = $parent->snData;
203 $parentData->nonblankNodeCount++;
205 $this->serializer->characters( $preposition, $refNode, $text, $start,
206 $length, $sourceStart, $sourceLength );
268 public function insertElement( $preposition, $refElement, Element $element, $void,
269 $sourceStart, $sourceLength
272 $parentData = $parent->snData;
273 $elementName = $element->htmlName;
275 $inline = isset( self::$onlyInlineElements[$elementName] );
276 $under = $preposition === TreeBuilder::UNDER;
277 $elementToEnd =
null;
279 if ( isset( self::$metadataElements[$elementName] ) ) {
282 $this->
trace(
'insert metadata' );
283 } elseif ( $under && $parentData->isPWrapper && !$inline ) {
286 $this->
trace(
'insert B/b' );
287 $newParent = $this->serializer->getParentNode( $parent );
288 $parent = $newParent;
289 $parentData = $parent->snData;
290 $parentData->childPElement =
null;
291 $newRef = $refElement->userData;
292 } elseif ( $under && $parentData->isSplittable
293 && (
bool)$parentData->ancestorPNode !== $inline
298 $this->
trace( $inline ?
'insert DS/i' :
'insert CS/b' );
299 $newRef = $this->
splitTagStack( $newRef, $inline, $sourceStart );
301 $parentData = $parent->snData;
302 } elseif ( $under && $parentData->needsPWrapping && $inline ) {
305 $this->
trace(
'insert A/i' );
308 $parentData = $parent->snData;
309 } elseif ( $parentData->ancestorPNode && !$inline ) {
313 $this->
trace(
'insert CU/b' );
317 $this->
trace(
'insert normal' );
321 $parentData->nonblankNodeCount++;
324 $this->serializer->insertElement( $preposition, $newRef,
325 $element, $void, $sourceStart, $sourceLength );
328 if ( !$element->userData->snData ) {
331 $elementData = $element->userData->snData;
333 if ( ( $parentData->isPWrapper || $parentData->isSplittable )
334 && isset( self::$formattingElements[$elementName] )
336 $elementData->isSplittable =
true;
338 if ( $parentData->isPWrapper ) {
339 $elementData->ancestorPNode = $parent;
340 } elseif ( $parentData->ancestorPNode ) {
341 $elementData->ancestorPNode = $parentData->ancestorPNode;
343 if ( $parentData->wrapBaseNode ) {
344 $elementData->wrapBaseNode = $parentData->wrapBaseNode;
345 } elseif ( $parentData->needsPWrapping ) {
346 $elementData->wrapBaseNode = $parent;
348 if ( $elementName ===
'body'
349 || $elementName ===
'blockquote'
350 || $elementName ===
'html'
352 $elementData->needsPWrapping =
true;
364 private function splitTagStack( SerializerNode $parentNode, $inline, $pos ) {
365 $parentData = $parentNode->snData;
366 $wrapBase = $parentData->wrapBaseNode;
367 $pWrap = $parentData->ancestorPNode;
369 $cloneEnd = $wrapBase;
371 $cloneEnd = $parentData->ancestorPNode;
378 $removableNodes = [];
379 while ( $node !== $cloneEnd ) {
381 if ( $nextParent === $root ) {
382 throw new \Exception(
'Did not find end of clone range' );
385 if ( $node->snData->nonblankNodeCount === 0 ) {
386 $removableNodes[] = $node;
387 $nextParent->snData->nonblankNodeCount--;
398 $wrapBase->snData->childPElement =
null;
404 for ( $i = count( $nodes ) - 1; $i >= 0; $i-- ) {
405 $oldNode = $nodes[$i];
406 $oldData = $oldNode->snData;
408 $element =
new Element( $oldNode->namespace, $oldNode->name, $oldNode->attrs );
409 $this->serializer->insertElement( TreeBuilder::UNDER, $nodeParent,
410 $element,
false, $pos, 0 );
411 $oldData->currentCloneElement = $element;
413 $newNode = $element->userData;
416 $newData->ancestorPNode = $pWrap;
418 $newData->isSplittable =
true;
419 $newData->wrapBaseNode = $wrapBase;
420 $newData->isPWrapper = $oldData->isPWrapper;
422 $nodeParent->snData->nonblankNodeCount++;
426 foreach ( $removableNodes as $rNode ) {
427 $fakeElement =
new Element( $rNode->namespace, $rNode->name, $rNode->attrs );
428 $fakeElement->userData = $rNode;
429 $this->serializer->removeNode( $fakeElement, $pos );
441 $nodeData = $node->snData;
442 $pWrapNode = $nodeData->ancestorPNode;
443 $newParent = $this->serializer->getParentNode( $pWrapNode );
444 if ( $pWrapNode !== $this->serializer->getLastChild( $newParent ) ) {
451 $victim = $nextParent;
452 $victim->snData->ancestorPNode =
null;
453 $nextParent = $this->serializer->getParentNode( $victim );
454 }
while ( $nextParent !== $pWrapNode );
457 $victimElement =
new Element( $victim->namespace, $victim->name, $victim->attrs );
458 $victimElement->userData = $victim;
461 $this->serializer->insertElement( TreeBuilder::UNDER, $newParent, $victimElement,
462 false, $sourceStart, 0 );
465 $pWrapNode->snData->nonblankNodeCount--;
468 $newParent->snData->childPElement =
null;
471 public function endTag( Element $element, $sourceStart, $sourceLength ) {
472 $data = $element->userData->snData;
473 if ( $data->childPElement ) {
474 $this->
endTag( $data->childPElement, $sourceStart, 0 );
476 $this->serializer->endTag( $element, $sourceStart, $sourceLength );
477 $element->userData->snData =
null;
478 $element->userData =
null;
481 public function doctype( $name, $public, $system, $quirks, $sourceStart, $sourceLength ) {
482 $this->serializer->doctype( $name, $public, $system, $quirks,
483 $sourceStart, $sourceLength );
486 public function comment( $preposition, $refElement, $text, $sourceStart, $sourceLength ) {
488 $this->serializer->comment( $preposition, $refNode, $text, $sourceStart, $sourceLength );
504 $self = $element->userData;
505 if (
$self->snData->childPElement ) {
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;
530 $newParentNode->children = $children;