From a0750129e786bd9f764e41638fb881525e3b6850 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartosz=20Dziewo=C5=84ski?= Date: Wed, 15 Jun 2022 02:30:54 +0200 Subject: [PATCH] Set rootNode for items from in ThreadItemSet#newFromAnnotatedNodes It's needed for the fix for T303396 to work correctly (in the case where there's exactly 1 thread item on the page), and for the highlight improvements in Iadbf83e02801fd9bd6336c84015994ee2819ce51. Change-Id: I2b2f5535ebbf792910133622a271ac1491c74de6 --- modules/ThreadItem.js | 5 ++++- modules/ThreadItemSet.js | 7 ++++--- modules/controller.js | 2 +- 3 files changed, 9 insertions(+), 5 deletions(-) diff --git a/modules/ThreadItem.js b/modules/ThreadItem.js index e133f97fe..2238f883e 100644 --- a/modules/ThreadItem.js +++ b/modules/ThreadItem.js @@ -51,10 +51,11 @@ OO.initClass( ThreadItem ); * Create a new ThreadItem from a JSON serialization * * @param {string|Object} json JSON serialization or hash object + * @param {HTMLElement} rootNode * @return {ThreadItem} * @throws {Error} Unknown ThreadItem type */ -ThreadItem.static.newFromJSON = function ( json ) { +ThreadItem.static.newFromJSON = function ( json, rootNode ) { // The page can be served from the HTTP cache (Varnish), and the JSON may be generated // by an older version of our PHP code. Code below must be able to handle that. // See ThreadItem::jsonSerialize() in PHP. @@ -92,6 +93,8 @@ ThreadItem.static.newFromJSON = function ( json ) { item.name = hash.name; item.id = hash.id; + item.rootNode = rootNode; + var idEscaped = $.escapeSelector( item.id ); var startMarker = document.getElementById( item.id ); var endMarker = document.querySelector( '[data-mw-comment-end="' + idEscaped + '"]' ); diff --git a/modules/ThreadItemSet.js b/modules/ThreadItemSet.js index c3d0de584..0fe3f93b2 100644 --- a/modules/ThreadItemSet.js +++ b/modules/ThreadItemSet.js @@ -21,11 +21,12 @@ OO.initClass( ThreadItemSet ); * Created a ThreadItemSet from DOM nodes that have been annotated by the PHP CommentFormatter with * metadata about the thread structure. * - * @param {HTMLElement} nodes + * @param {HTMLElement[]} nodes + * @param {HTMLElement} rootNode * @param {mw.dt.Parser} parser * @return {ThreadItemSet} */ -ThreadItemSet.static.newFromAnnotatedNodes = function ( nodes, parser ) { +ThreadItemSet.static.newFromAnnotatedNodes = function ( nodes, rootNode, parser ) { var result = new ThreadItemSet(); // The page can be served from the HTTP cache (Varnish), containing data-mw-comment generated @@ -41,7 +42,7 @@ ThreadItemSet.static.newFromAnnotatedNodes = function ( nodes, parser ) { // Create ThreadItem objects with basic data for ( i = 0; i < nodes.length; i++ ) { var hash = JSON.parse( nodes[ i ].getAttribute( 'data-mw-comment' ) ); - item = ThreadItem.static.newFromJSON( hash ); + item = ThreadItem.static.newFromJSON( hash, rootNode ); result.addThreadItem( item ); // Store info for second pass diff --git a/modules/controller.js b/modules/controller.js index 1ff7dd7eb..125416a3e 100644 --- a/modules/controller.js +++ b/modules/controller.js @@ -254,7 +254,7 @@ function init( $container, state ) { var parser = new Parser( require( './parser/data.json' ) ); var commentNodes = $pageContainer[ 0 ].querySelectorAll( '[data-mw-comment]' ); - pageThreads = ThreadItemSet.static.newFromAnnotatedNodes( commentNodes, parser ); + pageThreads = ThreadItemSet.static.newFromAnnotatedNodes( commentNodes, $pageContainer[ 0 ], parser ); if ( featuresEnabled.topicsubscription ) { topicSubscriptions.initTopicSubscriptions( $container, pageThreads );