| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264 |
- /**
- * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
- * For licensing, see LICENSE.md.
- */
- /* bender-tags: view, browser-only */
- import { wrap } from '/ckeditor5/engine/view/writer.js';
- import Element from '/ckeditor5/engine/view/element.js';
- import ContainerElement from '/ckeditor5/engine/view/containerelement.js';
- import AttributeElement from '/ckeditor5/engine/view/attributeelement.js';
- import Position from '/ckeditor5/engine/view/position.js';
- import Range from '/ckeditor5/engine/view/range.js';
- import Text from '/ckeditor5/engine/view/text.js';
- import CKEditorError from '/ckeditor5/utils/ckeditorerror.js';
- import { stringify, parse } from '/tests/engine/_utils/view.js';
- describe( 'writer', () => {
- /**
- * Executes test using `parse` and `stringify` utils functions.
- *
- * @param {String} input
- * @param {String} wrapAttribute
- * @param {String} expected
- */
- function test( input, wrapAttribute, expected ) {
- let { view, selection } = parse( input );
- const newRange = wrap( selection.getFirstRange(), parse( wrapAttribute ) );
- expect( stringify( view.root, newRange, { showType: true, showPriority: true } ) ).to.equal( expected );
- }
- describe( 'wrap', () => {
- it( 'should do nothing on collapsed ranges', () => {
- test(
- '<container:p>f{}oo</container:p>',
- '<attribute:b></attribute:b>',
- '<container:p>f{}oo</container:p>'
- );
- } );
- it( 'wraps single text node', () => {
- test(
- '<container:p>[foobar]</container:p>',
- '<attribute:b:1></attribute:b:1>',
- '<container:p>[<attribute:b:1>foobar</attribute:b:1>]</container:p>'
- );
- } );
- it( 'wraps single text node in document fragment', () => {
- test(
- '{foobar}',
- '<attribute:b:1></attribute:b:1>',
- '[<attribute:b:1>foobar</attribute:b:1>]'
- );
- } );
- it( 'should throw error when element is not instance of AttributeElement', () => {
- const container = new ContainerElement( 'p', null, new Text( 'foo' ) );
- const range = new Range(
- new Position( container, 0 ),
- new Position( container, 1 )
- );
- const b = new Element( 'b' );
- expect( () => {
- wrap( range, b );
- } ).to.throw( CKEditorError, 'view-writer-wrap-invalid-attribute' );
- } );
- 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 AttributeElement( 'b' );
- expect( () => {
- wrap( range, b );
- } ).to.throw( CKEditorError, 'view-writer-invalid-range-container' );
- } );
- it( 'should throw when range has no parent container', () => {
- const el = new AttributeElement( 'b' );
- const b = new AttributeElement( 'b' );
- expect( () => {
- wrap( Range.createFromParentsAndOffsets( el, 0, el, 0 ), b );
- } ).to.throw( CKEditorError, 'view-writer-invalid-range-container' );
- } );
- it( 'wraps part of a single text node #1', () => {
- test(
- '<container:p>[foo}bar</container:p>',
- '<attribute:b:1></attribute:b:1>',
- '<container:p>[<attribute:b:1>foo</attribute:b:1>]bar</container:p>'
- );
- } );
- it( 'wraps part of a single text node #2', () => {
- test(
- '<container:p>{foo}bar</container:p>',
- '<attribute:b:1></attribute:b:1>',
- '<container:p>[<attribute:b:1>foo</attribute:b:1>]bar</container:p>'
- );
- } );
- it( 'should support unicode', () => {
- test(
- '<container:p>நி{லை}க்கு</container:p>',
- '<attribute:b:1></attribute:b:1>',
- '<container:p>நி[<attribute:b:1>லை</attribute:b:1>]க்கு</container:p>'
- );
- } );
- it( 'wraps part of a single text node #3', () => {
- test(
- '<container:p>foo{bar}</container:p>',
- '<attribute:b:1></attribute:b:1>',
- '<container:p>foo[<attribute:b:1>bar</attribute:b:1>]</container:p>'
- );
- } );
- it( 'should not wrap inside nested containers', () => {
- test(
- '<container:div>[foobar<container:p>baz</container:p>]</container:div>',
- '<attribute:b:1></attribute:b:1>',
- '<container:div>[<attribute:b:1>foobar</attribute:b:1><container:p>baz</container:p>]</container:div>'
- );
- } );
- it( 'wraps according to priorities', () => {
- test(
- '<container:p>[<attribute:u:1>foobar</attribute:u:1>]</container:p>',
- '<attribute:b:2></attribute:b:2>',
- '<container:p>[<attribute:u:1><attribute:b:2>foobar</attribute:b:2></attribute:u:1>]</container:p>'
- );
- } );
- it( 'merges wrapped nodes #1', () => {
- test(
- '<container:p>[<attribute:b:1>foo</attribute:b:1>bar<attribute:b:1>baz</attribute:b:1>]</container:p>',
- '<attribute:b:1></attribute:b:1>',
- '<container:p>[<attribute:b:1>foobarbaz</attribute:b:1>]</container:p>'
- );
- } );
- it( 'merges wrapped nodes #2', () => {
- test(
- '<container:p><attribute:b:1>foo</attribute:b:1>[bar}baz</container:p>',
- '<attribute:b:1></attribute:b:1>',
- '<container:p><attribute:b:1>foo{bar</attribute:b:1>]baz</container:p>'
- );
- } );
- it( 'merges wrapped nodes #3', () => {
- test(
- '<container:p><attribute:b:1>foobar</attribute:b:1>[baz]</container:p>',
- '<attribute:b:1></attribute:b:1>',
- '<container:p><attribute:b:1>foobar{baz</attribute:b:1>]</container:p>'
- );
- } );
- it( 'merges wrapped nodes #4', () => {
- test(
- '<container:p>[foo<attribute:i:1>bar</attribute:i:1>]baz</container:p>',
- '<attribute:b:1></attribute:b:1>',
- '<container:p>[<attribute:b:1>foo<attribute:i:1>bar</attribute:i:1></attribute:b:1>]baz</container:p>'
- );
- } );
- it( 'merges wrapped nodes #5', () => {
- test(
- '<container:p>[foo<attribute:i:1>bar</attribute:i:1>baz]</container:p>',
- '<attribute:b:2></attribute:b:2>',
- '<container:p>' +
- '[' +
- '<attribute:b:2>foo</attribute:b:2>' +
- '<attribute:i:1>' +
- '<attribute:b:2>bar</attribute:b:2>' +
- '</attribute:i:1>' +
- '<attribute:b:2>baz</attribute:b:2>' +
- ']' +
- '</container:p>'
- );
- } );
- it( 'should wrap single element by merging attributes', () => {
- test(
- '<container:p>[<attribute:b:1 foo="bar" one="two"></attribute:b:1>]</container:p>',
- '<attribute:b:1 baz="qux" one="two"></attribute:b:1>',
- '<container:p>[<attribute:b:1 baz="qux" foo="bar" one="two"></attribute:b:1>]</container:p>'
- );
- } );
- it( 'should not merge attributes when they differ', () => {
- test(
- '<container:p>[<attribute:b:1 foo="bar"></attribute:b:1>]</container:p>',
- '<attribute:b:1 foo="baz"></attribute:b:1>',
- '<container:p>[<attribute:b:1 foo="baz"><attribute:b:1 foo="bar"></attribute:b:1></attribute:b:1>]</container:p>'
- );
- } );
- it( 'should wrap single element by merging classes', () => {
- test(
- '<container:p>[<attribute:b:1 class="foo bar baz"></attribute:b:1>]</container:p>',
- '<attribute:b:1 class="foo bar qux jax"></attribute:b:1>',
- '<container:p>[<attribute:b:1 class="foo bar baz qux jax"></attribute:b:1>]</container:p>'
- );
- } );
- it( 'should wrap single element by merging styles', () => {
- test(
- '<container:p>[<attribute:b:1 style="color:red; position: absolute;"></attribute:b:1>]</container:p>',
- '<attribute:b:1 style="color:red; top: 20px;"></attribute:b:1>',
- '<container:p>[<attribute:b:1 style="color:red;position:absolute;top:20px;"></attribute:b:1>]</container:p>'
- );
- } );
- it( 'should not merge styles when they differ', () => {
- test(
- '<container:p>[<attribute:b:1 style="color:red;"></attribute:b:1>]</container:p>',
- '<attribute:b:1 style="color:black;"></attribute:b:1>',
- '<container:p>' +
- '[' +
- '<attribute:b:1 style="color:black;">' +
- '<attribute:b:1 style="color:red;"></attribute:b:1>' +
- '</attribute:b:1>' +
- ']' +
- '</container:p>'
- );
- } );
- it( 'should not merge single elements when they have different priority', () => {
- test(
- '<container:p>[<attribute:b:2 style="color:red;"></attribute:b:2>]</container:p>',
- '<attribute:b:1 style="color:red;"></attribute:b:1>',
- '<container:p>' +
- '[' +
- '<attribute:b:1 style="color:red;">' +
- '<attribute:b:2 style="color:red;"></attribute:b:2>' +
- '</attribute:b:1>' +
- ']</container:p>'
- );
- } );
- it( 'should be merged with outside element when wrapping all children', () => {
- test(
- '<container:p><attribute:b:1 foo="bar">[foobar<attribute:i:1>baz</attribute:i:1>]</attribute:b:1></container:p>',
- '<attribute:b:1 baz="qux"></attribute:b:1>',
- '<container:p>' +
- '[' +
- '<attribute:b:1 baz="qux" foo="bar">' +
- 'foobar' +
- '<attribute:i:1>baz</attribute:i:1>' +
- '</attribute:b:1>' +
- ']' +
- '</container:p>'
- );
- } );
- } );
- } );
|