/*! * VisualEditor ContentEditable Document tests. * * @copyright See AUTHORS.txt */ QUnit.module( 've.ce.Document' ); /* Tests */ QUnit.test( 'Converter tests', ( assert ) => { const cases = ve.dm.example.domToDataCases; for ( const msg in cases ) { if ( cases[ msg ].ceHtml ) { const caseItem = ve.copy( cases[ msg ] ); caseItem.base = caseItem.base || ve.dm.example.baseUri; const model = ve.test.utils.getModelFromTestCase( caseItem ); const view = new ve.ce.Document( model ); const $documentElement = view.getDocumentNode().$element; // Simplify slugs $documentElement.find( '.ve-ce-branchNode-slug' ).contents().remove(); assert.equalDomElement( // Wrap both in plain DIVs as we are only comparing the child nodes $( '
' ).append( $documentElement.contents() )[ 0 ], $( '
' ).html( caseItem.ceHtml )[ 0 ], msg ); } } } ); // TODO: getNodeFromOffset // TODO: getSlugAtOffset // TODO: getDirectionalityFromRange QUnit.test( 'getNodeAndOffset', ( assert ) => { /* eslint-disable quotes */ const docNodeStart = "
", pNodeStart = "

"; // Each test below has the following: // html: an input document // data: the expected DM content // positions: the node+offset corresponding to each DM offset, shown by marking pipe // characters on a modified HTML representation in which text nodes are wrapped in // <#text>… tags (and most attributes are omitted) // dies (optional): a list of DM offsets where getNodeAndOffset is expected to die const cases = [ { title: 'Simple para', html: '

x

', data: [ '', 'x', '' ], positions: docNodeStart + "|" + pNodeStart + "<#text>|x|

" }, { title: 'Bold', html: '

xyz

', data: [ '', ...'xyz', '' ], positions: docNodeStart + "|" + pNodeStart + "<#text>|x|<#text>y|<#text>z|

" }, { title: 'Nested block nodes', html: '

x

', data: [ '
', '', 'x', '', '
' ], positions: docNodeStart + "
|
|" + pNodeStart + "<#text>|x|

|
" }, { title: 'Empty document', html: '

', unwrap: [ { type: 'paragraph' } ], data: [], positions: "", dies: [ 1 ] }, { title: 'Slugless emptied paragraph', html: '

', replacement: { path: [ 0 ], innerHtml: '' }, data: [ '', '' ], positions: docNodeStart + "|" + pNodeStart + "|

" }, { title: 'Slugless emptied paragraph with raw
in DOM only', html: '

', replacement: { path: [ 0 ], innerHtml: '
' }, data: [ '', '' ], positions: docNodeStart + "|" + pNodeStart + "|

" }, { title: 'Paragraph with links', html: '

AB

', data: [ '', ...'AB', '' ], positions: docNodeStart + "|" + pNodeStart + "|<#text>A|<#text>B|

" }, { title: 'Paragraph with links, non-text nodes', html: '

A

', data: [ '', 'A', '', '', '' ], positions: docNodeStart + "|" + pNodeStart + "|<#text>A|||

" }, { title: 'About grouped aliens', html: "

FooBar

", data: [ '', '', '', '' ], positions: docNodeStart + "|" + pNodeStart + "|<#text>|Foo<#text>Bar|

" }, { title: 'Non-about grouped aliens', html: "

FooBar

", data: [ '', '', '', '', '', '' ], positions: docNodeStart + "|" + pNodeStart + "|<#text>|Foo|<#text>|Bar|

" }, { title: 'Meta outside of CBN', html: "

X

Y

", data: [ '', 'X', '', '', '', '', 'Y', '' ], positions: docNodeStart + "|" + pNodeStart + "<#text>|X|

|||" + pNodeStart + "<#text>|Y|

" }, { title: 'Meta does not cause double block slug', html: "

X

", data: [ '', '', '', '' ], positions: docNodeStart + "
|

|<#text>X

||" } ]; /* eslint-enable quotes */ function showModelItem( item ) { if ( item.type ) { return '<' + item.type + '>'; } else if ( Array.isArray( item ) ) { return item[ 0 ]; } else if ( typeof item === 'string' ) { return item; } else { return '(unexpected: ' + item + ')'; } } cases.forEach( ( caseItem ) => { const parts = caseItem.positions.split( /[|]/ ); const view = ve.test.utils.createSurfaceViewFromHtml( caseItem.html ); const dmDoc = view.getModel().getDocument(); if ( caseItem.unwrap ) { new ve.dm.Surface( dmDoc ).change( ve.dm.TransactionBuilder.static.newFromWrap( dmDoc, new ve.Range( 0, dmDoc.data.countNonInternalElements() ), [], [], caseItem.unwrap, [] ) ); } const data = dmDoc.data.data .slice( 0, -2 ) .map( showModelItem ); const ceDoc = view.documentView; const rootNode = ceDoc.getDocumentNode().$element.get( 0 ); assert.deepEqual( data, caseItem.data, caseItem.title + ' (data)' ); const offsetCount = data.length; assert.strictEqual( offsetCount, caseItem.positions.replace( /[^|]/g, '' ).length, caseItem.title + ' (offset count)' ); if ( caseItem.replacement ) { let node = rootNode; for ( let j = 0, jLen = caseItem.replacement.path.length; j < jLen; j++ ) { node = node.childNodes[ caseItem.replacement.path[ j ] ]; } $( node ).closest( '.ve-ce-branchNode,.ve-ce-leafNode' ).data( 'view' ).removeSlugs(); node.innerHTML = caseItem.replacement.innerHtml; } for ( let offset = 0; offset < offsetCount; offset++ ) { assert.strictEqual( ve.test.utils.serializePosition( rootNode, ceDoc.getNodeAndOffset( offset ), { ignore: '.ve-ce-branchNode-slug>*' } ), [].concat( parts.slice( 0, offset + 1 ), [ '|' ], parts.slice( offset + 1 ) ).join( '' ), caseItem.title + ' (' + offset + ')' ); } for ( let k = 0; caseItem.dies && k < caseItem.dies.length; k++ ) { const dieOffset = caseItem.dies[ k ]; let ex = null; try { ceDoc.getNodeAndOffset( dieOffset ); } catch ( e ) { ex = e; } assert.notStrictEqual( ex, null, caseItem.title + ' (' + dieOffset + ') dies' ); } view.destroy(); } ); } ); QUnit.test( 'attachedRoot', ( assert ) => { const dmDoc = ve.dm.converter.getModelFromDom( ve.createDocumentFromHtml( '
Foo
Bar
Baz
' ) ), attachedRoot = dmDoc.getDocumentNode().children[ 1 ], surfaceModel = new ve.dm.Surface( dmDoc, attachedRoot ), surfaceView = ve.test.utils.createSurfaceViewFromDocument( surfaceModel ); assert.deepEqual( surfaceView.getDocument().getDocumentNode().children.map( ( node ) => node.type ), [ 'unrendered', 'section', 'unrendered', 'unrendered' ], 'Only attached root is rendered' ); } );