Debian Codegraph @

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

raw symbols SCIP
1 /*!
2 * VisualEditor DataModel MWInlineImage 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.LeafNode
13 * @mixes ve.dm.MWImageNode
14 *
15 * @constructor
16 * @param {Object} [element] Reference to element in linear model
17 */
18 ve.dm.MWInlineImageNode = function VeDmMWInlineImageNode() {
19 // Parent constructor
20 ve.dm.MWInlineImageNode.super.apply( this, arguments );
21
22 // Mixin constructors
23 ve.dm.MWImageNode.call( this );
24 };
25
26 /* Inheritance */
27
28 OO.inheritClass( ve.dm.MWInlineImageNode, ve.dm.LeafNode );
29
30 // Need to mixin base class as well (T92540)
31 OO.mixinClass( ve.dm.MWInlineImageNode, ve.dm.GeneratedContentNode );
32
33 OO.mixinClass( ve.dm.MWInlineImageNode, ve.dm.MWImageNode );
34
35 /* Static Properties */
36
37 ve.dm.MWInlineImageNode.static.isContent = true;
38
39 ve.dm.MWInlineImageNode.static.name = 'mwInlineImage';
40
41 ve.dm.MWInlineImageNode.static.preserveHtmlAttributes = function ( attribute ) {
42 const attributes = [ 'typeof', 'class', 'src', 'resource', 'width', 'height', 'href', 'data-mw' ];
43 return attributes.indexOf( attribute ) === -1;
44 };
45
46 ve.dm.MWInlineImageNode.static.matchTagNames = [ 'span' ];
47
48 ve.dm.MWInlineImageNode.static.disallowedAnnotationTypes = [ 'link' ];
49
50 ve.dm.MWInlineImageNode.static.toDataElement = function ( domElements, converter ) {
51 const container = domElements[ 0 ]; // <span>
52 if ( !container.children.length ) {
53 // Malformed image, alienate (T267282)
54 return null;
55 }
56 const img = container.querySelector( '.mw-file-element' ); // <img>, <video>, <audio>, or <span> if mw:Error
57 // Images copied from the old parser output can have typeof=mw:Image but no resource information. T337438
58 if ( !img || !img.hasAttribute( 'resource' ) ) {
59 return [];
60 }
61 const imgWrapper = img.parentNode; // <a> or <span>
62 const typeofAttrs = ( container.getAttribute( 'typeof' ) || '' ).trim().split( /\s+/ );
63 const mwDataJSON = container.getAttribute( 'data-mw' );
64 const mwData = mwDataJSON ? JSON.parse( mwDataJSON ) : {};
65 let classes = container.getAttribute( 'class' );
66 const recognizedClasses = [];
67 const errorIndex = typeofAttrs.indexOf( 'mw:Error' );
68 const isError = errorIndex !== -1;
69 const errorText = isError ? img.textContent : null;
70 const width = img.getAttribute( isError ? 'data-width' : 'width' );
71 const height = img.getAttribute( isError ? 'data-height' : 'height' );
72
73 let href = imgWrapper.getAttribute( 'href' );
74 if ( href ) {
75 // Convert absolute URLs to relative if the href refers to a page on this wiki.
76 // Otherwise Parsoid generates |link= options for copy-pasted images (T193253).
77 const targetData = mw.libs.ve.getTargetDataFromHref( href, converter.getTargetHtmlDocument() );
78 if ( targetData.isInternal ) {
79 href = mw.libs.ve.encodeParsoidResourceName( targetData.title );
80 }
81 }
82
83 if ( isError ) {
84 typeofAttrs.splice( errorIndex, 1 );
85 }
86
87 const types = this.rdfaToTypes[ typeofAttrs[ 0 ] ];
88
89 const attributes = {
90 mediaClass: types.mediaClass,
91 mediaTag: img.nodeName.toLowerCase(),
92 type: types.frameType,
93 src: img.getAttribute( 'src' ) || img.getAttribute( 'poster' ),
94 href: href,
95 imageClassAttr: img.getAttribute( 'class' ),
96 imgWrapperClassAttr: imgWrapper.getAttribute( 'class' ),
97 resource: img.getAttribute( 'resource' ),
98 originalClasses: classes,
99 width: width !== null && width !== '' ? +width : null,
100 height: height !== null && height !== '' ? +height : null,
101 alt: img.getAttribute( 'alt' ),
102 mw: mwData,
103 isError: isError,
104 errorText: errorText
105 };
106
107 // Extract individual classes
108 classes = typeof classes === 'string' ? classes.trim().split( /\s+/ ) : [];
109
110 // Deal with border flag
111 if ( classes.indexOf( 'mw-image-border' ) !== -1 ) {
112 attributes.borderImage = true;
113 recognizedClasses.push( 'mw-image-border' );
114 }
115
116 // Vertical alignment
117 attributes.valign = 'default';
118 [ 'midde', 'baseline', 'sub', 'super', 'top', 'text-top', 'bottom', 'text-bottom' ].some( ( valign ) => {
119 const className = 'mw-valign-' + valign;
120 if ( classes.indexOf( className ) !== -1 ) {
121 attributes.valign = valign;
122 recognizedClasses.push( className );
123 return true;
124 }
125 return false;
126 } );
127
128 // Border
129 if ( classes.indexOf( 'mw-image-border' ) !== -1 ) {
130 attributes.borderImage = true;
131 recognizedClasses.push( 'mw-image-border' );
132 }
133
134 // Default-size
135 if ( classes.indexOf( 'mw-default-size' ) !== -1 ) {
136 attributes.defaultSize = true;
137 recognizedClasses.push( 'mw-default-size' );
138 }
139
140 // Store unrecognized classes so we can restore them on the way out
141 attributes.unrecognizedClasses = OO.simpleArrayDifference( classes, recognizedClasses );
142
143 const dataElement = { type: this.name, attributes: attributes };
144
145 this.storeGeneratedContents( dataElement, dataElement.attributes.src, converter.getStore() );
146
147 return dataElement;
148 };
149
150 ve.dm.MWInlineImageNode.static.toDomElements = function ( dataElement, doc, converter ) {
151 const attributes = dataElement.attributes,
152 container = doc.createElement( 'span' ),
153 imgWrapper = doc.createElement( attributes.href ? 'a' : 'span' ),
154 img = doc.createElement( attributes.isError ? 'span' : attributes.mediaTag ),
155 originalClasses = attributes.originalClasses;
156
157 ve.setDomAttributes( img, attributes, [ 'resource' ] );
158 const width = attributes.width;
159 const height = attributes.height;
160 if ( width !== null ) {
161 img.setAttribute( attributes.isError ? 'data-width' : 'width', width );
162 }
163 if ( height !== null ) {
164 img.setAttribute( attributes.isError ? 'data-width' : 'height', height );
165 }
166
167 const srcAttr = this.tagsToSrcAttrs[ img.nodeName.toLowerCase() ];
168 if ( srcAttr && !attributes.isError ) {
169 img.setAttribute( srcAttr, attributes.src );
170 }
171
172 // TODO: This does not make sense for broken images (when img is a span node)
173 if ( typeof attributes.alt === 'string' ) {
174 img.setAttribute( 'alt', attributes.alt );
175 }
176
177 // RDFa type
178 container.setAttribute( 'typeof', this.getRdfa( attributes.mediaClass, attributes.type, attributes.isError ) );
179 if ( !ve.isEmptyObject( attributes.mw ) ) {
180 container.setAttribute( 'data-mw', JSON.stringify( attributes.mw ) );
181 }
182
183 let classes = [];
184 if ( attributes.defaultSize ) {
185 classes.push( 'mw-default-size' );
186 }
187
188 if ( attributes.borderImage ) {
189 classes.push( 'mw-image-border' );
190 }
191
192 if ( attributes.valign && attributes.valign !== 'default' ) {
193 classes.push( 'mw-valign-' + attributes.valign );
194 }
195
196 if ( attributes.unrecognizedClasses ) {
197 classes = OO.simpleArrayUnion( classes, attributes.unrecognizedClasses );
198 }
199
200 if (
201 originalClasses &&
202 ve.compare( originalClasses.trim().split( /\s+/ ).sort(), classes.sort() )
203 ) {
204 // eslint-disable-next-line mediawiki/class-doc
205 container.className = originalClasses;
206 } else if ( classes.length > 0 ) {
207 // eslint-disable-next-line mediawiki/class-doc
208 container.className = classes.join( ' ' );
209 }
210
211 if ( attributes.href ) {
212 imgWrapper.setAttribute( 'href', attributes.href );
213 }
214
215 if ( attributes.imgWrapperClassAttr ) {
216 // eslint-disable-next-line mediawiki/class-doc
217 imgWrapper.className = attributes.imgWrapperClassAttr;
218 }
219
220 if ( attributes.imageClassAttr ) {
221 // eslint-disable-next-line mediawiki/class-doc
222 img.className = attributes.imageClassAttr;
223 }
224
225 if ( attributes.isError ) {
226 if ( converter.isForPreview() ) {
227 imgWrapper.classList.add( 'new' );
228 }
229 const filename = mw.libs.ve.normalizeParsoidResourceName( attributes.resource || '' );
230 img.appendChild( doc.createTextNode( attributes.errorText ? attributes.errorText : filename ) );
231 }
232
233 imgWrapper.appendChild( img );
234 container.appendChild( imgWrapper );
235
236 return [ container ];
237 };
238
239 /* Registration */
240
241 ve.dm.modelRegistry.unregister( ve.dm.InlineImageNode );
242 ve.dm.modelRegistry.register( ve.dm.MWInlineImageNode );