Debian Codegraph @

extensions/VisualEditor/lib/ve/tests/dm/ve.dm.Converter.test.js

raw symbols SCIP
1 /*!
2 * VisualEditor DataModel Converter tests.
3 *
4 * @copyright See AUTHORS.txt
5 */
6
7 QUnit.module( 've.dm.Converter' );
8
9 /* Tests */
10
11 QUnit.test( 'getModelFromDom', ( assert ) => {
12 const cases = ve.dm.example.domToDataCases;
13
14 for ( const msg in cases ) {
15 const caseItem = ve.copy( cases[ msg ] );
16 caseItem.base = caseItem.base || ve.dm.example.baseUri;
17 ve.test.utils.runGetModelFromDomTest( assert, caseItem, msg );
18 }
19 } );
20
21 QUnit.test( 'getModelFromDom with store argument', ( assert ) => {
22 const store = new ve.dm.HashValueStore();
23 const model = ve.dm.converter.getModelFromDom(
24 ve.createDocumentFromHtml( '<p>foo</p>' ),
25 { lang: 'en', dir: 'ltr' },
26 store
27 );
28 assert.strictEqual( model.getStore(), store, 'Document store is reference-equal to store argument' );
29 } );
30
31 QUnit.test( 'getDomFromModel', ( assert ) => {
32 const cases = ve.dm.example.domToDataCases;
33
34 for ( const msg in cases ) {
35 const caseItem = ve.copy( cases[ msg ] );
36 caseItem.base = caseItem.base || ve.dm.example.baseUri;
37 ve.test.utils.runGetDomFromModelTest( assert, caseItem, msg );
38 }
39 } );
40
41 QUnit.test( 'getFullData', ( assert ) => {
42 const cases = [
43 {
44 msg: 'Metadata in ContentBranchNode gets moved outside by any change',
45 beforeHtml: '<p>x</p><!-- w --><meta foo="x"><p>ab<meta foo="y">cd</p><p>ef<meta foo="z">gh</p>',
46 transaction: function ( doc ) {
47 return ve.dm.TransactionBuilder.static.newFromRemoval( doc, new ve.Range( 13, 14 ) );
48 },
49 afterHtml: '<p>x</p><!-- w --><meta foo="x"><p>abc</p><meta foo="y"><p>ef<meta foo="z">gh</p>'
50 },
51 {
52 msg: 'Metadata in ContentBranchNode is NOT removed when the whole node is removed',
53 beforeHtml: '<p>x</p><!-- w --><meta foo="x"><p>ab<meta foo="y">cd</p><p>ef<meta foo="z">gh</p>',
54 transaction: function ( doc ) {
55 return ve.dm.TransactionBuilder.static.newFromRemoval( doc, new ve.Range( 7, 15 ) );
56 },
57 afterHtml: '<p>x</p><!-- w --><meta foo="x"><meta foo="y"><p>ef<meta foo="z">gh</p>',
58 // BUG! <meta foo="y"> is not restored to the correct place after undoing
59 // EXPECTED: Same as beforeHtml
60 beforeHtmlUndo: '<p>x</p><!-- w --><meta foo="x"><p>abcd</p><meta foo="y"><p>ef<meta foo="z">gh</p>'
61 },
62 {
63 msg: 'Removable metadata (empty annotation) in ContentBranchNode is removed by any change',
64 beforeHtml: '<p>x</p><p>ab<i></i>cd</p><p>efgh</p>',
65 transaction: function ( doc ) {
66 return ve.dm.TransactionBuilder.static.newFromRemoval( doc, new ve.Range( 7, 8 ) );
67 },
68 afterHtml: '<p>x</p><p>abc</p><p>efgh</p>'
69 },
70 {
71 msg: 'Removable metadata (empty annotation) in ContentBranchNode is removed when the whole node is removed',
72 beforeHtml: '<p>x</p><p>ab<i></i>cd</p><p>efgh</p>',
73 transaction: function ( doc ) {
74 return ve.dm.TransactionBuilder.static.newFromRemoval( doc, new ve.Range( 3, 9 ) );
75 },
76 afterHtml: '<p>x</p><p>efgh</p>',
77 // BUG! <i></i> is not restored
78 // EXPECTED: Same as beforeHtml
79 beforeHtmlUndo: '<p>x</p><p>abcd</p><p>efgh</p>'
80 }
81 ];
82
83 cases.forEach( ( caseItem ) => {
84 const doc = ve.dm.converter.getModelFromDom( ve.createDocumentFromHtml( caseItem.beforeHtml ) ),
85 tx = caseItem.transaction( doc );
86
87 doc.commit( tx );
88 assert.strictEqual(
89 ve.dm.converter.getDomFromModel( doc ).body.innerHTML,
90 caseItem.afterHtml,
91 caseItem.msg
92 );
93
94 doc.commit( tx.reversed() );
95 assert.strictEqual(
96 ve.dm.converter.getDomFromModel( doc ).body.innerHTML,
97 caseItem.beforeHtmlUndo || caseItem.beforeHtml,
98 caseItem.msg + ' (undo)'
99 );
100 } );
101 } );
102
103 QUnit.test( 'roundTripMetadata', ( assert ) => {
104 const beforeHtml = '<!-- w --><meta foo="x"><p>ab<meta foo="y">cd</p><p>ef<meta foo="z">gh</p>',
105 afterHtml = '<!-- w --><meta foo="x"><p>abc</p><meta foo="y"><p>ef<meta foo="z">gh</p>';
106
107 const doc = ve.dm.converter.getModelFromDom( ve.createDocumentFromHtml( beforeHtml ) );
108 const tx = ve.dm.TransactionBuilder.static.newFromRemoval( doc, new ve.Range( 10, 11 ) );
109 doc.commit( tx );
110 assert.strictEqual(
111 ve.dm.converter.getDomFromModel( doc ).body.innerHTML,
112 afterHtml,
113 'Metadata in ContentBranchNode gets moved outside by change to ContentBranchNode'
114 );
115 doc.commit( tx.reversed() );
116 assert.strictEqual(
117 ve.dm.converter.getDomFromModel( doc ).body.innerHTML,
118 beforeHtml,
119 'Undo restores metadata to inside ContentBranchNode'
120 );
121 } );