| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
ve.dm.MWGalleryImageNode = function VeDmMWGalleryImageNode() { |
| 19 |
|
| 20 |
ve.dm.MWGalleryImageNode.super.apply( this, arguments ); |
| 21 |
}; |
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
OO.inheritClass( ve.dm.MWGalleryImageNode, ve.dm.BranchNode ); |
| 26 |
|
| 27 |
|
| 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 |
|
| 48 |
|
| 49 |
ve.dm.MWGalleryImageNode.static.handlesOwnChildren = true; |
| 50 |
|
| 51 |
|
| 52 |
|
| 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 |
|
| 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 |
|
| 80 |
let captionNode = li.querySelector( '.gallerytext' ); |
| 81 |
if ( captionNode ) { |
| 82 |
captionNode = captionNode.cloneNode( true ); |
| 83 |
|
| 84 |
const filename = captionNode.querySelector( '.galleryfilename' ); |
| 85 |
if ( filename ) { |
| 86 |
filename.remove(); |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
|
| 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 |
|
| 141 |
|
| 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 |
|
| 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 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 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 |
|
| 205 |
img.className = attributes.imageClassAttr; |
| 206 |
} |
| 207 |
|
| 208 |
if ( attributes.imgWrapperClassAttr ) { |
| 209 |
|
| 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 |
|
| 249 |
|
| 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 |
|
| 268 |
return ve.dm.MWGalleryImageNode.super.static.describeChange.apply( this, arguments ); |
| 269 |
} |
| 270 |
|
| 271 |
return null; |
| 272 |
}; |
| 273 |
|
| 274 |
ve.dm.MWGalleryImageNode.static.isDiffComparable = function ( element, other ) { |
| 275 |
|
| 276 |
return element.type === other.type && element.attributes.resource === other.attributes.resource; |
| 277 |
}; |
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
ve.dm.MWGalleryImageNode.prototype.getCaptionNode = function () { |
| 287 |
return this.children.length > 0 ? this.children[ 0 ] : null; |
| 288 |
}; |
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
ve.dm.modelRegistry.register( ve.dm.MWGalleryImageNode ); |