Debian Codegraph @

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

raw symbols SCIP
1 /*!
2 * VisualEditor DataModel MWBlockImageNode class.
3 *
4 * @copyright See AUTHORS.txt
5 * @license The MIT License (MIT); see LICENSE.txt
6 */
7
8 /**
9 * DataModel MediaWiki image node.
10 *
11 * @class
12 * @extends ve.dm.BranchNode
13 * @mixes ve.dm.MWImageNode
14 * @mixes ve.dm.ClassAttributeNode
15 *
16 * @constructor
17 * @param {Object} [element] Reference to element in linear model
18 * @param {ve.dm.Node[]} [children]
19 */
20 ve.dm.MWBlockImageNode = function VeDmMWBlockImageNode() {
21 // Parent constructor
22 ve.dm.MWBlockImageNode.super.apply( this, arguments );
23
24 // Mixin constructors
25 ve.dm.MWImageNode.call( this );
26 ve.dm.ClassAttributeNode.call( this );
27 };
28
29 /* Inheritance */
30
31 OO.inheritClass( ve.dm.MWBlockImageNode, ve.dm.BranchNode );
32
33 // Need to mixin base class as well (T92540)
34 OO.mixinClass( ve.dm.MWBlockImageNode, ve.dm.GeneratedContentNode );
35
36 OO.mixinClass( ve.dm.MWBlockImageNode, ve.dm.MWImageNode );
37
38 OO.mixinClass( ve.dm.MWBlockImageNode, ve.dm.ClassAttributeNode );
39
40 /* Static Properties */
41
42 ve.dm.MWBlockImageNode.static.name = 'mwBlockImage';
43
44 ve.dm.MWBlockImageNode.static.preserveHtmlAttributes = function ( attribute ) {
45 const attributes = [ 'typeof', 'class', 'src', 'resource', 'width', 'height', 'href', 'rel', 'data-mw', 'alt' ];
46 return attributes.indexOf( attribute ) === -1;
47 };
48
49 ve.dm.MWBlockImageNode.static.handlesOwnChildren = true;
50
51 ve.dm.MWBlockImageNode.static.childNodeTypes = [ 'mwImageCaption' ];
52
53 ve.dm.MWBlockImageNode.static.matchTagNames = [ 'figure' ];
54
55 ve.dm.MWBlockImageNode.static.disallowedAnnotationTypes = [ 'link' ];
56
57 ve.dm.MWBlockImageNode.static.classAttributes = {
58 'mw-image-border': { borderImage: true },
59 'mw-halign-left': { align: 'left' },
60 'mw-halign-right': { align: 'right' },
61 'mw-halign-center': { align: 'center' },
62 'mw-halign-none': { align: 'none' },
63 'mw-default-size': { defaultSize: true }
64 };
65
66 ve.dm.MWBlockImageNode.static.toDataElement = function ( domElements, converter ) {
67 const figure = domElements[ 0 ];
68 const img = figure.querySelector( '.mw-file-element' ); // <img>, <video>, <audio>, or <span> if mw:Error
69 // Images copied from the old parser output can have typeof=mw:Image but no resource information. T337438
70 if ( !img || !img.hasAttribute( 'resource' ) ) {
71 return [];
72 }
73 const imgWrapper = img.parentNode; // <a> or <span>
74 // NB: A caption could contain another block image with a caption, but the outer
75 // image must always contain a caption for that to happen, and that one will match first.
76 const captionNode = figure.querySelector( 'figcaption' );
77 const classAttr = figure.getAttribute( 'class' );
78 const typeofAttrs = figure.getAttribute( 'typeof' ).trim().split( /\s+/ );
79 const mwDataJSON = figure.getAttribute( 'data-mw' );
80 const mwData = mwDataJSON ? JSON.parse( mwDataJSON ) : {};
81 const errorIndex = typeofAttrs.indexOf( 'mw:Error' );
82 const isError = errorIndex !== -1;
83 const errorText = isError ? img.textContent : null;
84 const width = img.getAttribute( isError ? 'data-width' : 'width' );
85 const height = img.getAttribute( isError ? 'data-height' : 'height' );
86
87 let href = imgWrapper.getAttribute( 'href' );
88 if ( href ) {
89 // Convert absolute URLs to relative if the href refers to a page on this wiki.
90 // Otherwise Parsoid generates |link= options for copy-pasted images (T193253).
91 const targetData = mw.libs.ve.getTargetDataFromHref( href, converter.getTargetHtmlDocument() );
92 if ( targetData.isInternal ) {
93 href = mw.libs.ve.encodeParsoidResourceName( targetData.title );
94 }
95 }
96
97 if ( isError ) {
98 typeofAttrs.splice( errorIndex, 1 );
99 }
100
101 const types = this.rdfaToTypes[ typeofAttrs[ 0 ] ];
102
103 const attributes = {
104 mediaClass: types.mediaClass,
105 mediaTag: img.nodeName.toLowerCase(),
106 type: types.frameType,
107 src: img.getAttribute( 'src' ) || img.getAttribute( 'poster' ),
108 href: href,
109 imageClassAttr: img.getAttribute( 'class' ),
110 imgWrapperClassAttr: imgWrapper.getAttribute( 'class' ),
111 resource: img.getAttribute( 'resource' ),
112 width: width !== null && width !== '' ? +width : null,
113 height: height !== null && height !== '' ? +height : null,
114 alt: img.getAttribute( 'alt' ),
115 mw: mwData,
116 isError: isError,
117 errorText: errorText
118 };
119
120 this.setClassAttributes( attributes, classAttr );
121
122 attributes.align = attributes.align || 'default';
123
124 // Default-size
125 if ( attributes.defaultSize ) {
126 // Force wiki-default size for thumb and frameless
127 if (
128 attributes.type === 'thumb' ||
129 attributes.type === 'frameless'
130 ) {
131 // We're going to change .width and .height, store the original
132 // values so we can restore them later.
133 // FIXME "just" don't modify .width and .height instead
134 attributes.originalWidth = attributes.width;
135 attributes.originalHeight = attributes.height;
136 // Parsoid hands us images with default Wikipedia dimensions
137 // rather than default MediaWiki configuration dimensions.
138 // We must force local wiki default in edit mode for default
139 // size images.
140 const newDimensions = this.scaleToThumbnailSize( attributes );
141 if ( newDimensions ) {
142 attributes.width = newDimensions.width;
143 attributes.height = newDimensions.height;
144 }
145 }
146 }
147
148 let caption;
149 if ( captionNode ) {
150 caption = converter.getDataFromDomClean( captionNode, { type: 'mwImageCaption' } );
151 } else {
152 caption = [
153 { type: 'mwImageCaption' },
154 { type: 'paragraph', internal: { generated: 'wrapper' } },
155 { type: '/paragraph' },
156 { type: '/mwImageCaption' }
157 ];
158 }
159
160 const dataElement = { type: this.name, attributes: attributes };
161
162 this.storeGeneratedContents( dataElement, dataElement.attributes.src, converter.getStore() );
163
164 return [].concat(
165 dataElement,
166 caption,
167 { type: '/' + this.name }
168 );
169 };
170
171 // TODO: At this moment node is not resizable but when it will be then adding defaultSize class
172 // should be more conditional.
173 ve.dm.MWBlockImageNode.static.toDomElements = function ( data, doc, converter ) {
174 const dataElement = data[ 0 ],
175 attributes = dataElement.attributes,
176 figure = doc.createElement( 'figure' ),
177 imgWrapper = doc.createElement( attributes.href ? 'a' : 'span' ),
178 img = doc.createElement( attributes.isError ? 'span' : attributes.mediaTag ),
179 classAttr = this.getClassAttrFromAttributes( attributes );
180
181 // RDFa type
182 figure.setAttribute( 'typeof', this.getRdfa( attributes.mediaClass, attributes.type, attributes.isError ) );
183 if ( !ve.isEmptyObject( attributes.mw ) ) {
184 figure.setAttribute( 'data-mw', JSON.stringify( attributes.mw ) );
185 }
186
187 if ( classAttr ) {
188 // eslint-disable-next-line mediawiki/class-doc
189 figure.className = classAttr;
190 }
191
192 if ( attributes.href ) {
193 imgWrapper.setAttribute( 'href', attributes.href );
194 }
195
196 if ( attributes.imageClassAttr ) {
197 // eslint-disable-next-line mediawiki/class-doc
198 img.className = attributes.imageClassAttr;
199 }
200
201 if ( attributes.imgWrapperClassAttr ) {
202 // eslint-disable-next-line mediawiki/class-doc
203 imgWrapper.className = attributes.imgWrapperClassAttr;
204 }
205
206 let width = attributes.width;
207 let height = attributes.height;
208 // If defaultSize is set, and was set on the way in, use the original width and height
209 // we got on the way in.
210 if ( attributes.defaultSize ) {
211 if ( attributes.originalWidth !== undefined ) {
212 width = attributes.originalWidth;
213 }
214 if ( attributes.originalHeight !== undefined ) {
215 height = attributes.originalHeight;
216 }
217 }
218
219 const srcAttr = this.tagsToSrcAttrs[ img.nodeName.toLowerCase() ];
220 if ( srcAttr && !attributes.isError ) {
221 img.setAttribute( srcAttr, attributes.src );
222 }
223 if ( attributes.isError ) {
224 if ( converter.isForPreview() ) {
225 imgWrapper.classList.add( 'new' );
226 }
227 const filename = mw.libs.ve.normalizeParsoidResourceName( attributes.resource || '' );
228 img.appendChild( doc.createTextNode( attributes.errorText ? attributes.errorText : filename ) );
229 }
230
231 if ( width !== null ) {
232 img.setAttribute( attributes.isError ? 'data-width' : 'width', width );
233 }
234 if ( height !== null ) {
235 img.setAttribute( attributes.isError ? 'data-height' : 'height', height );
236 }
237
238 img.setAttribute( 'resource', attributes.resource );
239 // TODO: This does not make sense for broken images (when img is a span node)
240 if ( typeof attributes.alt === 'string' ) {
241 img.setAttribute( 'alt', attributes.alt );
242 }
243 figure.appendChild( imgWrapper );
244 imgWrapper.appendChild( img );
245
246 const captionData = data.slice( 1, -1 );
247 // If length of captionData is smaller or equal to 2 it means that there is no caption or that
248 // it is empty - in both cases we are going to skip appending <figcaption>.
249 if ( captionData.length > 2 ) {
250 const captionWrapper = doc.createElement( 'div' );
251 converter.getDomSubtreeFromData( captionData, captionWrapper );
252 while ( captionWrapper.firstChild ) {
253 figure.appendChild( captionWrapper.firstChild );
254 }
255 }
256 return [ figure ];
257 };
258
259 /* Methods */
260
261 /**
262 * Get the caption node of the image.
263 *
264 * @return {ve.dm.MWImageCaptionNode|null} Caption node, if present
265 */
266 ve.dm.MWBlockImageNode.prototype.getCaptionNode = function () {
267 const node = this.children[ 0 ];
268 return node instanceof ve.dm.MWImageCaptionNode ? node : null;
269 };
270
271 /**
272 * @inheritdoc
273 */
274 ve.dm.MWBlockImageNode.prototype.suppressSlugType = function () {
275 // TODO: Have alignment attribute changes trigger a parent branch node re-render
276 const align = this.getAttribute( 'align' );
277 return align !== 'none' && align !== 'center' ? 'float' : null;
278 };
279
280 /* Registration */
281
282 ve.dm.modelRegistry.unregister( ve.dm.BlockImageNode );
283 ve.dm.modelRegistry.register( ve.dm.MWBlockImageNode );