| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
ve.dm.BlockImageNode = function VeDmBlockImageNode() { |
| 20 |
|
| 21 |
ve.dm.BlockImageNode.super.apply( this, arguments ); |
| 22 |
|
| 23 |
|
| 24 |
ve.dm.ImageNode.call( this ); |
| 25 |
ve.dm.AlignableNode.call( this ); |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
}; |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
OO.inheritClass( ve.dm.BlockImageNode, ve.dm.BranchNode ); |
| 36 |
|
| 37 |
OO.mixinClass( ve.dm.BlockImageNode, ve.dm.ImageNode ); |
| 38 |
|
| 39 |
|
| 40 |
OO.mixinClass( ve.dm.BlockImageNode, ve.dm.ClassAttributeNode ); |
| 41 |
|
| 42 |
OO.mixinClass( ve.dm.BlockImageNode, ve.dm.AlignableNode ); |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
ve.dm.BlockImageNode.static.name = 'blockImage'; |
| 47 |
|
| 48 |
ve.dm.BlockImageNode.static.preserveHtmlAttributes = function ( attribute ) { |
| 49 |
const attributes = [ 'class', 'src', 'width', 'height' ]; |
| 50 |
return attributes.indexOf( attribute ) === -1; |
| 51 |
}; |
| 52 |
|
| 53 |
ve.dm.BlockImageNode.static.handlesOwnChildren = true; |
| 54 |
|
| 55 |
ve.dm.BlockImageNode.static.childNodeTypes = [ 'imageCaption' ]; |
| 56 |
|
| 57 |
ve.dm.BlockImageNode.static.matchTagNames = [ 'figure' ]; |
| 58 |
|
| 59 |
ve.dm.BlockImageNode.static.matchFunction = function ( element ) { |
| 60 |
return element.children[ 0 ] && element.children[ 0 ].nodeName === 'IMG'; |
| 61 |
}; |
| 62 |
|
| 63 |
ve.dm.BlockImageNode.static.toDataElement = function ( domElements, converter ) { |
| 64 |
const figure = domElements[ 0 ]; |
| 65 |
const classAttr = figure.getAttribute( 'class' ); |
| 66 |
const img = figure.children[ 0 ]; |
| 67 |
const width = img.getAttribute( 'width' ); |
| 68 |
const height = img.getAttribute( 'height' ); |
| 69 |
const caption = figure.children[ 1 ]; |
| 70 |
|
| 71 |
const attributes = { |
| 72 |
src: img.getAttribute( 'src' ), |
| 73 |
width: width !== null && width !== '' ? +width : null, |
| 74 |
height: height !== null && height !== '' ? +height : null, |
| 75 |
alt: img.getAttribute( 'alt' ) |
| 76 |
}; |
| 77 |
|
| 78 |
this.setClassAttributes( attributes, classAttr ); |
| 79 |
|
| 80 |
const dataElement = { |
| 81 |
type: this.name, |
| 82 |
attributes: attributes |
| 83 |
}; |
| 84 |
|
| 85 |
if ( !caption ) { |
| 86 |
return [ |
| 87 |
dataElement, |
| 88 |
{ type: 'imageCaption' }, |
| 89 |
{ type: '/imageCaption' }, |
| 90 |
{ type: '/' + this.name } |
| 91 |
]; |
| 92 |
} else { |
| 93 |
return [ dataElement ].concat( |
| 94 |
converter.getDataFromDomClean( caption, { type: 'imageCaption' } ), |
| 95 |
{ type: '/' + this.name } |
| 96 |
); |
| 97 |
} |
| 98 |
}; |
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
ve.dm.BlockImageNode.static.toDomElements = function ( data, doc, converter ) { |
| 104 |
const dataElement = data[ 0 ], |
| 105 |
figure = doc.createElement( 'figure' ), |
| 106 |
img = doc.createElement( 'img' ); |
| 107 |
|
| 108 |
ve.setDomAttributes( img, dataElement.attributes, [ 'alt', 'src', 'width', 'height' ] ); |
| 109 |
|
| 110 |
figure.appendChild( img ); |
| 111 |
|
| 112 |
const classAttr = this.getClassAttrFromAttributes( dataElement.attributes ); |
| 113 |
if ( classAttr ) { |
| 114 |
|
| 115 |
figure.className = classAttr; |
| 116 |
} |
| 117 |
|
| 118 |
const captionData = data.slice( 1, -1 ); |
| 119 |
|
| 120 |
|
| 121 |
if ( captionData.length > 2 ) { |
| 122 |
const wrapper = doc.createElement( 'div' ); |
| 123 |
converter.getDomSubtreeFromData( captionData, wrapper ); |
| 124 |
while ( wrapper.firstChild ) { |
| 125 |
figure.appendChild( wrapper.firstChild ); |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
return [ figure ]; |
| 130 |
}; |
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
ve.dm.BlockImageNode.prototype.getCaptionNode = function () { |
| 140 |
const node = this.children[ 0 ]; |
| 141 |
return node instanceof ve.dm.BlockImageCaptionNode ? node : null; |
| 142 |
}; |
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
ve.dm.BlockImageNode.prototype.suppressSlugType = function () { |
| 148 |
|
| 149 |
return this.getAttribute( 'align' ) !== 'center' ? 'float' : null; |
| 150 |
}; |
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
ve.dm.modelRegistry.register( ve.dm.BlockImageNode ); |