Debian Codegraph @

extensions/VisualEditor/modules/ve-mw/dm/nodes/ve.dm.MWExtensionNode.js

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