Debian Codegraph @

extensions/VisualEditor/modules/ve-mw/ce/nodes/ve.ce.MWTransclusionNode.js

raw symbols SCIP
1 /*!
2 * VisualEditor ContentEditable MWTransclusionNode class.
3 *
4 * @copyright See AUTHORS.txt
5 * @license The MIT License (MIT); see LICENSE.txt
6 */
7
8 /**
9 * ContentEditable MediaWiki transclusion node.
10 *
11 * @class
12 * @abstract
13 * @extends ve.ce.LeafNode
14 * @mixes ve.ce.GeneratedContentNode
15 * @mixes ve.ce.FocusableNode
16 *
17 * @constructor
18 * @param {ve.dm.MWTransclusionNode} model Model to observe
19 * @param {Object} [config] Configuration options
20 */
21 ve.ce.MWTransclusionNode = function VeCeMWTransclusionNode( model, config ) {
22 // Parent constructor
23 ve.ce.MWTransclusionNode.super.call( this, model, config );
24
25 // Mixin constructors
26 ve.ce.GeneratedContentNode.call( this );
27 ve.ce.FocusableNode.call( this );
28 };
29
30 /* Inheritance */
31
32 OO.inheritClass( ve.ce.MWTransclusionNode, ve.ce.LeafNode );
33
34 OO.mixinClass( ve.ce.MWTransclusionNode, ve.ce.GeneratedContentNode );
35 OO.mixinClass( ve.ce.MWTransclusionNode, ve.ce.FocusableNode );
36
37 /* Static Properties */
38
39 ve.ce.MWTransclusionNode.static.name = 'mwTransclusion';
40
41 ve.ce.MWTransclusionNode.static.primaryCommandName = 'transclusion';
42
43 ve.ce.MWTransclusionNode.static.iconWhenInvisible = 'puzzle';
44
45 /* Static Methods */
46
47 /**
48 * Get a plain text description of the template parts in a transclusion node, excluding raw wikitext
49 * snippets.
50 *
51 * @static
52 * @param {ve.dm.MWTransclusionNode} model
53 * @return {string} Comma-separated list of template names
54 */
55 ve.ce.MWTransclusionNode.static.getDescription = function ( model ) {
56 return model.getPartsList()
57 .map( ( part ) => {
58 if ( part.templatePage ) {
59 return mw.Title.newFromText( part.templatePage )
60 .getRelativeText( mw.config.get( 'wgNamespaceIds' ).template );
61 }
62 // Not actually a template, but e.g. a parser function
63 return part.template || '';
64 } )
65 .filter( ( desc ) => desc )
66 .join( ve.msg( 'comma-separator' ) );
67 };
68
69 /**
70 * Get a formatted description of the template parts in a transclusion node, excluding raw wikitext
71 * snippets.
72 *
73 * Like #getDescription, but parts generated from templates are linked to
74 * those templates
75 *
76 * @static
77 * @param {ve.dm.MWTransclusionNode} model
78 * @return {HTMLElement} DOM node with comma-separated list of template names
79 */
80 ve.ce.MWTransclusionNode.static.getDescriptionDom = function ( model ) {
81 const nodes = model.getPartsList()
82 .map( ( part ) => {
83 if ( part.templatePage ) {
84 const title = mw.Title.newFromText( part.templatePage );
85 const link = document.createElement( 'a' );
86 link.textContent = title.getRelativeText( mw.config.get( 'wgNamespaceIds' ).template );
87 link.setAttribute( 'href', title.getUrl() );
88 return link;
89 }
90 // Not actually a template, but e.g. a parser function
91 return part.template ? document.createTextNode( part.template ) : null;
92 } )
93 .filter( ( desc ) => desc );
94 const span = document.createElement( 'span' );
95 nodes.forEach( ( node, i ) => {
96 if ( i ) {
97 span.appendChild( document.createTextNode( ve.msg( 'comma-separator' ) ) );
98 }
99 span.appendChild( node );
100 } );
101 ve.targetLinksToNewWindow( span );
102 return span;
103 };
104
105 /**
106 * Filter rendering to remove auto-generated content and wrappers
107 *
108 * @static
109 * @param {Node[]} contentNodes Rendered nodes
110 * @return {Node[]} Filtered rendered nodes
111 */
112 ve.ce.MWTransclusionNode.static.filterRendering = function ( contentNodes ) {
113 if ( !contentNodes.length ) {
114 return [];
115 }
116
117 const whitespaceRegex = new RegExp( '^[' + ve.dm.Converter.static.whitespaceList + ']+$' );
118
119 // Filter out auto-generated items, e.g. reference lists
120 contentNodes = contentNodes.filter( ( node ) => {
121 const dataMw = node &&
122 node.nodeType === Node.ELEMENT_NODE &&
123 node.hasAttribute( 'data-mw' ) &&
124 JSON.parse( node.getAttribute( 'data-mw' ) );
125
126 return !dataMw || !dataMw.autoGenerated;
127 } );
128
129 contentNodes.forEach( ( node ) => {
130 if ( node.nodeType === Node.ELEMENT_NODE ) {
131 mw.libs.ve.stripParsoidFallbackIds( node );
132 }
133 } );
134
135 function isWhitespaceNode( node ) {
136 return node && node.nodeType === Node.TEXT_NODE && whitespaceRegex.test( node.data );
137 }
138
139 while ( isWhitespaceNode( contentNodes[ 0 ] ) ) {
140 contentNodes.shift();
141 }
142 while ( isWhitespaceNode( contentNodes[ contentNodes.length - 1 ] ) ) {
143 contentNodes.pop();
144 }
145 // HACK: if $content consists of a single paragraph, unwrap it.
146 // We have to do this because the parser wraps everything in <p>s, and inline templates
147 // will render strangely when wrapped in <p>s.
148 if ( contentNodes.length === 1 && contentNodes[ 0 ].nodeName.toLowerCase() === 'p' ) {
149 contentNodes = Array.prototype.slice.call( contentNodes[ 0 ].childNodes );
150 }
151 return contentNodes;
152 };
153
154 /* Methods */
155
156 /** @inheritDoc */
157 ve.ce.MWTransclusionNode.prototype.executeCommand = function () {
158 const contextItems = this.focusableSurface.getSurface().getContext().items;
159 const contextClicked = contextItems.some( ( contextItem ) => {
160 if ( contextItem instanceof ve.ui.MWTransclusionContextItem ) {
161 // Utilize the context item when it's there instead of triggering the command manually.
162 // Required to make the context item show the "Loading…" message (see T297773).
163 contextItem.onEditButtonClick();
164 return true;
165 }
166 return false;
167 } );
168
169 if ( contextClicked ) {
170 return;
171 }
172
173 // Parent method
174 ve.ce.FocusableNode.prototype.executeCommand.apply( this, arguments );
175 };
176
177 /**
178 * @inheritdoc
179 */
180 ve.ce.MWTransclusionNode.prototype.generateContents = function ( config ) {
181 const deferred = ve.createDeferred();
182 const xhr = ve.init.target.parseWikitextFragment(
183 ( config && config.wikitext ) || this.model.getWikitext(),
184 true,
185 this.getModel().getDocument()
186 )
187 .done( this.onParseSuccess.bind( this, deferred ) )
188 .fail( this.onParseError.bind( this, deferred ) );
189
190 return deferred.promise( { abort: xhr.abort } );
191 };
192
193 /**
194 * Handle a successful response from the parser for the wikitext fragment.
195 *
196 * @param {jQuery.Deferred} deferred The Deferred object created by #generateContents
197 * @param {Object} response Response data
198 */
199 ve.ce.MWTransclusionNode.prototype.onParseSuccess = function ( deferred, response ) {
200 if ( ve.getProp( response, 'visualeditor', 'result' ) !== 'success' ) {
201 this.onParseError( deferred );
202 return;
203 }
204
205 // Work around https://github.com/jquery/jquery/issues/1997
206 const contentNodes = $.parseHTML( response.visualeditor.content, this.model && this.getModelHtmlDocument() ) || [];
207 deferred.resolve( this.constructor.static.filterRendering( contentNodes ) );
208 };
209
210 /**
211 * Extend the ve.ce.GeneratedContentNode render method to check for hidden templates.
212 *
213 * Check if the final result of the imported template is empty.
214 *
215 * @inheritdoc ve.ce.GeneratedContentNode
216 */
217 ve.ce.MWTransclusionNode.prototype.render = function ( generatedContents ) {
218 // Call parent mixin
219 ve.ce.GeneratedContentNode.prototype.render.call( this, generatedContents );
220 };
221
222 /**
223 * @inheritdoc
224 */
225 ve.ce.MWTransclusionNode.prototype.onSetup = function () {
226 // Parent method
227 ve.ce.MWTransclusionNode.super.prototype.onSetup.apply( this, arguments );
228
229 // Render replaces this.$element with a new node so re-add classes
230 this.$element.addClass( 've-ce-mwTransclusionNode' );
231 };
232
233 /**
234 * @inheritdoc
235 */
236 ve.ce.MWTransclusionNode.prototype.getRenderedDomElements = function () {
237 // Parent method
238 const elements = ve.ce.GeneratedContentNode.prototype.getRenderedDomElements.apply( this, arguments );
239
240 if ( this.model && this.getModelHtmlDocument() ) {
241 ve.init.platform.linkCache.styleParsoidElements(
242 $( elements ),
243 this.getModelHtmlDocument()
244 );
245 }
246 return elements;
247 };
248
249 /**
250 * @inheritdoc
251 */
252 ve.ce.MWTransclusionNode.prototype.filterRenderedDomElements = function ( domElements ) {
253 // We want to remove all styles and links which aren't from TemplateStyles.
254 const selector = 'style:not([data-mw-deduplicate^="TemplateStyles:"]), link:not([rel~="mw-deduplicated-inline-style"][href^="mw-data:TemplateStyles:"])';
255 return $( domElements ).find( selector ).addBack( selector ).remove().end().end().toArray();
256 };
257
258 /**
259 * Handle an unsuccessful response from the parser for the wikitext fragment.
260 *
261 * @param {jQuery.Deferred} deferred The promise object created by #generateContents
262 * @param {Object} response Response data
263 */
264 ve.ce.MWTransclusionNode.prototype.onParseError = function ( deferred ) {
265 deferred.reject();
266 };
267
268 /* Registration */
269
270 ve.ce.nodeFactory.register( ve.ce.MWTransclusionNode );