2023-08-21 08:08:23 +00:00
|
|
|
'use strict';
|
|
|
|
|
2016-02-03 21:03:41 +00:00
|
|
|
/*!
|
|
|
|
* VisualEditor DataModel MWReferenceNode class.
|
|
|
|
*
|
2018-01-03 01:05:45 +00:00
|
|
|
* @copyright 2011-2018 VisualEditor Team's Cite sub-team and others; see AUTHORS.txt
|
2017-12-29 12:12:35 +00:00
|
|
|
* @license MIT
|
2016-02-03 21:03:41 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* DataModel MediaWiki reference node.
|
|
|
|
*
|
|
|
|
* @constructor
|
2024-02-28 08:57:24 +00:00
|
|
|
* @extends ve.dm.LeafNode
|
|
|
|
* @mixes ve.dm.FocusableNode
|
2016-02-03 21:03:41 +00:00
|
|
|
* @param {Object} [element] Reference to element in linear model
|
|
|
|
*/
|
|
|
|
ve.dm.MWReferenceNode = function VeDmMWReferenceNode() {
|
|
|
|
// Parent constructor
|
2016-11-02 12:43:14 +00:00
|
|
|
ve.dm.MWReferenceNode.super.apply( this, arguments );
|
2016-02-03 21:03:41 +00:00
|
|
|
|
|
|
|
// Mixin constructors
|
|
|
|
ve.dm.FocusableNode.call( this );
|
|
|
|
|
|
|
|
// Event handlers
|
|
|
|
this.connect( this, {
|
|
|
|
root: 'onRoot',
|
|
|
|
unroot: 'onUnroot',
|
|
|
|
attributeChange: 'onAttributeChange'
|
|
|
|
} );
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Inheritance */
|
|
|
|
|
|
|
|
OO.inheritClass( ve.dm.MWReferenceNode, ve.dm.LeafNode );
|
|
|
|
|
|
|
|
OO.mixinClass( ve.dm.MWReferenceNode, ve.dm.FocusableNode );
|
|
|
|
|
|
|
|
/* Static members */
|
|
|
|
|
|
|
|
ve.dm.MWReferenceNode.static.name = 'mwReference';
|
|
|
|
|
|
|
|
ve.dm.MWReferenceNode.static.matchTagNames = null;
|
|
|
|
|
|
|
|
ve.dm.MWReferenceNode.static.matchRdfaTypes = [ 'mw:Extension/ref' ];
|
|
|
|
|
2022-01-20 17:35:23 +00:00
|
|
|
// Handle nodes with mw:Error as this probably just means the ref list doesn't exist (T299672)
|
|
|
|
ve.dm.MWReferenceNode.static.allowedRdfaTypes = [ 'dc:references', 'mw:Error' ];
|
2016-02-03 21:03:41 +00:00
|
|
|
|
|
|
|
ve.dm.MWReferenceNode.static.isContent = true;
|
|
|
|
|
2020-06-10 11:16:19 +00:00
|
|
|
ve.dm.MWReferenceNode.static.disallowedAnnotationTypes = [ 'link' ];
|
2016-02-03 21:03:41 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Regular expression for parsing the listKey attribute
|
2020-06-09 10:27:52 +00:00
|
|
|
*
|
2020-09-24 12:09:51 +00:00
|
|
|
* Use [\s\S]* instead of .* to catch esoteric whitespace (T263698)
|
|
|
|
*
|
2016-02-03 21:03:41 +00:00
|
|
|
* @static
|
|
|
|
* @property {RegExp}
|
|
|
|
* @inheritable
|
|
|
|
*/
|
2020-09-24 12:09:51 +00:00
|
|
|
ve.dm.MWReferenceNode.static.listKeyRegex = /^(auto|literal)\/([\s\S]*)$/;
|
2016-02-03 21:03:41 +00:00
|
|
|
|
|
|
|
ve.dm.MWReferenceNode.static.toDataElement = function ( domElements, converter ) {
|
|
|
|
function getReflistItemHtml( id ) {
|
2023-08-21 08:08:23 +00:00
|
|
|
const elem = converter.getHtmlDocument().getElementById( id );
|
2024-02-20 12:07:54 +00:00
|
|
|
return elem && elem.innerHTML;
|
2016-02-03 21:03:41 +00:00
|
|
|
}
|
|
|
|
|
2023-08-21 08:08:23 +00:00
|
|
|
const mwDataJSON = domElements[ 0 ].getAttribute( 'data-mw' );
|
|
|
|
const mwData = mwDataJSON ? JSON.parse( mwDataJSON ) : {};
|
2024-02-21 12:52:12 +00:00
|
|
|
const mwAttrs = mwData.attrs || {};
|
|
|
|
const reflistItemId = ve.getProp( mwData, 'body', 'id' );
|
|
|
|
const body = ve.getProp( mwData, 'body', 'html' ) ||
|
2016-02-03 21:03:41 +00:00
|
|
|
( reflistItemId && getReflistItemHtml( reflistItemId ) ) ||
|
|
|
|
'';
|
2024-02-21 12:52:12 +00:00
|
|
|
const refGroup = mwAttrs.group || '';
|
2023-08-21 08:08:23 +00:00
|
|
|
const listGroup = this.name + '/' + refGroup;
|
2024-02-21 12:52:12 +00:00
|
|
|
const listKey = !mwAttrs.name ?
|
2016-02-03 21:03:41 +00:00
|
|
|
'auto/' + converter.internalList.getNextUniqueNumber() :
|
2024-02-21 12:52:12 +00:00
|
|
|
'literal/' + mwAttrs.name;
|
2023-08-21 08:08:23 +00:00
|
|
|
const queueResult = converter.internalList.queueItemHtml( listGroup, listKey, body );
|
2016-02-03 21:03:41 +00:00
|
|
|
|
2023-08-21 08:08:23 +00:00
|
|
|
const dataElement = {
|
2016-02-03 21:03:41 +00:00
|
|
|
type: this.name,
|
|
|
|
attributes: {
|
|
|
|
mw: mwData,
|
|
|
|
originalMw: mwDataJSON,
|
2024-02-20 12:07:54 +00:00
|
|
|
listIndex: queueResult.index,
|
2016-02-03 21:03:41 +00:00
|
|
|
listGroup: listGroup,
|
|
|
|
listKey: listKey,
|
|
|
|
refGroup: refGroup,
|
2024-02-20 12:07:54 +00:00
|
|
|
contentsUsed: body !== '' && queueResult.isNew
|
2016-02-03 21:03:41 +00:00
|
|
|
}
|
|
|
|
};
|
2024-02-20 12:07:54 +00:00
|
|
|
if ( mwAttrs.extends && mw.config.get( 'wgCiteBookReferencing' ) ) {
|
2024-06-13 07:19:34 +00:00
|
|
|
dataElement.attributes.extendsRef = mwAttrs.extends ? 'literal/' + mwAttrs.extends : null;
|
2023-05-04 16:56:41 +00:00
|
|
|
}
|
2016-02-03 21:03:41 +00:00
|
|
|
if ( reflistItemId ) {
|
|
|
|
dataElement.attributes.refListItemId = reflistItemId;
|
|
|
|
}
|
|
|
|
return dataElement;
|
|
|
|
};
|
|
|
|
|
|
|
|
ve.dm.MWReferenceNode.static.toDomElements = function ( dataElement, doc, converter ) {
|
2023-08-21 08:08:23 +00:00
|
|
|
const isForClipboard = converter.isForClipboard();
|
|
|
|
const el = doc.createElement( 'sup' );
|
2016-02-03 21:03:41 +00:00
|
|
|
|
|
|
|
el.setAttribute( 'typeof', 'mw:Extension/ref' );
|
|
|
|
|
2023-08-21 08:08:23 +00:00
|
|
|
const mwData = dataElement.attributes.mw ? ve.copy( dataElement.attributes.mw ) : {};
|
2016-02-03 21:03:41 +00:00
|
|
|
mwData.name = 'ref';
|
|
|
|
|
2018-04-28 13:46:23 +00:00
|
|
|
if ( isForClipboard || converter.isForParser() ) {
|
2023-08-21 08:08:23 +00:00
|
|
|
let setContents = dataElement.attributes.contentsUsed;
|
2016-02-03 21:03:41 +00:00
|
|
|
|
2018-04-28 13:46:23 +00:00
|
|
|
// This call rebuilds the document tree if it isn't built already (e.g. on a
|
|
|
|
// document slice), so only use when necessary (i.e. not in preview mode)
|
2023-08-21 08:08:23 +00:00
|
|
|
const itemNode = converter.internalList.getItemNode( dataElement.attributes.listIndex );
|
|
|
|
const itemNodeRange = itemNode.getRange();
|
2016-02-03 21:03:41 +00:00
|
|
|
|
2023-08-21 08:08:23 +00:00
|
|
|
const keyedNodes = converter.internalList
|
2018-04-28 13:46:23 +00:00
|
|
|
.getNodeGroup( dataElement.attributes.listGroup )
|
|
|
|
.keyedNodes[ dataElement.attributes.listKey ];
|
|
|
|
|
2024-06-07 20:54:28 +00:00
|
|
|
const extendsNodes = converter.internalList.getNodeGroup( dataElement.attributes.listGroup ).firstNodes.filter(
|
|
|
|
( node ) => node.element.attributes.extendsRef === dataElement.attributes.listKey
|
|
|
|
);
|
|
|
|
|
2023-08-21 08:08:23 +00:00
|
|
|
let contentsAlreadySet = false;
|
2018-04-28 13:46:23 +00:00
|
|
|
if ( setContents ) {
|
|
|
|
// Check if a previous node has already set the content. If so, we don't overwrite this
|
|
|
|
// node's contents.
|
|
|
|
if ( keyedNodes ) {
|
2023-08-21 08:08:23 +00:00
|
|
|
for ( let i = 0; i < keyedNodes.length; i++ ) {
|
2021-11-19 14:13:07 +00:00
|
|
|
if (
|
|
|
|
ve.compare(
|
|
|
|
this.getInstanceHashObject( keyedNodes[ i ].element ),
|
|
|
|
this.getInstanceHashObject( dataElement )
|
|
|
|
)
|
|
|
|
) {
|
2018-04-28 13:46:23 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
if ( keyedNodes[ i ].element.attributes.contentsUsed ) {
|
|
|
|
contentsAlreadySet = true;
|
|
|
|
break;
|
|
|
|
}
|
2016-02-03 21:03:41 +00:00
|
|
|
}
|
|
|
|
}
|
2018-04-28 13:46:23 +00:00
|
|
|
} else {
|
|
|
|
// Check if any other nodes with this key provided content. If not
|
|
|
|
// then we attach the contents to the first reference with this key
|
|
|
|
|
|
|
|
// Check that this is the first reference with its key
|
2021-11-19 14:13:07 +00:00
|
|
|
if (
|
|
|
|
keyedNodes &&
|
|
|
|
ve.compare(
|
|
|
|
this.getInstanceHashObject( dataElement ),
|
|
|
|
this.getInstanceHashObject( keyedNodes[ 0 ].element )
|
|
|
|
)
|
|
|
|
) {
|
2018-04-28 13:46:23 +00:00
|
|
|
setContents = true;
|
|
|
|
// Check no other reference originally defined the contents
|
|
|
|
// As this is keyedNodes[0] we can start at 1
|
2023-08-21 08:08:23 +00:00
|
|
|
for ( let i = 1; i < keyedNodes.length; i++ ) {
|
2018-04-28 13:46:23 +00:00
|
|
|
if ( keyedNodes[ i ].element.attributes.contentsUsed ) {
|
|
|
|
setContents = false;
|
|
|
|
break;
|
|
|
|
}
|
2016-02-03 21:03:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-28 13:46:23 +00:00
|
|
|
// Add reference contents to data-mw.
|
|
|
|
if ( setContents && !contentsAlreadySet ) {
|
2023-08-21 08:08:23 +00:00
|
|
|
const itemNodeWrapper = doc.createElement( 'div' );
|
|
|
|
const originalHtmlWrapper = doc.createElement( 'div' );
|
2018-04-28 13:46:23 +00:00
|
|
|
converter.getDomSubtreeFromData(
|
2018-07-19 14:46:23 +00:00
|
|
|
itemNode.getDocument().getFullData( itemNodeRange, 'roundTrip' ),
|
2018-04-28 13:46:23 +00:00
|
|
|
itemNodeWrapper
|
|
|
|
);
|
2024-01-30 11:18:52 +00:00
|
|
|
// Returns '' if itemNodeWrapper is empty
|
|
|
|
const itemNodeHtml = itemNodeWrapper.innerHTML;
|
2023-08-21 08:08:23 +00:00
|
|
|
const originalHtml = ve.getProp( mwData, 'body', 'html' ) ||
|
2018-04-28 13:46:23 +00:00
|
|
|
( ve.getProp( mwData, 'body', 'id' ) !== undefined && itemNode.getAttribute( 'originalHtml' ) ) ||
|
|
|
|
'';
|
|
|
|
originalHtmlWrapper.innerHTML = originalHtml;
|
|
|
|
// Only set body.html if itemNodeHtml and originalHtml are actually different,
|
|
|
|
// or we are writing the clipboard for use in another VE instance
|
|
|
|
if ( isForClipboard || !originalHtmlWrapper.isEqualNode( itemNodeWrapper ) ) {
|
|
|
|
ve.setProp( mwData, 'body', 'html', itemNodeHtml );
|
|
|
|
}
|
2016-02-03 21:03:41 +00:00
|
|
|
}
|
|
|
|
|
2018-04-28 13:46:23 +00:00
|
|
|
// If we have no internal item data for this reference, don't let it get pasted into
|
|
|
|
// another VE document. T110479
|
|
|
|
if ( isForClipboard && itemNodeRange.isCollapsed() ) {
|
|
|
|
el.setAttribute( 'data-ve-ignore', 'true' );
|
|
|
|
}
|
2016-02-03 21:03:41 +00:00
|
|
|
|
2023-05-04 16:56:41 +00:00
|
|
|
// Set extends
|
|
|
|
if ( dataElement.attributes.extendsRef ) {
|
2024-06-13 07:19:34 +00:00
|
|
|
let extendsAttr;
|
|
|
|
const extendsKeyParts = dataElement.attributes.extendsRef.match( this.listKeyRegex );
|
|
|
|
if ( extendsKeyParts[ 1 ] === 'auto' ) {
|
2024-06-07 20:54:28 +00:00
|
|
|
// Allocate a unique list key, then strip the 'literal/'' prefix
|
|
|
|
extendsAttr = converter.internalList.getUniqueListKey(
|
|
|
|
dataElement.attributes.listGroup,
|
|
|
|
dataElement.attributes.extendsRef,
|
|
|
|
// Generate a name starting with ':' to distinguish it from normal names
|
|
|
|
'literal/:'
|
|
|
|
).slice( 'literal/'.length );
|
2024-06-13 07:19:34 +00:00
|
|
|
} else {
|
|
|
|
extendsAttr = extendsKeyParts[ 2 ];
|
|
|
|
}
|
|
|
|
ve.setProp( mwData, 'attrs', 'extends', extendsAttr );
|
2023-05-04 16:56:41 +00:00
|
|
|
}
|
|
|
|
|
2018-04-28 13:46:23 +00:00
|
|
|
// Generate name
|
2023-08-21 08:08:23 +00:00
|
|
|
let name;
|
|
|
|
const listKeyParts = dataElement.attributes.listKey.match( this.listKeyRegex );
|
2018-04-28 13:46:23 +00:00
|
|
|
if ( listKeyParts[ 1 ] === 'auto' ) {
|
|
|
|
// Only render a name if this key was reused
|
2024-06-07 20:54:28 +00:00
|
|
|
if ( keyedNodes.length > 1 || extendsNodes.length ) {
|
2018-04-28 13:46:23 +00:00
|
|
|
// Allocate a unique list key, then strip the 'literal/'' prefix
|
|
|
|
name = converter.internalList.getUniqueListKey(
|
|
|
|
dataElement.attributes.listGroup,
|
|
|
|
dataElement.attributes.listKey,
|
|
|
|
// Generate a name starting with ':' to distinguish it from normal names
|
|
|
|
'literal/:'
|
|
|
|
).slice( 'literal/'.length );
|
|
|
|
}
|
2016-02-03 21:03:41 +00:00
|
|
|
} else {
|
2018-04-28 13:46:23 +00:00
|
|
|
// Use literal name
|
|
|
|
name = listKeyParts[ 2 ];
|
|
|
|
}
|
|
|
|
// Set name
|
|
|
|
if ( name !== undefined ) {
|
|
|
|
ve.setProp( mwData, 'attrs', 'name', name );
|
2016-02-03 21:03:41 +00:00
|
|
|
}
|
|
|
|
|
2018-04-28 13:46:23 +00:00
|
|
|
// Set or clear group
|
|
|
|
if ( dataElement.attributes.refGroup !== '' ) {
|
|
|
|
ve.setProp( mwData, 'attrs', 'group', dataElement.attributes.refGroup );
|
|
|
|
} else if ( mwData.attrs ) {
|
2024-02-21 12:27:19 +00:00
|
|
|
delete mwData.attrs.group;
|
2018-04-28 13:46:23 +00:00
|
|
|
}
|
2016-02-03 21:03:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// If mwAttr and originalMw are the same, use originalMw to prevent reserialization,
|
|
|
|
// unless we are writing the clipboard for use in another VE instance
|
|
|
|
// Reserialization has the potential to reorder keys and so change the DOM unnecessarily
|
2023-08-21 08:08:23 +00:00
|
|
|
const originalMw = dataElement.attributes.originalMw;
|
2018-04-28 13:46:23 +00:00
|
|
|
if ( converter.isForParser() && originalMw && ve.compare( mwData, JSON.parse( originalMw ) ) ) {
|
2016-02-03 21:03:41 +00:00
|
|
|
el.setAttribute( 'data-mw', originalMw );
|
|
|
|
|
|
|
|
// Return the original DOM elements if possible
|
2018-03-06 13:00:35 +00:00
|
|
|
if ( dataElement.originalDomElementsHash !== undefined ) {
|
2024-01-30 11:18:52 +00:00
|
|
|
return ve.copyDomElements(
|
|
|
|
converter.getStore().value( dataElement.originalDomElementsHash ), doc );
|
2016-02-03 21:03:41 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
el.setAttribute( 'data-mw', JSON.stringify( mwData ) );
|
2017-06-12 14:03:00 +00:00
|
|
|
|
2016-02-03 21:03:41 +00:00
|
|
|
// HTML for the external clipboard, it will be ignored by the converter
|
2023-08-21 08:08:23 +00:00
|
|
|
const $link = $( '<a>', doc )
|
2023-05-17 09:18:33 +00:00
|
|
|
.attr( 'data-mw-group', this.getGroup( dataElement ) || null );
|
2024-07-23 14:02:59 +00:00
|
|
|
$( el ).addClass( 'mw-ref reference' ).html(
|
2017-06-12 14:03:00 +00:00
|
|
|
$link.append(
|
2024-07-23 14:02:59 +00:00
|
|
|
$( '<span>', doc ).addClass( 'mw-reflink-text' ).html( this.getIndexLabel( dataElement, converter.internalList ) )
|
2017-03-17 15:45:06 +00:00
|
|
|
)
|
2016-02-03 21:03:41 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return [ el ];
|
|
|
|
};
|
|
|
|
|
2024-01-30 11:18:52 +00:00
|
|
|
ve.dm.MWReferenceNode.static.remapInternalListIndexes = function (
|
|
|
|
dataElement, mapping, internalList
|
|
|
|
) {
|
2016-02-03 21:03:41 +00:00
|
|
|
// Remap listIndex
|
|
|
|
dataElement.attributes.listIndex = mapping[ dataElement.attributes.listIndex ];
|
|
|
|
|
|
|
|
// Remap listKey if it was automatically generated
|
2023-08-21 08:08:23 +00:00
|
|
|
const listKeyParts = dataElement.attributes.listKey.match( this.listKeyRegex );
|
2016-02-03 21:03:41 +00:00
|
|
|
if ( listKeyParts[ 1 ] === 'auto' ) {
|
|
|
|
dataElement.attributes.listKey = 'auto/' + internalList.getNextUniqueNumber();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
ve.dm.MWReferenceNode.static.remapInternalListKeys = function ( dataElement, internalList ) {
|
2023-08-21 08:08:23 +00:00
|
|
|
let suffix = '';
|
2016-02-03 21:03:41 +00:00
|
|
|
// Try name, name2, name3, ... until unique
|
|
|
|
while ( internalList.keys.indexOf( dataElement.attributes.listKey + suffix ) !== -1 ) {
|
|
|
|
suffix = suffix ? suffix + 1 : 2;
|
|
|
|
}
|
|
|
|
if ( suffix ) {
|
2023-05-17 09:18:33 +00:00
|
|
|
dataElement.attributes.listKey += suffix;
|
2016-02-03 21:03:41 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the group for the reference
|
|
|
|
*
|
|
|
|
* @static
|
|
|
|
* @param {Object} dataElement Element data
|
|
|
|
* @return {string} Group
|
|
|
|
*/
|
|
|
|
ve.dm.MWReferenceNode.static.getGroup = function ( dataElement ) {
|
|
|
|
return dataElement.attributes.refGroup;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the index label for the reference
|
|
|
|
*
|
|
|
|
* @static
|
|
|
|
* @param {Object} dataElement Element data
|
|
|
|
* @param {ve.dm.InternalList} internalList Internal list
|
2024-07-23 14:02:59 +00:00
|
|
|
* @return {string} Reference label as HTML
|
2016-02-03 21:03:41 +00:00
|
|
|
*/
|
|
|
|
ve.dm.MWReferenceNode.static.getIndexLabel = function ( dataElement, internalList ) {
|
2023-08-21 08:08:23 +00:00
|
|
|
const refGroup = dataElement.attributes.refGroup;
|
2024-06-28 10:12:37 +00:00
|
|
|
const indexNumber = dataElement.attributes.placeholder ? '…' :
|
|
|
|
ve.dm.MWReferenceNode.static.findIndexNumber( dataElement, internalList );
|
2024-07-23 14:02:59 +00:00
|
|
|
const label = ( refGroup ? refGroup + ' ' : '' ) + indexNumber;
|
2016-02-03 21:03:41 +00:00
|
|
|
|
2024-07-23 14:02:59 +00:00
|
|
|
return `<span class="cite-bracket">[</span>${ label }<span class="cite-bracket">]</span>`;
|
2024-06-28 10:12:37 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* TODO: replace with a simple property
|
|
|
|
*
|
|
|
|
* @private
|
|
|
|
* @param {Object} dataElement data for the node to be looked up
|
|
|
|
* @param {ve.dm.InternalList} internalList document internalList
|
|
|
|
* @return {string} footnote number ready for rendering
|
|
|
|
*/
|
|
|
|
ve.dm.MWReferenceNode.static.findIndexNumber = function ( dataElement, internalList ) {
|
|
|
|
return ve.getProp( dataElement, 'internal', 'overrideIndex' ) ||
|
|
|
|
ve.dm.MWDocumentReferences.static.refsForDoc( internalList.getDocument() )
|
2024-07-27 17:04:33 +00:00
|
|
|
.getIndexLabel( dataElement.attributes.refGroup, dataElement.attributes.listKey );
|
2016-02-03 21:03:41 +00:00
|
|
|
};
|
|
|
|
|
2016-05-04 13:34:23 +00:00
|
|
|
/**
|
2024-02-28 08:57:24 +00:00
|
|
|
* @override
|
|
|
|
* @see ve.dm.Node
|
2016-05-04 13:34:23 +00:00
|
|
|
*/
|
|
|
|
ve.dm.MWReferenceNode.static.cloneElement = function () {
|
2023-08-21 08:08:23 +00:00
|
|
|
const clone = ve.dm.MWReferenceNode.super.static.cloneElement.apply( this, arguments );
|
2016-05-04 13:34:23 +00:00
|
|
|
delete clone.attributes.contentsUsed;
|
|
|
|
delete clone.attributes.mw;
|
|
|
|
delete clone.attributes.originalMw;
|
2022-01-27 01:14:52 +00:00
|
|
|
// HACK: Generate a fake hash so this element is never instance comparable to other elements
|
|
|
|
// Without originalMw this hash will not get used in toDomElements
|
|
|
|
clone.originalDomElementsHash = Math.random();
|
2016-05-04 13:34:23 +00:00
|
|
|
return clone;
|
|
|
|
};
|
|
|
|
|
2021-01-21 14:34:36 +00:00
|
|
|
/**
|
2024-02-28 08:57:24 +00:00
|
|
|
* @override
|
|
|
|
* @see ve.dm.LeafNode
|
2021-01-21 14:34:36 +00:00
|
|
|
*/
|
|
|
|
ve.dm.MWReferenceNode.static.getHashObject = function ( dataElement ) {
|
|
|
|
// Consider all references in the same group to be comparable:
|
|
|
|
// References can't be usefully compared statically, as they are mostly
|
|
|
|
// defined by the contents of their internal item, which exists
|
|
|
|
// elsewhere in the document.
|
|
|
|
// For diffing, comparing reference indexes is not useful as
|
|
|
|
// they are auto-generated, and the reference list diff is
|
|
|
|
// already handled separately, so will show moves etc.
|
2021-11-19 14:13:07 +00:00
|
|
|
//
|
|
|
|
// If you need to compare references with the same name, use
|
|
|
|
// #getInstanceHashObject
|
2021-01-21 14:34:36 +00:00
|
|
|
return {
|
|
|
|
type: dataElement.type,
|
|
|
|
attributes: {
|
|
|
|
listGroup: dataElement.attributes.listGroup
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2021-11-19 14:13:07 +00:00
|
|
|
/**
|
|
|
|
* Get a hash unique to this instance of the reference
|
|
|
|
*
|
|
|
|
* As #getHashObject has been simplified to make re-used references
|
|
|
|
* all equal (to support visual diffing), provide access to a more
|
|
|
|
* typical hash that can be used to compare instances of reference
|
|
|
|
* which have the same "name".
|
|
|
|
*
|
|
|
|
* @param {Object} dataElement
|
|
|
|
* @return {Object}
|
|
|
|
*/
|
|
|
|
ve.dm.MWReferenceNode.static.getInstanceHashObject = function () {
|
|
|
|
return ve.dm.MWReferenceNode.super.static.getHashObject.apply( this, arguments );
|
|
|
|
};
|
|
|
|
|
2017-08-11 23:26:02 +00:00
|
|
|
/**
|
2024-02-28 08:57:24 +00:00
|
|
|
* @override
|
|
|
|
* @see ve.dm.Model
|
2017-08-11 23:26:02 +00:00
|
|
|
*/
|
2017-03-16 21:58:53 +00:00
|
|
|
ve.dm.MWReferenceNode.static.describeChange = function ( key, change ) {
|
|
|
|
if ( key === 'refGroup' ) {
|
2018-06-20 09:01:57 +00:00
|
|
|
if ( !change.from ) {
|
2018-09-28 09:55:45 +00:00
|
|
|
return ve.htmlMsg( 'cite-ve-changedesc-ref-group-to', this.wrapText( 'ins', change.to ) );
|
2018-06-20 09:01:57 +00:00
|
|
|
} else if ( !change.to ) {
|
2018-09-28 09:55:45 +00:00
|
|
|
return ve.htmlMsg( 'cite-ve-changedesc-ref-group-from', this.wrapText( 'del', change.from ) );
|
2018-06-20 09:01:57 +00:00
|
|
|
} else {
|
2018-09-28 09:55:45 +00:00
|
|
|
return ve.htmlMsg( 'cite-ve-changedesc-ref-group-both', this.wrapText( 'del', change.from ), this.wrapText( 'ins', change.to ) );
|
2017-03-16 21:58:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-02-03 21:03:41 +00:00
|
|
|
/* Methods */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Don't allow reference nodes to be edited if we can't find their contents.
|
|
|
|
*
|
2024-02-28 08:57:24 +00:00
|
|
|
* @override
|
|
|
|
* @see ve.dm.Model
|
2016-02-03 21:03:41 +00:00
|
|
|
*/
|
|
|
|
ve.dm.MWReferenceNode.prototype.isEditable = function () {
|
2023-08-21 08:08:23 +00:00
|
|
|
const internalItem = this.getInternalItem();
|
2016-02-03 21:03:41 +00:00
|
|
|
return internalItem && internalItem.getLength() > 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the internal item node associated with this node
|
|
|
|
*
|
|
|
|
* @return {ve.dm.InternalItemNode} Item node
|
|
|
|
*/
|
|
|
|
ve.dm.MWReferenceNode.prototype.getInternalItem = function () {
|
|
|
|
return this.getDocument().getInternalList().getItemNode( this.getAttribute( 'listIndex' ) );
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the index for the reference
|
|
|
|
*
|
|
|
|
* @return {number} Index
|
|
|
|
*/
|
|
|
|
ve.dm.MWReferenceNode.prototype.getIndex = function () {
|
|
|
|
return this.constructor.static.getIndex( this.element, this.getDocument().getInternalList() );
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the group for the reference
|
|
|
|
*
|
|
|
|
* @return {string} Group
|
|
|
|
*/
|
|
|
|
ve.dm.MWReferenceNode.prototype.getGroup = function () {
|
|
|
|
return this.constructor.static.getGroup( this.element );
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the index label for the reference
|
|
|
|
*
|
|
|
|
* @return {string} Reference label
|
|
|
|
*/
|
|
|
|
ve.dm.MWReferenceNode.prototype.getIndexLabel = function () {
|
2024-01-30 11:18:52 +00:00
|
|
|
return this.constructor.static.getIndexLabel(
|
|
|
|
this.element, this.getDocument().getInternalList() );
|
2016-02-03 21:03:41 +00:00
|
|
|
};
|
2024-06-28 10:12:37 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* FIXME: This will be replaced by a simple property.
|
|
|
|
*
|
|
|
|
* @return {string} Footnote number ready for rendering
|
|
|
|
*/
|
|
|
|
ve.dm.MWReferenceNode.prototype.getIndexNumber = function () {
|
|
|
|
return this.constructor.static.findIndexNumber( this.element, this.getDocument().getInternalList() );
|
|
|
|
};
|
2016-02-03 21:03:41 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle the node being attached to the root
|
|
|
|
*/
|
|
|
|
ve.dm.MWReferenceNode.prototype.onRoot = function () {
|
|
|
|
this.addToInternalList();
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle the node being detached from the root
|
2017-04-20 15:15:08 +00:00
|
|
|
*
|
|
|
|
* @param {ve.dm.DocumentNode} oldRoot Old document root
|
2016-02-03 21:03:41 +00:00
|
|
|
*/
|
2017-04-20 15:15:08 +00:00
|
|
|
ve.dm.MWReferenceNode.prototype.onUnroot = function ( oldRoot ) {
|
|
|
|
if ( this.getDocument().getDocumentNode() === oldRoot ) {
|
|
|
|
this.removeFromInternalList();
|
|
|
|
}
|
2016-02-03 21:03:41 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Register the node with the internal list
|
|
|
|
*/
|
|
|
|
ve.dm.MWReferenceNode.prototype.addToInternalList = function () {
|
|
|
|
if ( this.getRoot() === this.getDocument().getDocumentNode() ) {
|
2024-02-21 12:45:59 +00:00
|
|
|
const attributes = this.element.attributes;
|
|
|
|
this.registeredListGroup = attributes.listGroup;
|
|
|
|
this.registeredListKey = attributes.listKey;
|
|
|
|
this.registeredListIndex = attributes.listIndex;
|
2016-02-03 21:03:41 +00:00
|
|
|
this.getDocument().getInternalList().addNode(
|
|
|
|
this.registeredListGroup,
|
|
|
|
this.registeredListKey,
|
|
|
|
this.registeredListIndex,
|
|
|
|
this
|
|
|
|
);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Unregister the node from the internal list
|
|
|
|
*/
|
|
|
|
ve.dm.MWReferenceNode.prototype.removeFromInternalList = function () {
|
2017-04-20 07:35:50 +00:00
|
|
|
if ( !this.registeredListGroup ) {
|
|
|
|
// Don't try to remove if we haven't been added in the first place.
|
|
|
|
return;
|
|
|
|
}
|
2016-02-03 21:03:41 +00:00
|
|
|
this.getDocument().getInternalList().removeNode(
|
|
|
|
this.registeredListGroup,
|
|
|
|
this.registeredListKey,
|
|
|
|
this.registeredListIndex,
|
|
|
|
this
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
ve.dm.MWReferenceNode.prototype.onAttributeChange = function ( key, from, to ) {
|
2023-10-16 14:48:41 +00:00
|
|
|
if ( key === 'placeholder' ) {
|
|
|
|
this.getDocument().getInternalList().markGroupAsChanged( this.registeredListGroup );
|
|
|
|
}
|
2016-02-03 21:03:41 +00:00
|
|
|
if (
|
|
|
|
( key !== 'listGroup' && key !== 'listKey' ) ||
|
|
|
|
( key === 'listGroup' && this.registeredListGroup === to ) ||
|
|
|
|
( key === 'listKey' && this.registeredListKey === to )
|
|
|
|
) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Need the old list keys and indexes, so we register them in addToInternalList
|
|
|
|
// They've already been updated in this.element.attributes before this code runs
|
|
|
|
this.removeFromInternalList();
|
|
|
|
this.addToInternalList();
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Registration */
|
|
|
|
|
|
|
|
ve.dm.modelRegistry.register( ve.dm.MWReferenceNode );
|