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.
|
|
|
|
*
|
|
|
|
* @class
|
|
|
|
* @extends ve.dm.BranchNode
|
|
|
|
* @mixins ve.dm.FocusableNode
|
|
|
|
*
|
|
|
|
* @constructor
|
|
|
|
* @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' ];
|
|
|
|
|
|
|
|
ve.dm.MWReferencesListNode.static.matchFunction = function ( domElement ) {
|
|
|
|
function isRefList( el ) {
|
|
|
|
return el && el.nodeType === Node.ELEMENT_NODE && ( el.getAttribute( 'typeof' ) || '' ).indexOf( 'mw:Extension/references' ) !== -1;
|
|
|
|
}
|
|
|
|
// If the template generated only a reference list, treat it as a ref list (T52769)
|
|
|
|
return isRefList( domElement ) ||
|
|
|
|
// A div-wrapped reference list
|
|
|
|
( domElement.children.length === 1 && isRefList( domElement.children[ 0 ] ) );
|
|
|
|
};
|
2016-02-03 21:03:41 +00:00
|
|
|
|
|
|
|
ve.dm.MWReferencesListNode.static.preserveHtmlAttributes = false;
|
|
|
|
|
|
|
|
ve.dm.MWReferencesListNode.static.toDataElement = function ( domElements, converter ) {
|
2018-02-06 20:14:40 +00:00
|
|
|
var referencesListData, contentsDiv, contentsData, refListNode,
|
|
|
|
mwDataJSON, mwData, refGroup, responsiveAttr, listGroup,
|
2018-03-05 16:08:36 +00:00
|
|
|
type = domElements[ 0 ].getAttribute( 'typeof' ) || '',
|
|
|
|
templateGenerated = type.indexOf( 'mw:Transclusion' ) !== -1,
|
2018-02-06 20:14:40 +00:00
|
|
|
isResponsiveDefault = mw.config.get( 'wgCiteResponsiveReferences' );
|
|
|
|
|
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 {
|
|
|
|
refListNode = domElements[ 0 ].querySelectorAll( '[typeof*="mw:Extension/references"]' )[ 0 ];
|
|
|
|
}
|
|
|
|
|
|
|
|
mwDataJSON = refListNode.getAttribute( 'data-mw' );
|
|
|
|
mwData = mwDataJSON ? JSON.parse( mwDataJSON ) : {};
|
|
|
|
refGroup = ve.getProp( mwData, 'attrs', 'group' ) || '';
|
|
|
|
responsiveAttr = ve.getProp( mwData, 'attrs', 'responsive' );
|
|
|
|
listGroup = 'mwReference/' + refGroup;
|
2016-02-03 21:03:41 +00:00
|
|
|
|
|
|
|
referencesListData = {
|
|
|
|
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
|
|
|
}
|
|
|
|
};
|
|
|
|
if ( mwData.body && mwData.body.html ) {
|
|
|
|
// Process the nodes in .body.html as if they were this node's children
|
|
|
|
contentsDiv = domElements[ 0 ].ownerDocument.createElement( 'div' );
|
|
|
|
contentsDiv.innerHTML = mwData.body.html;
|
|
|
|
contentsData = converter.getDataFromDomClean( contentsDiv );
|
|
|
|
referencesListData = [ referencesListData ]
|
|
|
|
.concat( contentsData )
|
|
|
|
.concat( [ { type: '/' + this.name } ] );
|
|
|
|
}
|
|
|
|
return referencesListData;
|
|
|
|
};
|
|
|
|
|
|
|
|
ve.dm.MWReferencesListNode.static.toDomElements = function ( data, doc, converter ) {
|
2018-02-04 20:07:28 +00:00
|
|
|
var el, els, mwData, originalMw, contentsHtml, originalHtml, nextIndex, nextElement, modelNode, viewNode,
|
2017-09-19 15:35:27 +00:00
|
|
|
isResponsiveDefault = mw.config.get( 'wgCiteResponsiveReferences' ),
|
2018-09-13 17:54:11 +00:00
|
|
|
isForParser = converter.isForParser(),
|
2016-02-03 21:03:41 +00:00
|
|
|
wrapper = doc.createElement( 'div' ),
|
|
|
|
originalHtmlWrapper = doc.createElement( 'div' ),
|
|
|
|
dataElement = data[ 0 ],
|
2017-09-19 15:35:27 +00:00
|
|
|
attrs = dataElement.attributes,
|
2016-02-03 21:03:41 +00:00
|
|
|
contentsData = data.slice( 1, -1 );
|
|
|
|
|
2018-02-06 20:14:40 +00:00
|
|
|
// If we are sending a template generated ref back to Parsoid, output it as a template.
|
2018-09-13 17:54:11 +00:00
|
|
|
// This works because the dataElement already has mw, originalMw and originalDomIndex properties.
|
|
|
|
if ( attrs.templateGenerated && isForParser ) {
|
2018-02-06 20:14:40 +00:00
|
|
|
return ve.dm.MWTransclusionNode.static.toDomElements.call( this, dataElement, doc, converter );
|
|
|
|
}
|
|
|
|
|
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
|
2018-05-10 16:21:51 +00:00
|
|
|
modelNode = ve.dm.nodeFactory.createFromElement( dataElement );
|
2018-02-04 20:07:28 +00:00
|
|
|
modelNode = new ve.dm.MWReferencesListNode( dataElement );
|
|
|
|
// Build from original doc's internal list to get all refs (T186407)
|
|
|
|
modelNode.setDocument( converter.originalDocInternalList.getDocument() );
|
2018-05-10 16:21:51 +00:00
|
|
|
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 ] );
|
2018-03-03 18:58:33 +00:00
|
|
|
// Destroy the view node so it doesn't try to update the DOM node later (e.g. updateDebounced)
|
|
|
|
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
|
2018-03-06 13:00:35 +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
|
|
|
}
|
|
|
|
|
2017-09-19 15:35:27 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2017-09-19 15:35:27 +00:00
|
|
|
if ( attrs.isResponsive !== isResponsiveDefault ) {
|
|
|
|
ve.setProp( mwData, 'attrs', 'responsive', attrs.isResponsive ? '' : '0' );
|
|
|
|
} else if ( mwData.attrs ) {
|
|
|
|
delete mwData.attrs.responsive;
|
|
|
|
}
|
|
|
|
|
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?
|
|
|
|
nextIndex = converter.documentData.indexOf( data[ data.length - 1 ] ) + 1;
|
|
|
|
while ( ( nextElement = converter.documentData[ nextIndex ] ) ) {
|
|
|
|
if ( nextElement.type[ 0 ] !== '/' ) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
nextIndex++;
|
|
|
|
}
|
|
|
|
if ( nextElement && nextElement.type !== 'internalList' ) {
|
|
|
|
delete mwData.autoGenerated;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-17 19:52:28 +00:00
|
|
|
el = els[ 0 ];
|
2016-02-03 21:03:41 +00:00
|
|
|
el.setAttribute( 'typeof', 'mw:Extension/references' );
|
|
|
|
|
|
|
|
if ( contentsData.length > 2 ) {
|
|
|
|
converter.getDomSubtreeFromData( data.slice( 1, -1 ), wrapper );
|
|
|
|
contentsHtml = wrapper.innerHTML; // Returns '' if wrapper is empty
|
|
|
|
originalHtml = ve.getProp( mwData, 'body', 'html' ) || '';
|
|
|
|
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
|
2017-09-19 15:35:27 +00:00
|
|
|
originalMw = attrs.originalMw;
|
2016-02-03 21:03:41 +00:00
|
|
|
if ( originalMw && ve.compare( mwData, JSON.parse( originalMw ) ) ) {
|
|
|
|
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 ) {
|
|
|
|
return ve.htmlMsg( 'cite-ve-changedesc-reflist-group-to', $( '<ins>' ).text( change.to ) );
|
|
|
|
} else if ( !change.to ) {
|
|
|
|
return ve.htmlMsg( 'cite-ve-changedesc-reflist-group-from', $( '<del>' ).text( change.from ) );
|
|
|
|
} else {
|
|
|
|
return ve.htmlMsg( 'cite-ve-changedesc-reflist-group-both', $( '<del>' ).text( change.from ), $( '<ins>' ).text( 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 );
|