Debian Codegraph @

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

raw symbols SCIP
1 /*!
2 * VisualEditor ContentEditable Document tests.
3 *
4 * @copyright See AUTHORS.txt
5 */
6
7 QUnit.module( 've.ce.Document' );
8
9 /* Tests */
10
11 QUnit.test( 'Converter tests', ( assert ) => {
12 const cases = ve.dm.example.domToDataCases;
13
14 for ( const msg in cases ) {
15 if ( cases[ msg ].ceHtml ) {
16 const caseItem = ve.copy( cases[ msg ] );
17 caseItem.base = caseItem.base || ve.dm.example.baseUri;
18 const model = ve.test.utils.getModelFromTestCase( caseItem );
19 const view = new ve.ce.Document( model );
20 const $documentElement = view.getDocumentNode().$element;
21 // Simplify slugs
22 $documentElement.find( '.ve-ce-branchNode-slug' ).contents().remove();
23 assert.equalDomElement(
24 // Wrap both in plain DIVs as we are only comparing the child nodes
25 $( '<div>' ).append( $documentElement.contents() )[ 0 ],
26 $( '<div>' ).html( caseItem.ceHtml )[ 0 ],
27 msg
28 );
29 }
30 }
31 } );
32
33 // TODO: getNodeFromOffset
34 // TODO: getSlugAtOffset
35 // TODO: getDirectionalityFromRange
36
37 QUnit.test( 'getNodeAndOffset', ( assert ) => {
38 /* eslint-disable quotes */
39 const docNodeStart = "<div class='ve-ce-branchNode ve-ce-documentNode ve-ce-attachedRootNode ve-ce-rootNode'>",
40 pNodeStart = "<p class='ve-ce-branchNode ve-ce-contentBranchNode ve-ce-paragraphNode'>";
41
42 // Each test below has the following:
43 // html: an input document
44 // data: the expected DM content
45 // positions: the node+offset corresponding to each DM offset, shown by marking pipe
46 // characters on a modified HTML representation in which text nodes are wrapped in
47 // <#text>…</#text> tags (and most attributes are omitted)
48 // dies (optional): a list of DM offsets where getNodeAndOffset is expected to die
49 const cases = [
50 {
51 title: 'Simple para',
52 html: '<p>x</p>',
53 data: [ '<paragraph>', 'x', '</paragraph>' ],
54 positions: docNodeStart + "|" + pNodeStart + "<#text>|x|</#text></p></div>"
55 },
56 {
57 title: 'Bold',
58 html: '<p>x<b>y</b>z</p>',
59 data: [ '<paragraph>', ...'xyz', '</paragraph>' ],
60 positions: docNodeStart + "|" + pNodeStart + "<#text>|x|</#text><b class='ve-ce-annotation ve-ce-textStyleAnnotation ve-ce-boldAnnotation'><#text>y|</#text></b><#text>z|</#text></p></div>"
61 },
62 {
63 title: 'Nested block nodes',
64 html: '<div><p>x</p></div>',
65 data: [ '<div>', '<paragraph>', 'x', '</paragraph>', '</div>' ],
66 positions: docNodeStart + "<div class='ve-ce-branchNode-slug ve-ce-branchNode-blockSlug'></div>|<div class='ve-ce-branchNode'>|" + pNodeStart + "<#text>|x|</#text></p>|</div><div class='ve-ce-branchNode-slug ve-ce-branchNode-blockSlug'></div></div>"
67 },
68 {
69 title: 'Empty document',
70 html: '<p></p>',
71 unwrap: [ { type: 'paragraph' } ],
72 data: [],
73 positions: "",
74 dies: [ 1 ]
75 },
76 {
77 title: 'Slugless emptied paragraph',
78 html: '<p></p>',
79 replacement: { path: [ 0 ], innerHtml: '' },
80 data: [ '<paragraph>', '</paragraph>' ],
81 positions: docNodeStart + "|" + pNodeStart + "|</p></div>"
82 },
83 {
84 title: 'Slugless emptied paragraph with raw <br> in DOM only',
85 html: '<p></p>',
86 replacement: { path: [ 0 ], innerHtml: '<br>' },
87 data: [ '<paragraph>', '</paragraph>' ],
88 positions: docNodeStart + "|" + pNodeStart + "|<br></br></p></div>"
89 },
90 {
91 title: 'Paragraph with links',
92 html: '<p><a href="A">A</a><a href="B">B</a></p>',
93 data: [ '<paragraph>', ...'AB', '</paragraph>' ],
94 positions: docNodeStart + "|" + pNodeStart + "|<img class='ve-ce-nail ve-ce-nail-pre-open'></img><a class='ve-ce-annotation ve-ce-nailedAnnotation ve-ce-linkAnnotation'><img class='ve-ce-nail ve-ce-nail-post-open'></img><#text>A</#text><img class='ve-ce-nail ve-ce-nail-pre-close'></img></a><img class='ve-ce-nail ve-ce-nail-post-close'></img>|<img class='ve-ce-nail ve-ce-nail-pre-open'></img><a class='ve-ce-annotation ve-ce-nailedAnnotation ve-ce-linkAnnotation'><img class='ve-ce-nail ve-ce-nail-post-open'></img><#text>B</#text><img class='ve-ce-nail ve-ce-nail-pre-close'></img></a><img class='ve-ce-nail ve-ce-nail-post-close'></img>|</p></div>"
95 },
96 {
97 title: 'Paragraph with links, non-text nodes',
98 html: '<p><a href="A"><b>A<img src="' + ve.dm.example.imgSrc + '"></b></a></p>',
99 data: [ '<paragraph>', 'A', '<inlineImage>', '</inlineImage>', '</paragraph>' ],
100 positions: docNodeStart + "|" + pNodeStart + "|<img class='ve-ce-nail ve-ce-nail-pre-open'></img><a class='ve-ce-annotation ve-ce-nailedAnnotation ve-ce-linkAnnotation'><img class='ve-ce-nail ve-ce-nail-post-open'></img><b class='ve-ce-annotation ve-ce-textStyleAnnotation ve-ce-boldAnnotation'><#text>A|</#text><img class='ve-ce-leafNode ve-ce-focusableNode ve-ce-imageNode ve-ce-inlineImageNode'></img></b><img class='ve-ce-nail ve-ce-nail-pre-close'></img></a><img class='ve-ce-nail ve-ce-nail-post-close'></img>||<span class='ve-ce-branchNode-slug ve-ce-branchNode-inlineSlug'></span></p></div>"
101 },
102 {
103 title: 'About grouped aliens',
104 html: "<p><span rel='ve:Alien' about='x'>Foo</span><span rel='ve:Alien' about='x'>Bar</span></p>",
105 data: [ '<paragraph>', '<alienInline>', '</alienInline>', '</paragraph>' ],
106 positions: docNodeStart + "|" + pNodeStart + "|<span class='ve-ce-branchNode-slug ve-ce-branchNode-inlineSlug'></span><span class='ve-ce-focusableNode ve-ce-leafNode'><#text>|Foo</#text></span><span class='ve-ce-focusableNode ve-ce-leafNode'><#text>Bar</#text></span>|<span class='ve-ce-branchNode-slug ve-ce-branchNode-inlineSlug'></span></p></div>"
107 },
108 {
109 title: 'Non-about grouped aliens',
110 html: "<p><span rel='ve:Alien' about='x'>Foo</span><span rel='ve:Alien' about='y'>Bar</span></p>",
111 data: [ '<paragraph>', '<alienInline>', '</alienInline>', '<alienInline>', '</alienInline>', '</paragraph>' ],
112 positions: docNodeStart + "|" + pNodeStart + "|<span class='ve-ce-branchNode-slug ve-ce-branchNode-inlineSlug'></span><span class='ve-ce-focusableNode ve-ce-leafNode'><#text>|Foo</#text></span>|<span class='ve-ce-branchNode-slug ve-ce-branchNode-inlineSlug'></span><span class='ve-ce-focusableNode ve-ce-leafNode'><#text>|Bar</#text></span>|<span class='ve-ce-branchNode-slug ve-ce-branchNode-inlineSlug'></span></p></div>"
113 },
114 {
115 title: 'Meta outside of CBN',
116 html: "<p>X</p><!----><p>Y</p>",
117 data: [ '<paragraph>', 'X', '</paragraph>', '<commentMeta>', '</commentMeta>', '<paragraph>', 'Y', '</paragraph>' ],
118 positions: docNodeStart + "|" + pNodeStart + "<#text>|X|</#text></p>|||" + pNodeStart + "<#text>|Y|</#text></p></div>"
119 },
120 {
121 title: 'Meta does not cause double block slug',
122 html: "<p rel='ve:Alien'>X</p><!---->",
123 data: [ '<alienBlock>', '</alienBlock>', '<commentMeta>', '</commentMeta>' ],
124 positions: docNodeStart + "<div class='ve-ce-branchNode-slug ve-ce-branchNode-blockSlug'></div>|<p class='ve-ce-focusableNode ve-ce-leafNode'>|<#text>X</#text></p><div class='ve-ce-branchNode-slug ve-ce-branchNode-blockSlug'></div>||</div>"
125 }
126 ];
127 /* eslint-enable quotes */
128
129 function showModelItem( item ) {
130 if ( item.type ) {
131 return '<' + item.type + '>';
132 } else if ( Array.isArray( item ) ) {
133 return item[ 0 ];
134 } else if ( typeof item === 'string' ) {
135 return item;
136 } else {
137 return '(unexpected: ' + item + ')';
138 }
139 }
140
141 cases.forEach( ( caseItem ) => {
142 const parts = caseItem.positions.split( /[|]/ );
143 const view = ve.test.utils.createSurfaceViewFromHtml( caseItem.html );
144 const dmDoc = view.getModel().getDocument();
145 if ( caseItem.unwrap ) {
146 new ve.dm.Surface( dmDoc ).change(
147 ve.dm.TransactionBuilder.static.newFromWrap(
148 dmDoc,
149 new ve.Range( 0, dmDoc.data.countNonInternalElements() ),
150 [],
151 [],
152 caseItem.unwrap,
153 []
154 )
155 );
156 }
157 const data = dmDoc.data.data
158 .slice( 0, -2 )
159 .map( showModelItem );
160 const ceDoc = view.documentView;
161 const rootNode = ceDoc.getDocumentNode().$element.get( 0 );
162 assert.deepEqual( data, caseItem.data, caseItem.title + ' (data)' );
163
164 const offsetCount = data.length;
165 assert.strictEqual(
166 offsetCount,
167 caseItem.positions.replace( /[^|]/g, '' ).length,
168 caseItem.title + ' (offset count)'
169 );
170
171 if ( caseItem.replacement ) {
172 let node = rootNode;
173 for ( let j = 0, jLen = caseItem.replacement.path.length; j < jLen; j++ ) {
174 node = node.childNodes[ caseItem.replacement.path[ j ] ];
175 }
176 $( node ).closest(
177 '.ve-ce-branchNode,.ve-ce-leafNode'
178 ).data( 'view' ).removeSlugs();
179 node.innerHTML = caseItem.replacement.innerHtml;
180 }
181
182 for ( let offset = 0; offset < offsetCount; offset++ ) {
183 assert.strictEqual(
184 ve.test.utils.serializePosition(
185 rootNode,
186 ceDoc.getNodeAndOffset( offset ),
187 { ignore: '.ve-ce-branchNode-slug>*' }
188 ),
189 [].concat(
190 parts.slice( 0, offset + 1 ),
191 [ '|' ],
192 parts.slice( offset + 1 )
193 ).join( '' ),
194 caseItem.title + ' (' + offset + ')'
195 );
196 }
197 for ( let k = 0; caseItem.dies && k < caseItem.dies.length; k++ ) {
198 const dieOffset = caseItem.dies[ k ];
199 let ex = null;
200 try {
201 ceDoc.getNodeAndOffset( dieOffset );
202 } catch ( e ) {
203 ex = e;
204 }
205 assert.notStrictEqual( ex, null, caseItem.title + ' (' + dieOffset + ') dies' );
206 }
207 view.destroy();
208 } );
209 } );
210
211 QUnit.test( 'attachedRoot', ( assert ) => {
212 const dmDoc = ve.dm.converter.getModelFromDom(
213 ve.createDocumentFromHtml(
214 '<section>Foo</section><section>Bar</section><section>Baz</section>'
215 )
216 ),
217 attachedRoot = dmDoc.getDocumentNode().children[ 1 ],
218 surfaceModel = new ve.dm.Surface( dmDoc, attachedRoot ),
219 surfaceView = ve.test.utils.createSurfaceViewFromDocument( surfaceModel );
220
221 assert.deepEqual(
222 surfaceView.getDocument().getDocumentNode().children.map( ( node ) => node.type ),
223 [ 'unrendered', 'section', 'unrendered', 'unrendered' ],
224 'Only attached root is rendered'
225 );
226 } );