| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
ve.dm.MWBlockImageNode = function VeDmMWBlockImageNode() { |
| 21 |
|
| 22 |
ve.dm.MWBlockImageNode.super.apply( this, arguments ); |
| 23 |
|
| 24 |
|
| 25 |
ve.dm.MWImageNode.call( this ); |
| 26 |
ve.dm.ClassAttributeNode.call( this ); |
| 27 |
}; |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
OO.inheritClass( ve.dm.MWBlockImageNode, ve.dm.BranchNode ); |
| 32 |
|
| 33 |
|
| 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 |
|
| 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' ); |
| 69 |
|
| 70 |
if ( !img || !img.hasAttribute( 'resource' ) ) { |
| 71 |
return []; |
| 72 |
} |
| 73 |
const imgWrapper = img.parentNode; |
| 74 |
|
| 75 |
|
| 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 |
|
| 90 |
|
| 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 |
|
| 125 |
if ( attributes.defaultSize ) { |
| 126 |
|
| 127 |
if ( |
| 128 |
attributes.type === 'thumb' || |
| 129 |
attributes.type === 'frameless' |
| 130 |
) { |
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
attributes.originalWidth = attributes.width; |
| 135 |
attributes.originalHeight = attributes.height; |
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 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 |
|
| 172 |
|
| 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 |
|
| 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 |
|
| 189 |
figure.className = classAttr; |
| 190 |
} |
| 191 |
|
| 192 |
if ( attributes.href ) { |
| 193 |
imgWrapper.setAttribute( 'href', attributes.href ); |
| 194 |
} |
| 195 |
|
| 196 |
if ( attributes.imageClassAttr ) { |
| 197 |
|
| 198 |
img.className = attributes.imageClassAttr; |
| 199 |
} |
| 200 |
|
| 201 |
if ( attributes.imgWrapperClassAttr ) { |
| 202 |
|
| 203 |
imgWrapper.className = attributes.imgWrapperClassAttr; |
| 204 |
} |
| 205 |
|
| 206 |
let width = attributes.width; |
| 207 |
let height = attributes.height; |
| 208 |
|
| 209 |
|
| 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 |
|
| 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 |
|
| 248 |
|
| 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 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 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 |
|
| 273 |
|
| 274 |
ve.dm.MWBlockImageNode.prototype.suppressSlugType = function () { |
| 275 |
|
| 276 |
const align = this.getAttribute( 'align' ); |
| 277 |
return align !== 'none' && align !== 'center' ? 'float' : null; |
| 278 |
}; |
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
ve.dm.modelRegistry.unregister( ve.dm.BlockImageNode ); |
| 283 |
ve.dm.modelRegistry.register( ve.dm.MWBlockImageNode ); |