| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180 |
- /**
- * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
- * For licensing, see LICENSE.md.
- */
- import VirtualTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/virtualtesteditor';
- import { getData as getViewData } from '@ckeditor/ckeditor5-engine/src/dev-utils/view';
- import { setData as setModelData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
- import {
- downcastAttributeChange,
- downcastInsertCell,
- downcastInsertRow,
- downcastInsertTable,
- downcastRemoveRow
- } from '../../src/converters/downcast';
- import { formatTable, formattedViewTable, modelTable, viewTable } from '../_utils/utils';
- describe( 'downcast converters', () => {
- let editor, model, doc, root, viewDocument;
- beforeEach( () => {
- return VirtualTestEditor.create()
- .then( newEditor => {
- editor = newEditor;
- model = editor.model;
- doc = model.document;
- root = doc.getRoot( 'main' );
- viewDocument = editor.editing.view;
- const conversion = editor.conversion;
- const schema = model.schema;
- schema.register( 'table', {
- allowWhere: '$block',
- allowAttributes: [ 'headingRows', 'headingColumns' ],
- isBlock: true,
- isObject: true
- } );
- schema.register( 'tableRow', {
- allowIn: 'table',
- allowAttributes: [],
- isBlock: true,
- isLimit: true
- } );
- schema.register( 'tableCell', {
- allowIn: 'tableRow',
- allowContentOf: '$block',
- allowAttributes: [ 'colspan', 'rowspan' ],
- isBlock: true,
- isLimit: true
- } );
- conversion.for( 'downcast' ).add( downcastInsertTable() );
- conversion.attributeToAttribute( { model: 'colspan', view: 'colspan' } );
- conversion.attributeToAttribute( { model: 'rowspan', view: 'rowspan' } );
- // Insert conversion
- conversion.for( 'downcast' ).add( downcastInsertRow() );
- conversion.for( 'downcast' ).add( downcastInsertCell() );
- conversion.for( 'downcast' ).add( downcastRemoveRow() );
- conversion.for( 'downcast' ).add( downcastAttributeChange( { attribute: 'headingRows' } ) );
- conversion.for( 'downcast' ).add( downcastAttributeChange( { attribute: 'headingColumns' } ) );
- } );
- } );
- describe( 'downcastInsertTable()', () => {
- it( 'should create table with tbody', () => {
- setModelData( model,
- '<table>' +
- '<tableRow><tableCell></tableCell></tableRow>' +
- '</table>'
- );
- expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal(
- '<table>' +
- '<tbody>' +
- '<tr><td></td></tr>' +
- '</tbody>' +
- '</table>'
- );
- } );
- it( 'should create table with tbody and thead', () => {
- setModelData( model,
- '<table headingRows="1">' +
- '<tableRow><tableCell>1</tableCell></tableRow>' +
- '<tableRow><tableCell>2</tableCell></tableRow>' +
- '</table>'
- );
- expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal(
- '<table>' +
- '<thead>' +
- '<tr><th>1</th></tr>' +
- '</thead>' +
- '<tbody>' +
- '<tr><td>2</td></tr>' +
- '</tbody>' +
- '</table>'
- );
- } );
- it( 'should create table with thead', () => {
- setModelData( model,
- '<table headingRows="2">' +
- '<tableRow><tableCell>1</tableCell></tableRow>' +
- '<tableRow><tableCell>2</tableCell></tableRow>' +
- '</table>'
- );
- expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal(
- '<table>' +
- '<thead>' +
- '<tr><th>1</th></tr>' +
- '<tr><th>2</th></tr>' +
- '</thead>' +
- '</table>'
- );
- } );
- it( 'should create table with heading columns and rows', () => {
- setModelData( model,
- '<table headingColumns="3" headingRows="1">' +
- '<tableRow>' +
- '<tableCell>11</tableCell><tableCell>12</tableCell><tableCell>13</tableCell><tableCell>14</tableCell>' +
- '</tableRow>' +
- '<tableRow>' +
- '<tableCell>21</tableCell><tableCell>22</tableCell><tableCell>23</tableCell><tableCell>24</tableCell>' +
- '</tableRow>' +
- '</table>'
- );
- expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal(
- '<table>' +
- '<thead>' +
- '<tr><th>11</th><th>12</th><th>13</th><th>14</th></tr>' +
- '</thead>' +
- '<tbody>' +
- '<tr><th>21</th><th>22</th><th>23</th><td>24</td></tr>' +
- '</tbody>' +
- '</table>'
- );
- } );
- it( 'should be possible to overwrite', () => {
- editor.conversion.elementToElement( { model: 'tableRow', view: 'tr', converterPriority: 'high' } );
- editor.conversion.elementToElement( { model: 'tableCell', view: 'td', converterPriority: 'high' } );
- editor.conversion.for( 'downcast' ).add( dispatcher => {
- dispatcher.on( 'insert:table', ( evt, data, conversionApi ) => {
- conversionApi.consumable.consume( data.item, 'insert' );
- const tableElement = conversionApi.writer.createContainerElement( 'table', { foo: 'bar' } );
- const viewPosition = conversionApi.mapper.toViewPosition( data.range.start );
- conversionApi.mapper.bindElements( data.item, tableElement );
- conversionApi.writer.insert( viewPosition, tableElement );
- }, { priority: 'high' } );
- } );
- setModelData( model,
- '<table>' +
- '<tableRow><tableCell></tableCell></tableRow>' +
- '</table>'
- );
- expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal(
- '<table foo="bar">' +
- '<tr><td></td></tr>' +
- '</table>'
- );
- } );
- describe( 'headingColumns attribute', () => {
- it( 'should mark heading columns table cells', () => {
- setModelData( model,
- '<table headingColumns="2">' +
- '<tableRow><tableCell>11</tableCell><tableCell>12</tableCell><tableCell>13</tableCell></tableRow>' +
- '<tableRow><tableCell>21</tableCell><tableCell>22</tableCell><tableCell>23</tableCell></tableRow>' +
- '</table>'
- );
- expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal(
- '<table>' +
- '<tbody>' +
- '<tr><th>11</th><th>12</th><td>13</td></tr>' +
- '<tr><th>21</th><th>22</th><td>23</td></tr>' +
- '</tbody>' +
- '</table>'
- );
- } );
- it( 'should mark heading columns table cells when one has colspan attribute', () => {
- setModelData( model,
- '<table headingColumns="3">' +
- '<tableRow>' +
- '<tableCell>11</tableCell><tableCell>12</tableCell><tableCell>13</tableCell><tableCell>14</tableCell>' +
- '</tableRow>' +
- '<tableRow><tableCell colspan="2">21</tableCell><tableCell>23</tableCell><tableCell>24</tableCell></tableRow>' +
- '</table>'
- );
- expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal(
- '<table>' +
- '<tbody>' +
- '<tr><th>11</th><th>12</th><th>13</th><td>14</td></tr>' +
- '<tr><th colspan="2">21</th><th>23</th><td>24</td></tr>' +
- '</tbody>' +
- '</table>'
- );
- } );
- it( 'should work with colspan and rowspan attributes on table cells', () => {
- // The table in this test looks like a table below:
- //
- // Row headings | Normal cells
- // |
- // +----+----+----+----+
- // | 11 | 12 | 13 | 14 |
- // | +----+ +----+
- // | | 22 | | 24 |
- // |----+----+ +----+
- // | 31 | | 34 |
- // | +----+----+
- // | | 43 | 44 |
- // +----+----+----+----+
- setModelData( model,
- '<table headingColumns="3">' +
- '<tableRow>' +
- '<tableCell rowspan="2">11</tableCell>' +
- '<tableCell>12</tableCell>' +
- '<tableCell rowspan="3">13</tableCell>' +
- '<tableCell>14</tableCell>' +
- '</tableRow>' +
- '<tableRow><tableCell>22</tableCell><tableCell>24</tableCell></tableRow>' +
- '<tableRow><tableCell colspan="2" rowspan="2">31</tableCell><tableCell>34</tableCell></tableRow>' +
- '<tableRow><tableCell>43</tableCell><tableCell>44</tableCell></tableRow>' +
- '</table>'
- );
- expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal(
- '<table>' +
- '<tbody>' +
- '<tr><th rowspan="2">11</th><th>12</th><th rowspan="3">13</th><td>14</td></tr>' +
- '<tr><th>22</th><td>24</td></tr>' +
- '<tr><th colspan="2" rowspan="2">31</th><td>34</td></tr>' +
- '<tr><th>43</th><td>44</td></tr>' +
- '</tbody>' +
- '</table>'
- );
- } );
- } );
- describe( 'asWidget', () => {
- beforeEach( () => {
- return VirtualTestEditor.create()
- .then( newEditor => {
- editor = newEditor;
- model = editor.model;
- doc = model.document;
- root = doc.getRoot( 'main' );
- viewDocument = editor.editing.view;
- const conversion = editor.conversion;
- const schema = model.schema;
- schema.register( 'table', {
- allowWhere: '$block',
- allowAttributes: [ 'headingRows', 'headingColumns' ],
- isBlock: true,
- isObject: true
- } );
- schema.register( 'tableRow', {
- allowIn: 'table',
- allowAttributes: [],
- isBlock: true,
- isLimit: true
- } );
- schema.register( 'tableCell', {
- allowIn: 'tableRow',
- allowContentOf: '$block',
- allowAttributes: [ 'colspan', 'rowspan' ],
- isBlock: true,
- isLimit: true
- } );
- conversion.for( 'downcast' ).add( downcastInsertTable( { asWidget: true } ) );
- conversion.for( 'downcast' ).add( downcastInsertRow( { asWidget: true } ) );
- conversion.for( 'downcast' ).add( downcastInsertCell( { asWidget: true } ) );
- conversion.attributeToAttribute( { model: 'colspan', view: 'colspan' } );
- conversion.attributeToAttribute( { model: 'rowspan', view: 'rowspan' } );
- } );
- } );
- it( 'should create table as a widget', () => {
- setModelData( model,
- '<table>' +
- '<tableRow><tableCell></tableCell></tableRow>' +
- '</table>'
- );
- expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal(
- '<table class="ck-widget" contenteditable="false">' +
- '<tbody>' +
- '<tr><td class="ck-editor__editable ck-editor__nested-editable" contenteditable="true"></td></tr>' +
- '</tbody>' +
- '</table>'
- );
- } );
- } );
- } );
- describe( 'downcastInsertRow()', () => {
- it( 'should react to changed rows', () => {
- setModelData( model, modelTable( [
- [ '11', '12' ]
- ] ) );
- const table = root.getChild( 0 );
- model.change( writer => {
- const row = writer.createElement( 'tableRow' );
- writer.insert( row, table, 1 );
- writer.insertElement( 'tableCell', row, 'end' );
- writer.insertElement( 'tableCell', row, 'end' );
- } );
- expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal( viewTable( [
- [ '11', '12' ],
- [ '', '' ]
- ] ) );
- } );
- it( 'should properly consume already added rows', () => {
- setModelData( model, modelTable( [
- [ '11', '12' ]
- ] ) );
- const table = root.getChild( 0 );
- model.change( writer => {
- const row = writer.createElement( 'tableRow' );
- writer.insert( row, table, 1 );
- writer.insertElement( 'tableCell', row, 'end' );
- writer.insertElement( 'tableCell', row, 'end' );
- } );
- expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal( viewTable( [
- [ '11', '12' ],
- [ '', '' ]
- ] ) );
- model.change( writer => {
- const row = writer.createElement( 'tableRow' );
- writer.insert( row, table, 2 );
- writer.insertElement( 'tableCell', row, 'end' );
- writer.insertElement( 'tableCell', row, 'end' );
- } );
- expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal( viewTable( [
- [ '11', '12' ],
- [ '', '' ],
- [ '', '' ]
- ] ) );
- } );
- it( 'should insert row on proper index', () => {
- setModelData( model, modelTable( [
- [ '11', '12' ],
- [ '21', '22' ],
- [ '31', '32' ]
- ] ) );
- const table = root.getChild( 0 );
- model.change( writer => {
- const row = writer.createElement( 'tableRow' );
- writer.insert( row, table, 1 );
- writer.insertElement( 'tableCell', row, 'end' );
- writer.insertElement( 'tableCell', row, 'end' );
- } );
- expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal( viewTable( [
- [ '11', '12' ],
- [ '', '' ],
- [ '21', '22' ],
- [ '31', '32' ]
- ] ) );
- } );
- it( 'should insert row on proper index when table has heading rows defined - insert in body', () => {
- setModelData( model, modelTable( [
- [ '11', '12' ],
- [ '21', '22' ],
- [ '31', '32' ]
- ], { headingRows: 1 } ) );
- const table = root.getChild( 0 );
- model.change( writer => {
- const row = writer.createElement( 'tableRow' );
- writer.insert( row, table, 1 );
- writer.insertElement( 'tableCell', row, 'end' );
- writer.insertElement( 'tableCell', row, 'end' );
- } );
- expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal( viewTable( [
- [ '11', '12' ],
- [ '', '' ],
- [ '21', '22' ],
- [ '31', '32' ]
- ], { headingRows: 1 } ) );
- } );
- it( 'should insert row on proper index when table has heading rows defined - insert in heading', () => {
- setModelData( model, modelTable( [
- [ '11', '12' ],
- [ '21', '22' ],
- [ '31', '32' ]
- ], { headingRows: 2 } ) );
- const table = root.getChild( 0 );
- model.change( writer => {
- const row = writer.createElement( 'tableRow' );
- writer.insert( row, table, 1 );
- writer.insertElement( 'tableCell', row, 'end' );
- writer.insertElement( 'tableCell', row, 'end' );
- } );
- expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal( viewTable( [
- [ '11', '12' ],
- [ '', '' ],
- [ '21', '22' ],
- [ '31', '32' ]
- ], { headingRows: 3 } ) );
- } );
- it( 'should react to changed rows when previous rows\' cells has rowspans', () => {
- setModelData( model, modelTable( [
- [ { rowspan: 3, contents: '11' }, '12' ],
- [ '22' ]
- ] ) );
- const table = root.getChild( 0 );
- model.change( writer => {
- const row = writer.createElement( 'tableRow' );
- writer.insert( row, table, 2 );
- writer.insertElement( 'tableCell', row, 'end' );
- } );
- expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal( viewTable( [
- [ { rowspan: 3, contents: '11' }, '12' ],
- [ '22' ],
- [ '' ]
- ] ) );
- } );
- it( 'should properly create row headings', () => {
- setModelData( model, modelTable( [
- [ { rowspan: 3, contents: '11' }, '12' ],
- [ '22' ]
- ], { headingColumns: 1 } ) );
- const table = root.getChild( 0 );
- model.change( writer => {
- const firstRow = writer.createElement( 'tableRow' );
- writer.insert( firstRow, table, 2 );
- writer.insert( writer.createElement( 'tableCell' ), firstRow, 'end' );
- const secondRow = writer.createElement( 'tableRow' );
- writer.insert( secondRow, table, 3 );
- writer.insert( writer.createElement( 'tableCell' ), secondRow, 'end' );
- writer.insert( writer.createElement( 'tableCell' ), secondRow, 'end' );
- } );
- expect( formatTable( getViewData( viewDocument, { withoutSelection: true } ) ) ).to.equal( formattedViewTable( [
- [ { rowspan: 3, contents: '11', isHeading: true }, '12' ],
- [ '22' ],
- [ '' ],
- [ { contents: '', isHeading: true }, '' ]
- ] ) );
- } );
- describe( 'asWidget', () => {
- beforeEach( () => {
- return VirtualTestEditor.create()
- .then( newEditor => {
- editor = newEditor;
- model = editor.model;
- doc = model.document;
- root = doc.getRoot( 'main' );
- viewDocument = editor.editing.view;
- const conversion = editor.conversion;
- const schema = model.schema;
- schema.register( 'table', {
- allowWhere: '$block',
- allowAttributes: [ 'headingRows', 'headingColumns' ],
- isBlock: true,
- isObject: true
- } );
- schema.register( 'tableRow', {
- allowIn: 'table',
- allowAttributes: [],
- isBlock: true,
- isLimit: true
- } );
- schema.register( 'tableCell', {
- allowIn: 'tableRow',
- allowContentOf: '$block',
- allowAttributes: [ 'colspan', 'rowspan' ],
- isBlock: true,
- isLimit: true
- } );
- conversion.for( 'downcast' ).add( downcastInsertTable( { asWidget: true } ) );
- conversion.for( 'downcast' ).add( downcastInsertRow( { asWidget: true } ) );
- conversion.for( 'downcast' ).add( downcastInsertCell( { asWidget: true } ) );
- conversion.attributeToAttribute( { model: 'colspan', view: 'colspan' } );
- conversion.attributeToAttribute( { model: 'rowspan', view: 'rowspan' } );
- } );
- } );
- it( 'should create table cell inside inserted row as a widget', () => {
- setModelData( model,
- '<table>' +
- '<tableRow><tableCell>foo</tableCell></tableRow>' +
- '</table>'
- );
- const table = root.getChild( 0 );
- model.change( writer => {
- const firstRow = writer.createElement( 'tableRow' );
- writer.insert( firstRow, table, 1 );
- writer.insert( writer.createElement( 'tableCell' ), firstRow, 'end' );
- } );
- expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal(
- '<table class="ck-widget" contenteditable="false">' +
- '<tbody>' +
- '<tr><td class="ck-editor__editable ck-editor__nested-editable" contenteditable="true">foo</td></tr>' +
- '<tr><td class="ck-editor__editable ck-editor__nested-editable" contenteditable="true"></td></tr>' +
- '</tbody>' +
- '</table>'
- );
- } );
- } );
- } );
- describe( 'downcastInsertCell()', () => {
- it( 'should add tableCell on proper index in tr', () => {
- setModelData( model, modelTable( [
- [ '11', '12' ]
- ] ) );
- const table = root.getChild( 0 );
- model.change( writer => {
- const row = table.getChild( 0 );
- writer.insertElement( 'tableCell', row, 1 );
- } );
- expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal( viewTable( [
- [ '11', '', '12' ]
- ] ) );
- } );
- it( 'should add tableCell on proper index in tr when previous have colspans', () => {
- setModelData( model, modelTable( [
- [ { colspan: 2, contents: '11' }, '13' ]
- ] ) );
- const table = root.getChild( 0 );
- model.change( writer => {
- const row = table.getChild( 0 );
- writer.insertElement( 'tableCell', row, 1 );
- } );
- expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal( viewTable( [
- [ { colspan: 2, contents: '11' }, '', '13' ]
- ] ) );
- } );
- it( 'should add tableCell on proper index in tr when previous row have rowspans', () => {
- setModelData( model, modelTable( [
- [ { rowspan: 2, contents: '11' }, '13' ],
- [ '22', '23' ]
- ] ) );
- const table = root.getChild( 0 );
- model.change( writer => {
- writer.insertElement( 'tableCell', table.getChild( 0 ), 1 );
- writer.insertElement( 'tableCell', table.getChild( 1 ), 0 );
- } );
- expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal( viewTable( [
- [ { rowspan: 2, contents: '11' }, '', '13' ],
- [ '', '22', '23' ]
- ] ) );
- } );
- it( 'split cell simulation - simple', () => {
- setModelData( model, modelTable( [
- [ '11', '12' ],
- [ '21', '22' ]
- ] ) );
- const table = root.getChild( 0 );
- model.change( writer => {
- const firstRow = table.getChild( 0 );
- const secondRow = table.getChild( 1 );
- writer.insertElement( 'tableCell', firstRow, 1 );
- writer.setAttribute( 'colspan', 2, secondRow.getChild( 0 ) );
- } );
- expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal( viewTable( [
- [ '11', '', '12' ],
- [ { colspan: 2, contents: '21' }, '22' ]
- ] ) );
- } );
- it( 'merge simulation - simple', () => {
- setModelData( model, modelTable( [
- [ '11', '12' ],
- [ '21', '22' ]
- ] ) );
- const table = root.getChild( 0 );
- model.change( writer => {
- const firstRow = table.getChild( 0 );
- writer.setAttribute( 'colspan', 2, firstRow.getChild( 0 ) );
- writer.remove( firstRow.getChild( 1 ) );
- } );
- expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal( viewTable( [
- [ { colspan: 2, contents: '11' } ],
- [ '21', '22' ]
- ] ) );
- } );
- describe( 'asWidget', () => {
- beforeEach( () => {
- return VirtualTestEditor.create()
- .then( newEditor => {
- editor = newEditor;
- model = editor.model;
- doc = model.document;
- root = doc.getRoot( 'main' );
- viewDocument = editor.editing.view;
- const conversion = editor.conversion;
- const schema = model.schema;
- schema.register( 'table', {
- allowWhere: '$block',
- allowAttributes: [ 'headingRows', 'headingColumns' ],
- isBlock: true,
- isObject: true
- } );
- schema.register( 'tableRow', {
- allowIn: 'table',
- allowAttributes: [],
- isBlock: true,
- isLimit: true
- } );
- schema.register( 'tableCell', {
- allowIn: 'tableRow',
- allowContentOf: '$block',
- allowAttributes: [ 'colspan', 'rowspan' ],
- isBlock: true,
- isLimit: true
- } );
- conversion.for( 'downcast' ).add( downcastInsertTable( { asWidget: true } ) );
- conversion.for( 'downcast' ).add( downcastInsertRow( { asWidget: true } ) );
- conversion.for( 'downcast' ).add( downcastInsertCell( { asWidget: true } ) );
- conversion.attributeToAttribute( { model: 'colspan', view: 'colspan' } );
- conversion.attributeToAttribute( { model: 'rowspan', view: 'rowspan' } );
- } );
- } );
- it( 'should create inserted table cell as a widget', () => {
- setModelData( model,
- '<table>' +
- '<tableRow><tableCell>foo</tableCell></tableRow>' +
- '</table>'
- );
- const table = root.getChild( 0 );
- model.change( writer => {
- const row = table.getChild( 0 );
- writer.insert( writer.createElement( 'tableCell' ), row, 'end' );
- } );
- expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal(
- '<table class="ck-widget" contenteditable="false">' +
- '<tbody>' +
- '<tr>' +
- '<td class="ck-editor__editable ck-editor__nested-editable" contenteditable="true">foo</td>' +
- '<td class="ck-editor__editable ck-editor__nested-editable" contenteditable="true"></td>' +
- '</tr>' +
- '</tbody>' +
- '</table>'
- );
- } );
- } );
- } );
- describe( 'downcastAttributeChange()', () => {
- it( 'should work for adding heading rows', () => {
- setModelData( model, modelTable( [
- [ '11', '12' ],
- [ '21', '22' ],
- [ '31', '32' ]
- ] ) );
- const table = root.getChild( 0 );
- model.change( writer => {
- writer.setAttribute( 'headingRows', 2, table );
- } );
- expect( formatTable( getViewData( viewDocument, { withoutSelection: true } ) ) ).to.equal( formattedViewTable( [
- [ '11', '12' ],
- [ '21', '22' ],
- [ '31', '32' ]
- ], { headingRows: 2 } ) );
- } );
- it( 'should work for changing number of heading rows to a bigger number', () => {
- setModelData( model, modelTable( [
- [ '11', '12' ],
- [ '21', '22' ],
- [ '31', '32' ]
- ], { headingRows: 1 } ) );
- const table = root.getChild( 0 );
- model.change( writer => {
- writer.setAttribute( 'headingRows', 2, table );
- } );
- expect( formatTable( getViewData( viewDocument, { withoutSelection: true } ) ) ).to.equal( formattedViewTable( [
- [ '11', '12' ],
- [ '21', '22' ],
- [ '31', '32' ]
- ], { headingRows: 2 } ) );
- } );
- it( 'should work for changing number of heading rows to a smaller number', () => {
- setModelData( model, modelTable( [
- [ '11', '12' ],
- [ '21', '22' ],
- [ '31', '32' ],
- [ '41', '42' ]
- ], { headingRows: 3 } ) );
- const table = root.getChild( 0 );
- model.change( writer => {
- writer.setAttribute( 'headingRows', 2, table );
- } );
- expect( formatTable( getViewData( viewDocument, { withoutSelection: true } ) ) ).to.equal( formattedViewTable( [
- [ '11', '12' ],
- [ '21', '22' ],
- [ '31', '32' ],
- [ '41', '42' ]
- ], { headingRows: 2 } ) );
- } );
- it( 'should work for removing heading rows', () => {
- setModelData( model, modelTable( [
- [ '11', '12' ],
- [ '21', '22' ]
- ], { headingRows: 2 } ) );
- const table = root.getChild( 0 );
- model.change( writer => {
- writer.removeAttribute( 'headingRows', table );
- } );
- expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal( viewTable( [
- [ '11', '12' ],
- [ '21', '22' ]
- ] ) );
- } );
- it( 'should work for making heading rows without tbody', () => {
- setModelData( model, modelTable( [
- [ '11', '12' ],
- [ '21', '22' ]
- ] ) );
- const table = root.getChild( 0 );
- model.change( writer => {
- writer.setAttribute( 'headingRows', 2, table );
- } );
- expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal( viewTable( [
- [ '11', '12' ],
- [ '21', '22' ]
- ], { headingRows: 2 } ) );
- } );
- it( 'should work for adding heading columns', () => {
- setModelData( model, modelTable( [
- [ '11', '12' ],
- [ '21', '22' ]
- ] ) );
- const table = root.getChild( 0 );
- model.change( writer => {
- writer.setAttribute( 'headingColumns', 1, table );
- } );
- expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal( viewTable( [
- [ { isHeading: true, contents: '11' }, '12' ],
- [ { isHeading: true, contents: '21' }, '22' ]
- ] ) );
- } );
- it( 'should work for changing heading columns to a bigger number', () => {
- setModelData( model, modelTable( [
- [ '11', '12', '13', '14' ],
- [ '21', '22', '23', '24' ]
- ], { headingColumns: 1 } ) );
- const table = root.getChild( 0 );
- model.change( writer => {
- writer.setAttribute( 'headingColumns', 3, table );
- } );
- expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal( viewTable( [
- [ { isHeading: true, contents: '11' }, { isHeading: true, contents: '12' }, { isHeading: true, contents: '13' }, '14' ],
- [ { isHeading: true, contents: '21' }, { isHeading: true, contents: '22' }, { isHeading: true, contents: '23' }, '24' ]
- ] ) );
- } );
- it( 'should work for changing heading columns to a smaller number', () => {
- setModelData( model, modelTable( [
- [ { isHeading: true, contents: '11' }, { isHeading: true, contents: '12' }, { isHeading: true, contents: '13' }, '14' ],
- [ { isHeading: true, contents: '21' }, { isHeading: true, contents: '22' }, { isHeading: true, contents: '23' }, '24' ]
- ], { headingColumns: 3 } ) );
- const table = root.getChild( 0 );
- model.change( writer => {
- writer.setAttribute( 'headingColumns', 1, table );
- } );
- expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal( viewTable( [
- [ { isHeading: true, contents: '11' }, '12', '13', '14' ],
- [ { isHeading: true, contents: '21' }, '22', '23', '24' ]
- ], { headingColumns: 3 } ) );
- } );
- it( 'should work for removing heading columns', () => {
- setModelData( model, modelTable( [
- [ '11', '12' ],
- [ '21', '22' ]
- ], { headingColumns: 1 } ) );
- const table = root.getChild( 0 );
- model.change( writer => {
- writer.removeAttribute( 'headingColumns', table );
- } );
- expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal( viewTable( [
- [ '11', '12' ],
- [ '21', '22' ]
- ] ) );
- } );
- it( 'should be possible to overwrite', () => {
- editor.conversion.attributeToAttribute( { model: 'headingColumns', view: 'headingColumns', converterPriority: 'high' } );
- setModelData( model, modelTable( [ [ '11' ] ] ) );
- const table = root.getChild( 0 );
- model.change( writer => {
- writer.setAttribute( 'headingColumns', 1, table );
- } );
- expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal(
- '<table headingColumns="1"><tbody><tr><td>11</td></tr></tbody></table>'
- );
- } );
- it( 'should work with adding table cells', () => {
- setModelData( model, modelTable( [
- [ { rowspan: 2, contents: '11' }, '12', '13', '14' ],
- [ '22', '23', '24' ],
- [ { colspan: 2, contents: '31' }, '33', '34' ]
- ], { headingColumns: 2 } ) );
- const table = root.getChild( 0 );
- model.change( writer => {
- // Inserting column in heading columns so update table's attribute also
- writer.setAttribute( 'headingColumns', 3, table );
- writer.insertElement( 'tableCell', table.getChild( 0 ), 2 );
- writer.insertElement( 'tableCell', table.getChild( 1 ), 1 );
- writer.insertElement( 'tableCell', table.getChild( 2 ), 1 );
- } );
- expect( formatTable( getViewData( viewDocument, { withoutSelection: true } ) ) ).to.equal( formattedViewTable( [
- [
- { isHeading: true, rowspan: 2, contents: '11' },
- { isHeading: true, contents: '12' },
- { isHeading: true, contents: '' },
- '13',
- '14'
- ],
- [
- { isHeading: true, contents: '22' },
- { isHeading: true, contents: '' },
- '23',
- '24'
- ],
- [
- { isHeading: true, colspan: 2, contents: '31' },
- { isHeading: true, contents: '' },
- '33',
- '34'
- ]
- ] ) );
- } );
- it( 'should work with adding table rows at the beginning of a table', () => {
- setModelData( model, modelTable( [
- [ '00', '01' ],
- [ '10', '11' ]
- ], { headingRows: 1 } ) );
- const table = root.getChild( 0 );
- model.change( writer => {
- writer.setAttribute( 'headingRows', 2, table );
- const tableRow = writer.createElement( 'tableRow' );
- writer.insert( tableRow, table, 0 );
- writer.insertElement( 'tableCell', tableRow, 'end' );
- writer.insertElement( 'tableCell', tableRow, 'end' );
- } );
- expect( formatTable( getViewData( viewDocument, { withoutSelection: true } ) ) ).to.equal( formattedViewTable( [
- [ '', '' ],
- [ '00', '01' ],
- [ '10', '11' ]
- ], { headingRows: 2 } ) );
- } );
- describe( 'asWidget', () => {
- beforeEach( () => {
- return VirtualTestEditor.create()
- .then( newEditor => {
- editor = newEditor;
- model = editor.model;
- doc = model.document;
- root = doc.getRoot( 'main' );
- viewDocument = editor.editing.view;
- const conversion = editor.conversion;
- const schema = model.schema;
- schema.register( 'table', {
- allowWhere: '$block',
- allowAttributes: [ 'headingRows', 'headingColumns' ],
- isBlock: true,
- isObject: true
- } );
- schema.register( 'tableRow', {
- allowIn: 'table',
- allowAttributes: [],
- isBlock: true,
- isLimit: true
- } );
- schema.register( 'tableCell', {
- allowIn: 'tableRow',
- allowContentOf: '$block',
- allowAttributes: [ 'colspan', 'rowspan' ],
- isBlock: true,
- isLimit: true
- } );
- conversion.for( 'downcast' ).add( downcastInsertTable( { asWidget: true } ) );
- conversion.for( 'downcast' ).add( downcastInsertRow( { asWidget: true } ) );
- conversion.for( 'downcast' ).add( downcastInsertCell( { asWidget: true } ) );
- conversion.for( 'downcast' ).add( downcastAttributeChange( { attribute: 'headingRows', asWidget: true } ) );
- conversion.for( 'downcast' ).add( downcastAttributeChange( { attribute: 'headingColumns', asWidget: true } ) );
- conversion.attributeToAttribute( { model: 'colspan', view: 'colspan' } );
- conversion.attributeToAttribute( { model: 'rowspan', view: 'rowspan' } );
- } );
- } );
- it( 'should create renamed cell as a widget', () => {
- setModelData( model,
- '<table>' +
- '<tableRow><tableCell>foo</tableCell></tableRow>' +
- '</table>'
- );
- const table = root.getChild( 0 );
- model.change( writer => {
- writer.setAttribute( 'headingColumns', 1, table );
- } );
- expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal(
- '<table class="ck-widget" contenteditable="false">' +
- '<tbody>' +
- '<tr><th class="ck-editor__editable ck-editor__nested-editable" contenteditable="true">foo</th></tr>' +
- '</tbody>' +
- '</table>'
- );
- } );
- } );
- } );
- describe( 'downcastRemoveRow()', () => {
- it( 'should react to removed row from the beginning of a tbody', () => {
- setModelData( model, modelTable( [
- [ '00[]', '01' ],
- [ '10', '11' ]
- ] ) );
- const table = root.getChild( 0 );
- model.change( writer => {
- writer.remove( table.getChild( 1 ) );
- } );
- expect( formatTable( getViewData( viewDocument, { withoutSelection: true } ) ) ).to.equal( formattedViewTable( [
- [ '00', '01' ]
- ] ) );
- } );
- it( 'should react to removed row form the end of a tbody', () => {
- setModelData( model, modelTable( [
- [ '00[]', '01' ],
- [ '10', '11' ]
- ] ) );
- const table = root.getChild( 0 );
- model.change( writer => {
- writer.remove( table.getChild( 0 ) );
- } );
- expect( formatTable( getViewData( viewDocument, { withoutSelection: true } ) ) ).to.equal( formattedViewTable( [
- [ '10', '11' ]
- ] ) );
- } );
- it( 'should react to removed row from the beginning of a thead', () => {
- setModelData( model, modelTable( [
- [ '00[]', '01' ],
- [ '10', '11' ]
- ], { headingRows: 2 } ) );
- const table = root.getChild( 0 );
- model.change( writer => {
- writer.remove( table.getChild( 1 ) );
- } );
- expect( formatTable( getViewData( viewDocument, { withoutSelection: true } ) ) ).to.equal( formattedViewTable( [
- [ '00', '01' ]
- ], { headingRows: 2 } ) );
- } );
- it( 'should react to removed row form the end of a thead', () => {
- setModelData( model, modelTable( [
- [ '00[]', '01' ],
- [ '10', '11' ]
- ], { headingRows: 2 } ) );
- const table = root.getChild( 0 );
- model.change( writer => {
- writer.remove( table.getChild( 0 ) );
- } );
- expect( formatTable( getViewData( viewDocument, { withoutSelection: true } ) ) ).to.equal( formattedViewTable( [
- [ '10', '11' ]
- ], { headingRows: 2 } ) );
- } );
- it( 'should remove empty thead section if a last row was removed from thead', () => {
- setModelData( model, modelTable( [
- [ '00[]', '01' ],
- [ '10', '11' ]
- ], { headingRows: 1 } ) );
- const table = root.getChild( 0 );
- model.change( writer => {
- writer.setAttribute( 'headingRows', 0, table );
- writer.remove( table.getChild( 0 ) );
- } );
- expect( formatTable( getViewData( viewDocument, { withoutSelection: true } ) ) ).to.equal( formattedViewTable( [
- [ '10', '11' ]
- ] ) );
- } );
- it( 'should remove empty tbody section if a last row was removed from tbody', () => {
- setModelData( model, modelTable( [
- [ '00[]', '01' ],
- [ '10', '11' ]
- ], { headingRows: 1 } ) );
- const table = root.getChild( 0 );
- model.change( writer => {
- writer.remove( table.getChild( 1 ) );
- } );
- expect( formatTable( getViewData( viewDocument, { withoutSelection: true } ) ) ).to.equal( formattedViewTable( [
- [ '00', '01' ]
- ], { headingRows: 1 } ) );
- } );
- } );
- } );
|