| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268 |
- /**
- * @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 {
- downcastInsertCell,
- downcastInsertRow,
- downcastInsertTable,
- downcastRemoveRow,
- downcastTableHeadingColumnsChange,
- downcastTableHeadingRowsChange
- } 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( downcastTableHeadingRowsChange() );
- conversion.for( 'downcast' ).add( downcastTableHeadingColumnsChange() );
- } );
- } );
- 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( 'downcastTableHeadingColumnsChange()', () => {
- 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'
- ]
- ] ) );
- } );
- 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( downcastTableHeadingColumnsChange( { asWidget: true } ) );
- conversion.for( 'downcast' ).add( downcastTableHeadingRowsChange( { 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( 'headingRows', 1, table );
- } );
- expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal(
- '<table class="ck-widget" contenteditable="false">' +
- '<thead>' +
- '<tr><th class="ck-editor__editable ck-editor__nested-editable" contenteditable="true">foo</th></tr>' +
- '</thead>' +
- '</table>'
- );
- } );
- } );
- } );
- describe( 'downcastTableHeadingRowsChange()', () => {
- 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 be possible to overwrite', () => {
- editor.conversion.attributeToAttribute( { model: 'headingRows', view: 'headingRows', converterPriority: 'high' } );
- setModelData( model, modelTable( [ [ '11' ] ] ) );
- const table = root.getChild( 0 );
- model.change( writer => {
- writer.setAttribute( 'headingRows', 1, table );
- } );
- expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal(
- '<table headingRows="1"><tbody><tr><td>11</td></tr></tbody></table>'
- );
- } );
- 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( downcastTableHeadingColumnsChange( { asWidget: true } ) );
- conversion.for( 'downcast' ).add( downcastTableHeadingRowsChange( { 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 } ) );
- } );
- } );
- } );
|