Debian Codegraph @

extensions/VisualEditor/lib/ve/src/dm/nodes/ve.dm.BlockImageNode.js

raw symbols SCIP
1 /*!
2 * VisualEditor DataModel BlockImageNode class.
3 *
4 * @copyright See AUTHORS.txt
5 */
6
7 /**
8 * DataModel block image node.
9 *
10 * @class
11 * @extends ve.dm.BranchNode
12 * @mixes ve.dm.ImageNode
13 * @mixes ve.dm.AlignableNode
14 *
15 * @constructor
16 * @param {Object} [element] Reference to element in linear model
17 * @param {ve.dm.Node[]} [children]
18 */
19 ve.dm.BlockImageNode = function VeDmBlockImageNode() {
20 // Parent constructor
21 ve.dm.BlockImageNode.super.apply( this, arguments );
22
23 // Mixin constructors
24 ve.dm.ImageNode.call( this );
25 ve.dm.AlignableNode.call( this );
26
27 // <figure>
28 // <img/>
29 // [<figcaption/>]
30 // </figure>
31 };
32
33 /* Inheritance */
34
35 OO.inheritClass( ve.dm.BlockImageNode, ve.dm.BranchNode );
36
37 OO.mixinClass( ve.dm.BlockImageNode, ve.dm.ImageNode );
38
39 // Mixin Alignable's parent class
40 OO.mixinClass( ve.dm.BlockImageNode, ve.dm.ClassAttributeNode );
41
42 OO.mixinClass( ve.dm.BlockImageNode, ve.dm.AlignableNode );
43
44 /* Static Properties */
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 // TODO: Consider using jQuery instead of pure JS.
101 // TODO: At this moment node is not resizable but when it will be then adding defaultSize class
102 // should be more conditional.
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 // eslint-disable-next-line mediawiki/class-doc
115 figure.className = classAttr;
116 }
117
118 const captionData = data.slice( 1, -1 );
119 // If length of captionData is smaller or equal to 2 it means that there is no caption or that
120 // it is empty - in both cases we are going to skip appending <figcaption>.
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 /* Methods */
133
134 /**
135 * Get the caption node of the image.
136 *
137 * @return {ve.dm.BlockImageCaptionNode|null} Caption node, if present
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 * @inheritdoc
146 */
147 ve.dm.BlockImageNode.prototype.suppressSlugType = function () {
148 // TODO: Have alignment attribute changes trigger a parent branch node re-render
149 return this.getAttribute( 'align' ) !== 'center' ? 'float' : null;
150 };
151
152 /* Registration */
153
154 ve.dm.modelRegistry.register( ve.dm.BlockImageNode );