| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691 |
- /**
- * @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved.
- * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
- */
- import { stringify, parse, getData, setData } from '../../src/dev-utils/model';
- import Model from '../../src/model/model';
- import DocumentFragment from '../../src/model/documentfragment';
- import Element from '../../src/model/element';
- import Text from '../../src/model/text';
- import Range from '../../src/model/range';
- import Position from '../../src/model/position';
- import count from '@ckeditor/ckeditor5-utils/src/count';
- describe( 'model test utils', () => {
- let model, document, root, selection;
- beforeEach( () => {
- model = new Model();
- document = model.document;
- root = document.createRoot();
- selection = document.selection;
- model.change( writer => {
- writer.setSelection( null );
- } );
- model.schema.register( 'a', {
- allowWhere: '$text',
- allowIn: '$root',
- allowAttributes: [ 'bar', 'car', 'foo' ]
- } );
- model.schema.register( 'b', {
- allowWhere: '$text',
- allowIn: '$root',
- allowAttributes: [ 'barFoo', 'fooBar', 'x' ]
- } );
- model.schema.register( 'c', {
- allowWhere: '$text',
- allowIn: [ '$root', 'b' ]
- } );
- model.schema.register( 'paragraph', { inheritAllFrom: '$block' } );
- model.schema.extend( '$text', {
- allowIn: [ '$root', 'a', 'b' ]
- } );
- } );
- afterEach( () => {
- sinon.restore();
- } );
- describe( 'getData', () => {
- it( 'should use stringify method', () => {
- const stringifySpy = sinon.spy( getData, '_stringify' );
- root._appendChild( new Element( 'b', null, new Text( 'btext' ) ) );
- expect( getData( model, { withoutSelection: true } ) ).to.equal( '<b>btext</b>' );
- sinon.assert.calledOnce( stringifySpy );
- sinon.assert.calledWithExactly( stringifySpy, root, null, null );
- } );
- it( 'should use stringify method with selection', () => {
- const stringifySpy = sinon.spy( getData, '_stringify' );
- root._appendChild( new Element( 'b', null, new Text( 'btext' ) ) );
- model.change( writer => {
- writer.setSelection( new Range( Position._createAt( root, 0 ), Position._createAt( root, 1 ) ) );
- } );
- expect( getData( model ) ).to.equal( '[<b>btext</b>]' );
- sinon.assert.calledOnce( stringifySpy );
- sinon.assert.calledWithExactly( stringifySpy, root, document.selection, null );
- } );
- it( 'should throw an error when passing invalid document', () => {
- expect( () => {
- getData( { invalid: 'document' } );
- } ).to.throw( TypeError, 'Model needs to be an instance of module:engine/model/model~Model.' );
- } );
- describe( 'markers', () => {
- it( 'should stringify collapsed marker', () => {
- setData( model, '<paragraph>bar</paragraph>' );
- model.markers._set( 'foo', new Range( Position._createAt( document.getRoot(), 0 ) ) );
- expect( getData( model, { convertMarkers: true, withoutSelection: true } ) )
- .to.equal( '<foo:start></foo:start><paragraph>bar</paragraph>' );
- } );
- it( 'should stringify non-collapsed marker', () => {
- setData( model, '<paragraph>bar</paragraph>' );
- const markerRange = new Range( Position._createAt( document.getRoot(), 0 ), Position._createAt( document.getRoot(), 1 ) );
- model.markers._set( 'foo', markerRange );
- expect( getData( model, { convertMarkers: true, withoutSelection: true } ) )
- .to.equal( '<foo:start></foo:start><paragraph>bar</paragraph><foo:end></foo:end>' );
- } );
- } );
- } );
- describe( 'setData', () => {
- it( 'should use parse method', () => {
- const parseSpy = sinon.spy( setData, '_parse' );
- const options = {};
- const data = '<b>btext</b>text';
- setData( model, data, options );
- expect( getData( model, { withoutSelection: true } ) ).to.equal( data );
- sinon.assert.calledOnce( parseSpy );
- const args = parseSpy.firstCall.args;
- expect( args[ 0 ] ).to.equal( data );
- } );
- it( 'should use parse method with selection', () => {
- const parseSpy = sinon.spy( setData, '_parse' );
- const options = {};
- const data = '[<b>btext</b>]';
- setData( model, data, options );
- expect( getData( model ) ).to.equal( data );
- sinon.assert.calledOnce( parseSpy );
- const args = parseSpy.firstCall.args;
- expect( args[ 0 ] ).to.equal( data );
- } );
- it( 'should insert text', () => {
- testUtils( 'this is test text', '[]this is test text' );
- } );
- it( 'should insert text with selection around', () => {
- testUtils( '[this is test text]' );
- } );
- it( 'should insert text with selection inside #1', () => {
- testUtils( 'this [is test] text' );
- } );
- it( 'should insert text with selection inside #2', () => {
- testUtils( '[this is test] text' );
- } );
- it( 'should insert text with selection inside #3', () => {
- testUtils( 'this is [test text]' );
- } );
- it( 'should insert unicode text with selection', () => {
- testUtils( 'நி[லைக்]கு' );
- } );
- it( 'should insert element', () => {
- testUtils( '<b>foo bar</b>', '[]<b>foo bar</b>' );
- } );
- it( 'should insert element with selection inside #1', () => {
- testUtils( '<b>[foo ]bar</b>' );
- } );
- it( 'should insert element with selection inside #2', () => {
- testUtils( '[<b>foo ]bar</b>' );
- } );
- it( 'should insert element with selection inside #3', () => {
- testUtils( '<b>[foo bar</b>]' );
- } );
- it( 'should insert backward selection', () => {
- setData( model, '<b>[foo bar</b>]', { lastRangeBackward: true } );
- expect( getData( model ) ).to.equal( '<b>[foo bar</b>]' );
- expect( document.selection.isBackward ).to.true;
- } );
- it( 'should throw an error when passing invalid document', () => {
- expect( () => {
- setData( { invalid: 'document' } );
- } ).to.throw( TypeError, 'Model needs to be an instance of module:engine/model/model~Model.' );
- } );
- it( 'should set attributes to the selection', () => {
- setData( model, '<b>[foo bar]</b>', { selectionAttributes: { foo: 'bar' } } );
- expect( document.selection.getAttribute( 'foo' ) ).to.equal( 'bar' );
- } );
- // #815.
- it( 'should work in a special root', () => {
- const model = new Model();
- model.schema.register( 'textOnly' );
- model.schema.extend( '$text', { allowIn: 'textOnly' } );
- model.document.createRoot( 'textOnly', 'textOnly' );
- setData( model, 'a[b]c', { rootName: 'textOnly' } );
- expect( getData( model, { rootName: 'textOnly' } ) ).to.equal( 'a[b]c' );
- } );
- function testUtils( data, expected ) {
- expected = expected || data;
- setData( model, data );
- expect( getData( model ) ).to.equal( expected );
- }
- } );
- describe( 'stringify', () => {
- it( 'should stringify text', () => {
- const text = new Text( 'text', { underline: true, bold: true } );
- expect( stringify( text ) ).to.equal( '<$text bold="true" underline="true">text</$text>' );
- } );
- it( 'should stringify element', () => {
- const element = new Element( 'a', null, [
- new Element( 'b', null, new Text( 'btext' ) ),
- new Text( 'atext' )
- ] );
- expect( stringify( element ) ).to.equal( '<a><b>btext</b>atext</a>' );
- } );
- it( 'should stringify document fragment', () => {
- const fragment = new DocumentFragment( [
- new Element( 'b', null, new Text( 'btext' ) ),
- new Text( 'atext' )
- ] );
- expect( stringify( fragment ) ).to.equal( '<b>btext</b>atext' );
- } );
- it( 'writes elements and texts', () => {
- root._appendChild( [
- new Element( 'a', null, new Text( 'atext' ) ),
- new Element( 'b', null, [
- new Element( 'c1' ),
- new Text( 'ctext' ),
- new Element( 'c2' )
- ] ),
- new Element( 'd' )
- ] );
- expect( stringify( root ) ).to.equal(
- '<a>atext</a><b><c1></c1>ctext<c2></c2></b><d></d>'
- );
- } );
- it( 'writes element attributes', () => {
- root._appendChild(
- new Element( 'a', { foo: true, bar: 1, car: false }, [
- new Element( 'b', { fooBar: 'x y', barFoo: { x: 1, y: 2 } } )
- ] )
- );
- // Note: attributes are written in a very simplistic way, because they are not to be parsed. They are just
- // to be compared in the tests with some patterns.
- expect( stringify( root ) ).to.equal(
- '<a bar="1" car="false" foo="true"><b barFoo="{"x":1,"y":2}" fooBar="x y"></b></a>'
- );
- } );
- it( 'writes text attributes', () => {
- root._appendChild( [
- new Text( 'foo', { bold: true } ),
- new Text( 'bar' ),
- new Text( 'bom', { bold: true, italic: true } ),
- new Element( 'a', null, [
- new Text( 'pom', { underline: true, bold: true } )
- ] )
- ] );
- expect( stringify( root ) ).to.equal(
- '<$text bold="true">foo</$text>bar<$text bold="true" italic="true">bom</$text>' +
- '<a><$text bold="true" underline="true">pom</$text></a>'
- );
- } );
- it( 'writes unicode text', () => {
- root._appendChild( new Text( 'நிலைக்கு' ) );
- expect( stringify( root ) ).to.equal( 'நிலைக்கு' );
- } );
- describe( 'selection', () => {
- let elA, elB;
- beforeEach( () => {
- elA = new Element( 'a' );
- elB = new Element( 'b' );
- root._appendChild( [
- elA,
- new Text( 'foo' ),
- new Text( 'bar', { bold: true } ),
- elB
- ] );
- } );
- it( 'writes selection in an empty root', () => {
- const root = document.createRoot( '$root', 'empty' );
- model.change( writer => {
- writer.setSelection( root, 0 );
- } );
- expect( stringify( root, selection ) ).to.equal(
- '[]'
- );
- } );
- it( 'writes selection collapsed in an element', () => {
- model.change( writer => {
- writer.setSelection( root, 0 );
- } );
- expect( stringify( root, selection ) ).to.equal(
- '[]<a></a>foo<$text bold="true">bar</$text><b></b>'
- );
- } );
- it( 'writes selection collapsed in a text', () => {
- model.change( writer => {
- writer.setSelection( root, 3 );
- } );
- expect( stringify( root, selection ) ).to.equal(
- '<a></a>fo[]o<$text bold="true">bar</$text><b></b>'
- );
- } );
- it( 'writes selection collapsed at the text left boundary', () => {
- model.change( writer => {
- writer.setSelection( elA, 'after' );
- } );
- expect( stringify( root, selection ) ).to.equal(
- '<a></a>[]foo<$text bold="true">bar</$text><b></b>'
- );
- } );
- it( 'writes selection collapsed at the text right boundary', () => {
- model.change( writer => {
- writer.setSelection( elB, 'before' );
- } );
- expect( stringify( root, selection ) ).to.equal(
- '<a></a>foo<$text bold="true">bar[]</$text><b></b>'
- );
- } );
- it( 'writes selection collapsed at the end of the root', () => {
- model.change( writer => {
- writer.setSelection( root, 'end' );
- // Needed due to https://github.com/ckeditor/ckeditor5-engine/issues/320.
- writer.removeSelectionAttribute( model.document.selection.getAttributeKeys() );
- } );
- expect( stringify( root, selection ) ).to.equal(
- '<a></a>foo<$text bold="true">bar</$text><b></b>[]'
- );
- } );
- it( 'writes selection collapsed selection in a text with attributes', () => {
- model.change( writer => {
- writer.setSelection( root, 5 );
- } );
- expect( stringify( root, selection ) ).to.equal(
- '<a></a>foo<$text bold="true">b[]ar</$text><b></b>'
- );
- } );
- it( 'writes flat selection containing couple of nodes', () => {
- model.change( writer => {
- writer.setSelection( new Range( Position._createAt( root, 0 ), Position._createAt( root, 4 ) ) );
- } );
- expect( stringify( root, selection ) ).to.equal(
- '[<a></a>foo]<$text bold="true">bar</$text><b></b>'
- );
- } );
- it( 'writes flat selection within text', () => {
- model.change( writer => {
- writer.setSelection( new Range( Position._createAt( root, 2 ), Position._createAt( root, 3 ) ) );
- } );
- expect( stringify( root, selection ) ).to.equal(
- '<a></a>f[o]o<$text bold="true">bar</$text><b></b>'
- );
- } );
- it( 'writes multi-level selection', () => {
- model.change( writer => {
- writer.setSelection( new Range( Position._createAt( elA, 0 ), Position._createAt( elB, 0 ) ) );
- } );
- expect( stringify( root, selection ) ).to.equal(
- '<a>[</a>foo<$text bold="true">bar</$text><b>]</b>'
- );
- } );
- it( 'writes selection when is backward', () => {
- model.change( writer => {
- writer.setSelection( new Range( Position._createAt( elA, 0 ), Position._createAt( elB, 0 ) ), { backward: true } );
- } );
- expect( stringify( root, selection ) ).to.equal(
- '<a>[</a>foo<$text bold="true">bar</$text><b>]</b>'
- );
- } );
- it( 'writes selection in unicode text', () => {
- const root = document.createRoot( '$root', 'empty' );
- root._appendChild( new Text( 'நிலைக்கு' ) );
- model.change( writer => {
- writer.setSelection( new Range( Position._createAt( root, 2 ), Position._createAt( root, 6 ) ) );
- } );
- expect( stringify( root, selection ) ).to.equal( 'நி[லைக்]கு' );
- } );
- it( 'uses range and coverts it to selection', () => {
- const range = new Range( Position._createAt( elA, 0 ), Position._createAt( elB, 0 ) );
- expect( stringify( root, range ) ).to.equal(
- '<a>[</a>foo<$text bold="true">bar</$text><b>]</b>'
- );
- } );
- it( 'uses position and converts it to collapsed selection', () => {
- const position = new Position( root, [ 0 ] );
- expect( stringify( root, position ) ).to.equal(
- '[]<a></a>foo<$text bold="true">bar</$text><b></b>'
- );
- } );
- } );
- } );
- describe( 'parse', () => {
- test( 'creates empty DocumentFragment from empty string', {
- data: '',
- check( fragment ) {
- expect( fragment ).to.be.instanceOf( DocumentFragment );
- }
- } );
- test( 'creates empty DocumentFragment with selection', {
- data: '[]'
- } );
- test( 'returns Element if range is around single element', {
- data: '[<a></a>]',
- check( el, selection ) {
- const fragment = el.parent;
- expect( el ).to.be.instanceOf( Element );
- expect( fragment ).to.be.instanceOf( DocumentFragment );
- expect( selection.rangeCount ).to.equal( 1 );
- const range = new Range( Position._createAt( fragment, 0 ), Position._createAt( fragment, 1 ) );
- expect( selection.getFirstRange().isEqual( range ) ).to.be.true;
- }
- } );
- test( 'returns DocumentFragment when multiple elements on root', {
- data: '<a></a><b></b>',
- check( fragment ) {
- expect( fragment ).to.be.instanceOf( DocumentFragment );
- expect( fragment.childCount ).to.equal( 2 );
- }
- } );
- test( 'creates elements', {
- data: '<a></a><b><c></c></b>'
- } );
- test( 'creates text nodes', {
- data: 'foo<a>bar</a>bom'
- } );
- test( 'creates text nodes with unicode text', {
- data: 'நிலைக்கு'
- } );
- test( 'sets elements attributes', {
- data: '<a bar="true" car="x y" foo="1"></a><b x="y"></b>',
- output: '<a bar="true" car="x y" foo="1"></a><b x="y"></b>',
- check( root ) {
- expect( root.getChild( 0 ).getAttribute( 'bar' ) ).to.equal( true );
- expect( root.getChild( 0 ).getAttribute( 'car' ) ).to.equal( 'x y' );
- expect( root.getChild( 0 ).getAttribute( 'foo' ) ).to.equal( 1 );
- expect( root.getChild( 1 ).getAttribute( 'x' ) ).to.equal( 'y' );
- }
- } );
- test( 'sets text attributes', {
- data: '<$text bar="true" car="x y" foo="1">foo</$text><$text x="y">bar</$text>bom',
- check( root ) {
- expect( root.childCount ).to.equal( 3 );
- expect( root.maxOffset ).to.equal( 9 );
- expect( root.getChild( 0 ).getAttribute( 'bar' ) ).to.equal( true );
- expect( root.getChild( 0 ).getAttribute( 'car' ) ).to.equal( 'x y' );
- expect( root.getChild( 0 ).getAttribute( 'foo' ) ).to.equal( 1 );
- expect( root.getChild( 1 ).getAttribute( 'x' ) ).to.equal( 'y' );
- expect( count( root.getChild( 2 ).getAttributes() ) ).to.equal( 0 );
- }
- } );
- test( 'creates element with complex attributes', {
- data: '<a foo=\'{"x":1,"y":2}\'></a>',
- output: '<a foo="{"x":1,"y":2}"></a>',
- check( a ) {
- expect( count( a.getAttributes() ) ).to.equal( 1 );
- expect( a.getAttribute( 'foo' ) ).to.have.property( 'x', 1 );
- expect( a.getAttribute( 'foo' ) ).to.have.property( 'y', 2 );
- }
- } );
- test( 'returns single parsed element', {
- data: '<paragraph></paragraph>',
- check( p ) {
- expect( p instanceof Element ).to.be.true;
- }
- } );
- test( 'returns DocumentFragment for multiple parsed elements', {
- data: '<paragraph></paragraph><paragraph></paragraph>',
- check( fragment ) {
- expect( fragment instanceof DocumentFragment ).to.be.true;
- }
- } );
- it( 'throws when invalid XML', () => {
- expect( () => {
- parse( '<a><b></a></b>', model.schema );
- } ).to.throw( Error, /Parse error/ );
- } );
- it( 'throws when try to set element not registered in schema', () => {
- expect( () => {
- parse( '<xyz></xyz>', model.schema );
- } ).to.throw( Error, 'Element \'xyz\' was not allowed in given position.' );
- } );
- it( 'throws when try to set text directly to $root without registering it', () => {
- const model = new Model();
- expect( () => {
- parse( 'text', model.schema );
- } ).to.throw( Error, 'Text was not allowed in given position.' );
- } );
- it( 'converts data in the specified context', () => {
- const model = new Model();
- model.schema.register( 'foo' );
- model.schema.extend( '$text', { allowIn: 'foo' } );
- expect( () => {
- parse( 'text', model.schema, { context: [ 'foo' ] } );
- } ).to.not.throw();
- } );
- describe( 'selection', () => {
- test( 'sets collapsed selection in an element', {
- data: '<a>[]</a>',
- check( root, selection ) {
- expect( selection.getFirstPosition().parent ).to.have.property( 'name', 'a' );
- }
- } );
- test( 'sets collapsed selection between elements', {
- data: '<a></a>[]<b></b>'
- } );
- test( 'sets collapsed selection before a text', {
- data: '<a></a>[]foo'
- } );
- test( 'sets collapsed selection after a text', {
- data: 'foo[]'
- } );
- test( 'sets collapsed selection within a text', {
- data: 'foo[]bar',
- check( text, selection ) {
- expect( text.offsetSize ).to.equal( 6 );
- expect( text.getPath() ).to.deep.equal( [ 0 ] );
- expect( selection.getFirstRange().start.path ).to.deep.equal( [ 3 ] );
- expect( selection.getFirstRange().end.path ).to.deep.equal( [ 3 ] );
- }
- } );
- it( 'sets selection attributes', () => {
- const result = parse( 'foo[]bar', model.schema, {
- selectionAttributes: {
- bold: true,
- italic: true
- }
- } );
- expect( stringify( result.model, result.selection ) ).to.equal( 'foo<$text bold="true" italic="true">[]</$text>bar' );
- } );
- test( 'sets collapsed selection between text and text with attributes', {
- data: 'foo[]<$text bold="true">bar</$text>',
- check( root, selection ) {
- expect( root.maxOffset ).to.equal( 6 );
- expect( selection.getAttribute( 'bold' ) ).to.be.undefined;
- }
- } );
- test( 'sets selection containing an element', {
- data: 'x[<a></a>]'
- } );
- it( 'sets selection with attribute containing an element', () => {
- const result = parse( 'x[<a></a>]', model.schema, {
- selectionAttributes: {
- bold: true
- }
- } );
- expect( stringify( result.model, result.selection ) ).to.equal( 'x[<a></a>]' );
- expect( result.selection.getAttribute( 'bold' ) ).to.be.true;
- } );
- it( 'sets a backward selection containing an element', () => {
- const result = parse( 'x[<a></a>]', model.schema, {
- lastRangeBackward: true
- } );
- expect( stringify( result.model, result.selection ) ).to.equal( 'x[<a></a>]' );
- expect( result.selection.isBackward ).to.true;
- } );
- test( 'sets selection within a text', {
- data: 'x[y]z'
- } );
- it( 'sets selection within a text with different attributes', () => {
- const result = parse( '<$text bold="true">fo[o</$text>ba]r', model.schema, {
- selectionAttributes: { bold: true }
- } );
- expect( stringify( result.model, result.selection ) ).to.equal( '<$text bold="true">fo[o</$text>ba]r' );
- expect( result.selection.getAttribute( 'bold' ) ).to.true;
- } );
- it( 'throws when missing selection start', () => {
- expect( () => {
- parse( 'foo]', model.schema );
- } ).to.throw( Error, /^Parse error/ );
- } );
- it( 'throws when missing selection end', () => {
- expect( () => {
- parse( '[foo', model.schema );
- } ).to.throw( Error, /^Parse error/ );
- } );
- } );
- function test( title, options ) {
- it( title, () => {
- const output = options.output || options.data;
- const data = parse( options.data, model.schema );
- let converted, selection;
- if ( data.selection && data.model ) {
- converted = data.model;
- selection = data.selection;
- } else {
- converted = data;
- }
- expect( stringify( converted, selection ) ).to.equal( output );
- if ( options.check ) {
- options.check( converted, selection );
- }
- } );
- }
- } );
- } );
|