Handle category links like comments (rendering-transparent nodes)

Bug: T269036
Change-Id: Id4321ad09907b5030881456c93da90a39bdfdd75
This commit is contained in:
Bartosz Dziewoński 2020-12-05 22:49:23 +01:00 committed by Ed Sanders
parent 113ad01d5e
commit 8c9230fa10
6 changed files with 44 additions and 9 deletions

View file

@ -2,7 +2,6 @@
namespace MediaWiki\Extension\DiscussionTools;
use DOMComment;
use DOMDocument;
use DOMDocumentFragment;
use DOMElement;
@ -140,13 +139,14 @@ class CommentModifier {
$target = $target->parentNode;
}
// Parsoid puts HTML comments which appear at the end of the line in wikitext outside the paragraph,
// 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.
$pointer = $target;
while (
$pointer->nextSibling && (
$pointer->nextSibling instanceof DOMComment ||
CommentUtils::isRenderingTransparentNode( $pointer->nextSibling ) ||
(
$pointer->nextSibling instanceof DOMText &&
CommentUtils::htmlTrim( $pointer->nextSibling->nodeValue ) === '' &&
@ -157,7 +157,7 @@ class CommentModifier {
)
) {
$pointer = $pointer->nextSibling;
if ( $pointer instanceof DOMComment ) {
if ( CommentUtils::isRenderingTransparentNode( $pointer ) ) {
$target = $pointer;
}
}

View file

@ -2,6 +2,7 @@
namespace MediaWiki\Extension\DiscussionTools;
use DOMComment;
use DOMElement;
use DOMNode;
use DOMXPath;
@ -37,6 +38,20 @@ class CommentUtils {
in_array( strtolower( $node->tagName ), self::$blockElementTypes );
}
/**
* @param DOMNode $node
* @return bool Node is considered a rendering-transparent node in Parsoid
*/
public static function isRenderingTransparentNode( DOMNode $node ) : bool {
return (
$node instanceof DOMComment ||
$node instanceof DOMElement && (
strtolower( $node->tagName ) === 'meta' ||
strtolower( $node->tagName ) === 'link'
)
);
}
/**
* Get the index of $child in its parent
*

View file

@ -130,13 +130,14 @@ function addListItem( comment ) {
target = target.parentNode;
}
// Parsoid puts HTML comments which appear at the end of the line in wikitext outside the paragraph,
// 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.
pointer = target;
while (
pointer.nextSibling && (
pointer.nextSibling.nodeType === Node.COMMENT_NODE ||
utils.isRenderingTransparentNode( pointer.nextSibling ) ||
(
pointer.nextSibling.nodeType === Node.TEXT_NODE &&
utils.htmlTrim( pointer.nextSibling.textContent ) === '' &&
@ -147,7 +148,7 @@ function addListItem( comment ) {
)
) {
pointer = pointer.nextSibling;
if ( pointer.nodeType === Node.COMMENT_NODE ) {
if ( utils.isRenderingTransparentNode( pointer ) ) {
target = pointer;
}
}

View file

@ -13,6 +13,20 @@ function isBlockElement( node ) {
return node instanceof HTMLElement && ve.isBlockElement( node );
}
/**
* @param {Node} node
* @return {boolean} Node is considered a rendering-transparent node in Parsoid
*/
function isRenderingTransparentNode( node ) {
return (
node.nodeType === Node.COMMENT_NODE ||
node.nodeType === Node.ELEMENT_NODE && (
node.tagName.toLowerCase() === 'meta' ||
node.tagName.toLowerCase() === 'link'
)
);
}
/**
* Get the index of a node in its parentNode's childNode list
*
@ -283,6 +297,7 @@ function getFullyCoveredSiblings( item ) {
module.exports = {
isBlockElement: isBlockElement,
isRenderingTransparentNode: isRenderingTransparentNode,
childIndexOf: childIndexOf,
closestElement: closestElement,
getIndentLevel: getIndentLevel,

View file

@ -10,7 +10,9 @@
<p>hello <a rel="mw:WikiLink" href="./User:Matma_Rex" title="User:Matma Rex">Matma Rex</a> (<a rel="mw:WikiLink" href="./User_talk:Matma_Rex" title="User talk:Matma Rex"><font color="green"><i>talk</i></font></a>) 18:44, 22 July 2020 (UTC) </p>
<!-- Above comment added by announce-bot --><dl><dd data-parsoid="{}">Reply to c|Matma Rex|2020-07-22T18:44:00.000Z|comments_T257651</dd></dl>
<p>hello <a rel="mw:WikiLink" href="./User:Matma_Rex" title="User:Matma Rex">Matma Rex</a> (<a rel="mw:WikiLink" href="./User_talk:Matma_Rex" title="User talk:Matma Rex"><font color="green"><i>talk</i></font></a>) 18:45, 22 July 2020 (UTC) </p><dl><dd data-parsoid="{}">Reply to c|Matma Rex|2020-07-22T18:45:00.000Z|comments_T257651</dd></dl>
<p>hello <a rel="mw:WikiLink" href="./User:Matma_Rex" title="User:Matma Rex">Matma Rex</a> (<a rel="mw:WikiLink" href="./User_talk:Matma_Rex" title="User talk:Matma Rex"><font color="green"><i>talk</i></font></a>) 18:45, 22 July 2020 (UTC) </p><link rel="mw:PageProp/Category" href="./Category:Test"/><dl><dd data-parsoid="{}">Reply to c|Matma Rex|2020-07-22T18:45:00.000Z|comments_T257651</dd></dl>
<p>hello <a rel="mw:WikiLink" href="./User:Matma_Rex" title="User:Matma Rex">Matma Rex</a> (<a rel="mw:WikiLink" href="./User_talk:Matma_Rex" title="User talk:Matma Rex"><font color="green"><i>talk</i></font></a>) 18:46, 22 July 2020 (UTC) </p><dl><dd data-parsoid="{}">Reply to c|Matma Rex|2020-07-22T18:46:00.000Z|comments_T257651</dd></dl>
<!-- No replies below this line -->

View file

@ -10,7 +10,9 @@
<p>hello <a rel="mw:WikiLink" href="./User:Matma_Rex" title="User:Matma Rex">Matma Rex</a> (<a rel="mw:WikiLink" href="./User_talk:Matma_Rex" title="User talk:Matma Rex"><font color="green"><i>talk</i></font></a>) 18:44, 22 July 2020 (UTC) </p>
<!-- Above comment added by announce-bot -->
<p>hello <a rel="mw:WikiLink" href="./User:Matma_Rex" title="User:Matma Rex">Matma Rex</a> (<a rel="mw:WikiLink" href="./User_talk:Matma_Rex" title="User talk:Matma Rex"><font color="green"><i>talk</i></font></a>) 18:45, 22 July 2020 (UTC) </p>
<p>hello <a rel="mw:WikiLink" href="./User:Matma_Rex" title="User:Matma Rex">Matma Rex</a> (<a rel="mw:WikiLink" href="./User_talk:Matma_Rex" title="User talk:Matma Rex"><font color="green"><i>talk</i></font></a>) 18:45, 22 July 2020 (UTC) </p><link rel="mw:PageProp/Category" href="./Category:Test">
<p>hello <a rel="mw:WikiLink" href="./User:Matma_Rex" title="User:Matma Rex">Matma Rex</a> (<a rel="mw:WikiLink" href="./User_talk:Matma_Rex" title="User talk:Matma Rex"><font color="green"><i>talk</i></font></a>) 18:46, 22 July 2020 (UTC) </p>
<!-- No replies below this line -->