, instead we need a function that returns parent and target,
// so that we can insert nodes in this place in other code
} else if ( curLevel < desiredLevel ) {
// Insert more lists after the target to increase nesting.
// Parsoid puts HTML comments (and other "rendering-transparent nodes", e.g. category links)
// which appear at the end of the line in wikitext outside the paragraph,
// but we usually shouldn't insert replies between the paragraph and such comments. (T257651)
// Skip over comments and whitespace, but only update target when skipping past comments.
let pointer = target;
while (
pointer.nextSibling && (
utils.isRenderingTransparentNode( pointer.nextSibling ) ||
(
pointer.nextSibling.nodeType === Node.TEXT_NODE &&
utils.htmlTrim( pointer.nextSibling.textContent ) === '' &&
// If at least two lines of whitespace are detected, the following HTML
// comments are not considered to be part of the reply (T264026, T301214)
!/\n[^\n]*\n/.test( pointer.nextSibling.textContent )
)
)
) {
pointer = pointer.nextSibling;
if ( utils.isRenderingTransparentNode( pointer ) ) {
target = pointer;
}
}
// Insert required number of wrappers
while ( curLevel < desiredLevel ) {
list = target.ownerDocument.createElement( listType );
list.discussionToolsModified = 'new';
item = target.ownerDocument.createElement( itemType );
item.discussionToolsModified = 'new';
parent.insertBefore( list, target.nextSibling );
list.appendChild( item );
target = item;
parent = list;
curLevel++;
}
} else {
// Split the ancestor nodes after the target to decrease nesting.
let newNode;
do {
if ( !target || !parent ) {
throw new Error( 'Can not decrease nesting any more' );
}
// If target is the last child of its parent, no need to split it
if ( target.nextSibling ) {
// Create new identical node after the parent
newNode = parent.cloneNode( false );
parent.discussionToolsModified = 'split';
parent.parentNode.insertBefore( newNode, parent.nextSibling );
// Move nodes following target to the new node
while ( target.nextSibling ) {
newNode.appendChild( target.nextSibling );
}
}
target = parent;
parent = parent.parentNode;
// Decrease nesting level if we escaped outside of a list
if ( listTypeMap[ target.tagName.toLowerCase() ] ) {
curLevel--;
}
} while ( curLevel >= desiredLevel );
// parent is now a list, target is a list item
if ( itemType === target.tagName.toLowerCase() ) {
item = target.ownerDocument.createElement( itemType );
item.discussionToolsModified = 'new';
parent.insertBefore( item, target.nextSibling );
} else {
// This is the wrong type of list, split it one more time
// If target is the last child of its parent, no need to split it
if ( target.nextSibling ) {
// Create new identical node after the parent
newNode = parent.cloneNode( false );
parent.discussionToolsModified = 'split';
parent.parentNode.insertBefore( newNode, parent.nextSibling );
// Move nodes following target to the new node
while ( target.nextSibling ) {
newNode.appendChild( target.nextSibling );
}
}
target = parent;
parent = parent.parentNode;
// Insert a list of the right type in the middle
list = target.ownerDocument.createElement( listType );
list.discussionToolsModified = 'new';
item = target.ownerDocument.createElement( itemType );
item.discussionToolsModified = 'new';
parent.insertBefore( list, target.nextSibling );
list.appendChild( item );
}
}
return item;
}
/**
* Undo the effects of #addListItem, also removing or merging any affected parent nodes.
*
* @param {HTMLElement} node
*/
function removeAddedListItem( node ) {
while ( node && node.discussionToolsModified ) {
let nextNode;
if ( node.discussionToolsModified === 'new' ) {
nextNode = node.previousSibling || node.parentNode;
// Remove this node
delete node.discussionToolsModified;
node.parentNode.removeChild( node );
} else if ( node.discussionToolsModified === 'split' ) {
// Children might be split too, if so, descend into them afterwards
if ( node.lastChild && node.lastChild.discussionToolsModified === 'split' ) {
node.discussionToolsModified = 'done';
nextNode = node.lastChild;
} else {
delete node.discussionToolsModified;
nextNode = node.parentNode;
}
// Merge the following sibling node back into this one
while ( node.nextSibling.firstChild ) {
node.appendChild( node.nextSibling.firstChild );
}
node.parentNode.removeChild( node.nextSibling );
} else {
nextNode = node.parentNode;
}
node = nextNode;
}
}
/**
* Unwrap a top level list, converting list item text to paragraphs
*
* Assumes that the list has a parent node.
*
* @param {Node} list DOM node, will be wrapped if it is a list element (dl/ol/ul)
* @param {DocumentFragment|null} fragment Containing document fragment if list has no parent
*/
function unwrapList( list, fragment ) {
if ( !(
list.nodeType === Node.ELEMENT_NODE && (
list.tagName.toLowerCase() === 'dl' ||
list.tagName.toLowerCase() === 'ol' ||
list.tagName.toLowerCase() === 'ul'
)
) ) {
// Not a list, leave alone (e.g. auto-generated ref block)
return;
}
// If the whole list is a template return it unmodified (T253150)
if ( utils.getTranscludedFromElement( list ) ) {
return;
}
const doc = list.ownerDocument;
const container = fragment || list.parentNode;
let referenceNode = list;
while ( list.firstChild ) {
if ( list.firstChild.nodeType === Node.ELEMENT_NODE ) {
// Move
contents to
let p = doc.createElement( 'p' );
while ( list.firstChild.firstChild ) {
// If contents is a block element, place outside the paragraph
// and start a new paragraph after
if ( utils.isBlockElement( list.firstChild.firstChild ) ) {
if ( p.firstChild ) {
const insertBefore2 = referenceNode.nextSibling;
referenceNode = p;
container.insertBefore( p, insertBefore2 );
}
const 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 ) {
const insertBefore = referenceNode.nextSibling;
referenceNode = p;
container.insertBefore( p, insertBefore );
}
list.removeChild( list.firstChild );
} else {
// Text node / comment node, probably empty
const insertBefore = referenceNode.nextSibling;
referenceNode = list.firstChild;
container.insertBefore( list.firstChild, insertBefore );
}
}
container.removeChild( list );
}
/**
* Add another list item after the given one.
*
* @param {HTMLElement} previousItem
* @return {HTMLElement}
*/
function addSiblingListItem( previousItem ) {
const listItem = previousItem.ownerDocument.createElement( previousItem.tagName );
previousItem.parentNode.insertBefore( listItem, previousItem.nextSibling );
return listItem;
}
module.exports = {
addReplyLink: addReplyLink,
addListItem: addListItem,
removeAddedListItem: removeAddedListItem,
addSiblingListItem: addSiblingListItem,
unwrapList: unwrapList,
sanitizeWikitextLinebreaks: sanitizeWikitextLinebreaks
};