Fix whitespace handling in unwrapList

Update 'referenceNode' before modifying the DOM.

Bug: T254308
Change-Id: I76af898e238c1e6d3db96d3e6156e80e55c87820
This commit is contained in:
Ed Sanders 2020-06-03 13:53:36 +01:00
parent 0d14fcea6a
commit 62c0080850
4 changed files with 24 additions and 10 deletions

View file

@ -237,25 +237,29 @@ class CommentModifier {
// and start a new paragraph after
if ( self::isBlockElement( $list->firstChild->firstChild ) ) {
if ( $p->firstChild ) {
$container->insertBefore( $p, $referenceNode->nextSibling );
$insertBefore = $referenceNode->nextSibling;
$referenceNode = $p;
$container->insertBefore( $p, $insertBefore );
}
$container->insertBefore( $list->firstChild->firstChild, $referenceNode->nextSibling );
$insertBefore = $referenceNode->nextSibling;
$referenceNode = $list->firstChild->firstChild;
$container->insertBefore( $list->firstChild->firstChild, $insertBefore );
$p = $doc->createElement( 'p' );
} else {
$p->appendChild( $list->firstChild->firstChild );
}
}
if ( $p->firstChild ) {
$container->insertBefore( $p, $referenceNode->nextSibling );
$insertBefore = $referenceNode->nextSibling;
$referenceNode = $p;
$container->insertBefore( $p, $insertBefore );
}
$list->removeChild( $list->firstChild );
} else {
// Text node / comment node, probably empty
$container->insertBefore( $list->firstChild, $referenceNode->nextSibling );
$insertBefore = $referenceNode->nextSibling;
$referenceNode = $list->firstChild;
$container->insertBefore( $list->firstChild, $insertBefore );
}
}
$container->removeChild( $list );

View file

@ -217,7 +217,7 @@ function removeAddedListItem( node ) {
* @param {HTMLElement} list List element (dl/ol/ul)
*/
function unwrapList( list ) {
var p,
var p, insertBefore,
doc = list.ownerDocument,
container = list.parentNode,
referenceNode = list;
@ -236,25 +236,29 @@ function unwrapList( list ) {
// and start a new paragraph after
if ( ve.isBlockElement( list.firstChild.firstChild ) ) {
if ( p.firstChild ) {
container.insertBefore( p, referenceNode.nextSibling );
insertBefore = referenceNode.nextSibling;
referenceNode = p;
container.insertBefore( p, insertBefore );
}
container.insertBefore( list.firstChild.firstChild, referenceNode.nextSibling );
insertBefore = referenceNode.nextSibling;
referenceNode = list.firstChild.firstChild;
container.insertBefore( list.firstChild.firstChild, insertBefore );
p = doc.createElement( 'p' );
} else {
p.appendChild( list.firstChild.firstChild );
}
}
if ( p.firstChild ) {
container.insertBefore( p, referenceNode.nextSibling );
insertBefore = referenceNode.nextSibling;
referenceNode = p;
container.insertBefore( p, insertBefore );
}
list.removeChild( list.firstChild );
} else {
// Text node / comment node, probably empty
container.insertBefore( list.firstChild, referenceNode.nextSibling );
insertBefore = referenceNode.nextSibling;
referenceNode = list.firstChild;
container.insertBefore( list.firstChild, insertBefore );
}
}
container.removeChild( list );

View file

@ -41,6 +41,12 @@
"index": 0,
"expected": "<p>Foo</p><p>Bar</p>"
},
{
"name": "multiple items with whitespace",
"html": "<dl>\n<dd>Foo</dd>\n<dd>Bar</dd>\n</dl>",
"index": 0,
"expected": "\n<p>Foo</p>\n<p>Bar</p>\n"
},
{
"name": "nested list",
"html": "<dl><dd>Foo<dl><dd>Bar</dd></dl></dd></dl>",

View file

@ -124,7 +124,7 @@ QUnit.test( '#unwrapList', function ( assert ) {
modifier.unwrapList( container.childNodes[ caseItem.index || 0 ] );
assert.strictEqual(
container.innerHTML.trim(),
container.innerHTML,
caseItem.expected,
caseItem.name
);