| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175 |
- /**
- * @license Copyright (c) 2003-2015, CKSource - Frederico Knabben. All rights reserved.
- * For licensing, see LICENSE.md.
- */
- /* bender-tags: treeview */
- 'use strict';
- import Writer from '/ckeditor5/core/treeview/writer.js';
- import Element from '/ckeditor5/core/treeview/element.js';
- import Text from '/ckeditor5/core/treeview/text.js';
- import Position from '/ckeditor5/core/treeview/position.js';
- import Range from '/ckeditor5/core/treeview/range.js';
- describe( 'Writer', () => {
- /**
- * Helper function that is used to test output of writer methods by providing declarative description of the
- * expected output.
- * Examples:
- * test element: `<p>fo[o<b>ba]r</b></p>`
- * description: {
- * name: 'p',
- * instanceOf: Element,
- * children:[
- * {
- * instanceOf: Text,
- * data: 'foo',
- * rangeStart: 2
- * },
- * {
- * name: 'b'
- * instanceOf: Element
- * priority: 1,
- * children: [
- * { instanceOf: Text, data: 'bar', rangeEnd: 2 }
- * ]
- * }
- * ]
- * }
- *
- *
- * @param {core.treeView.Writer} writer Writer instance. Used to test priority.
- * @param {core.treeView.Range|core.treeView.Position } location Range instance or Position instance.
- * Treated as Range when when `rangeStart`, `rangeEnd` is used, treated as Position when `position` is used.
- * @param {core.treeView.Node} node Element to check.
- * @param {Object} description Object describing expected element and its children.
- */
- function test( writer, location, node, description ) {
- if ( description.instanceOf ) {
- expect( node ).to.be.instanceof( description.instanceOf );
- }
- if ( description.name ) {
- expect( node.name ).to.equal( description.name );
- }
- if ( description.data ) {
- expect( node.data ).to.equal( description.data );
- }
- if ( description.priority !== undefined ) {
- expect( writer.getPriority( node ) ).to.equal( description.priority );
- }
- if ( description.rangeStart !== undefined ) {
- expect( location.start.parent ).to.equal( node );
- expect( location.start.offset ).to.equal( description.rangeStart );
- }
- if ( description.attributes ) {
- Object.keys( description.attributes ).forEach( ( key ) => {
- expect( node.getAttribute( key ) ).to.equal( description.attributes[ key ] );
- } );
- }
- if ( description.rengeEnd !== undefined ) {
- expect( location.end.parent ).to.equal( node );
- expect( location.end.offset ).to.equal( description.rengeEnd );
- }
- if ( description.position !== undefined ) {
- expect( location.parent ).to.equal( node );
- expect( location.offset ).to.equal( description.position );
- }
- if ( description.children ) {
- expect( node.getChildCount() ).to.equal( description.children.length );
- description.children.forEach( ( desc, index ) => {
- test( writer, location, node.getChild( index ), desc );
- } );
- }
- }
- /**
- * Helper function that is used to create treeView elements from description object.
- *
- * @param {core.treeView.Writer} writer Writer instance. Used to set priorities.
- * @param {Object} description Description object.
- * @param {core.treeView.Range} [range] Optional parameter, used in recurrent calls.
- * @param {core.treeView.Position} [position] Optional parameter, used in recurrent calls.
- * @returns {Object} Returns object with `node`, `range`, `position` fields, containing created node and, optionally
- * range and position if description object contain information about them.
- */
- function create( writer, description, range, position ) {
- const node = new description.instanceOf();
- if ( !range ) {
- range = Range.createFromParentsAndOffsets( node, 0, node, 0 );
- }
- if ( !position ) {
- position = new Position( node, 0 );
- }
- if ( description.name ) {
- node.name = description.name;
- }
- if ( description.data ) {
- node.data = description.data;
- }
- if ( description.attributes ) {
- Object.keys( description.attributes ).forEach( ( key ) => {
- node.setAttribute( key, description.attributes[ key ] );
- } );
- }
- if ( description.priority !== undefined ) {
- writer.setPriority( node, description.priority );
- }
- if ( description.rangeStart !== undefined ) {
- range.start.parent = node;
- range.start.offset = description.rangeStart;
- }
- if ( description.rangeEnd !== undefined ) {
- range.end.parent = node;
- range.end.offset = description.rangeEnd;
- }
- if ( description.position !== undefined ) {
- position.parent = node;
- position.offset = description.position;
- }
- if ( description.children ) {
- description.children.forEach( ( desc, index ) => {
- const created = create( writer, desc, range, position );
- node.insertChildren( index, created.node );
- } );
- }
- return { node, range, position };
- }
- describe( 'isContainer', () => {
- it( 'should return true for container elements', () => {
- const containerElement = new Element( 'p' );
- const attributeElement = new Element( 'b' );
- const writer = new Writer();
- writer._priorities.set( attributeElement, 1 );
- expect( writer.isContainer( containerElement ) ).to.be.true;
- expect( writer.isContainer( attributeElement ) ).to.be.false;
- } );
- } );
- describe( 'isAttribute', () => {
- it( 'should return true for container elements', () => {
- const containerElement = new Element( 'p' );
- const attributeElement = new Element( 'b' );
- const writer = new Writer();
- writer._priorities.set( attributeElement, 1 );
- expect( writer.isAttribute( containerElement ) ).to.be.false;
- expect( writer.isAttribute( attributeElement ) ).to.be.true;
- } );
- } );
- describe( 'setPriority', () => {
- it( 'sets node priority', () => {
- const writer = new Writer();
- const nodeMock = {};
- writer.setPriority( nodeMock, 10 );
- expect( writer._priorities.get( nodeMock ) ).to.equal( 10 );
- } );
- } );
- describe( 'getPriority', () => {
- it( 'gets node priority', () => {
- const writer = new Writer();
- const nodeMock = {};
- writer._priorities.set( nodeMock, 12 );
- expect( writer.getPriority( nodeMock ) ).to.equal( 12 );
- } );
- } );
- describe( 'breakAttributes', () => {
- // <p>{|foobar}</p> -> <p>|{foobar}</p>
- it( '<p>{|foobar}</p>', () => {
- const writer = new Writer();
- const created = create( writer, {
- instanceOf: Element,
- name: 'p',
- children: [
- { instanceOf: Text, data: 'foobar', position: 0 }
- ]
- } );
- const newPosition = writer.breakAttributes( created.position );
- test( writer, newPosition, created.node, {
- instanceOf: Element,
- name: 'p',
- position: 0,
- children: [
- { instanceOf: Text, data: 'foobar' }
- ]
- } );
- } );
- it( '<p>foo|bar</p>', () => {
- // <p>{foo|bar}</p> -> <p>{foo}|{bar}</p>
- const writer = new Writer();
- const created = create( writer, {
- instanceOf: Element,
- name: 'p',
- children: [
- { instanceOf: Text, data: 'foobar', position: 3 }
- ]
- } );
- const newPosition = writer.breakAttributes( created.position );
- test( writer, newPosition, created.node, {
- instanceOf: Element,
- name: 'p',
- position: 1,
- children: [
- { instanceOf: Text, data: 'foo' },
- { instanceOf: Text, data: 'bar' }
- ]
- } );
- } );
- it( '<p>{foobar|}</p>', () => {
- // <p>{foobar|}</p> -> <p>{foobar}|</p>
- const writer = new Writer();
- const created = create( writer, {
- instanceOf: Element,
- name: 'p',
- children: [
- { instanceOf: Text, data: 'foobar', position: 6 }
- ]
- } );
- const newPosition = writer.breakAttributes( created.position );
- test( writer, newPosition, created.node, {
- instanceOf: Element,
- name: 'p',
- position: 1,
- children: [
- { instanceOf: Text, data: 'foobar' }
- ]
- } );
- } );
- it( '<p><b>{foo|bar}</b></p>', () => {
- // <p><b>{foo|bar}</b></p> -> <p><b>{foo}</b>|<b>{bar}</b></p>
- const writer = new Writer();
- const created = create( writer, {
- instanceOf: Element,
- name: 'p',
- children: [
- {
- instanceOf: Element,
- name: 'b',
- priority: 1,
- children: [
- { instanceOf: Text, data: 'foobar', position: 3 }
- ]
- }
- ]
- } );
- const newPosition = writer.breakAttributes( created.position );
- test( writer, newPosition, created.node, {
- instanceOf: Element,
- name: 'p',
- position: 1,
- children: [
- {
- instanceOf: Element,
- name: 'b',
- priority: 1,
- children: [
- { instanceOf: Text, data: 'foo' }
- ]
- },
- {
- instanceOf: Element,
- name: 'b',
- priority: 1,
- children: [
- { instanceOf: Text, data: 'bar' }
- ]
- }
- ]
- } );
- } );
- it( '<p><b><u>{|foobar}</u></b></p>', () => {
- // <p><b><u>{|foobar}</u></b></p> -> <p>|<b><u>{foobar}</u></b></p>
- const writer = new Writer();
- const created = create( writer, {
- instanceOf: Element,
- name: 'p',
- children: [
- {
- instanceOf: Element,
- name: 'b',
- priority: 1,
- children: [
- {
- instanceOf: Element,
- name: 'u',
- priority: 1,
- children: [
- { instanceOf: Text, data: 'foobar', position: 0 }
- ]
- }
- ]
- }
- ]
- } );
- const newPosition = writer.breakAttributes( created.position );
- test( writer, newPosition, created.node, {
- instanceOf: Element,
- name: 'p',
- position: 0,
- children: [
- {
- instanceOf: Element,
- name: 'b',
- priority: 1,
- children: [
- {
- instanceOf: Element,
- name: 'u',
- priority: 1,
- children: [
- { instanceOf: Text, data: 'foobar' }
- ]
- }
- ]
- }
- ]
- } );
- } );
- // <p><b><u>{foo|ba}r</u></b></p> -> <p><b><u>{foo}</u></b>|<b></u>{bar}</u></b></p>
- it( '<p><b><u>{foo|bar}</u></b></p>', () => {
- const writer = new Writer();
- const created = create( writer, {
- instanceOf: Element,
- name: 'p',
- children: [
- {
- instanceOf: Element,
- name: 'b',
- priority: 1,
- children: [
- {
- instanceOf: Element,
- name: 'u',
- priority: 1,
- children: [
- { instanceOf: Text, data: 'foobar', position: 3 }
- ]
- }
- ]
- }
- ]
- } );
- const newPosition = writer.breakAttributes( created.position );
- test( writer, newPosition, created.node, {
- instanceOf: Element,
- name: 'p',
- position: 1,
- children: [
- {
- instanceOf: Element,
- name: 'b',
- priority: 1,
- children: [
- {
- instanceOf: Element,
- name: 'u',
- priority: 1,
- children: [
- { instanceOf: Text, data: 'foo' }
- ]
- }
- ]
- },
- {
- instanceOf: Element,
- name: 'b',
- priority: 1,
- children: [
- {
- instanceOf: Element,
- name: 'u',
- priority: 1,
- children: [
- { instanceOf: Text, data: 'bar' }
- ]
- }
- ]
- }
- ]
- } );
- } );
- it( '<p><b><u>{foobar|}</u></b></p>', () => {
- // <p><b><u>{foobar|}</u></b></p> -> <p><b><u>{foobar}</u></b>|</p>
- const writer = new Writer();
- const created = create( writer, {
- instanceOf: Element,
- name: 'p',
- children: [
- {
- instanceOf: Element,
- name: 'b',
- priority: 1,
- children: [
- {
- instanceOf: Element,
- name: 'u',
- priority: 1,
- children: [
- { instanceOf: Text, data: 'foobar', position: 6 }
- ]
- }
- ]
- }
- ]
- } );
- const newPosition = writer.breakAttributes( created.position );
- test( writer, newPosition, created.node, {
- instanceOf: Element,
- name: 'p',
- position: 1,
- children: [
- {
- instanceOf: Element,
- name: 'b',
- priority: 1,
- children: [
- {
- instanceOf: Element,
- name: 'u',
- priority: 1,
- children: [
- { instanceOf: Text, data: 'foobar' }
- ]
- }
- ]
- }
- ]
- } );
- } );
- } );
- describe( 'mergeAttributes', () => {
- it( 'should not merge if inside text node', () => {
- // <p>{fo|obar}</p>
- const writer = new Writer();
- const description = {
- instanceOf: Element,
- name: 'p',
- children: [
- { instanceOf: Text, data: 'foobar', position: 2 }
- ]
- };
- const created = create( writer, description );
- const newPosition = writer.mergeAttributes( created.position );
- test( writer, newPosition, created.node, description );
- } );
- it( 'should return same position when inside empty container', () => {
- // <p>|</p>
- const writer = new Writer();
- const description = { instanceOf: Element, name: 'p', position: 0 };
- const created = create( writer, description );
- const newPosition = writer.mergeAttributes( created.position );
- test( writer, newPosition, created.node, description );
- } );
- it( 'should not merge when position is placed at the beginning of the container', () => {
- // <p>|<b></b></p>
- const writer = new Writer();
- const description = {
- instanceOf: Element,
- name: 'p',
- position: 0,
- children: [
- {
- instanceOf: Element,
- name: 'b',
- priority: 1
- }
- ]
- };
- const created = create( writer, description );
- const newPosition = writer.mergeAttributes( created.position );
- test( writer, newPosition, created.node, description );
- } );
- it( 'should not merge when position is placed at the end of the container', () => {
- // <p><b></b>|</p>
- const writer = new Writer();
- const description = {
- instanceOf: Element,
- name: 'p',
- position: 1,
- children: [
- {
- instanceOf: Element,
- name: 'b',
- priority: 1
- }
- ]
- };
- const created = create( writer, description );
- const newPosition = writer.mergeAttributes( created.position );
- test( writer, newPosition, created.node, description );
- } );
- it( 'should merge when placed between two text nodes', () => {
- // <p>{foo}|{bar}</p> -> <p>{foo|bar}</p>
- const writer = new Writer();
- const created = create( writer, {
- instanceOf: Element,
- name: 'p',
- position: 1,
- children: [
- { instanceOf: Text, data: 'foo' },
- { instanceOf: Text, data: 'bar' }
- ]
- } );
- const newPosition = writer.mergeAttributes( created.position );
- test( writer, newPosition, created.node, {
- instanceOf: Element,
- name: 'p',
- children: [
- { instanceOf: Text, data: 'foobar', position: 3 }
- ]
- } );
- } );
- it( 'should merge when placed between similar attribute nodes', () => {
- // <p><b foo="bar"></b>|<b foo="bar"></b></p> -> <p><b foo="bar">|</b></p>
- const writer = new Writer();
- const created = create( writer, {
- instanceOf: Element,
- name: 'p',
- position: 1,
- children: [
- {
- instanceOf: Element,
- name: 'b',
- priority: 1,
- attributes: { foo: 'bar' }
- },
- {
- instanceOf: Element,
- name: 'b',
- priority: 1,
- attributes: { foo: 'bar' }
- }
- ]
- } );
- const newPosition = writer.mergeAttributes( created.position );
- test( writer, newPosition, created.node, {
- instanceOf: Element,
- name: 'p',
- children: [
- {
- instanceOf: Element,
- name: 'b',
- priority: 1,
- position: 0,
- attributes: { foo: 'bar' }
- }
- ]
- } );
- } );
- it( 'should not merge when placed between non-similar attribute nodes', () => {
- // <p><b foo="bar"></b>|<b foo="baz"></b></p> ->
- // <p><b foo="bar"></b>|<b foo="baz"></b></p>
- const writer = new Writer();
- const description = {
- instanceOf: Element,
- name: 'p',
- position: 1,
- children: [
- {
- instanceOf: Element,
- name: 'b',
- priority: 1,
- attributes: { foo: 'bar' }
- },
- {
- instanceOf: Element,
- name: 'b',
- priority: 1,
- attributes: { foo: 'baz' }
- }
- ]
- };
- const created = create( writer, description );
- const newPosition = writer.mergeAttributes( created.position );
- test( writer, newPosition, created.node, description );
- } );
- it( 'should not merge when placed between similar attribute nodes with different priority', () => {
- // <p><b foo="bar"></b>|<b foo="bar"></b></p> -> <p><b foo="bar"></b>|<b foo="bar"></b></p>
- const writer = new Writer();
- const description = {
- instanceOf: Element,
- name: 'p',
- position: 1,
- children: [
- {
- instanceOf: Element,
- name: 'b',
- priority: 1,
- attributes: { foo: 'bar' }
- },
- {
- instanceOf: Element,
- name: 'b',
- priority: 2,
- attributes: { foo: 'bar' }
- }
- ]
- };
- const created = create( writer,description );
- const newPosition = writer.mergeAttributes( created.position );
- test( writer, newPosition, created.node, description );
- } );
- it( 'should merge attribute nodes and their contents if possible', () => {
- // <p><b foo="bar">{foo}</b>|<b foo="bar">{bar}</b></p>
- // <p><b foo="bar">{foo}|{bar}</b></p>
- // <p><b foo="bar">{foo|bar}</b></p>
- const writer = new Writer();
- const created = create( writer, {
- instanceOf: Element,
- name: 'p',
- position: 1,
- children: [
- {
- instanceOf: Element,
- name: 'b',
- priority: 1,
- attributes: { foo: 'bar' },
- children: [
- { instanceOf: Text, data: 'foo' }
- ]
- },
- {
- instanceOf: Element,
- name: 'b',
- priority: 1,
- attributes: { foo: 'bar' },
- children: [
- { instanceOf: Text, data: 'bar' }
- ]
- }
- ]
- } );
- const newPosition = writer.mergeAttributes( created.position );
- test( writer, newPosition, created.node, {
- instanceOf: Element,
- name: 'p',
- children: [
- {
- instanceOf: Element,
- name: 'b',
- priority: 1,
- attributes: { foo: 'bar' },
- children: [
- { instanceOf: Text, data: 'foobar', position: 3 }
- ]
- }
- ]
- } );
- } );
- } );
- describe( 'insert', () => {
- it( 'should insert text', () => {
- // <p>{foo|bar}</p> insert {baz}
- // <p>{foo[baz]bar}</p>
- const writer = new Writer();
- const created = create( writer, {
- instanceOf: Element,
- name: 'p',
- children: [
- { instanceOf: Text, data: 'foobar', position: 3 }
- ]
- } );
- const newRange = writer.insert( created.position, new Text( 'baz' ) );
- test( writer, newRange, created.node, {
- instanceOf: Element,
- name: 'p',
- children: [
- { instanceOf: Text, data: 'foobazbar' }
- ]
- } );
- } );
- it( 'should merge attributes', () => {
- // <p><b>{foo|bar}</b></p> insert <b>{baz}</b>
- // <p><b>{foobazbar}</b></p>
- const writer = new Writer();
- const text = new Text( 'foobar' );
- const b1 = new Element( 'b', null, text );
- const p = new Element( 'p', null, b1 );
- const position = new Position( text, 3 );
- const insertText = new Text( 'baz' );
- const b2 = new Element( 'b', null, insertText );
- writer.setPriority( b1, 1 );
- writer.setPriority( b2, 1 );
- writer.insert( position, b2 );
- expect( p.getChildCount() ).to.equal( 1 );
- const b3 = p.getChild( 0 );
- expect( b3 ).to.be.instanceof( Element );
- expect( b3.name ).to.equal( 'b' );
- expect( b3.getChildCount() ).to.equal( 1 );
- const newText = b3.getChild( 0 );
- expect( newText ).to.be.instanceof( Text );
- expect( newText.data ).to.equal( 'foobazbar' );
- } );
- } );
- describe( 'wrap', () => {
- //// <p>[{foobar}]</p>
- //// wrap <b>
- //// <p>[<b>{foobar}<b>]</p>
- //it( 'wraps single text node', () => {
- // const writer = new Writer();
- // const text = new Text( 'foobar' );
- // const p = new Element( 'p', null, [ text ] );
- // const b = new Element( 'b' );
- // const range = new Range(
- // new Position( p, 0 ),
- // new Position( p, 1 )
- // );
- //
- // const newRange = writer.wrap( range, b, 1 );
- //
- // expect( p.getChildCount() ).to.equal( 1 );
- // expect( p.getChild( 0 ) ).to.equal( b );
- // expect( b.getChildCount() ).to.equal( 1 );
- // expect( b.getChild( 0 ) ).to.equal( text );
- // expect( text.data ).to.equal( 'foobar' );
- // expect( newRange.start.parent ).to.equal( p );
- // expect( newRange.start.offset ).to.equal( 0 );
- // expect( newRange.end.parent ).to.equal( p );
- // expect( newRange.end.offset ).to.equal( 1 );
- //} );
- //// <p>[{foo]bar}</p>
- //// <p>[<b>{foo}</b>]{bar}</p>
- //it( 'wraps part of single text node', () => {
- // const writer = new Writer();
- // const text = new Text( 'foobar' );
- // const p = new Element( 'p', null, [ text ] );
- // const b = new Element( 'b' );
- // const range = new Range(
- // new Position( p, 0 ),
- // new Position( text, 3 )
- // );
- //
- // const newRange = writer.wrap( range, b, 1 );
- // expect( p.getChildCount() ).to.equal( 2 );
- // const child1 = p.getChild( 0 );
- // const child2 = p.getChild( 1 );
- //
- // expect( child1 ).to.be.instanceof( Element );
- // expect( child1.same( b ) ).to.be.true;
- // expect( child1.getChildCount() ).to.equal( 1 );
- // const newText = child1.getChild( 0 );
- // expect( newText ).to.be.instanceof( Text );
- // expect( newText.data ).to.equal( 'foo' );
- // expect( child2 ).to.be.instanceof( Text );
- // expect( child2.data ).to.equal( 'bar' );
- //
- // expect( newRange.start.parent ).to.equal( p );
- // expect( newRange.start.offset ).to.equal( 0 );
- // expect( newRange.end.parent ).to.equal( p );
- // expect( newRange.end.offset ).to.equal( 1 );
- //} );
- //it( 'tests', () => {
- // const writer = new Writer();
- // const text = new Text( 'foobar' );
- // const text2 = new Text( 'bazquix' );
- // const u = new Element( 'u', null, [ text2 ] );
- // const p = new Element( 'p', null, [ text, u ] );
- // const b = new Element( 'b' );
- // const range = new Range(
- // new Position( p, 0 ),
- // new Position( p, 2 )
- // );
- //
- // const newRange = writer.wrap( range, b, 1 );
- //
- // expect( p.getChildCount() ).to.equal( 1 );
- // //expect( p.getChild( 0 ) ).to.equal( b );
- // //expect( b.getChildCount() ).to.equal( 1 );
- // //expect( b.getChild( 0 ) ).to.equal( text );
- // //expect( text.data ).to.equal( 'foobar' );
- // //expect( newRange.start.parent ).to.equal( p );
- // //expect( newRange.start.offset ).to.equal( 0 );
- // //expect( newRange.end.parent ).to.equal( p );
- // //expect( newRange.end.offset ).to.equal( 1 );
- //} );
- } );
- describe( 'unwrap', () => {
- it( 'should do nothing on single text node', () => {
- // <p>[{foobar}]</p>
- const writer = new Writer();
- const description = {
- instanceOf: Element,
- name: 'p',
- rangeStart: 0,
- rangeEnd: 1,
- children: [
- { instanceOf: Text, data: 'foobar' }
- ]
- };
- const created = create( writer, description );
- const b = new Element( 'b' );
- writer.setPriority( b, 1 );
- const newRange = writer.unwrap( created.range, b );
- test( writer, newRange, created.node, description );
- } );
- it( 'should unwrap single node', () => {
- // <p>[<b>{foobar}</b>]<p> -> <p>[{foobar}]</p>
- const writer = new Writer();
- const created = create( writer, {
- instanceOf: Element,
- name: 'p',
- rangeStart: 0,
- rangeEnd: 1,
- children: [
- {
- instanceOf: Element,
- priority: 1,
- name: 'b',
- children: [
- { instanceOf: Text, data: 'foobar' }
- ]
- }
- ]
- } );
- const b = new Element( 'b' );
- writer.setPriority( b, 1 );
- const newRange = writer.unwrap( created.range, b );
- test( writer, newRange, created.node, {
- instanceOf: Element,
- name: 'p',
- rangeStart: 0,
- rangeEnd: 1,
- children: [
- { instanceOf: Text, data: 'foobar' }
- ]
- } );
- } );
- it( 'should not unwrap attributes with different priorities #1', () => {
- // <p>[<b>{foobar}</b>]<p> -> <p>[<b>{foobar}</b>]</p>
- const writer = new Writer();
- const description = {
- instanceOf: Element,
- name: 'p',
- rangeStart: 0,
- rangeEnd: 1,
- children: [
- {
- instanceOf: Element,
- priority: 1,
- name: 'b',
- children: [
- { instanceOf: Text, data: 'foobar' }
- ]
- }
- ]
- };
- const created = create( writer, description );
- const b = new Element( 'b' );
- writer.setPriority( b, 2 );
- const newRange = writer.unwrap( created.range, b );
- test( writer, newRange, created.node, description );
- } );
- it( 'should not unwrap attributes with different priorities #2', () => {
- // <p>[<b>{foo}</b><b>{bar}</b><b>{baz}</b>]<p> -> <p>[{foo}<b>bar</b>{baz}]</p>
- const writer = new Writer();
- const created = create( writer, {
- instanceOf: Element,
- name: 'p',
- rangeStart: 0,
- rangeEnd: 3,
- children: [
- {
- instanceOf: Element,
- priority: 2,
- name: 'b',
- children: [
- { instanceOf: Text, data: 'foo' }
- ]
- },
- {
- instanceOf: Element,
- priority: 1,
- name: 'b',
- children: [
- { instanceOf: Text, data: 'bar' }
- ]
- },
- {
- instanceOf: Element,
- priority: 2,
- name: 'b',
- children: [
- { instanceOf: Text, data: 'baz' }
- ]
- }
- ]
- } );
- const b = new Element( 'b' );
- writer.setPriority( b, 2 );
- const newRange = writer.unwrap( created.range, b );
- test( writer, newRange, created.node, {
- instanceOf: Element,
- name: 'p',
- rangeStart: 0,
- rangeEnd: 3,
- children: [
- { instanceOf: Text, data: 'foo' },
- {
- instanceOf: Element,
- priority: 1,
- name: 'b',
- children: [
- { instanceOf: Text, data: 'bar' }
- ]
- },
- { instanceOf: Text, data: 'baz' }
- ]
- } );
- } );
- it( 'should unwrap part of the node', () => {
- // <p>[{baz}<b>{foo]bar}</b><p> -> <p>[{bazfoo}]<b>{bar}</b></p>
- const writer = new Writer();
- const created = create( writer, {
- instanceOf: Element,
- name: 'p',
- rangeStart: 0,
- children: [
- { instanceOf: Text, data: 'baz' },
- {
- instanceOf: Element,
- name: 'b',
- priority: 1,
- children: [
- { instanceOf: Text, data: 'foobar', rangeEnd: 3 }
- ]
- }
- ]
- } );
- const b = new Element( 'b' );
- writer.setPriority( b, 1 );
- const newRange = writer.unwrap( created.range, b );
- test( writer, newRange, created.node, {
- instanceOf: Element,
- name: 'p',
- rangeStart: 0,
- rangeEnd: 1,
- children: [
- { instanceOf: Text, data: 'bazfoo' },
- {
- instanceOf: Element,
- name: 'b',
- priority: 1,
- children: [
- { instanceOf: Text, data: 'bar' }
- ]
- }
- ]
- } );
- } );
- it( 'should unwrap nested attributes', () => {
- // <p>[<u><b>{foobar}</b></u>]</p> -> <p>[<u>{foobar}</u>]</p>
- const writer = new Writer();
- const created = create( writer, {
- instanceOf: Element,
- name: 'p',
- rangeStart: 0,
- rangeEnd: 1,
- children: [
- {
- instanceOf: Element,
- name: 'u',
- priority: 1,
- children: [
- {
- instanceOf: Element,
- name: 'b',
- priority: 1,
- children: [
- { instanceOf: Text, data: 'foobar' }
- ]
- }
- ]
- }
- ]
- } );
- const b = new Element( 'b' );
- writer.setPriority( b, 1 );
- const newRange = writer.unwrap( created.range, b );
- test( writer, newRange, created.node, {
- instanceOf: Element,
- name: 'p',
- rangeStart: 0,
- rangeEnd: 1,
- children: [
- {
- instanceOf: Element,
- name: 'u',
- priority: 1,
- children: [
- { instanceOf: Text, data: 'foobar' }
- ]
- }
- ]
- } );
- } );
- it( 'should merge unwrapped nodes', () => {
- // <p>{foo}<u>{bar}</u>[<b><u>{baz]qux}</u></b></p> -> <p>{foo}<u>{bar[baz}</u>]<b><u>{qux}</u></b></p>
- const writer = new Writer();
- const created = create( writer, {
- instanceOf: Element,
- name: 'p',
- rangeStart: 2,
- children: [
- { instanceOf: Text, data: 'foo' },
- {
- instanceOf: Element,
- name: 'u',
- priority: 1,
- children: [
- { instanceOf: Text, data: 'bar' }
- ]
- },
- {
- instanceOf: Element,
- name: 'b',
- priority: 1,
- children: [
- {
- instanceOf: Element,
- name: 'u',
- priority: 1,
- children: [
- { instanceOf: Text, data: 'bazqux', rangeEnd: 3 }
- ]
- }
- ]
- }
- ]
- } );
- const b = new Element( 'b' );
- writer.setPriority( b, 1 );
- const newRange = writer.unwrap( created.range, b );
- test( writer, newRange, created.node, {
- instanceOf: Element,
- name: 'p',
- rangeEnd: 2,
- children: [
- {
- instanceOf: Text,
- data: 'foo'
- },
- {
- instanceOf: Element,
- name: 'u',
- priority: 1,
- children: [
- { instanceOf: Text, data: 'barbaz', rangeStart: 3 }
- ]
- },
- {
- instanceOf: Element,
- name: 'b',
- priority: 1,
- children: [
- {
- instanceOf: Element,
- name: 'u',
- priority: 1,
- children: [
- { instanceOf: Text, data: 'qux' }
- ]
- }
- ]
- }
- ]
- } );
- } );
- } );
- } );
|