Debian Codegraph @

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

raw symbols SCIP
1 /*!
2 * VisualEditor ContentEditable MWExtensionNode class.
3 *
4 * @copyright See AUTHORS.txt
5 * @license The MIT License (MIT); see LICENSE.txt
6 */
7
8 /**
9 * ContentEditable MediaWiki extension node.
10 *
11 * Configuration options for .update():
12 * - extsrc: override the contents of the tag (string)
13 * - attrs: override the attributes of the tag (object)
14 *
15 * @class
16 * @abstract
17 * @extends ve.ce.LeafNode
18 * @mixes ve.ce.FocusableNode
19 * @mixes ve.ce.GeneratedContentNode
20 *
21 * @constructor
22 */
23 ve.ce.MWExtensionNode = function VeCeMWExtensionNode() {
24 // Parent constructor
25 ve.ce.MWExtensionNode.super.apply( this, arguments );
26
27 // Mixin constructors
28 ve.ce.FocusableNode.call( this, this.getFocusableElement() );
29 ve.ce.GeneratedContentNode.call( this );
30 };
31
32 /* Inheritance */
33
34 OO.inheritClass( ve.ce.MWExtensionNode, ve.ce.LeafNode );
35 OO.mixinClass( ve.ce.MWExtensionNode, ve.ce.FocusableNode );
36 OO.mixinClass( ve.ce.MWExtensionNode, ve.ce.GeneratedContentNode );
37
38 /* Static properties */
39
40 /**
41 * Extension renders visible content when empty
42 *
43 * @static
44 * @property {boolean}
45 * @inheritable
46 */
47 ve.ce.MWExtensionNode.static.rendersEmpty = false;
48
49 ve.ce.MWExtensionNode.static.iconWhenInvisible = 'markup';
50
51 ve.ce.MWExtensionNode.static.getDescription = function ( model ) {
52 const body = ve.getProp( model.getAttribute( 'mw' ), 'body', 'extsrc' ) || '';
53 return body.slice( 0, 100 ) + ( body.length > 100 ? '…' : '' );
54 };
55
56 /* Methods */
57
58 /**
59 * @inheritdoc ve.ce.GeneratedContentNode
60 */
61 ve.ce.MWExtensionNode.prototype.generateContents = function ( config ) {
62 const deferred = ve.createDeferred(),
63 mwData = ve.copy( this.getModel().getAttribute( 'mw' ) ),
64 extsrc = config && config.extsrc !== undefined ? config.extsrc : ( ve.getProp( mwData, 'body', 'extsrc' ) || '' ),
65 attrs = config && config.attrs || mwData.attrs,
66 tagName = this.getModel().getExtensionName();
67
68 // undefined means omit the attribute, not convert it to string 'undefined'
69 for ( const attr in attrs ) {
70 if ( attrs[ attr ] === undefined ) {
71 delete attrs[ attr ];
72 }
73 }
74
75 // XML-like tags in wikitext are not actually XML and don't expect their contents to be escaped.
76 const wikitext = mw.html.element( tagName, attrs, new mw.html.Raw( extsrc ) );
77
78 if ( this.constructor.static.rendersEmpty || extsrc.trim() !== '' ) {
79 const xhr = ve.init.target.parseWikitextFragment( wikitext, false, this.getModel().getDocument() )
80 .done( this.onParseSuccess.bind( this, deferred ) )
81 .fail( this.onParseError.bind( this, deferred ) );
82 return deferred.promise( { abort: xhr.abort } );
83 } else {
84 deferred.resolve( $( '<span>' ).text( '\u00a0' ).get() );
85 return deferred.promise();
86 }
87 };
88
89 /**
90 * @inheritdoc
91 */
92 ve.ce.MWExtensionNode.prototype.getRenderedDomElements = function () {
93 // Parent method
94 const elements = ve.ce.GeneratedContentNode.prototype.getRenderedDomElements.apply( this, arguments );
95
96 if ( this.getModelHtmlDocument() ) {
97 ve.init.platform.linkCache.styleParsoidElements(
98 $( elements ),
99 this.getModelHtmlDocument()
100 );
101 }
102 return elements;
103 };
104
105 /**
106 * Handle a successful response from the parser for the wikitext fragment.
107 *
108 * @param {jQuery.Deferred} deferred The Deferred object created by generateContents
109 * @param {Object} response Response data
110 */
111 ve.ce.MWExtensionNode.prototype.onParseSuccess = function ( deferred, response ) {
112 const data = response.visualeditor,
113 contentNodes = $( data.content ).get();
114 deferred.resolve( contentNodes );
115 };
116
117 ve.ce.MWExtensionNode.prototype.afterRender = function () {
118 const $images = this.$element
119 .find( 'img:not([width]),img:not([height])' )
120 .addBack( 'img:not([width]),img:not([height])' );
121
122 // Mixin method
123 ve.ce.GeneratedContentNode.prototype.afterRender.call( this );
124
125 // Images missing a dimension change size after load
126 // TODO: Ignore images which have dimensions defined in CSS, if performant
127 if ( $images.length ) {
128 $images.on( 'load', () => {
129 // Mixin method
130 ve.ce.GeneratedContentNode.prototype.afterRender.call( this );
131 } );
132 }
133 };
134
135 /**
136 * Handle an unsuccessful response from the parser for the wikitext fragment.
137 *
138 * @param {jQuery.Deferred} deferred The promise object created by generateContents
139 * @param {Object} response Response data
140 */
141 ve.ce.MWExtensionNode.prototype.onParseError = function ( deferred ) {
142 deferred.reject();
143 };