1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
|
/*!
* VisualEditor DataModel MWExtensionNode class.
*
* @copyright 2011-2020 VisualEditor Team and others; see AUTHORS.txt
* @license The MIT License (MIT); see LICENSE.txt
*/
/**
* DataModel MediaWiki extension node.
*
* @class
* @abstract
* @extends ve.dm.LeafNode
* @mixins ve.dm.FocusableNode
* @mixins ve.dm.GeneratedContentNode
*
* @constructor
*/
ve.dm.MWExtensionNode = function VeDmMWExtensionNode() {
// Parent constructor
ve.dm.MWExtensionNode.super.apply( this, arguments );
// Mixin constructors
ve.dm.GeneratedContentNode.call( this );
ve.dm.FocusableNode.call( this );
};
/* Inheritance */
OO.inheritClass( ve.dm.MWExtensionNode, ve.dm.LeafNode );
OO.mixinClass( ve.dm.MWExtensionNode, ve.dm.FocusableNode );
OO.mixinClass( ve.dm.MWExtensionNode, ve.dm.GeneratedContentNode );
/* Static members */
ve.dm.MWExtensionNode.static.enableAboutGrouping = true;
ve.dm.MWExtensionNode.static.matchTagNames = null;
ve.dm.MWExtensionNode.static.childNodeTypes = [];
/**
* HTML tag name.
*
* @static
* @property {string}
* @inheritable
*/
ve.dm.MWExtensionNode.static.tagName = null;
/**
* Name of the MediaWiki parser extension tag. (Not related to the name of the MediaWiki extension.)
*
* @static
* @property {string}
* @inheritable
*/
ve.dm.MWExtensionNode.static.extensionName = null;
ve.dm.MWExtensionNode.static.getMatchRdfaTypes = function () {
return [ 'mw:Extension/' + this.extensionName ];
};
/**
* @inheritdoc
* @param {Node[]} domElements
* @param {ve.dm.Converter} converter
* @param {string} [type] Type to give dataElement, defaults to static.name
*/
ve.dm.MWExtensionNode.static.toDataElement = function ( domElements, converter, type ) {
var mwDataJSON = domElements[ 0 ].getAttribute( 'data-mw' ),
mwData = mwDataJSON ? JSON.parse( mwDataJSON ) : {};
var dataElement = {
type: type || this.name,
attributes: {
mw: mwData,
originalMw: mwDataJSON
}
};
this.storeGeneratedContents( dataElement, domElements, converter.getStore() );
// Sub-classes should not modify dataElement beyond this point as it will invalidate the cache
return dataElement;
};
/**
* @inheritdoc ve.dm.Node
*/
ve.dm.MWExtensionNode.static.cloneElement = function () {
// Parent method
var clone = ve.dm.MWExtensionNode.super.static.cloneElement.apply( this, arguments );
delete clone.attributes.originalMw;
return clone;
};
ve.dm.MWExtensionNode.static.toDomElements = function ( dataElement, doc, converter ) {
var originalMw = dataElement.attributes.originalMw;
var els;
// If the transclusion is unchanged just send back the
// original DOM elements so selser can skip over it
if (
dataElement.originalDomElementsHash &&
originalMw && ve.compare( dataElement.attributes.mw, JSON.parse( originalMw ) )
) {
// originalDomElements is also used for CE rendering so return a copy
els = ve.copyDomElements( converter.getStore().value( dataElement.originalDomElementsHash ), doc );
} else {
var store = converter.getStore();
var value;
if (
converter.doesModeNeedRendering() &&
// Use getHashObjectForRendering to get the rendering from the store
( value = store.value( store.hashOfValue( null, OO.getHash( [ this.getHashObjectForRendering( dataElement ), undefined ] ) ) ) )
) {
// For the clipboard use the current DOM contents so the user has something
// meaningful to paste into external applications
els = ve.copyDomElements( value, doc );
} else {
var el = doc.createElement( this.tagName );
el.setAttribute( 'typeof', 'mw:Extension/' + this.getExtensionName( dataElement ) );
el.setAttribute( 'data-mw', JSON.stringify( dataElement.attributes.mw ) );
els = [ el ];
}
}
return els;
};
ve.dm.MWExtensionNode.static.getHashObject = function ( dataElement ) {
return {
type: dataElement.type,
mw: ve.copy( dataElement.attributes.mw )
};
};
/**
* Get name of the MediaWiki parser extension tag.
*
* Static version for toDomElements
*
* @static
* @param {Object} dataElement Data element
* @return {string} Extension name
*/
ve.dm.MWExtensionNode.static.getExtensionName = function () {
return this.extensionName;
};
ve.dm.MWExtensionNode.static.describeChanges = function ( attributeChanges, attributes, element ) {
var descriptions = [],
fromBody = attributeChanges.mw.from.body,
toBody = attributeChanges.mw.to.body;
if ( attributeChanges.mw ) {
// HACK: Try to generate an '<Extension> has changed' message using the associated tool's title
var tools = ve.ui.toolFactory.getRelatedItems( [ ve.dm.nodeFactory.createFromElement( element ) ] );
if ( tools.length ) {
descriptions.push( ve.msg( 'visualeditor-changedesc-unknown',
OO.ui.resolveMsg( ve.ui.toolFactory.lookup( tools[ 0 ].name ).static.title )
) );
}
// Compare body - default behaviour in #describeChange does nothing
if ( !ve.compare( fromBody, toBody ) ) {
var change = this.describeChange( 'body', {
from: fromBody && fromBody.extsrc,
to: toBody && toBody.extsrc
} );
if ( change ) {
descriptions.push( change );
}
}
// Append attribute changes
// Parent method
Array.prototype.push.apply( descriptions, ve.dm.MWExtensionNode.super.static.describeChanges.call(
this,
ve.ui.DiffElement.static.compareAttributes( attributeChanges.mw.from.attrs || {}, attributeChanges.mw.to.attrs || {} ),
attributes
) );
return descriptions;
}
// 'mw' should be the only attribute that changes...
return [];
};
ve.dm.MWExtensionNode.static.describeChange = function ( key ) {
if ( key === 'body' ) {
// TODO: Produce a diff of the body, suitable to display in the sidebar.
return null;
}
// Parent method
return ve.dm.MWExtensionNode.super.static.describeChange.apply( this, arguments );
};
/* Methods */
/**
* Get name of the MediaWiki parser extension tag.
*
* @return {string} Extension name
*/
ve.dm.MWExtensionNode.prototype.getExtensionName = function () {
return this.constructor.static.getExtensionName( this.element );
};
|