mirror of
https://gerrit.wikimedia.org/r/mediawiki/extensions/VisualEditor
synced 2024-11-30 00:55:00 +00:00
74b8807df5
This is done by using the computed property value rather than the literal attribute value when rendering href and src attributes. Helpfully, this provides perfect URL resolution natively in the browser, which means the document's <base> is respected and all that good stuff. For GeneratedContentNodes, we also need to find all DOM elements inside the rendered DOM that have href or src attributes and resolve those. This is done in the new getRenderedDomElements() function, which the existing cleanup steps (remove <link>/<meta>/<style>, clone for correct document) were moved into. In order to make sure that the computed values are always computed correctly, we need to make sure that in cases where HTML strings in data-mw are parsed, they're parsed in the context of the correct document so the correct <base> is applied. We still need to solve this problem for models that actually store and edit an href or src as an attribute. I'll post more about that on bug 48915. Bug: 48915 Change-Id: Iaccb9e3fc05cd151a0f5e632c8d3bd3568735309
120 lines
3.6 KiB
JavaScript
120 lines
3.6 KiB
JavaScript
/*!
|
|
* VisualEditor DataModel MWReferenceListNode class.
|
|
*
|
|
* @copyright 2011-2013 VisualEditor Team and others; see AUTHORS.txt
|
|
* @license The MIT License (MIT); see LICENSE.txt
|
|
*/
|
|
|
|
/**
|
|
* DataModel MediaWiki reference list node.
|
|
*
|
|
* @class
|
|
* @extends ve.dm.BranchNode
|
|
* @constructor
|
|
* @param {number} [length] Length of content data in document; ignored and overridden to 0
|
|
* @param {Object} [element] Reference to element in linear model
|
|
*/
|
|
ve.dm.MWReferenceListNode = function VeDmMWReferenceListNode( length, element ) {
|
|
// Parent constructor
|
|
ve.dm.BranchNode.call( this, 0, element );
|
|
};
|
|
|
|
/* Inheritance */
|
|
|
|
OO.inheritClass( ve.dm.MWReferenceListNode, ve.dm.BranchNode );
|
|
|
|
/* Static members */
|
|
|
|
ve.dm.MWReferenceListNode.static.name = 'mwReferenceList';
|
|
|
|
ve.dm.MWReferenceListNode.static.handlesOwnChildren = true;
|
|
|
|
ve.dm.MWReferenceListNode.static.matchTagNames = null;
|
|
|
|
ve.dm.MWReferenceListNode.static.matchRdfaTypes = [ 'mw:Extension/references' ];
|
|
|
|
ve.dm.MWReferenceListNode.static.storeHtmlAttributes = false;
|
|
|
|
ve.dm.MWReferenceListNode.static.toDataElement = function ( domElements, converter ) {
|
|
var referenceListData, $contents, contentsData,
|
|
mwDataJSON = domElements[0].getAttribute( 'data-mw' ),
|
|
mwData = mwDataJSON ? JSON.parse( mwDataJSON ) : {},
|
|
refGroup = mwData.attrs && mwData.attrs.group || '',
|
|
listGroup = 'mwReference/' + refGroup;
|
|
|
|
referenceListData = {
|
|
'type': this.name,
|
|
'attributes': {
|
|
'mw': mwData,
|
|
'originalMw': mwDataJSON,
|
|
'domElements': ve.copy( domElements ),
|
|
'refGroup': refGroup,
|
|
'listGroup': listGroup
|
|
}
|
|
};
|
|
if ( mwData.body && mwData.body.html ) {
|
|
$contents = $( '<div>', domElements[0].ownerDocument ).append( mwData.body.html );
|
|
contentsData = converter.getDataFromDomRecursionClean( $contents[0] );
|
|
return [ referenceListData ].
|
|
concat( contentsData ).
|
|
concat( [ { 'type': '/' + this.name } ] );
|
|
} else {
|
|
return referenceListData;
|
|
}
|
|
|
|
};
|
|
|
|
ve.dm.MWReferenceListNode.static.toDomElements = function ( data, doc, converter ) {
|
|
var el, els, mwData, originalMw, wrapper, contentsHtml, originalHtml,
|
|
dataElement = data[0],
|
|
attribs = dataElement.attributes,
|
|
contentsData = data.slice( 1, -1 );
|
|
|
|
if ( attribs.domElements ) {
|
|
// If there's more than 1 element, preserve entire array, not just first element
|
|
els = ve.copyDomElements( attribs.domElements, doc );
|
|
el = els[0];
|
|
} else {
|
|
el = doc.createElement( 'div' );
|
|
els = [ el ];
|
|
}
|
|
|
|
mwData = attribs.mw ? ve.copy( attribs.mw ) : {};
|
|
|
|
mwData.name = 'references';
|
|
|
|
if ( attribs.refGroup ) {
|
|
ve.setProp( mwData, 'attrs', 'group', attribs.refGroup );
|
|
} else if ( mwData.attrs ) {
|
|
delete mwData.attrs.refGroup;
|
|
}
|
|
|
|
el.setAttribute( 'typeof', 'mw:Extension/references' );
|
|
|
|
if ( contentsData.length > 2 ) {
|
|
wrapper = doc.createElement( 'div' );
|
|
converter.getDomSubtreeFromData( data.slice( 1, -1 ), wrapper );
|
|
contentsHtml = $( wrapper ).html(); // Returns '' if wrapper is empty
|
|
originalHtml = ve.getProp( mwData, 'body', 'html' ) || '';
|
|
// Only set body.html if contentsHtml and originalHtml are actually different
|
|
if ( !$( '<div>' ).html( originalHtml ).get( 0 ).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
|
|
originalMw = attribs.originalMw;
|
|
if ( originalMw && ve.compare( mwData, JSON.parse( originalMw ) ) ) {
|
|
el.setAttribute( 'data-mw', originalMw );
|
|
} else {
|
|
el.setAttribute( 'data-mw', JSON.stringify( mwData ) );
|
|
}
|
|
|
|
return els;
|
|
};
|
|
|
|
/* Registration */
|
|
|
|
ve.dm.modelRegistry.register( ve.dm.MWReferenceListNode );
|