Fix issues with treating section "numbers" as integers

In MediaWiki, section numbers may be prefixed with 'T-' if they refer
to sections on a transcluded page, so they are not really numbers.

Change e2cb9ce93e caused us to treat them as strings most of the time,
but it looks like there are several places where we treated them as
integer numbers, which I missed when making that patch.

The first two changes in ArticleTarget#restoreEditSection fix T248795
and T248968/T249112, respectively. The other changes are cleanup.

Bug: T248795
Bug: T248968
Bug: T249112
Change-Id: I8373a7ab515595769ce6f3051a182c922415b643
This commit is contained in:
Bartosz Dziewoński 2020-03-31 22:04:30 +02:00
parent 55a868812c
commit 92c3055628
7 changed files with 16 additions and 15 deletions

View file

@ -1932,7 +1932,7 @@ ve.init.mw.ArticleTarget.prototype.restoreEditSection = function () {
surface = this.getSurface(),
mode = surface.getMode();
if ( section !== null && section !== 'new' && section !== 0 && section !== 'T-0' ) {
if ( section !== null && section !== 'new' && section !== '0' && section !== 'T-0' ) {
if ( mode === 'visual' ) {
dmDoc = surface.getModel().getDocument();
// In mw.libs.ve.unwrapParsoidSections we copy the data-mw-section-id from the section element
@ -1942,7 +1942,7 @@ ve.init.mw.ArticleTarget.prototype.restoreEditSection = function () {
var domElements = heading.getOriginalDomElements( dmDoc.getStore() );
if (
domElements && domElements[ 0 ].nodeType === Node.ELEMENT_NODE &&
+domElements[ 0 ].getAttribute( 'data-mw-section-id' ) === section
domElements[ 0 ].getAttribute( 'data-mw-section-id' ) === section
) {
headingModel = heading;
return true;
@ -2259,7 +2259,8 @@ ve.init.mw.ArticleTarget.prototype.switchToVisualEditor = function () {
/**
* Switch to a different wikitext section
*
* @param {number|string|null} section New section, number, 'new' or null (whole document)
* @param {string|null} section Section to switch to: a number, 'T-'-prefixed number, 'new'
* or null (whole document)
* @param {boolean} noConfirm Switch without prompting (changes will be lost either way)
*/
ve.init.mw.ArticleTarget.prototype.switchToWikitextSection = function ( section, noConfirm ) {

View file

@ -14,7 +14,7 @@
* @constructor
* @param {VisualEditorOverlay} overlay Mobile frontend overlay
* @param {Object} [config] Configuration options
* @cfg {number} [section] Number of the section target should scroll to
* @cfg {string} [section] Number of the section target should scroll to
*/
ve.init.mw.MobileArticleTarget = function VeInitMwMobileArticleTarget( overlay, config ) {
this.overlay = overlay;

View file

@ -188,7 +188,7 @@ ve.init.mw.Target.prototype.createModelFromDom = function () {
* @inheritdoc
* @param {string} documentString
* @param {string} mode
* @param {number|string|null} section Section. Use null to unwrap all sections.
* @param {string|null} [section] Section. Use null to unwrap all sections.
* @param {boolean} [onlySection] Only return the requested section, otherwise returns the
* whole document with just the requested section still wrapped (visual mode only).
* @return {HTMLDocument|string} HTML document, or document string (source mode)

View file

@ -173,7 +173,7 @@
* @param {string} pageName Page name to request, in prefixed DB key form (underscores instead of spaces)
* @param {Object} [options] Options
* @param {boolean} [options.sessionStore] Store result in session storage (by page+mode+section) for auto-save
* @param {number|null|string} [options.section] Section to edit; number, null or 'new' (currently just source mode)
* @param {null|string} [options.section] Section to edit; number, 'T-'-prefixed, null or 'new' (currently just source mode)
* @param {number} [options.oldId] Old revision ID. Current if omitted.
* @param {string} [options.targetName] Optional target name for tracking
* @param {boolean} [options.modified] The page was been modified before loading (e.g. in source mode)

View file

@ -197,7 +197,7 @@
*
* @private
* @param {string} mode Target mode: 'visual' or 'source'
* @param {number|string} section Section to edit
* @param {string} section Section to edit
* @return {jQuery.Promise}
*/
function getTarget( mode, section ) {
@ -344,7 +344,7 @@
*
* @private
* @param {string} mode Target mode: 'visual' or 'source'
* @param {number|string} [section] Section to edit (currently just source mode)
* @param {string} [section] Section to edit (currently just source mode)
* @param {jQuery.Promise} [targetPromise] Promise that will be resolved with a ve.init.mw.DesktopArticleTarget
* @param {boolean} [modified] The page was been modified before loading (e.g. in source mode)
*/
@ -897,7 +897,7 @@
activateVe: function ( mode ) {
var wikitext = $( '#wpTextbox1' ).textSelection( 'getContents' ),
sectionVal = $( 'input[name=wpSection]' ).val(),
section = sectionVal !== '' && sectionVal !== undefined ? +sectionVal : null,
section = sectionVal !== '' && sectionVal !== undefined ? sectionVal : null,
config = mw.config.get( 'wgVisualEditorConfig' ),
canSwitch = config.fullRestbaseUrl || config.allowLossySwitching,
modified = mw.config.get( 'wgAction' ) === 'submit' ||
@ -947,7 +947,7 @@
*
* @param {string} mode Edit mode
* @param {jQuery.Event} e Click event
* @param {number|string} [section] Override edit section, taken from link URL if not specified
* @param {string} [section] Override edit section, taken from link URL if not specified
*/
onEditSectionLinkClick: function ( mode, e, section ) {
var targetPromise,

View file

@ -22,7 +22,7 @@
* Get a ve.dm.Document model from a Parsoid response
*
* @param {Object} response Parsoid response from the VisualEditor API
* @param {number|null} section Section. Null for the whole document.
* @param {string|null} section Section. Null for the whole document.
* @return {ve.dm.Document|null} Document, or null if an invalid response
*/
getModelFromResponse: function ( response, section ) {
@ -51,7 +51,7 @@
*
* @param {number} revId Revision ID
* @param {string} [pageName] Page name, defaults to wgRelevantPageName
* @param {number|null} [section=null] Section. Null for the whole document.
* @param {string|null} [section=null] Section. Null for the whole document.
* @param {jQuery.Promise} [parseDocumentModulePromise] Promise which resolves when Target#parseDocument is available
* @return {jQuery.Promise} Promise which resolves with a document model
*/

View file

@ -49,17 +49,17 @@ mw.libs.ve.decodeURIComponentIntoArticleTitle = function ( s, preserveUnderscore
* Unwrap Parsoid sections
*
* @param {HTMLElement} element Parent element, e.g. document body
* @param {number} [keepSection] Section to keep
* @param {string} [keepSection] Section to keep
*/
mw.libs.ve.unwrapParsoidSections = function ( element, keepSection ) {
Array.prototype.forEach.call( element.querySelectorAll( 'section[data-mw-section-id]' ), function ( section ) {
var parent = section.parentNode,
sectionId = section.getAttribute( 'data-mw-section-id' );
// Copy section ID to first child (should be a heading)
if ( sectionId > 0 ) {
if ( sectionId !== '0' ) {
section.firstChild.setAttribute( 'data-mw-section-id', sectionId );
}
if ( keepSection !== undefined && +sectionId === keepSection ) {
if ( keepSection !== undefined && sectionId === keepSection ) {
return;
}
while ( section.firstChild ) {