From fb0d2cbe7f461121719579068eef7761a5e84bb0 Mon Sep 17 00:00:00 2001 From: Ed Sanders Date: Fri, 26 Jun 2020 23:00:34 +0100 Subject: [PATCH] Move some postReply logic to modifier.js Re-create the function in PHP Change-Id: I1c8b505542c3b82c10b7b258e48054bc59cb4836 --- includes/CommentModifier.php | 23 +++++++++++++++++++++++ modules/CommentController.js | 16 ++-------------- modules/modifier.js | 25 +++++++++++++++++++++++++ 3 files changed, 50 insertions(+), 14 deletions(-) diff --git a/includes/CommentModifier.php b/includes/CommentModifier.php index 5cb79c1f8..6676fe6c7 100644 --- a/includes/CommentModifier.php +++ b/includes/CommentModifier.php @@ -294,6 +294,29 @@ class CommentModifier { return $listItem; } + /** + * Add a reply to a specific comment + * + * @param CommentItem $comment Comment being replied to + * @param DOMElement $container Container of comment DOM nodes + */ + public static function addReply( CommentItem $comment, DOMElement $container ) { + $newParsoidItem = null; + // Transfer comment DOM to Parsoid DOM + // Wrap every root node of the document in a new list item (dd/li). + // In wikitext mode every root node is a paragraph. + // In visual mode the editor takes care of preventing problematic nodes + // like or

from ever occurring in the comment. + while ( $container->childNodes->length ) { + if ( !$newParsoidItem ) { + $newParsoidItem = self::addListItem( $comment ); + } else { + $newParsoidItem = self::addSiblingListItem( $newParsoidItem ); + } + $newParsoidItem->appendChild( $container->firstChild ); + } + } + /** * Create an element that will convert to the provided wikitext * diff --git a/modules/CommentController.js b/modules/CommentController.js index 334a1d3e5..fa7a83b87 100644 --- a/modules/CommentController.js +++ b/modules/CommentController.js @@ -304,7 +304,7 @@ CommentController.prototype.createCommentContainerFromHtml = function ( doc, htm }; CommentController.prototype.postReply = function ( comment ) { - var container, newParsoidItem, wikitext, + var container, wikitext, doc = comment.range.endContainer.ownerDocument; if ( this.replyWidget.getMode() === 'source' ) { @@ -319,19 +319,7 @@ CommentController.prototype.postReply = function ( comment ) { container = this.createCommentContainerFromHtml( doc, this.replyWidget.getValue() ); } - // Transfer comment DOM to Parsoid DOM - // Wrap every root node of the document in a new list item (dd/li). - // In wikitext mode every root node is a paragraph. - // In visual mode the editor takes care of preventing problematic nodes - // like

or

from ever occurring in the comment. - while ( container.children.length ) { - if ( !newParsoidItem ) { - newParsoidItem = modifier.addListItem( comment ); - } else { - newParsoidItem = modifier.addSiblingListItem( newParsoidItem ); - } - newParsoidItem.appendChild( container.firstChild ); - } + modifier.addReply( comment, container ); }; CommentController.prototype.save = function ( parsoidData ) { diff --git a/modules/modifier.js b/modules/modifier.js index f3000c393..cd0566b53 100644 --- a/modules/modifier.js +++ b/modules/modifier.js @@ -292,6 +292,30 @@ function addSiblingListItem( previousItem ) { return listItem; } +/** + * Add a reply to a specific comment + * + * @param {CommentItem} comment Comment being replied to + * @param {HTMLElement} container Container of comment DOM nodes + */ +function addReply( comment, container ) { + var newParsoidItem; + + // Transfer comment DOM to Parsoid DOM + // Wrap every root node of the document in a new list item (dd/li). + // In wikitext mode every root node is a paragraph. + // In visual mode the editor takes care of preventing problematic nodes + // like

or

from ever occurring in the comment. + while ( container.childNodes.length ) { + if ( !newParsoidItem ) { + newParsoidItem = addListItem( comment ); + } else { + newParsoidItem = addSiblingListItem( newParsoidItem ); + } + newParsoidItem.appendChild( container.firstChild ); + } +} + function createWikitextNode( doc, wt ) { var span = doc.createElement( 'span' ); @@ -303,6 +327,7 @@ function createWikitextNode( doc, wt ) { module.exports = { addReplyLink: addReplyLink, + addReply: addReply, addListItem: addListItem, removeAddedListItem: removeAddedListItem, addSiblingListItem: addSiblingListItem,