From f411f1c342f3064bebb175336aa46d403c6106fd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartosz=20Dziewo=C5=84ski?= Date: Wed, 20 Apr 2022 19:49:00 +0200 Subject: [PATCH] highlighter: Set the location hash for new topics on desktop as well The old section=new wikitext editor does this, and I'd rather support the same behavior on desktop and mobile than different ones. Follow-up to 89a1efc9d8fa3126d38d83540729926b4d56d6da. Change-Id: I5371f932680638ee159f2f713bbca44bba5675aa --- modules/highlighter.js | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/modules/highlighter.js b/modules/highlighter.js index 11e70d1b9..4c6d7c71d 100644 --- a/modules/highlighter.js +++ b/modules/highlighter.js @@ -174,12 +174,13 @@ function highlightPublishedComment( threadItemSet, threadItemId ) { ) { highlightComments.push( lastComment.parent ); lastHighlightedPublishedComment = lastComment.parent; - var $collapsibleHeading = $( lastHighlightedPublishedComment.range.startContainer ).closest( '.collapsible-heading' ); - // Expand collapsed section on mobile - if ( $collapsibleHeading.length ) { - var id = utils.getHeadlineNodeAndOffset( $collapsibleHeading[ 0 ] ).node.id; - location.hash = '#' + id; - } + // Change URL to point to this section, like the old section=new wikitext editor does. + // This also expands collapsed sections on mobile (T301840). + var sectionTitle = lastHighlightedPublishedComment.getLinkableTitle(); + var urlFragment = mw.util.escapeIdForLink( sectionTitle ); + // Navigate to fragment without scrolling + location.hash = '#' + urlFragment + '-DoesNotExist-DiscussionToolsHack'; + history.replaceState( null, document.title, '#' + urlFragment ); } } else { // Find the comment we replied to, then highlight the last reply @@ -318,11 +319,11 @@ function clearHighlightTargetComment( threadItemSet ) { // * Using history.pushState() does not trigger 'hashchange' or update the :target selector. // https://developer.mozilla.org/en-US/docs/Web/API/History/pushState#description // https://github.com/whatwg/html/issues/639 - // * Using window.location.hash does, but it also scrolls to the target, which is the top of the + // * Using location.hash does, but it also scrolls to the target, which is the top of the // page for the empty hash. - // Instead, we first use window.location.hash to navigate to a *different* hash (whose target + // Instead, we first use location.hash to navigate to a *different* hash (whose target // doesn't exist on the page, hopefully), and then use history.pushState() to clear it. - window.location.hash += '-DoesNotExist-DiscussionToolsHack'; + location.hash += '-DoesNotExist-DiscussionToolsHack'; url.hash = ''; history.replaceState( null, document.title, url ); } else if (