Debian Codegraph @

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

raw symbols SCIP
1 /*!
2 * VisualEditor DataModel AlienNode class.
3 *
4 * @copyright See AUTHORS.txt
5 */
6
7 /**
8 * DataModel alien node.
9 *
10 * @class
11 * @abstract
12 * @extends ve.dm.LeafNode
13 * @mixes ve.dm.FocusableNode
14 *
15 * @constructor
16 * @param {Object} [element] Reference to element in linear model
17 */
18 ve.dm.AlienNode = function VeDmAlienNode() {
19 // Parent constructor
20 ve.dm.AlienNode.super.apply( this, arguments );
21
22 // Mixin constructor
23 ve.dm.FocusableNode.call( this );
24 };
25
26 /* Inheritance */
27
28 OO.inheritClass( ve.dm.AlienNode, ve.dm.LeafNode );
29
30 OO.mixinClass( ve.dm.AlienNode, ve.dm.FocusableNode );
31
32 /* Static members */
33
34 ve.dm.AlienNode.static.name = 'alien';
35
36 ve.dm.AlienNode.static.preserveHtmlAttributes = false;
37
38 ve.dm.AlienNode.static.enableAboutGrouping = true;
39
40 ve.dm.AlienNode.static.matchRdfaTypes = [ 've:Alien' ];
41
42 ve.dm.AlienNode.static.matchFunction = function () {
43 return true;
44 };
45
46 ve.dm.AlienNode.static.toDataElement = function ( domElements, converter ) {
47 let element;
48
49 if ( this.name !== 'alien' ) {
50 element = { type: this.name };
51 } else if ( ve.dm.TableCellableNode.static.areNodesCellable( domElements ) ) {
52 element = { type: 'alienTableCell' };
53
54 const attributes = {};
55 ve.dm.TableCellableNode.static.setAttributes( attributes, domElements, true );
56 if ( !ve.isEmptyObject( attributes ) ) {
57 element.attributes = attributes;
58 }
59 } else {
60 const isInline = this.isHybridInline( domElements, converter );
61 const type = isInline ? 'alienInline' : 'alienBlock';
62 element = { type: type };
63 }
64
65 return element;
66 };
67
68 ve.dm.AlienNode.static.toDomElements = function ( dataElement, doc, converter ) {
69 return ve.copyDomElements( converter.getStore().value( dataElement.originalDomElementsHash ) || [], doc );
70 };
71
72 /**
73 * @inheritdoc
74 */
75 ve.dm.AlienNode.static.isDiffComparable = function ( element, other, elementStore, otherStore ) {
76 if ( element.type === other.type ) {
77 if ( element.originalDomElementsHash === other.originalDomElementsHash ) {
78 return true;
79 }
80 } else {
81 return false;
82 }
83
84 // HACK: We can't strip 'about' attributes before converting, as we need them
85 // for about grouping, but we should ignore them for diffing as they can be
86 // non-persistent in historical diffs.
87
88 function removeAboutAttributes( el ) {
89 Array.prototype.forEach.call( el.querySelectorAll( '[about]' ), ( e ) => {
90 e.removeAttribute( 'about' );
91 } );
92 }
93
94 // Deep copy DOM nodes from store
95 const elementOriginalDomElements = ve.copy( elementStore.value( element.originalDomElementsHash ) );
96 const otherOriginalDomElements = ve.copy( otherStore.value( other.originalDomElementsHash ) );
97 // Remove about attributes
98 elementOriginalDomElements.forEach( removeAboutAttributes );
99 otherOriginalDomElements.forEach( removeAboutAttributes );
100 // Compare DOM trees
101 return ve.compare(
102 ve.copy( elementOriginalDomElements, ve.convertDomElements ),
103 ve.copy( otherOriginalDomElements, ve.convertDomElements )
104 );
105 };
106
107 /**
108 * @inheritdoc
109 */
110 ve.dm.AlienNode.static.getHashObject = function ( dataElement ) {
111 return {
112 type: dataElement.type,
113 // Some comparison methods ignore the originalDomElementsHash
114 // property. Rename it so it doesn't get ignored for alien nodes.
115 alienDomElementsHash: dataElement.originalDomElementsHash
116 };
117 };
118
119 /* Registration */
120
121 ve.dm.modelRegistry.register( ve.dm.AlienNode );