/*! * VisualEditor DataModel MWGalleryImageNode class. * * @copyright 2016 VisualEditor Team and others; see AUTHORS.txt * @license The MIT License (MIT); see LICENSE.txt */ /** * DataModel MediaWiki gallery image node. * * @class * @extends ve.dm.BranchNode * * @constructor * @param {Object} [element] Reference to element in linear model * @param {ve.dm.Node[]} [children] */ ve.dm.MWGalleryImageNode = function VeDmMWGalleryImageNode() { // Parent constructor ve.dm.MWGalleryImageNode.super.apply( this, arguments ); }; /* Inheritance */ OO.inheritClass( ve.dm.MWGalleryImageNode, ve.dm.BranchNode ); /* Static members */ ve.dm.MWGalleryImageNode.static.name = 'mwGalleryImage'; ve.dm.MWGalleryImageNode.static.matchTagNames = [ 'li' ]; ve.dm.MWGalleryImageNode.static.childNodeTypes = [ 'mwGalleryImageCaption' ]; ve.dm.MWGalleryImageNode.static.matchFunction = function ( element ) { var parentTypeof = ( element.parentNode && element.parentNode.getAttribute( 'typeof' ) ) || ''; return element.getAttribute( 'class' ) === 'gallerybox' && parentTypeof.trim().split( /\s+/ ).indexOf( 'mw:Extension/gallery' ) !== -1; }; ve.dm.MWGalleryImageNode.static.parentNodeTypes = [ 'mwGallery' ]; ve.dm.MWGalleryImageNode.static.toDataElement = function ( domElements, converter ) { // TODO: Improve handling of missing files. See 'isError' in MWBlockImageNode#toDataElement var li = domElements[ 0 ]; var img = li.querySelector( 'img,audio,video,span[resource]' ); var figureInline = img.parentNode.parentNode; // Get caption (may be missing for mode="packed-hover" galleries) var captionNode = li.querySelector( '.gallerytext' ); if ( captionNode ) { captionNode = captionNode.cloneNode( true ); // If showFilename is 'yes', the filename is also inside the caption, so throw this out var filename = captionNode.querySelector( '.galleryfilename' ); if ( filename ) { filename.remove(); } } var caption; if ( captionNode ) { caption = converter.getDataFromDomClean( captionNode, { type: 'mwGalleryImageCaption' } ); } else { caption = [ { type: 'mwGalleryImageCaption' }, { type: 'paragraph', internal: { generated: 'wrapper' } }, { type: '/paragraph' }, { type: '/mwGalleryImageCaption' } ]; } var typeofAttrs = figureInline.getAttribute( 'typeof' ).trim().split( /\s+/ ); var errorIndex = typeofAttrs.indexOf( 'mw:Error' ); var isError = errorIndex !== -1; var width = img.getAttribute( isError ? 'data-width' : 'width' ); var height = img.getAttribute( isError ? 'data-height' : 'height' ); var dataElement = { type: this.name, attributes: { resource: './' + mw.libs.ve.normalizeParsoidResourceName( img.getAttribute( 'resource' ) ), altText: img.getAttribute( 'alt' ), // 'src' for images, 'poster' for video/audio src: img.getAttribute( 'src' ) || img.getAttribute( 'poster' ), width: width !== null && width !== '' ? +width : null, height: height !== null && height !== '' ? +height : null, tagName: figureInline.nodeName.toLowerCase(), isError: isError } }; return [ dataElement ] .concat( caption ) .concat( { type: '/' + this.name } ); }; ve.dm.MWGalleryImageNode.static.toDomElements = function ( data, doc ) { // ImageNode: //