- if ( !$furthestblock ) {
- $this->popTag( $fmtelt );
- $afe->remove( $fmtelt );
- return true;
- } else {
- // Let the common ancestor be the element immediately above
- // the formatting element in the stack of open elements.
- $ancestor = $this->node( $index-1 );
-
- // Let a bookmark note the position of the formatting
- // element in the list of active formatting elements
- // relative to the elements on either side of it in the
- // list.
- $BOOKMARK = new BalanceElement( '[bookmark]', '[bookmark]', [] );
- $afe->insertAfter( $fmtelt, $BOOKMARK );
-
- // Let node and last node be the furthest block.
- $node = $furthestblock;
- $lastnode = $furthestblock;
- $nodeindex = $furthestblockindex;
- $isAFE = false;
-
- // Let inner loop counter be zero.
- $inner = 0;
-
- while ( true ) {
-
- // Increment inner loop counter by one.
- $inner++;
-
- // Let node be the element immediately above node in
- // the stack of open elements, or if node is no longer
- // in the stack of open elements (e.g. because it got
- // removed by this algorithm), the element that was
- // immediately above node in the stack of open elements
- // before node was removed.
- $node = $this->node( --$nodeindex );
-
- // If node is the formatting element, then go
- // to the next step in the overall algorithm.
- if ( $node === $fmtelt ) break;
-
- // If the inner loop counter is greater than three and node
- // is in the list of active formatting elements, then remove
- // node from the list of active formatting elements.
- $isAFE = $afe->isInList( $node );
- if ( $inner > 3 && $isAFE ) {
- $afe->remove( $node );
- $isAFE = false;
- }
-
- // If node is not in the list of active formatting
- // elements, then remove node from the stack of open
- // elements and then go back to the step labeled inner
- // loop.
- if ( !$isAFE ) {
- // Don't flatten here, since we're about to relocate
- // parts of this $node.
- $this->removeElement( $node, false );
- continue;
- }
-
- // Create an element for the token for which the
- // element node was created with common ancestor as
- // the intended parent, replace the entry for node
- // in the list of active formatting elements with an
- // entry for the new element, replace the entry for
- // node in the stack of open elements with an entry for
- // the new element, and let node be the new element.
- $newelt = new BalanceElement(
- $node->namespaceURI, $node->localName, $node->attribs );
- $afe->replace( $node, $newelt );
- $this->replaceAt( $nodeindex, $newelt );
- $node = $newelt;
-
- // If last node is the furthest block, then move the
- // aforementioned bookmark to be immediately after the
- // new node in the list of active formatting elements.
- if ( $lastnode === $furthestblock ) {
- $afe->remove( $BOOKMARK );
- $afe->insertAfter( $newelt, $BOOKMARK );
- }
-
- // Insert last node into node, first removing it from
- // its previous parent node if any.
- $node->appendChild( $lastnode );
-
- // Let last node be node.
- $lastnode = $node;
- }
-
- // If the common ancestor node is a table, tbody, tfoot,
- // thead, or tr element, then, foster parent whatever last
- // node ended up being in the previous step, first removing
- // it from its previous parent node if any.
- if (
- $this->fosterParentMode &&
- $ancestor->isA( BalanceSets::$tableSectionRowSet )
- ) {
- $this->fosterParent( $lastnode );
- } else {
- // Otherwise, append whatever last node ended up being in
- // the previous step to the common ancestor node, first
- // removing it from its previous parent node if any.
- $ancestor->appendChild( $lastnode );
+ if ( !$furthestBlock ) {
+ $this->popTag( $fmtElt );
+ $afe->remove( $fmtElt );
+ return true;
+ }
+
+ // Let the common ancestor be the element immediately above
+ // the formatting element in the stack of open elements.
+ $ancestor = $this->node( $index-1 );
+
+ // Let a bookmark note the position of the formatting
+ // element in the list of active formatting elements
+ // relative to the elements on either side of it in the
+ // list.
+ $BOOKMARK = new BalanceElement( '[bookmark]', '[bookmark]', [] );
+ $afe->insertAfter( $fmtElt, $BOOKMARK );
+
+ // Let node and last node be the furthest block.
+ $node = $furthestBlock;
+ $lastNode = $furthestBlock;
+ $nodeIndex = $furthestBlockIndex;
+ $isAFE = false;
+
+ // Inner loop
+ for ( $inner = 1; true; $inner++ ) {
+ // Let node be the element immediately above node in
+ // the stack of open elements, or if node is no longer
+ // in the stack of open elements (e.g. because it got
+ // removed by this algorithm), the element that was
+ // immediately above node in the stack of open elements
+ // before node was removed.
+ $node = $this->node( --$nodeIndex );
+
+ // If node is the formatting element, then go
+ // to the next step in the overall algorithm.
+ if ( $node === $fmtElt ) break;
+
+ // If the inner loop counter is greater than three and node
+ // is in the list of active formatting elements, then remove
+ // node from the list of active formatting elements.
+ $isAFE = $afe->isInList( $node );
+ if ( $inner > 3 && $isAFE ) {
+ $afe->remove( $node );
+ $isAFE = false;
+ }
+
+ // If node is not in the list of active formatting
+ // elements, then remove node from the stack of open
+ // elements and then go back to the step labeled inner
+ // loop.
+ if ( !$isAFE ) {
+ // Don't flatten here, since we're about to relocate
+ // parts of this $node.
+ $this->removeElement( $node, false );
+ continue;