Debian Codegraph @

extensions/VisualEditor/lib/ve/tests/ce/ve.ce.BranchNode.test.js

raw symbols SCIP
1 /*!
2 * VisualEditor ContentEditable BranchNode tests.
3 *
4 * @copyright See AUTHORS.txt
5 */
6
7 QUnit.module( 've.ce.BranchNode' );
8
9 /* Stubs */
10
11 ve.ce.BranchNodeStub = function VeCeBranchNodeStub() {
12 // Parent constructor
13 ve.ce.BranchNodeStub.super.apply( this, arguments );
14 };
15
16 OO.inheritClass( ve.ce.BranchNodeStub, ve.ce.BranchNode );
17
18 ve.ce.BranchNodeStub.static.name = 'branch-stub';
19
20 ve.ce.BranchNodeStub.static.splitOnEnter = true;
21
22 ve.ce.BranchNodeStub.prototype.getTagName = function () {
23 const style = this.model.getAttribute( 'style' ),
24 types = { a: 'a', b: 'b' };
25
26 return types[ style ];
27 };
28
29 ve.ce.nodeFactory.register( ve.ce.BranchNodeStub );
30
31 /* Tests */
32
33 QUnit.test( 'splitOnEnter', ( assert ) => {
34 const node = new ve.ce.BranchNodeStub( new ve.dm.BranchNodeStub() );
35
36 assert.strictEqual( node.splitOnEnter(), true );
37 } );
38
39 QUnit.test( 'canHaveChildren', ( assert ) => {
40 const node = new ve.ce.BranchNodeStub( new ve.dm.BranchNodeStub() );
41
42 assert.strictEqual( node.canHaveChildren(), true );
43 } );
44
45 QUnit.test( 'canHaveChildrenNotContent', ( assert ) => {
46 const node = new ve.ce.BranchNodeStub( new ve.dm.BranchNodeStub() );
47
48 assert.strictEqual( node.canHaveChildrenNotContent(), true );
49 } );
50
51 QUnit.test( 'updateTagName', ( assert ) => {
52 const attributes = { style: 'a' },
53 node = new ve.ce.BranchNodeStub( new ve.dm.BranchNodeStub( {
54 type: 'branch-stub',
55 attributes: attributes
56 } ) );
57
58 // Add content to the node
59 node.$element.text( 'hello' );
60
61 // Modify attribute
62 attributes.style = 'b';
63 node.updateTagName();
64
65 assert.strictEqual( node.$element.get( 0 ).nodeName.toLowerCase(), 'b', 'DOM element type gets converted' );
66 assert.strictEqual( node.$element.hasClass( 've-ce-branchNode' ), true, 'old classes are added to new wrapper' );
67 assert.strictEqual( !!node.$element.data( 'view' ), true, 'data added to new wrapper' );
68 assert.strictEqual( node.$element.text(), 'hello', 'contents are added to new wrapper' );
69 } );
70
71 QUnit.test( 'getDomPosition', ( assert ) => {
72 const ceParent = new ve.ce.BranchNodeStub( new ve.dm.BranchNodeStub() );
73
74 // Create prior state by attaching manually, to avoid circular dependence on onSplice
75 ceParent.$element = $( '<p>' );
76 ceParent.children.push(
77 // Node with two DOM nodes
78 // TODO: The use of BranchNodeStub below is dissonant
79 new ve.ce.LeafNode( new ve.dm.BranchNodeStub() ),
80 // Node with no DOM nodes
81 new ve.ce.LeafNode( new ve.dm.BranchNodeStub() ),
82 new ve.ce.LeafNode( new ve.dm.BranchNodeStub() ),
83 // TextNode with no annotation
84 new ve.ce.TextNode( new ve.dm.BranchNodeStub() ),
85 // Node with one DOM node
86 new ve.ce.LeafNode( new ve.dm.BranchNodeStub() ),
87 // TextNode with some annotation
88 new ve.ce.TextNode( new ve.dm.BranchNodeStub() )
89 );
90 const expectedOffsets = [ 0, 2, 2, 2, 3, 4, 7 ];
91 ceParent.children[ 0 ].$element = $( '<img><img>' );
92 ceParent.children[ 1 ].$element = $();
93 ceParent.children[ 2 ].$element = $();
94 ceParent.children[ 3 ].$element = undefined;
95 ceParent.children[ 4 ].$element = $( '<img>' );
96 ceParent.children[ 5 ].$element = undefined;
97 ceParent.$element.empty()
98 .append( ceParent.children[ 0 ].$element )
99 .append( 'foo' )
100 .append( ceParent.children[ 4 ].$element )
101 .append( 'bar<b>baz</b>qux' );
102
103 for ( let i = 0, len = ceParent.children.length + 1; i < len; i++ ) {
104 const position = ceParent.getDomPosition( i );
105 assert.strictEqual( position.node, ceParent.$element.last()[ 0 ], 'i=' + i + ' node' );
106 assert.strictEqual( position.offset, expectedOffsets[ i ], 'i=' + i + ' position' );
107 }
108 } );
109
110 QUnit.test( 'onSplice', ( assert ) => {
111 const modelA = new ve.dm.BranchNodeStub(),
112 modelB = new ve.dm.BranchNodeStub(),
113 modelC = new ve.dm.BranchNodeStub();
114
115 const viewA = new ve.ce.BranchNodeStub( modelA );
116
117 // Insertion tests
118 modelA.splice( 0, 0, modelB, modelC );
119
120 assert.strictEqual( viewA.getChildren().length, 2 );
121 const viewB = viewA.getChildren()[ 0 ];
122 const viewC = viewA.getChildren()[ 1 ];
123 assert.deepEqual( viewB.getModel(), modelB, 'First view child matches model tree' );
124 assert.deepEqual( viewC.getModel(), modelC, 'Second view child matches model tree' );
125
126 // Removal tests
127 modelA.splice( 0, 1 );
128 assert.strictEqual( viewA.getChildren().length, 1 );
129 assert.deepEqual( viewA.getChildren()[ 0 ].getModel(), modelC, 'Only view child matches model tree' );
130 assert.strictEqual( !viewB.getModel(), true, 'Removed view node was destroyed' );
131
132 // Removal and insertion tests
133 modelA.splice( 0, 1, modelB );
134
135 assert.strictEqual( viewA.getChildren().length, 1 );
136 assert.deepEqual( viewA.getChildren()[ 0 ].getModel(), modelB, 'Replaced view child matches model tree' );
137 assert.strictEqual( !viewC.getModel(), true, 'Replaced view node was destroyed' );
138 } );