Debian Codegraph @

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

raw symbols SCIP
1 /*!
2 * VisualEditor DataModel MWGalleryImageNode class.
3 *
4 * @copyright See AUTHORS.txt
5 * @license The MIT License (MIT); see LICENSE.txt
6 */
7
8 /**
9 * DataModel MediaWiki gallery image node.
10 *
11 * @class
12 * @extends ve.dm.BranchNode
13 *
14 * @constructor
15 * @param {Object} [element] Reference to element in linear model
16 * @param {ve.dm.Node[]} [children]
17 */
18 ve.dm.MWGalleryImageNode = function VeDmMWGalleryImageNode() {
19 // Parent constructor
20 ve.dm.MWGalleryImageNode.super.apply( this, arguments );
21 };
22
23 /* Inheritance */
24
25 OO.inheritClass( ve.dm.MWGalleryImageNode, ve.dm.BranchNode );
26
27 /* Static members */
28
29 ve.dm.MWGalleryImageNode.static.name = 'mwGalleryImage';
30
31 ve.dm.MWGalleryImageNode.static.matchTagNames = [ 'li' ];
32
33 ve.dm.MWGalleryImageNode.static.childNodeTypes = [ 'mwGalleryImageCaption' ];
34
35 ve.dm.MWGalleryImageNode.static.matchFunction = function ( element ) {
36 const parentTypeof = ( element.parentNode && element.parentNode.getAttribute( 'typeof' ) ) || '';
37 return element.getAttribute( 'class' ) === 'gallerybox' &&
38 parentTypeof.trim().split( /\s+/ ).indexOf( 'mw:Extension/gallery' ) !== -1;
39 };
40
41 ve.dm.MWGalleryImageNode.static.parentNodeTypes = [ 'mwGallery' ];
42
43 ve.dm.MWGalleryImageNode.static.preserveHtmlAttributes = function ( attribute ) {
44 const attributes = [ 'typeof', 'class', 'src', 'resource', 'width', 'height', 'href', 'rel', 'alt', 'data-mw' ];
45 return attributes.indexOf( attribute ) === -1;
46 };
47 // By handling our own children we ensure that original DOM attributes
48 // are deep copied back by the converter (in renderHtmlAttributeList)
49 ve.dm.MWGalleryImageNode.static.handlesOwnChildren = true;
50
51 // This should be kept in sync with Parsoid's WTUtils::textContentFromCaption
52 // which drops <ref>s and metadata tags
53 ve.dm.MWGalleryImageNode.static.textContentFromCaption = function textContentFromCaption( node ) {
54 const metaDataTags = [ 'base', 'link', 'meta', 'noscript', 'script', 'style', 'template', 'title' ];
55 let content = '';
56 let c = node.firstChild;
57 while ( c ) {
58 if ( c.nodeName === '#text' ) {
59 content += c.nodeValue;
60 } else if (
61 c instanceof HTMLElement &&
62 ( metaDataTags.indexOf( c.nodeName.toLowerCase() ) === -1 ) &&
63 !/\bmw:Extension\/ref\b/.test( c.getAttribute( 'typeOf' ) )
64 ) {
65 content += textContentFromCaption( c );
66 }
67 c = c.nextSibling;
68 }
69 return content;
70 };
71
72 ve.dm.MWGalleryImageNode.static.toDataElement = function ( domElements, converter ) {
73 // TODO: Improve handling of missing files. See 'isError' in MWBlockImageNode#toDataElement
74 const li = domElements[ 0 ];
75 const img = li.querySelector( '.mw-file-element' );
76 const imgWrapper = img.parentNode;
77 const container = imgWrapper.parentNode;
78
79 // Get caption (may be missing for mode="packed-hover" galleries)
80 let captionNode = li.querySelector( '.gallerytext' );
81 if ( captionNode ) {
82 captionNode = captionNode.cloneNode( true );
83 // If showFilename is 'yes', the filename is also inside the caption, so throw this out
84 const filename = captionNode.querySelector( '.galleryfilename' );
85 if ( filename ) {
86 filename.remove();
87 }
88 }
89
90 // For video thumbnails, the `alt` attribute is only in the data-mw of the container (see: T348703)
91 const mwDataJSON = container.getAttribute( 'data-mw' );
92 const mwData = mwDataJSON ? JSON.parse( mwDataJSON ) : {};
93 const mwAttribs = mwData.attribs || [];
94 let containerAlt;
95 for ( let i = mwAttribs.length - 1; i >= 0; i-- ) {
96 if ( mwAttribs[ i ][ 0 ] === 'alt' && mwAttribs[ i ][ 1 ].txt ) {
97 containerAlt = mwAttribs[ i ][ 1 ].txt;
98 break;
99 }
100 }
101
102 const altPresent = img.hasAttribute( 'alt' ) || containerAlt !== undefined;
103 let altText = null;
104 if ( altPresent ) {
105 altText = img.hasAttribute( 'alt' ) ? img.getAttribute( 'alt' ) : containerAlt;
106 }
107
108 const altFromCaption = captionNode ?
109 ve.dm.MWGalleryImageNode.static.textContentFromCaption( captionNode ).trim() : '';
110 const altTextSame = altPresent && altFromCaption &&
111 ( altText.trim() === altFromCaption );
112
113 let caption;
114 if ( captionNode ) {
115 caption = converter.getDataFromDomClean( captionNode, { type: 'mwGalleryImageCaption' } );
116 } else {
117 caption = [
118 { type: 'mwGalleryImageCaption' },
119 { type: 'paragraph', internal: { generated: 'wrapper' } },
120 { type: '/paragraph' },
121 { type: '/mwGalleryImageCaption' }
122 ];
123 }
124
125 const typeofAttrs = container.getAttribute( 'typeof' ).trim().split( /\s+/ );
126 const errorIndex = typeofAttrs.indexOf( 'mw:Error' );
127 const isError = errorIndex !== -1;
128 const errorText = isError ? img.textContent : null;
129 const width = img.getAttribute( isError ? 'data-width' : 'width' );
130 const height = img.getAttribute( isError ? 'data-height' : 'height' );
131
132 if ( isError ) {
133 typeofAttrs.splice( errorIndex, 1 );
134 }
135
136 const types = ve.dm.MWImageNode.static.rdfaToTypes[ typeofAttrs[ 0 ] ];
137
138 let href = imgWrapper.getAttribute( 'href' );
139 if ( href ) {
140 // Convert absolute URLs to relative if the href refers to a page on this wiki.
141 // Otherwise Parsoid generates |link= options for copy-pasted images (T193253).
142 const targetData = mw.libs.ve.getTargetDataFromHref( href, converter.getTargetHtmlDocument() );
143 if ( targetData.isInternal ) {
144 href = mw.libs.ve.encodeParsoidResourceName( targetData.title );
145 }
146 }
147
148 const dataElement = {
149 type: this.name,
150 attributes: {
151 mediaClass: types.mediaClass,
152 mediaTag: img.nodeName.toLowerCase(),
153 resource: img.getAttribute( 'resource' ),
154 altText: altText,
155 altTextSame: altTextSame,
156 href: href,
157 // 'src' for images, 'poster' for video/audio
158 src: img.getAttribute( 'src' ) || img.getAttribute( 'poster' ),
159 width: width !== null && width !== '' ? +width : null,
160 height: height !== null && height !== '' ? +height : null,
161 isError: isError,
162 errorText: errorText,
163 mw: mwData,
164 imageClassAttr: img.getAttribute( 'class' ),
165 imgWrapperClassAttr: imgWrapper.getAttribute( 'class' )
166 }
167 };
168
169 return [].concat(
170 dataElement,
171 caption,
172 { type: '/' + this.name }
173 );
174 };
175
176 ve.dm.MWGalleryImageNode.static.toDomElements = function ( data, doc, converter ) {
177 // ImageNode:
178 // <li> li (gallerybox)
179 // <div> thumbDiv
180 // <span> container
181 // <a> a
182 // <img> img (or span if error)
183 const model = data[ 0 ],
184 attributes = model.attributes,
185 li = doc.createElement( 'li' ),
186 thumbDiv = doc.createElement( 'div' ),
187 container = doc.createElement( 'span' ),
188 imgWrapper = doc.createElement( attributes.href ? 'a' : 'span' ),
189 img = doc.createElement( attributes.isError ? 'span' : attributes.mediaTag ),
190 alt = attributes.altText,
191 mwData = ve.copy( attributes.mw ) || {};
192
193 li.classList.add( 'gallerybox' );
194 thumbDiv.classList.add( 'thumb' );
195 container.setAttribute( 'typeof', ve.dm.MWImageNode.static.getRdfa(
196 attributes.mediaClass, 'none', attributes.isError
197 ) );
198
199 if ( attributes.href ) {
200 imgWrapper.setAttribute( 'href', attributes.href );
201 }
202
203 if ( attributes.imageClassAttr ) {
204 // eslint-disable-next-line mediawiki/class-doc
205 img.className = attributes.imageClassAttr;
206 }
207
208 if ( attributes.imgWrapperClassAttr ) {
209 // eslint-disable-next-line mediawiki/class-doc
210 imgWrapper.className = attributes.imgWrapperClassAttr;
211 }
212
213 img.setAttribute( 'resource', attributes.resource );
214 if ( attributes.isError ) {
215 const filename = mw.libs.ve.normalizeParsoidResourceName( attributes.resource || '' );
216 img.appendChild( doc.createTextNode( attributes.errorText ? attributes.errorText : filename ) );
217 } else {
218 const srcAttr = ve.dm.MWImageNode.static.tagsToSrcAttrs[ img.nodeName.toLowerCase() ];
219 img.setAttribute( srcAttr, attributes.src );
220 }
221 img.setAttribute( attributes.isError ? 'data-width' : 'width', attributes.width );
222 img.setAttribute( attributes.isError ? 'data-height' : 'height', attributes.height );
223
224 imgWrapper.appendChild( img );
225 container.appendChild( imgWrapper );
226 thumbDiv.appendChild( container );
227 li.appendChild( thumbDiv );
228
229 const captionData = data.slice( 1, -1 );
230 const captionWrapper = doc.createElement( 'div' );
231 converter.getDomSubtreeFromData( captionData, captionWrapper );
232 while ( captionWrapper.firstChild ) {
233 li.appendChild( captionWrapper.firstChild );
234 }
235 const captionText = ve.dm.MWGalleryImageNode.static.textContentFromCaption( li ).trim();
236
237 if ( img.nodeName.toLowerCase() === 'img' ) {
238 if ( attributes.altTextSame && captionText ) {
239 img.setAttribute( 'alt', captionText );
240 } else if ( typeof alt === 'string' ) {
241 img.setAttribute( 'alt', alt );
242 }
243 } else {
244 let mwAttribs = mwData.attribs || [];
245 mwAttribs = mwAttribs.filter(
246 ( attr ) => attr[ 0 ] !== 'alt'
247 );
248 // Parsoid only sets an alt in the data-mw.attribs if it's explicit
249 // in the source
250 if ( !attributes.altTextSame && typeof alt === 'string' ) {
251 mwAttribs.push( [ 'alt', { txt: alt } ] );
252 }
253 if ( mwData.attribs || mwAttribs.length ) {
254 mwData.attribs = mwAttribs;
255 }
256 }
257
258 if ( !ve.isEmptyObject( mwData ) ) {
259 container.setAttribute( 'data-mw', JSON.stringify( mwData ) );
260 }
261
262 return [ li ];
263 };
264
265 ve.dm.MWGalleryImageNode.static.describeChange = function ( key ) {
266 if ( key === 'altText' ) {
267 // Parent method
268 return ve.dm.MWGalleryImageNode.super.static.describeChange.apply( this, arguments );
269 }
270 // All other attributes are computed, or result in nodes being incomparable (`resource`)
271 return null;
272 };
273
274 ve.dm.MWGalleryImageNode.static.isDiffComparable = function ( element, other ) {
275 // Images with different src's shouldn't be diffed
276 return element.type === other.type && element.attributes.resource === other.attributes.resource;
277 };
278
279 /* Methods */
280
281 /**
282 * Get the image's caption node.
283 *
284 * @return {ve.dm.MWImageCaptionNode|null} Caption node, if present
285 */
286 ve.dm.MWGalleryImageNode.prototype.getCaptionNode = function () {
287 return this.children.length > 0 ? this.children[ 0 ] : null;
288 };
289
290 /* Registration */
291
292 ve.dm.modelRegistry.register( ve.dm.MWGalleryImageNode );