Fix detecting decorative comment frames with whitespace

As a result of 0fc71f60cd, "empty" text
nodes (containing only whitespace) at the end of the comment may be
inside the comment's range, and trying to ignore them caused the
ranges not to match and the frame not to be detected.

Now the code works whether they're inside the comment's range or not.

Add a test case for wrapped discussion comments with HTML comments and
with whitespace.

Bug: T250126
Bug: T268407
Change-Id: I2217ff5a635fd1c9c9e803f46795b1bfb3d17535
This commit is contained in:
Bartosz Dziewoński 2020-12-08 23:46:11 +01:00
parent efccc28b5d
commit 6e37a172ae
6 changed files with 102 additions and 28 deletions

View file

@ -306,20 +306,22 @@ class CommentUtils {
return $node->nodeType === XML_TEXT_NODE && CommentUtils::htmlTrim( $node->nodeValue ) === '';
};
$firstNonemptyChild = function ( $node ) use ( $isIgnored ) {
$node = $node->firstChild;
while ( $node && $isIgnored( $node ) ) {
$node = $node->nextSibling;
$isFirstNonemptyChild = function ( $node ) use ( $isIgnored ) {
while ( ( $node = $node->previousSibling ) ) {
if ( !$isIgnored( $node ) ) {
return false;
}
}
return $node;
return true;
};
$lastNonemptyChild = function ( $node ) use ( $isIgnored ) {
$node = $node->lastChild;
while ( $node && $isIgnored( $node ) ) {
$node = $node->previousSibling;
$isLastNonemptyChild = function ( $node ) use ( $isIgnored ) {
while ( ( $node = $node->nextSibling ) ) {
if ( !$isIgnored( $node ) ) {
return false;
}
}
return $node;
return true;
};
$startMatches = false;
@ -329,7 +331,11 @@ class CommentUtils {
$startMatches = true;
break;
}
$node = $firstNonemptyChild( $node );
if ( $isIgnored( $node ) ) {
$node = $node->nextSibling;
} else {
$node = $node->firstChild;
}
}
$endMatches = false;
@ -344,15 +350,19 @@ class CommentUtils {
$endMatches = true;
break;
}
$node = $lastNonemptyChild( $node );
if ( $isIgnored( $node ) ) {
$node = $node->previousSibling;
} else {
$node = $node->lastChild;
}
}
if ( $startMatches && $endMatches ) {
// If these are all of the children (or the only child), go up one more level
while (
( $parent = $siblings[ 0 ]->parentNode ) &&
$firstNonemptyChild( $parent ) === $siblings[ 0 ] &&
$lastNonemptyChild( $parent ) === end( $siblings )
$isFirstNonemptyChild( $siblings[ 0 ] ) &&
$isLastNonemptyChild( end( $siblings ) )
) {
$siblings = [ $parent ];
}

View file

@ -249,20 +249,22 @@ function getFullyCoveredSiblings( item ) {
( n.className && n.className.indexOf( 'dt-init-replylink-buttons' ) !== -1 );
}
function firstNonemptyChild( n ) {
n = n.firstChild;
while ( n && isIgnored( n ) ) {
n = n.nextSibling;
function isFirstNonemptyChild( n ) {
while ( ( n = n.previousSibling ) ) {
if ( !isIgnored( n ) ) {
return false;
}
}
return n;
return true;
}
function lastNonemptyChild( n ) {
n = n.lastChild;
while ( n && isIgnored( n ) ) {
n = n.previousSibling;
function isLastNonemptyChild( n ) {
while ( ( n = n.nextSibling ) ) {
if ( !isIgnored( n ) ) {
return false;
}
}
return n;
return true;
}
startMatches = false;
@ -272,7 +274,11 @@ function getFullyCoveredSiblings( item ) {
startMatches = true;
break;
}
node = firstNonemptyChild( node );
if ( isIgnored( node ) ) {
node = node.nextSibling;
} else {
node = node.firstChild;
}
}
endMatches = false;
@ -285,15 +291,19 @@ function getFullyCoveredSiblings( item ) {
endMatches = true;
break;
}
node = lastNonemptyChild( node );
if ( isIgnored( node ) ) {
node = node.previousSibling;
} else {
node = node.lastChild;
}
}
if ( startMatches && endMatches ) {
// If these are all of the children (or the only child), go up one more level
while (
( parent = siblings[ 0 ].parentNode ) &&
firstNonemptyChild( parent ) === siblings[ 0 ] &&
lastNonemptyChild( parent ) === siblings[ siblings.length - 1 ]
isFirstNonemptyChild( siblings[ 0 ] ) &&
isLastNonemptyChild( siblings[ siblings.length - 1 ] )
) {
siblings = [ parent ];
}

View file

@ -34,6 +34,13 @@
"config": "../data/itwiki-config.json",
"data": "../data/itwiki-data.json"
},
{
"name": "Reply inserted inside/outside various wrapper elements",
"dom": "cases/wrappers/wrappers.html",
"expected": "cases/wrappers/wrappers-formattedreply.html",
"config": "../data/enwiki-config.json",
"data": "../data/enwiki-data.json"
},
{
"name": "Signatures in funny places",
"dom": "cases/signatures-funny/signatures-funny.html",

View file

@ -0,0 +1,35 @@
<h2 id="paragraph" data-mw-comment='{"type":"heading","level":0,"id":"h|paragraph|2020-01-22T23:19:00.000Z","replies":["c|Matma Rex|2020-01-22T23:19:00.000Z|paragraph"],"headingLevel":2,"placeholderHeading":false}'><span data-mw-comment-start="h|paragraph|2020-01-22T23:19:00.000Z"></span>paragraph<span data-mw-comment-end="h|paragraph|2020-01-22T23:19:00.000Z"></span></h2>
<p><span data-mw-comment-start="c|Matma Rex|2020-01-22T23:19:00.000Z|paragraph"></span>blah blah <b><a href="/wiki/User:Matma_Rex" title="User:Matma Rex">Matma Rex</a> | <a href="/wiki/User_talk:Matma_Rex" title="User talk:Matma Rex">talk</a></b> 23:19, 22 January 2020 (UTC)<span class="dt-init-replylink-buttons"><span class="dt-init-replylink-bracket">[</span><a class="dt-init-replylink-reply" role="button" tabindex="0" data-mw-comment='{"type":"comment","level":1,"id":"c|Matma Rex|2020-01-22T23:19:00.000Z|paragraph","replies":[],"timestamp":"2020-01-22T23:19:00.000Z","author":"Matma Rex"}'>reply</a><span class="dt-init-replylink-bracket">]</span></span><span data-mw-comment-end="c|Matma Rex|2020-01-22T23:19:00.000Z|paragraph"></span></p>
<h2 id="preformatted" data-mw-comment='{"type":"heading","level":0,"id":"h|preformatted|2020-01-22T23:19:00.000Z","replies":["c|Matma Rex|2020-01-22T23:19:00.000Z|preformatted"],"headingLevel":2,"placeholderHeading":false}'><span data-mw-comment-start="h|preformatted|2020-01-22T23:19:00.000Z"></span>preformatted<span data-mw-comment-end="h|preformatted|2020-01-22T23:19:00.000Z"></span></h2>
<pre><span data-mw-comment-start="c|Matma Rex|2020-01-22T23:19:00.000Z|preformatted"></span>blah blah <b><a href="/wiki/User:Matma_Rex" title="User:Matma Rex">Matma Rex</a> | <a href="/wiki/User_talk:Matma_Rex" title="User talk:Matma Rex">talk</a></b> 23:19, 22 January 2020 (UTC)<span class="dt-init-replylink-buttons"><span class="dt-init-replylink-bracket">[</span><a class="dt-init-replylink-reply" role="button" tabindex="0" data-mw-comment='{"type":"comment","level":1,"id":"c|Matma Rex|2020-01-22T23:19:00.000Z|preformatted","replies":[],"timestamp":"2020-01-22T23:19:00.000Z","author":"Matma Rex"}'>reply</a><span class="dt-init-replylink-bracket">]</span></span><span data-mw-comment-end="c|Matma Rex|2020-01-22T23:19:00.000Z|preformatted"></span></pre>
<h2 id="div_with_one_comment" data-mw-comment='{"type":"heading","level":0,"id":"h|div_with_one_comment|2020-01-22T23:19:00.000Z","replies":["c|Matma Rex|2020-01-22T23:19:00.000Z|div_with_one_comment"],"headingLevel":2,"placeholderHeading":false}'><span data-mw-comment-start="h|div_with_one_comment|2020-01-22T23:19:00.000Z"></span>div with one comment<span data-mw-comment-end="h|div_with_one_comment|2020-01-22T23:19:00.000Z"></span></h2>
<div style="background: beige;"><span data-mw-comment-start="c|Matma Rex|2020-01-22T23:19:00.000Z|div_with_one_comment"></span>
blah blah <b><a href="/wiki/User:Matma_Rex" title="User:Matma Rex">Matma Rex</a> | <a href="/wiki/User_talk:Matma_Rex" title="User talk:Matma Rex">talk</a></b> 23:19, 22 January 2020 (UTC)<span class="dt-init-replylink-buttons"><span class="dt-init-replylink-bracket">[</span><a class="dt-init-replylink-reply" role="button" tabindex="0" data-mw-comment='{"type":"comment","level":1,"id":"c|Matma Rex|2020-01-22T23:19:00.000Z|div_with_one_comment","replies":[],"timestamp":"2020-01-22T23:19:00.000Z","author":"Matma Rex"}'>reply</a><span class="dt-init-replylink-bracket">]</span></span><span data-mw-comment-end="c|Matma Rex|2020-01-22T23:19:00.000Z|div_with_one_comment"></span>
</div>
<h2 id="div_with_one_comment-htmlcomments" data-mw-comment='{"type":"heading","level":0,"id":"h|div_with_one_comment-htmlcomments|2020-01-22T23:19:00.000Z","replies":["c|Matma Rex|2020-01-22T23:19:00.000Z|div_with_one_comment-htmlcomments"],"headingLevel":2,"placeholderHeading":false}'><span data-mw-comment-start="h|div_with_one_comment-htmlcomments|2020-01-22T23:19:00.000Z"></span>div with one comment, and some HTML comments<span data-mw-comment-end="h|div_with_one_comment-htmlcomments|2020-01-22T23:19:00.000Z"></span></h2>
<div style="background: beige;">
<i><span data-mw-comment-start="c|Matma Rex|2020-01-22T23:19:00.000Z|div_with_one_comment-htmlcomments"></span>hello</i><br/>
<small>blah blah <b><a href="/wiki/User:Matma_Rex" title="User:Matma Rex">Matma Rex</a> | <a href="/wiki/User_talk:Matma_Rex" title="User talk:Matma Rex">talk</a></b> 23:19, 22 January 2020 (UTC)</small><!-- blah --><span class="dt-init-replylink-buttons"><span class="dt-init-replylink-bracket">[</span><a class="dt-init-replylink-reply" role="button" tabindex="0" data-mw-comment='{"type":"comment","level":1,"id":"c|Matma Rex|2020-01-22T23:19:00.000Z|div_with_one_comment-htmlcomments","replies":[],"timestamp":"2020-01-22T23:19:00.000Z","author":"Matma Rex"}'>reply</a><span class="dt-init-replylink-bracket">]</span></span><span data-mw-comment-end="c|Matma Rex|2020-01-22T23:19:00.000Z|div_with_one_comment-htmlcomments"></span>
</div><!-- blah -->
<h2 id="table_with_one_comment" data-mw-comment='{"type":"heading","level":0,"id":"h|table_with_one_comment|2020-01-22T23:19:00.000Z","replies":["c|Matma Rex|2020-01-22T23:19:00.000Z|table_with_one_comment"],"headingLevel":2,"placeholderHeading":false}'><span data-mw-comment-start="h|table_with_one_comment|2020-01-22T23:19:00.000Z"></span>table with one comment<span data-mw-comment-end="h|table_with_one_comment|2020-01-22T23:19:00.000Z"></span></h2>
<table border="1"><tbody><tr><td><span data-mw-comment-start="c|Matma Rex|2020-01-22T23:19:00.000Z|table_with_one_comment"></span>
blah blah <b><a href="/wiki/User:Matma_Rex" title="User:Matma Rex">Matma Rex</a> | <a href="/wiki/User_talk:Matma_Rex" title="User talk:Matma Rex">talk</a></b> 23:19, 22 January 2020 (UTC)<span class="dt-init-replylink-buttons"><span class="dt-init-replylink-bracket">[</span><a class="dt-init-replylink-reply" role="button" tabindex="0" data-mw-comment='{"type":"comment","level":1,"id":"c|Matma Rex|2020-01-22T23:19:00.000Z|table_with_one_comment","replies":[],"timestamp":"2020-01-22T23:19:00.000Z","author":"Matma Rex"}'>reply</a><span class="dt-init-replylink-bracket">]</span></span><span data-mw-comment-end="c|Matma Rex|2020-01-22T23:19:00.000Z|table_with_one_comment"></span>
</td></tr></tbody></table>
<h2 id="div_with_multiple_comments" data-mw-comment='{"type":"heading","level":0,"id":"h|div_with_multiple_comments|2020-01-22T23:19:00.000Z","replies":["c|Matma Rex|2020-01-22T23:19:00.000Z|div_with_multiple_comments","c|Matma Rex|2020-01-22T23:19:00.000Z|div_with_multiple_comments|1"],"headingLevel":2,"placeholderHeading":false}'><span data-mw-comment-start="h|div_with_multiple_comments|2020-01-22T23:19:00.000Z"></span>div with multiple comments<span data-mw-comment-end="h|div_with_multiple_comments|2020-01-22T23:19:00.000Z"></span></h2>
<div style="background: beige;"><span data-mw-comment-start="c|Matma Rex|2020-01-22T23:19:00.000Z|div_with_multiple_comments"></span>
blah blah <b><a href="/wiki/User:Matma_Rex" title="User:Matma Rex">Matma Rex</a> | <a href="/wiki/User_talk:Matma_Rex" title="User talk:Matma Rex">talk</a></b> 23:19, 22 January 2020 (UTC)<span data-mw-comment-start="c|Matma Rex|2020-01-22T23:19:00.000Z|div_with_multiple_comments|1"></span><span class="dt-init-replylink-buttons"><span class="dt-init-replylink-bracket">[</span><a class="dt-init-replylink-reply" role="button" tabindex="0" data-mw-comment='{"type":"comment","level":1,"id":"c|Matma Rex|2020-01-22T23:19:00.000Z|div_with_multiple_comments","replies":["c|Matma Rex|2020-01-22T23:19:00.000Z|Matma Rex|2020-01-22T23:19:00.000Z"],"timestamp":"2020-01-22T23:19:00.000Z","author":"Matma Rex"}'>reply</a><span class="dt-init-replylink-bracket">]</span></span><span data-mw-comment-end="c|Matma Rex|2020-01-22T23:19:00.000Z|div_with_multiple_comments"></span>
<dl><dd><span data-mw-comment-start="c|Matma Rex|2020-01-22T23:19:00.000Z|Matma Rex|2020-01-22T23:19:00.000Z"></span>blah blah <b><a href="/wiki/User:Matma_Rex" title="User:Matma Rex">Matma Rex</a> | <a href="/wiki/User_talk:Matma_Rex" title="User talk:Matma Rex">talk</a></b> 23:19, 22 January 2020 (UTC)<span class="dt-init-replylink-buttons"><span class="dt-init-replylink-bracket">[</span><a class="dt-init-replylink-reply" role="button" tabindex="0" data-mw-comment='{"type":"comment","level":2,"id":"c|Matma Rex|2020-01-22T23:19:00.000Z|Matma Rex|2020-01-22T23:19:00.000Z","replies":[],"timestamp":"2020-01-22T23:19:00.000Z","author":"Matma Rex"}'>reply</a><span class="dt-init-replylink-bracket">]</span></span><span data-mw-comment-end="c|Matma Rex|2020-01-22T23:19:00.000Z|Matma Rex|2020-01-22T23:19:00.000Z"></span></dd></dl>
blah blah <b><a href="/wiki/User:Matma_Rex" title="User:Matma Rex">Matma Rex</a> | <a href="/wiki/User_talk:Matma_Rex" title="User talk:Matma Rex">talk</a></b> 23:19, 22 January 2020 (UTC)<span class="dt-init-replylink-buttons"><span class="dt-init-replylink-bracket">[</span><a class="dt-init-replylink-reply" role="button" tabindex="0" data-mw-comment='{"type":"comment","level":1,"id":"c|Matma Rex|2020-01-22T23:19:00.000Z|div_with_multiple_comments|1","replies":[],"timestamp":"2020-01-22T23:19:00.000Z","author":"Matma Rex"}'>reply</a><span class="dt-init-replylink-bracket">]</span></span><span data-mw-comment-end="c|Matma Rex|2020-01-22T23:19:00.000Z|div_with_multiple_comments|1"></span>
</div>
<h2 id="table_with_multiple_comments" data-mw-comment='{"type":"heading","level":0,"id":"h|table_with_multiple_comments|2020-01-22T23:19:00.000Z","replies":["c|Matma Rex|2020-01-22T23:19:00.000Z|table_with_multiple_comments","c|Matma Rex|2020-01-22T23:19:00.000Z|table_with_multiple_comments|1"],"headingLevel":2,"placeholderHeading":false}'><span data-mw-comment-start="h|table_with_multiple_comments|2020-01-22T23:19:00.000Z"></span>table with multiple comments<span data-mw-comment-end="h|table_with_multiple_comments|2020-01-22T23:19:00.000Z"></span></h2>
<table border="1"><tbody><tr><td><span data-mw-comment-start="c|Matma Rex|2020-01-22T23:19:00.000Z|table_with_multiple_comments"></span>
blah blah <b><a href="/wiki/User:Matma_Rex" title="User:Matma Rex">Matma Rex</a> | <a href="/wiki/User_talk:Matma_Rex" title="User talk:Matma Rex">talk</a></b> 23:19, 22 January 2020 (UTC)<span data-mw-comment-start="c|Matma Rex|2020-01-22T23:19:00.000Z|table_with_multiple_comments|1"></span><span class="dt-init-replylink-buttons"><span class="dt-init-replylink-bracket">[</span><a class="dt-init-replylink-reply" role="button" tabindex="0" data-mw-comment='{"type":"comment","level":1,"id":"c|Matma Rex|2020-01-22T23:19:00.000Z|table_with_multiple_comments","replies":["c|Matma Rex|2020-01-22T23:19:00.000Z|Matma Rex|2020-01-22T23:19:00.000Z|1"],"timestamp":"2020-01-22T23:19:00.000Z","author":"Matma Rex"}'>reply</a><span class="dt-init-replylink-bracket">]</span></span><span data-mw-comment-end="c|Matma Rex|2020-01-22T23:19:00.000Z|table_with_multiple_comments"></span>
<dl><dd><span data-mw-comment-start="c|Matma Rex|2020-01-22T23:19:00.000Z|Matma Rex|2020-01-22T23:19:00.000Z|1"></span>blah blah <b><a href="/wiki/User:Matma_Rex" title="User:Matma Rex">Matma Rex</a> | <a href="/wiki/User_talk:Matma_Rex" title="User talk:Matma Rex">talk</a></b> 23:19, 22 January 2020 (UTC)<span class="dt-init-replylink-buttons"><span class="dt-init-replylink-bracket">[</span><a class="dt-init-replylink-reply" role="button" tabindex="0" data-mw-comment='{"type":"comment","level":2,"id":"c|Matma Rex|2020-01-22T23:19:00.000Z|Matma Rex|2020-01-22T23:19:00.000Z|1","replies":[],"timestamp":"2020-01-22T23:19:00.000Z","author":"Matma Rex"}'>reply</a><span class="dt-init-replylink-bracket">]</span></span><span data-mw-comment-end="c|Matma Rex|2020-01-22T23:19:00.000Z|Matma Rex|2020-01-22T23:19:00.000Z|1"></span></dd></dl>
blah blah <b><a href="/wiki/User:Matma_Rex" title="User:Matma Rex">Matma Rex</a> | <a href="/wiki/User_talk:Matma_Rex" title="User talk:Matma Rex">talk</a></b> 23:19, 22 January 2020 (UTC)<span class="dt-init-replylink-buttons"><span class="dt-init-replylink-bracket">[</span><a class="dt-init-replylink-reply" role="button" tabindex="0" data-mw-comment='{"type":"comment","level":1,"id":"c|Matma Rex|2020-01-22T23:19:00.000Z|table_with_multiple_comments|1","replies":[],"timestamp":"2020-01-22T23:19:00.000Z","author":"Matma Rex"}'>reply</a><span class="dt-init-replylink-bracket">]</span></span><span data-mw-comment-end="c|Matma Rex|2020-01-22T23:19:00.000Z|table_with_multiple_comments|1"></span>
</td></tr></tbody></table>

View file

@ -9,6 +9,12 @@
blah blah <b><a href="/wiki/User:Matma_Rex" title="User:Matma Rex">Matma Rex</a> | <a href="/wiki/User_talk:Matma_Rex" title="User talk:Matma Rex">talk</a></b> 23:19, 22 January 2020 (UTC)
</div><dl><dd data-parsoid="{}">Reply to c|Matma Rex|2020-01-22T23:19:00.000Z|div_with_one_comment</dd></dl>
<h2 id="div_with_one_comment-htmlcomments">div with one comment, and some HTML comments</h2>
<div style="background: beige;">
<i>hello</i><br/>
<small>blah blah <b><a href="/wiki/User:Matma_Rex" title="User:Matma Rex">Matma Rex</a> | <a href="/wiki/User_talk:Matma_Rex" title="User talk:Matma Rex">talk</a></b> 23:19, 22 January 2020 (UTC)</small><!-- blah -->
</div><!-- blah --><dl><dd data-parsoid="{}">Reply to c|Matma Rex|2020-01-22T23:19:00.000Z|div_with_one_comment-htmlcomments</dd></dl>
<h2 id="table_with_one_comment">table with one comment</h2>
<table border="1"><tbody><tr><td>
blah blah <b><a href="/wiki/User:Matma_Rex" title="User:Matma Rex">Matma Rex</a> | <a href="/wiki/User_talk:Matma_Rex" title="User talk:Matma Rex">talk</a></b> 23:19, 22 January 2020 (UTC)

View file

@ -9,6 +9,12 @@
blah blah <b><a href="/wiki/User:Matma_Rex" title="User:Matma Rex">Matma Rex</a> | <a href="/wiki/User_talk:Matma_Rex" title="User talk:Matma Rex">talk</a></b> 23:19, 22 January 2020 (UTC)
</div>
<h2 id="div_with_one_comment-htmlcomments">div with one comment, and some HTML comments</h2>
<div style="background: beige;">
<i>hello</i><br/>
<small>blah blah <b><a href="/wiki/User:Matma_Rex" title="User:Matma Rex">Matma Rex</a> | <a href="/wiki/User_talk:Matma_Rex" title="User talk:Matma Rex">talk</a></b> 23:19, 22 January 2020 (UTC)</small><!-- blah -->
</div><!-- blah -->
<h2 id="table_with_one_comment">table with one comment</h2>
<table border="1"><tbody><tr><td>
blah blah <b><a href="/wiki/User:Matma_Rex" title="User:Matma Rex">Matma Rex</a> | <a href="/wiki/User_talk:Matma_Rex" title="User talk:Matma Rex">talk</a></b> 23:19, 22 January 2020 (UTC)