| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
QUnit.module( 've.dm.MetaList' ); |
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
ve.test.utils.validateMetaListCache = function ( assert, metaList, msg ) { |
| 19 |
const oldList = metaList.getItems(); |
| 20 |
const newList = []; |
| 21 |
|
| 22 |
metaList.doc.documentNode.traverse( ( node ) => { |
| 23 |
if ( node instanceof ve.dm.MetaItem ) { |
| 24 |
newList.push( node ); |
| 25 |
} |
| 26 |
} ); |
| 27 |
let match = true; |
| 28 |
for ( let i = 0, len = newList.length; i < len; i++ ) { |
| 29 |
if ( newList[ i ] !== oldList[ i ] ) { |
| 30 |
match = false; |
| 31 |
break; |
| 32 |
} |
| 33 |
} |
| 34 |
assert.true( match, msg ); |
| 35 |
}; |
| 36 |
|
| 37 |
QUnit.test( 'constructor/getItems/getItemsInGroup/indexOf', ( assert ) => { |
| 38 |
const doc = ve.dm.example.createExampleDocument( 'withMeta' ), |
| 39 |
list = doc.getMetaList(); |
| 40 |
|
| 41 |
ve.test.utils.validateMetaListCache( assert, list, 'Constructor' ); |
| 42 |
|
| 43 |
assert.strictEqual( list.getItems().length, 9, 'getItems returns 9 items' ); |
| 44 |
assert.strictEqual( list.getItemsInGroup( 'misc' ).length, 9, '`misc` group has 9 items' ); |
| 45 |
assert.strictEqual( list.getItemsInGroup( 'foo' ).length, 0, '`foo` group has 0 items' ); |
| 46 |
assert.strictEqual( list.indexOf( list.items[ 3 ] ), 3, 'item found by indexOf' ); |
| 47 |
assert.strictEqual( list.indexOf( list.items[ 3 ], 'misc' ), 3, 'item found in group by indexOf' ); |
| 48 |
assert.strictEqual( list.indexOf( list.items[ 3 ], 'foo' ), -1, 'item not found in group by indexOf' ); |
| 49 |
} ); |
| 50 |
|
| 51 |
QUnit.test( 'onNodeAttached/onNodeDetached', ( assert ) => { |
| 52 |
const doc = ve.dm.example.createExampleDocument( 'withMeta' ), |
| 53 |
heading = { type: 'heading', attributes: { level: 2 } }, |
| 54 |
cases = [ |
| 55 |
{ |
| 56 |
|
| 57 |
calls: [ |
| 58 |
[ 'pushRetain', 5 ], |
| 59 |
[ 'pushReplacement', doc, 5, 0, [ ...'Quux' ] ], |
| 60 |
[ 'pushRetain', 3 ], |
| 61 |
[ 'pushReplacement', doc, 8, 7, [ '!' ] ] |
| 62 |
], |
| 63 |
msg: 'Transaction inserting, replacing and removing text' |
| 64 |
}, |
| 65 |
{ |
| 66 |
|
| 67 |
calls: [ |
| 68 |
[ 'pushReplacement', doc, 0, 5, [ heading ] ], |
| 69 |
[ 'pushRetain', 3 ], |
| 70 |
[ 'pushReplacement', doc, 8, 3, [ { type: '/heading' } ] ] |
| 71 |
], |
| 72 |
msg: 'Transaction converting paragraph to heading' |
| 73 |
}, |
| 74 |
{ |
| 75 |
|
| 76 |
calls: [ |
| 77 |
[ 'pushRetain', 5 ], |
| 78 |
[ 'pushReplacement', doc, 5, 3, [] ], |
| 79 |
[ 'pushRetain', 1 ] |
| 80 |
], |
| 81 |
msg: 'Transaction blanking paragraph' |
| 82 |
} |
| 83 |
]; |
| 84 |
|
| 85 |
cases.forEach( ( caseItem ) => { |
| 86 |
const txBuilder = new ve.dm.TransactionBuilder(); |
| 87 |
for ( let j = 0; j < caseItem.calls.length; j++ ) { |
| 88 |
txBuilder[ caseItem.calls[ j ][ 0 ] ].apply( txBuilder, caseItem.calls[ j ].slice( 1 ) ); |
| 89 |
} |
| 90 |
const tx = txBuilder.getTransaction(); |
| 91 |
const caseDoc = ve.dm.example.createExampleDocument( 'withMeta' ); |
| 92 |
const list = caseDoc.getMetaList(); |
| 93 |
const surface = new ve.dm.Surface( caseDoc ); |
| 94 |
|
| 95 |
surface.change( tx ); |
| 96 |
ve.test.utils.validateMetaListCache( assert, list, caseItem.msg ); |
| 97 |
surface.change( tx.reversed() ); |
| 98 |
ve.test.utils.validateMetaListCache( assert, list, caseItem.msg + ' (rollback)' ); |
| 99 |
} ); |
| 100 |
} ); |