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
|
2016-02-03 21:03:41 +00:00
|
|
|
* @license The MIT License (MIT); see LICENSE.txt
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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,
|
|
|
|
templateGenerated = false,
|
|
|
|
isResponsiveDefault = mw.config.get( 'wgCiteResponsiveReferences' );
|
|
|
|
|
|
|
|
if ( ( domElements[ 0 ].getAttribute( 'typeof' ) || '' ).indexOf( 'mw:Extension/references' ) !== -1 ) {
|
|
|
|
refListNode = domElements[ 0 ];
|
|
|
|
} else {
|
|
|
|
refListNode = domElements[ 0 ].querySelectorAll( '[typeof*="mw:Extension/references"]' )[ 0 ];
|
|
|
|
templateGenerated = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
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-02-06 20:14:40 +00:00
|
|
|
isForClipboard = converter.isForClipboard(),
|
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.
|
|
|
|
// This works because the dataElement already as mw, originalMw and originalDomIndex properties.
|
|
|
|
if ( attrs.templateGenerated && !isForClipboard ) {
|
|
|
|
return ve.dm.MWTransclusionNode.static.toDomElements.call( this, dataElement, doc, converter );
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( isForClipboard ) {
|
2018-02-04 20:07:28 +00:00
|
|
|
// Output needs to be read so re-render
|
|
|
|
modelNode = new ve.dm.MWReferencesListNode( dataElement );
|
|
|
|
// Build from original doc's internal list to get all refs (T186407)
|
|
|
|
modelNode.setDocument( converter.originalDocInternalList.getDocument() );
|
|
|
|
viewNode = new ve.ce.MWReferencesListNode( modelNode );
|
|
|
|
viewNode.update();
|
|
|
|
els = [ doc.createElement( 'div' ) ];
|
|
|
|
els[ 0 ].appendChild( viewNode.$reflist[ 0 ] );
|
|
|
|
} else if ( dataElement.originalDomElementsIndex !== undefined ) {
|
2016-02-03 21:03:41 +00:00
|
|
|
// If there's more than 1 element, preserve entire array, not just first element
|
2016-08-17 19:52:28 +00:00
|
|
|
els = ve.copyDomElements( converter.getStore().value( dataElement.originalDomElementsIndex ), 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' ) {
|
|
|
|
if ( change.from ) {
|
|
|
|
if ( change.to ) {
|
|
|
|
return ve.msg( 'cite-ve-changedesc-ref-group-both', change.from, change.to );
|
|
|
|
} else {
|
|
|
|
return ve.msg( 'cite-ve-changedesc-ref-group-from', change.from );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ve.msg( 'cite-ve-changedesc-ref-group-to', change.to );
|
2017-04-10 20:18:26 +00:00
|
|
|
}
|
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
|
|
|
};
|
|
|
|
|
2016-02-03 21:03:41 +00:00
|
|
|
/* Registration */
|
|
|
|
|
|
|
|
ve.dm.modelRegistry.register( ve.dm.MWReferencesListNode );
|