2023-08-21 08:08:23 +00:00
|
|
|
'use strict';
|
|
|
|
|
2016-02-03 21:03:41 +00:00
|
|
|
/*!
|
|
|
|
* VisualEditor DataModel MWReferencesListNode 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 references list node.
|
|
|
|
*
|
|
|
|
* @constructor
|
2024-02-28 08:57:24 +00:00
|
|
|
* @extends ve.dm.BranchNode
|
|
|
|
* @mixes ve.dm.FocusableNode
|
2016-02-03 21:03:41 +00:00
|
|
|
* @param {Object} [element] Reference to element in linear model
|
|
|
|
* @param {ve.dm.Node[]} [children]
|
|
|
|
*/
|
|
|
|
ve.dm.MWReferencesListNode = function VeDmMWReferencesListNode() {
|
|
|
|
// Parent constructor
|
2016-11-02 12:43:14 +00:00
|
|
|
ve.dm.MWReferencesListNode.super.apply( this, arguments );
|
2016-02-03 21:03:41 +00:00
|
|
|
|
|
|
|
// Mixin constructors
|
|
|
|
ve.dm.FocusableNode.call( this );
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Inheritance */
|
|
|
|
|
|
|
|
OO.inheritClass( ve.dm.MWReferencesListNode, ve.dm.BranchNode );
|
|
|
|
|
|
|
|
OO.mixinClass( ve.dm.MWReferencesListNode, ve.dm.FocusableNode );
|
|
|
|
|
2018-02-06 20:14:40 +00:00
|
|
|
/* Methods */
|
|
|
|
|
|
|
|
ve.dm.MWReferencesListNode.prototype.isEditable = function () {
|
|
|
|
return !this.getAttribute( 'templateGenerated' );
|
|
|
|
};
|
|
|
|
|
2016-02-03 21:03:41 +00:00
|
|
|
/* Static members */
|
|
|
|
|
|
|
|
ve.dm.MWReferencesListNode.static.name = 'mwReferencesList';
|
|
|
|
|
|
|
|
ve.dm.MWReferencesListNode.static.handlesOwnChildren = true;
|
|
|
|
|
|
|
|
ve.dm.MWReferencesListNode.static.ignoreChildren = true;
|
|
|
|
|
|
|
|
ve.dm.MWReferencesListNode.static.matchTagNames = null;
|
|
|
|
|
2018-02-06 20:14:40 +00:00
|
|
|
ve.dm.MWReferencesListNode.static.matchRdfaTypes = [ 'mw:Extension/references', 'mw:Transclusion' ];
|
|
|
|
|
2022-06-11 00:24:02 +00:00
|
|
|
// Allow TemplateStyles in the matching element
|
|
|
|
ve.dm.MWReferencesListNode.static.allowedRdfaTypes = [ 'mw:Extension/templatestyles' ];
|
|
|
|
|
|
|
|
// e.g. with a {{reflist}} with TemplateStyles
|
|
|
|
ve.dm.MWReferencesListNode.static.enableAboutGrouping = true;
|
|
|
|
|
2020-12-13 22:08:14 +00:00
|
|
|
// This node has the same specificity as ve.dm.MWTranslcusionNode and only matches
|
|
|
|
// ahead of it because it is registered later (via a dependency in ResourceLoader)
|
|
|
|
// TODO: Make this less fragile.
|
2018-02-06 20:14:40 +00:00
|
|
|
ve.dm.MWReferencesListNode.static.matchFunction = function ( domElement ) {
|
2022-06-11 00:24:02 +00:00
|
|
|
function hasTypeof( el, type ) {
|
|
|
|
return ( el.getAttribute( 'typeof' ) || '' ).indexOf( type ) !== -1;
|
|
|
|
}
|
2018-02-06 20:14:40 +00:00
|
|
|
function isRefList( el ) {
|
2022-06-11 00:24:02 +00:00
|
|
|
return el && el.nodeType === Node.ELEMENT_NODE && hasTypeof( el, 'mw:Extension/references' );
|
2018-02-06 20:14:40 +00:00
|
|
|
}
|
|
|
|
// If the template generated only a reference list, treat it as a ref list (T52769)
|
|
|
|
return isRefList( domElement ) ||
|
|
|
|
// A div-wrapped reference list
|
2022-06-11 00:24:02 +00:00
|
|
|
( domElement.children.length === 1 && isRefList( domElement.children[ 0 ] ) ) ||
|
|
|
|
// TemplateStyles, about-grouped to a div-wrapped reference list
|
|
|
|
(
|
|
|
|
hasTypeof( domElement, 'mw:Extension/templatestyles' ) &&
|
|
|
|
domElement.hasAttribute( 'about' ) &&
|
|
|
|
domElement.nextElementSibling &&
|
|
|
|
domElement.nextElementSibling.getAttribute( 'about' ) === domElement.getAttribute( 'about' ) &&
|
|
|
|
// A div-wrapped reference list
|
2024-01-30 11:18:52 +00:00
|
|
|
domElement.nextElementSibling.children.length === 1 &&
|
|
|
|
isRefList( domElement.nextElementSibling.children[ 0 ] )
|
|
|
|
// TODO: We should probably check there aren't subsequent elements. This
|
|
|
|
// and the above checks would be easier if the matchFunction was passed
|
|
|
|
// all the elements in the about group.
|
2022-06-11 00:24:02 +00:00
|
|
|
);
|
2018-02-06 20:14:40 +00:00
|
|
|
};
|
2016-02-03 21:03:41 +00:00
|
|
|
|
|
|
|
ve.dm.MWReferencesListNode.static.preserveHtmlAttributes = false;
|
|
|
|
|
|
|
|
ve.dm.MWReferencesListNode.static.toDataElement = function ( domElements, converter ) {
|
2023-08-21 08:08:23 +00:00
|
|
|
const type = domElements[ 0 ].getAttribute( 'typeof' ) || '';
|
2018-02-06 20:14:40 +00:00
|
|
|
|
2023-08-21 08:08:23 +00:00
|
|
|
let refListNode;
|
2018-03-05 16:08:36 +00:00
|
|
|
// We may have matched a mw:Transclusion wrapping a reference list, so pull out the refListNode
|
|
|
|
if ( type.indexOf( 'mw:Extension/references' ) !== -1 ) {
|
2018-02-06 20:14:40 +00:00
|
|
|
refListNode = domElements[ 0 ];
|
|
|
|
} else {
|
2022-06-11 00:24:02 +00:00
|
|
|
refListNode = domElements[ 0 ].querySelector( '[typeof*="mw:Extension/references"]' ) ||
|
|
|
|
// In the TemplateStyles case, the ref list is in the second element
|
|
|
|
domElements[ 1 ].querySelector( '[typeof*="mw:Extension/references"]' );
|
2018-02-06 20:14:40 +00:00
|
|
|
}
|
|
|
|
|
2023-08-21 08:08:23 +00:00
|
|
|
const mwDataJSON = refListNode.getAttribute( 'data-mw' );
|
|
|
|
const mwData = mwDataJSON ? JSON.parse( mwDataJSON ) : {};
|
|
|
|
const refGroup = ve.getProp( mwData, 'attrs', 'group' ) || '';
|
|
|
|
const responsiveAttr = ve.getProp( mwData, 'attrs', 'responsive' );
|
|
|
|
const listGroup = 'mwReference/' + refGroup;
|
|
|
|
const templateGenerated = type.indexOf( 'mw:Transclusion' ) !== -1;
|
|
|
|
const isResponsiveDefault = mw.config.get( 'wgCiteResponsiveReferences' );
|
2016-02-03 21:03:41 +00:00
|
|
|
|
2023-08-21 08:08:23 +00:00
|
|
|
let referencesListData = {
|
2016-02-03 21:03:41 +00:00
|
|
|
type: this.name,
|
|
|
|
attributes: {
|
|
|
|
mw: mwData,
|
|
|
|
originalMw: mwDataJSON,
|
|
|
|
refGroup: refGroup,
|
2017-09-19 15:35:27 +00:00
|
|
|
listGroup: listGroup,
|
2018-02-06 20:14:40 +00:00
|
|
|
isResponsive: responsiveAttr !== undefined ? responsiveAttr !== '0' : isResponsiveDefault,
|
|
|
|
templateGenerated: templateGenerated
|
2016-02-03 21:03:41 +00:00
|
|
|
}
|
|
|
|
};
|
2020-01-04 14:47:06 +00:00
|
|
|
if ( mwData.body && mwData.body.html && !templateGenerated ) {
|
2016-02-03 21:03:41 +00:00
|
|
|
// Process the nodes in .body.html as if they were this node's children
|
2020-01-04 14:47:06 +00:00
|
|
|
// Don't process template-generated reflists, that mangles the content (T209493)
|
2023-08-21 08:08:23 +00:00
|
|
|
const contentsDiv = domElements[ 0 ].ownerDocument.createElement( 'div' );
|
2016-02-03 21:03:41 +00:00
|
|
|
contentsDiv.innerHTML = mwData.body.html;
|
2023-08-21 08:08:23 +00:00
|
|
|
const contentsData = converter.getDataFromDomClean( contentsDiv );
|
2016-02-03 21:03:41 +00:00
|
|
|
referencesListData = [ referencesListData ]
|
|
|
|
.concat( contentsData )
|
|
|
|
.concat( [ { type: '/' + this.name } ] );
|
|
|
|
}
|
|
|
|
return referencesListData;
|
|
|
|
};
|
|
|
|
|
|
|
|
ve.dm.MWReferencesListNode.static.toDomElements = function ( data, doc, converter ) {
|
2023-08-21 08:08:23 +00:00
|
|
|
const isForParser = converter.isForParser();
|
|
|
|
const dataElement = data[ 0 ];
|
|
|
|
const attrs = dataElement.attributes;
|
2016-02-03 21:03:41 +00:00
|
|
|
|
2024-01-30 11:18:52 +00:00
|
|
|
// If we are sending a template generated ref back to Parsoid, output it as a
|
|
|
|
// template. This works because the dataElement already has mw, originalMw
|
|
|
|
// and originalDomIndex properties.
|
2018-09-13 17:54:11 +00:00
|
|
|
if ( attrs.templateGenerated && isForParser ) {
|
2024-01-30 11:18:52 +00:00
|
|
|
return ve.dm.MWTransclusionNode.static
|
|
|
|
.toDomElements.call( this, dataElement, doc, converter );
|
2018-02-06 20:14:40 +00:00
|
|
|
}
|
|
|
|
|
2023-08-21 08:08:23 +00:00
|
|
|
let els;
|
2018-09-13 17:54:11 +00:00
|
|
|
if ( !isForParser ) {
|
2018-02-04 20:07:28 +00:00
|
|
|
// Output needs to be read so re-render
|
2023-08-21 08:08:23 +00:00
|
|
|
const modelNode = ve.dm.nodeFactory.createFromElement( dataElement );
|
2018-02-04 20:07:28 +00:00
|
|
|
// Build from original doc's internal list to get all refs (T186407)
|
|
|
|
modelNode.setDocument( converter.originalDocInternalList.getDocument() );
|
2023-08-21 08:08:23 +00:00
|
|
|
const viewNode = ve.ce.nodeFactory.createFromModel( modelNode );
|
2018-03-24 16:42:13 +00:00
|
|
|
viewNode.modified = true;
|
2018-02-04 20:07:28 +00:00
|
|
|
viewNode.update();
|
|
|
|
els = [ doc.createElement( 'div' ) ];
|
|
|
|
els[ 0 ].appendChild( viewNode.$reflist[ 0 ] );
|
2024-01-30 11:18:52 +00:00
|
|
|
// Destroy the view node so it doesn't try to update the DOM node later
|
|
|
|
// (e.g. updateDebounced)
|
2018-03-03 18:58:33 +00:00
|
|
|
viewNode.destroy();
|
2018-03-06 13:00:35 +00:00
|
|
|
} else if ( dataElement.originalDomElementsHash !== undefined ) {
|
2016-02-03 21:03:41 +00:00
|
|
|
// If there's more than 1 element, preserve entire array, not just first element
|
2024-01-30 11:18:52 +00:00
|
|
|
els = ve.copyDomElements(
|
|
|
|
converter.getStore().value( dataElement.originalDomElementsHash ), doc );
|
2016-02-03 21:03:41 +00:00
|
|
|
} else {
|
2016-08-17 19:52:28 +00:00
|
|
|
els = [ doc.createElement( 'div' ) ];
|
2016-02-03 21:03:41 +00:00
|
|
|
}
|
|
|
|
|
2023-08-21 08:08:23 +00:00
|
|
|
const mwData = attrs.mw ? ve.copy( attrs.mw ) : {};
|
2016-02-03 21:03:41 +00:00
|
|
|
|
|
|
|
mwData.name = 'references';
|
|
|
|
|
2017-09-19 15:35:27 +00:00
|
|
|
if ( attrs.refGroup ) {
|
|
|
|
ve.setProp( mwData, 'attrs', 'group', attrs.refGroup );
|
2016-02-03 21:03:41 +00:00
|
|
|
} else if ( mwData.attrs ) {
|
|
|
|
delete mwData.attrs.refGroup;
|
|
|
|
}
|
|
|
|
|
2023-08-21 08:08:23 +00:00
|
|
|
const originalMw = attrs.originalMw;
|
|
|
|
const originalMwData = originalMw && JSON.parse( originalMw );
|
|
|
|
const originalResponsiveAttr = ve.getProp( originalMwData, 'attrs', 'responsive' );
|
|
|
|
const isResponsiveDefault = mw.config.get( 'wgCiteResponsiveReferences' );
|
2021-11-03 12:28:17 +00:00
|
|
|
|
2019-03-28 16:40:39 +00:00
|
|
|
if ( !(
|
|
|
|
// The original "responsive" attribute hasn't had its meaning changed
|
|
|
|
originalResponsiveAttr !== undefined && ( originalResponsiveAttr !== '0' ) === attrs.isResponsive
|
|
|
|
) ) {
|
|
|
|
if ( attrs.isResponsive !== isResponsiveDefault ) {
|
|
|
|
ve.setProp( mwData, 'attrs', 'responsive', attrs.isResponsive ? '' : '0' );
|
|
|
|
} else if ( mwData.attrs ) {
|
|
|
|
delete mwData.attrs.responsive;
|
|
|
|
}
|
2017-09-19 15:35:27 +00:00
|
|
|
}
|
|
|
|
|
2017-10-17 19:21:36 +00:00
|
|
|
if ( mwData.autoGenerated ) {
|
|
|
|
// This was an autogenerated reflist. We need to check whether changes
|
|
|
|
// have been made which make that no longer true. The reflist dialog
|
|
|
|
// handles unsetting this if changes to the properties have been made.
|
|
|
|
// Here we want to work out if it has been moved away from the end of
|
|
|
|
// the document.
|
|
|
|
// TODO: it would be better to do this without needing to fish through
|
|
|
|
// the converter's linear data. Use the DM tree instead?
|
2023-08-21 08:08:23 +00:00
|
|
|
let nextIndex = converter.documentData.indexOf( data[ data.length - 1 ] ) + 1;
|
|
|
|
let nextElement;
|
2017-10-17 19:21:36 +00:00
|
|
|
while ( ( nextElement = converter.documentData[ nextIndex ] ) ) {
|
|
|
|
if ( nextElement.type[ 0 ] !== '/' ) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
nextIndex++;
|
|
|
|
}
|
|
|
|
if ( nextElement && nextElement.type !== 'internalList' ) {
|
|
|
|
delete mwData.autoGenerated;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-21 08:08:23 +00:00
|
|
|
const el = els[ 0 ];
|
2016-02-03 21:03:41 +00:00
|
|
|
el.setAttribute( 'typeof', 'mw:Extension/references' );
|
|
|
|
|
2023-08-21 08:08:23 +00:00
|
|
|
const contentsData = data.slice( 1, -1 );
|
2016-02-03 21:03:41 +00:00
|
|
|
if ( contentsData.length > 2 ) {
|
2023-08-21 08:08:23 +00:00
|
|
|
const wrapper = doc.createElement( 'div' );
|
2016-02-03 21:03:41 +00:00
|
|
|
converter.getDomSubtreeFromData( data.slice( 1, -1 ), wrapper );
|
2023-08-21 08:08:23 +00:00
|
|
|
const contentsHtml = wrapper.innerHTML; // Returns '' if wrapper is empty
|
|
|
|
const originalHtml = ve.getProp( mwData, 'body', 'html' ) || '';
|
|
|
|
const originalHtmlWrapper = doc.createElement( 'div' );
|
2016-02-03 21:03:41 +00:00
|
|
|
originalHtmlWrapper.innerHTML = originalHtml;
|
|
|
|
// Only set body.html if contentsHtml and originalHtml are actually different
|
|
|
|
if ( !originalHtmlWrapper.isEqualNode( wrapper ) ) {
|
|
|
|
ve.setProp( mwData, 'body', 'html', contentsHtml );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If mwData and originalMw are the same, use originalMw to prevent reserialization.
|
|
|
|
// Reserialization has the potential to reorder keys and so change the DOM unnecessarily
|
2019-03-28 16:40:39 +00:00
|
|
|
if ( originalMw && ve.compare( mwData, originalMwData ) ) {
|
2016-02-03 21:03:41 +00:00
|
|
|
el.setAttribute( 'data-mw', originalMw );
|
|
|
|
} else {
|
|
|
|
el.setAttribute( 'data-mw', JSON.stringify( mwData ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
return els;
|
|
|
|
};
|
|
|
|
|
2017-03-16 21:58:53 +00:00
|
|
|
ve.dm.MWReferencesListNode.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-reflist-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-reflist-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-reflist-group-both', this.wrapText( 'del', change.from ), this.wrapText( 'ins', change.to ) );
|
2017-03-16 21:58:53 +00:00
|
|
|
}
|
2017-04-10 20:18:26 +00:00
|
|
|
}
|
2018-02-23 00:01:44 +00:00
|
|
|
|
|
|
|
if ( key === 'isResponsive' ) {
|
|
|
|
if ( change.from ) {
|
|
|
|
return ve.msg( 'cite-ve-changedesc-reflist-responsive-unset' );
|
|
|
|
}
|
|
|
|
return ve.msg( 'cite-ve-changedesc-reflist-responsive-set' );
|
|
|
|
}
|
|
|
|
|
2017-06-12 14:03:00 +00:00
|
|
|
if ( key === 'originalMw' ) {
|
|
|
|
return null;
|
|
|
|
}
|
2017-03-16 21:58:53 +00:00
|
|
|
|
|
|
|
return null;
|
2017-04-10 20:18:26 +00:00
|
|
|
};
|
|
|
|
|
2018-02-24 16:48:24 +00:00
|
|
|
ve.dm.MWReferencesListNode.static.getHashObject = function ( dataElement ) {
|
|
|
|
return {
|
|
|
|
type: dataElement.type,
|
|
|
|
attributes: {
|
|
|
|
refGroup: dataElement.attributes.refGroup,
|
|
|
|
listGroup: dataElement.attributes.listGroup,
|
|
|
|
isResponsive: dataElement.attributes.isResponsive,
|
|
|
|
templateGenerated: dataElement.attributes.templateGenerated
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2016-02-03 21:03:41 +00:00
|
|
|
/* Registration */
|
|
|
|
|
|
|
|
ve.dm.modelRegistry.register( ve.dm.MWReferencesListNode );
|