/*! * VisualEditor ContentEditable TextState tests. * * @copyright See AUTHORS.txt */ QUnit.module( 've.ce.TextState' ); /* Tests */ QUnit.test( 'getChangeTransaction', ( assert ) => { const underlineHash = ve.dm.example.underlineHash, boldHash = ve.dm.example.boldHash, annHash = ve.dm.example.annHash; const cases = [ { msg: 'Clear bold', oldRawHtml: '

foo bar baz

', oldInnerHtml: 'foo bar baz', newInnerHtml: 'foo bar baz', operations: [ { type: 'retain', length: 5 }, { type: 'replace', remove: [ [ 'b', [ annHash( 'b' ) ] ], [ 'a', [ annHash( 'b' ) ] ], [ 'r', [ annHash( 'b' ) ] ] ], insert: [ ...'bar' ] }, { type: 'retain', length: 7 } ] }, { msg: 'Extend bold', oldRawHtml: '

foo ba baz

', oldInnerHtml: 'foo ba baz', newInnerHtml: 'foo bar baz', operations: [ { type: 'retain', length: 7 }, { type: 'replace', remove: [], insert: [ [ 'r', [ annHash( 'b' ) ] ] ] }, { type: 'retain', length: 7 } ] }, { msg: 'Set bold', oldRawHtml: '

foo bar baz

', oldInnerHtml: 'foo bar baz', newInnerHtml: 'foo bar baz', operations: [ { type: 'retain', length: 5 }, { type: 'replace', remove: [ ...'bar' ], insert: [ [ 'b', [ boldHash ] ], [ 'a', [ boldHash ] ], [ 'r', [ boldHash ] ] ] }, { type: 'retain', length: 7 } ] }, { msg: 'Insert at start of bold', oldRawHtml: 'wxy', oldInnerHtml: 'wxy', newInnerHtml: 'wxzy', operations: [ { type: 'retain', length: 3 }, { type: 'replace', remove: [], insert: [ [ 'z', [ annHash( 'b' ) ] ] ] }, { type: 'retain', length: 4 } ] }, { msg: 'Insert before start of bold', oldRawHtml: 'wxy', oldInnerHtml: 'wxy', newInnerHtml: 'wxzy', operations: [ { type: 'retain', length: 3 }, { type: 'replace', remove: [], insert: [ 'z' ] }, { type: 'retain', length: 4 } ] }, { msg: 'Insert into insertion annotation', willFail: true, oldRawHtml: 'wxz', oldInnerHtml: 'wxz', newInnerHtml: 'wxyz', operations: [ { type: 'retain', length: 3 }, { type: 'replace', remove: [], insert: [ [ 'y', [ 'anything' ] ] ] }, { type: 'retain', length: 4 } ] }, { msg: 'Set bold underline before underline', oldRawHtml: 'foo barbaz', oldInnerHtml: 'foo barbaz', newInnerHtml: 'foo barbaz', operations: [ { type: 'retain', length: 5 }, { type: 'replace', remove: [ ...'bar' ], insert: [ [ 'b', [ annHash( 'u' ), boldHash ] ], [ 'a', [ annHash( 'u' ), boldHash ] ], [ 'r', [ annHash( 'u' ), boldHash ] ] ] }, { type: 'retain', length: 6 } ] }, { msg: 'Clear bold in italic', oldRawHtml: '

foo bar baz

', oldInnerHtml: 'foo bar baz', newInnerHtml: 'foo bar baz', operations: [ { type: 'retain', length: 5 }, { type: 'replace', remove: [ [ 'b', [ annHash( 'i' ), annHash( 'b' ) ] ], [ 'a', [ annHash( 'i' ), annHash( 'b' ) ] ], [ 'r', [ annHash( 'i' ), annHash( 'b' ) ] ] ], insert: [ [ 'b', [ annHash( 'i' ) ] ], [ 'a', [ annHash( 'i' ) ] ], [ 'r', [ annHash( 'i' ) ] ] ] }, { type: 'retain', length: 7 } ] }, { msg: 'Extend bold in italic', oldRawHtml: '

foo ba baz

', oldInnerHtml: 'foo ba baz', newInnerHtml: 'foo bar baz', operations: [ { type: 'retain', length: 7 }, { type: 'replace', remove: [], insert: [ [ 'r', [ annHash( 'i' ), annHash( 'b' ) ] ] ] }, { type: 'retain', length: 7 } ] }, { msg: 'Set bold in italic', oldRawHtml: '

foo bar baz

', oldInnerHtml: 'foo bar baz', newInnerHtml: 'foo bar baz', operations: [ { type: 'retain', length: 5 }, { type: 'replace', remove: [ [ 'b', [ annHash( 'i' ) ] ], [ 'a', [ annHash( 'i' ) ] ], [ 'r', [ annHash( 'i' ) ] ] ], insert: [ [ 'b', [ annHash( 'i' ), boldHash ] ], [ 'a', [ annHash( 'i' ), boldHash ] ], [ 'r', [ annHash( 'i' ), boldHash ] ] ] }, { type: 'retain', length: 7 } ] }, { msg: 'Insert at start of bold in italic', oldRawHtml: 'wxy', oldInnerHtml: 'wxy', newInnerHtml: 'wxzy', operations: [ { type: 'retain', length: 3 }, { type: 'replace', remove: [], insert: [ [ 'z', [ annHash( 'i' ), annHash( 'b' ) ] ] ] }, { type: 'retain', length: 4 } ] }, { msg: 'Insert before start of bold in italic', oldRawHtml: 'wxy', oldInnerHtml: 'wxy', newInnerHtml: 'wxzy', operations: [ { type: 'retain', length: 3 }, { type: 'replace', remove: [], insert: [ [ 'z', [ annHash( 'i' ) ] ] ] }, { type: 'retain', length: 4 } ] }, { msg: 'Insert into insertion annotation in italic', willFail: true, oldRawHtml: 'wxz', oldInnerHtml: 'wxz', newInnerHtml: 'wxyz', operations: [ { type: 'retain', length: 3 }, { type: 'replace', remove: [], insert: [ [ 'y', [ 'anything1', 'anything2' ] ] ] }, { type: 'retain', length: 4 } ] }, { msg: 'Set bold underline before underline in italic', oldRawHtml: 'foo barbaz', oldInnerHtml: 'foo barbaz', newInnerHtml: 'foo barbaz', operations: [ { type: 'retain', length: 5 }, { type: 'replace', remove: [ [ 'b', [ annHash( 'i' ) ] ], [ 'a', [ annHash( 'i' ) ] ], [ 'r', [ annHash( 'i' ) ] ] ], insert: [ [ 'b', [ annHash( 'i' ), annHash( 'u' ), boldHash ] ], [ 'a', [ annHash( 'i' ), annHash( 'u' ), boldHash ] ], [ 'r', [ annHash( 'i' ), annHash( 'u' ), boldHash ] ] ] }, { type: 'retain', length: 6 } ] }, { msg: 'Set bold with distant existing bold tag', oldRawHtml: 'foo bar baz', oldInnerHtml: 'foo bar baz', newInnerHtml: 'foo bar baz', operations: [ { type: 'retain', length: 9 }, { type: 'replace', remove: [ ...'baz' ], // TODO: Reuse bold instead of creating a new bold? // (Some annotation types may need specific rules as to // when this can be done) insert: [ [ 'b', [ boldHash ] ], [ 'a', [ boldHash ] ], [ 'z', [ boldHash ] ] ] }, { type: 'retain', length: 3 } ] }, { msg: 'Set underline across bold close', oldRawHtml: 'abcde', oldInnerHtml: 'abcde', newInnerHtml: 'abcde', operations: [ { type: 'retain', length: 3 }, { type: 'replace', remove: [ [ 'c', [ annHash( 'i' ), annHash( 'b' ) ] ], [ 'd', [ annHash( 'i' ) ] ] ], insert: [ [ 'c', [ annHash( 'i' ), annHash( 'b' ), underlineHash ] ], [ 'd', [ annHash( 'i' ), underlineHash ] ] ] }, { type: 'retain', length: 4 } ] }, { msg: 'Set bold in one place and normal in another', oldRawHtml: 'foo bar baz', oldInnerHtml: 'foo bar baz', newInnerHtml: 'foo bar baz', operations: [ { type: 'retain', length: 5 }, { type: 'replace', // This weird-looking removal is the correct output for // a diff algorithm that matches common start/end items // then replaces the entire interior. In real life usage // there won't usually be two separate changed regions. remove: [ [ 'b', [ annHash( 'i' ) ] ], [ 'a', [ annHash( 'i' ) ] ], [ 'r', [ annHash( 'i' ) ] ], ...' baz' ], // The first insertion get insert: [ ...'bar ', [ 'b', [ annHash( 'b' ) ] ], [ 'a', [ annHash( 'b' ) ] ], [ 'z', [ annHash( 'b' ) ] ] ] }, { type: 'retain', length: 3 } ] }, { msg: 'Insert new chunk whose annotations match end chunk\'s', oldRawHtml: 'xyz', oldInnerHtml: 'xyz', newInnerHtml: 'xywyz', operations: [ { type: 'retain', length: 2 }, { type: 'replace', remove: [], insert: [ 'y', [ 'w', [ annHash( 'u' ) ] ] ] }, { type: 'retain', length: 5 } ] }, { msg: 'Ambiguous insert with start and end both identical to original', oldRawHtml: 'ab', oldInnerHtml: 'ab', newInnerHtml: 'abxab', operations: [ { type: 'retain', length: 3 }, { type: 'replace', remove: [], insert: [ [ 'x', [ underlineHash ] ], 'a', 'b' ] }, { type: 'retain', length: 3 } ] } ]; cases.forEach( ( caseItem ) => { const view = ve.test.utils.createSurfaceViewFromHtml( caseItem.oldRawHtml ); const documentView = view.getDocument(); const documentNode = documentView.getDocumentNode(); const contentNode = documentNode.children[ 0 ]; ( caseItem.willFail ? assert.notEqualDomElement : assert.equalDomElement ).call( assert, $( '
' ).append( contentNode.$element.clone().contents() )[ 0 ], $( '
' ).html( caseItem.oldInnerHtml )[ 0 ], caseItem.msg + ' (oldInnerHtml)' ); view.model.setSelection( new ve.dm.LinearSelection( new ve.Range( 1 ) ) ); const oldState = new ve.ce.RangeState( null, documentNode, false ); contentNode.$element.html( caseItem.newInnerHtml ); view.model.setSelection( new ve.dm.LinearSelection( new ve.Range( 1 ) ) ); const newState = new ve.ce.RangeState( oldState, documentNode, false ); const change = newState.textState.getChangeTransaction( oldState.textState, view.model.getDocument(), newState.node.getOffset() ); ( caseItem.willFail ? assert.notDeepEqual : assert.deepEqual ).call( assert, change.operations, caseItem.operations, caseItem.msg + ' (operations)' ); view.destroy(); } ); } );