| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697 |
- /**
- * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
- * For licensing, see LICENSE.md.
- */
- /* bender-tags: treeview */
- 'use strict';
- import Writer from '/ckeditor5/engine/treeview/writer.js';
- import ContainerElement from '/ckeditor5/engine/treeview/containerelement.js';
- import AttributeElement from '/ckeditor5/engine/treeview/attributeelement.js';
- import Position from '/ckeditor5/engine/treeview/position.js';
- import Range from '/ckeditor5/engine/treeview/range.js';
- import Text from '/ckeditor5/engine/treeview/text.js';
- import utils from '/tests/engine/treeview/writer/_utils/utils.js';
- import CKEditorError from '/ckeditor5/utils/ckeditorerror.js';
- describe( 'Writer', () => {
- const create = utils.create;
- const test = utils.test;
- let writer;
- beforeEach( () => {
- writer = new Writer();
- } );
- describe( 'unwrap', () => {
- it( 'should do nothing on collapsed ranges', () => {
- const description = {
- instanceOf: ContainerElement,
- name: 'p',
- children: [
- { instanceOf: Text, data: 'foo', rangeStart: 1, rangeEnd: 1 }
- ]
- };
- const created = create( writer, description );
- const newRange = writer.unwrap( created.range, new ContainerElement( 'b' ), 1 );
- test( writer, newRange, created.node, description );
- } );
- it( 'should do nothing on single text node', () => {
- // <p>[{foobar}]</p>
- const description = {
- instanceOf: ContainerElement,
- name: 'p',
- rangeStart: 0,
- rangeEnd: 1,
- children: [
- { instanceOf: Text, data: 'foobar' }
- ]
- };
- const created = create( writer, description );
- const b = new ContainerElement( 'b' );
- b.priority = 1;
- const newRange = writer.unwrap( created.range, b );
- test( writer, newRange, created.node, description );
- } );
- it( 'should throw error when range placed in two containers', () => {
- const container1 = new ContainerElement( 'p' );
- const container2 = new ContainerElement( 'p' );
- const range = new Range(
- new Position( container1, 0 ),
- new Position( container2, 1 )
- );
- const b = new ContainerElement( 'b' );
- expect( () => {
- writer.unwrap( range, b, 1 );
- } ).to.throw( CKEditorError, 'treeview-writer-invalid-range-container' );
- } );
- it( 'should unwrap single node', () => {
- // <p>[<b>{foobar}</b>]<p> -> <p>[{foobar}]</p>
- const created = create( writer, {
- instanceOf: ContainerElement,
- name: 'p',
- rangeStart: 0,
- rangeEnd: 1,
- children: [
- {
- instanceOf: AttributeElement,
- name: 'b',
- priority: 1,
- children: [
- { instanceOf: Text, data: 'foobar' }
- ]
- }
- ]
- } );
- const b = new ContainerElement( 'b' );
- b.priority = 1;
- const newRange = writer.unwrap( created.range, b );
- test( writer, newRange, created.node, {
- instanceOf: ContainerElement,
- 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>
- // Unwrapped with <b> but using different priority.
- const description = {
- instanceOf: ContainerElement,
- name: 'p',
- rangeStart: 0,
- rangeEnd: 1,
- children: [
- {
- instanceOf: AttributeElement,
- name: 'b',
- priority: 1,
- children: [
- { instanceOf: Text, data: 'foobar' }
- ]
- }
- ]
- };
- const created = create( writer, description );
- const b = new ContainerElement( 'b' );
- b.priority = 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>
- // <b> around `bar` has different priority than others.
- const created = create( writer, {
- instanceOf: ContainerElement,
- name: 'p',
- rangeStart: 0,
- rangeEnd: 3,
- children: [
- {
- instanceOf: AttributeElement,
- name: 'b',
- priority: 2,
- children: [
- { instanceOf: Text, data: 'foo' }
- ]
- },
- {
- instanceOf: AttributeElement,
- name: 'b',
- priority: 1,
- children: [
- { instanceOf: Text, data: 'bar' }
- ]
- },
- {
- instanceOf: AttributeElement,
- name: 'b',
- priority: 2,
- children: [
- { instanceOf: Text, data: 'baz' }
- ]
- }
- ]
- } );
- const b = new ContainerElement( 'b' );
- b.priority = 2;
- const newRange = writer.unwrap( created.range, b );
- test( writer, newRange, created.node, {
- instanceOf: ContainerElement,
- name: 'p',
- rangeStart: 0,
- rangeEnd: 3,
- children: [
- { instanceOf: Text, data: 'foo' },
- {
- instanceOf: AttributeElement,
- name: 'b',
- priority: 1,
- 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 created = create( writer, {
- instanceOf: ContainerElement,
- name: 'p',
- rangeStart: 0,
- children: [
- { instanceOf: Text, data: 'baz' },
- {
- instanceOf: AttributeElement,
- name: 'b',
- priority: 1,
- children: [
- { instanceOf: Text, data: 'foobar', rangeEnd: 3 }
- ]
- }
- ]
- } );
- const b = new ContainerElement( 'b' );
- b.priority = 1;
- const newRange = writer.unwrap( created.range, b );
- test( writer, newRange, created.node, {
- instanceOf: ContainerElement,
- name: 'p',
- rangeStart: 0,
- rangeEnd: 1,
- children: [
- { instanceOf: Text, data: 'bazfoo' },
- {
- instanceOf: AttributeElement,
- 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 created = create( writer, {
- instanceOf: ContainerElement,
- name: 'p',
- rangeStart: 0,
- rangeEnd: 1,
- children: [
- {
- instanceOf: AttributeElement,
- name: 'u',
- priority: 1,
- children: [
- {
- instanceOf: AttributeElement,
- name: 'b',
- priority: 1,
- children: [
- { instanceOf: Text, data: 'foobar' }
- ]
- }
- ]
- }
- ]
- } );
- const b = new ContainerElement( 'b' );
- b.priority = 1;
- const newRange = writer.unwrap( created.range, b );
- test( writer, newRange, created.node, {
- instanceOf: ContainerElement,
- name: 'p',
- rangeStart: 0,
- rangeEnd: 1,
- children: [
- {
- instanceOf: AttributeElement,
- name: 'u',
- priority: 1,
- children: [
- { instanceOf: Text, data: 'foobar' }
- ]
- }
- ]
- } );
- } );
- it( 'should merge unwrapped nodes #1', () => {
- // <p>{foo}[<b>{bar}</b>]{bom}</p> -> <p>{foo[bar]bom}</p>
- const created = create( writer, {
- instanceOf: ContainerElement,
- name: 'p',
- rangeStart: 1,
- rangeEnd: 2,
- children: [
- { instanceOf: Text, data: 'foo' },
- {
- instanceOf: AttributeElement,
- name: 'b',
- priority: 1,
- children: [
- { instanceOf: Text, data: 'bar' }
- ]
- },
- { instanceOf: Text, data: 'bom' }
- ]
- } );
- const b = new ContainerElement( 'b' );
- b.priority = 1;
- const newRange = writer.unwrap( created.range, b );
- test( writer, newRange, created.node, {
- instanceOf: ContainerElement,
- name: 'p',
- children: [
- {
- instanceOf: Text,
- data: 'foobarbom',
- rangeStart: 3,
- rangeEnd: 6
- }
- ]
- } );
- } );
- it( 'should merge unwrapped nodes #2', () => {
- // <p>{foo}<u>{bar}</u>[<b><u>{bazqux}</u></b>]</p> -> <p>{foo}<u>{bar[bazqux}</u>]</p>
- const created = create( writer, {
- instanceOf: ContainerElement,
- name: 'p',
- rangeStart: 2,
- rangeEnd: 3,
- children: [
- { instanceOf: Text, data: 'foo' },
- {
- instanceOf: AttributeElement,
- name: 'u',
- priority: 1,
- children: [
- { instanceOf: Text, data: 'bar' }
- ]
- },
- {
- instanceOf: AttributeElement,
- name: 'b',
- priority: 1,
- children: [
- {
- instanceOf: AttributeElement,
- name: 'u',
- priority: 1,
- children: [
- { instanceOf: Text, data: 'bazqux' }
- ]
- }
- ]
- }
- ]
- } );
- const b = new ContainerElement( 'b' );
- b.priority = 1;
- const newRange = writer.unwrap( created.range, b );
- test( writer, newRange, created.node, {
- instanceOf: ContainerElement,
- name: 'p',
- rangeEnd: 2,
- children: [
- {
- instanceOf: Text,
- data: 'foo'
- },
- {
- instanceOf: AttributeElement,
- name: 'u',
- priority: 1,
- children: [
- { instanceOf: Text, data: 'barbazqux', rangeStart: 3 }
- ]
- }
- ]
- } );
- } );
- it( 'should merge unwrapped nodes #3', () => {
- // <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 created = create( writer, {
- instanceOf: ContainerElement,
- name: 'p',
- rangeStart: 2,
- children: [
- { instanceOf: Text, data: 'foo' },
- {
- instanceOf: AttributeElement,
- name: 'u',
- priority: 1,
- children: [
- { instanceOf: Text, data: 'bar' }
- ]
- },
- {
- instanceOf: AttributeElement,
- name: 'b',
- priority: 1,
- children: [
- {
- instanceOf: AttributeElement,
- name: 'u',
- priority: 1,
- children: [
- { instanceOf: Text, data: 'bazqux', rangeEnd: 3 }
- ]
- }
- ]
- }
- ]
- } );
- const b = new ContainerElement( 'b' );
- b.priority = 1;
- const newRange = writer.unwrap( created.range, b );
- test( writer, newRange, created.node, {
- instanceOf: ContainerElement,
- name: 'p',
- rangeEnd: 2,
- children: [
- {
- instanceOf: Text,
- data: 'foo'
- },
- {
- instanceOf: AttributeElement,
- name: 'u',
- priority: 1,
- children: [
- { instanceOf: Text, data: 'barbaz', rangeStart: 3 }
- ]
- },
- {
- instanceOf: AttributeElement,
- name: 'b',
- priority: 1,
- children: [
- {
- instanceOf: AttributeElement,
- name: 'u',
- priority: 1,
- children: [
- { instanceOf: Text, data: 'qux' }
- ]
- }
- ]
- }
- ]
- } );
- } );
- it( 'should merge unwrapped nodes #4', () => {
- // <p>{foo}<u>{bar}</u>[<b><u>{baz}</u></b>]<u>qux</u></p> -> <p>{foo}<u>{bar[baz]qux}</u></p>
- const created = create( writer, {
- instanceOf: ContainerElement,
- name: 'p',
- rangeStart: 2,
- rangeEnd: 3,
- children: [
- { instanceOf: Text, data: 'foo' },
- {
- instanceOf: AttributeElement,
- name: 'u',
- priority: 1,
- children: [
- { instanceOf: Text, data: 'bar' }
- ]
- },
- {
- instanceOf: AttributeElement,
- name: 'b',
- priority: 1,
- children: [
- {
- instanceOf: AttributeElement,
- name: 'u',
- priority: 1,
- children: [
- { instanceOf: Text, data: 'baz' }
- ]
- }
- ]
- },
- {
- instanceOf: AttributeElement,
- name: 'u',
- priority: 1,
- children: [
- { instanceOf: Text, data: 'qux' }
- ]
- }
- ]
- } );
- const b = new ContainerElement( 'b' );
- b.priority = 1;
- const newRange = writer.unwrap( created.range, b );
- test( writer, newRange, created.node, {
- instanceOf: ContainerElement,
- name: 'p',
- children: [
- {
- instanceOf: Text,
- data: 'foo'
- },
- {
- instanceOf: AttributeElement,
- name: 'u',
- priority: 1,
- children: [
- { instanceOf: Text, data: 'barbazqux', rangeStart: 3, rangeEnd: 6 }
- ]
- }
- ]
- } );
- } );
- it( 'should merge unwrapped nodes #5', () => {
- // <p>[<b><u>{foo}</u></b><b><u>{bar}</u></b><b><u>{baz}</u></b>]</p> -> <p>[<u>{foobarbaz}</u>]</p>
- const created = create( writer, {
- instanceOf: ContainerElement,
- name: 'p',
- rangeStart: 0,
- rangeEnd: 3,
- children: [
- {
- instanceOf: AttributeElement,
- name: 'b',
- priority: 1,
- children: [
- {
- instanceOf: AttributeElement,
- name: 'u',
- priority: 1,
- children: [
- { instanceOf: Text, data: 'foo' }
- ]
- }
- ]
- },
- {
- instanceOf: AttributeElement,
- name: 'b',
- priority: 1,
- children: [
- {
- instanceOf: AttributeElement,
- name: 'u',
- priority: 1,
- children: [
- { instanceOf: Text, data: 'bar' }
- ]
- }
- ]
- },
- {
- instanceOf: AttributeElement,
- name: 'b',
- priority: 1,
- children: [
- {
- instanceOf: AttributeElement,
- name: 'u',
- priority: 1,
- children: [
- { instanceOf: Text, data: 'baz' }
- ]
- }
- ]
- }
- ]
- } );
- const b = new ContainerElement( 'b' );
- b.priority = 1;
- const newRange = writer.unwrap( created.range, b );
- test( writer, newRange, created.node, {
- instanceOf: ContainerElement,
- name: 'p',
- rangeStart: 0,
- rangeEnd: 1,
- children: [
- {
- instanceOf: AttributeElement,
- name: 'u',
- priority: 1,
- children: [
- { instanceOf: Text, data: 'foobarbaz' }
- ]
- }
- ]
- } );
- } );
- it( 'should unwrap mixed ranges #1', () => {
- // <p>[<u><b>{foo}]</b></u></p> -> <p>[<u>{foo}</u>]</p
- const created = create( writer, {
- instanceOf: ContainerElement,
- name: 'p',
- rangeStart: 0,
- children: [
- {
- instanceOf: AttributeElement,
- name: 'u',
- priority: 1,
- children: [
- {
- instanceOf: AttributeElement,
- name: 'b',
- priority: 1,
- rangeEnd: 1,
- children: [
- { instanceOf: Text, data: 'foo' }
- ]
- }
- ]
- }
- ]
- } );
- const b = new ContainerElement( 'b' );
- b.priority = 1;
- const newRange = writer.unwrap( created.range, b );
- test( writer, newRange, created.node, {
- instanceOf: ContainerElement,
- name: 'p',
- rangeStart: 0,
- rangeEnd: 1,
- children: [
- {
- instanceOf: AttributeElement,
- name: 'u',
- priority: 1,
- children: [
- { instanceOf: Text, data: 'foo' }
- ]
- }
- ]
- } );
- } );
- it( 'should unwrap mixed ranges #2', () => {
- // <p>[<u><b>{foo]}</b></u></p> -> <p>[<u>{foo}</u>]</p
- const created = create( writer, {
- instanceOf: ContainerElement,
- name: 'p',
- rangeStart: 0,
- children: [
- {
- instanceOf: AttributeElement,
- name: 'u',
- priority: 1,
- children: [
- {
- instanceOf: AttributeElement,
- name: 'b',
- priority: 1,
- children: [
- { instanceOf: Text, data: 'foo', rangeEnd: 3 }
- ]
- }
- ]
- }
- ]
- } );
- const b = new ContainerElement( 'b' );
- b.priority = 1;
- const newRange = writer.unwrap( created.range, b );
- test( writer, newRange, created.node, {
- instanceOf: ContainerElement,
- name: 'p',
- rangeStart: 0,
- rangeEnd: 1,
- children: [
- {
- instanceOf: AttributeElement,
- name: 'u',
- priority: 1,
- children: [
- { instanceOf: Text, data: 'foo' }
- ]
- }
- ]
- } );
- } );
- } );
- } );
|