wrapdelta.js 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /**
  2. * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* bender-tags: treemodel, delta */
  6. 'use strict';
  7. import Document from '/ckeditor5/core/treemodel/document.js';
  8. import Position from '/ckeditor5/core/treemodel/position.js';
  9. import Range from '/ckeditor5/core/treemodel/range.js';
  10. import Element from '/ckeditor5/core/treemodel/element.js';
  11. import CKEditorError from '/ckeditor5/core/ckeditorerror.js';
  12. describe( 'Batch', () => {
  13. let doc, root, range;
  14. beforeEach( () => {
  15. doc = new Document();
  16. root = doc.createRoot( 'root' );
  17. root.insertChildren( 0, 'foobar' );
  18. range = new Range( new Position( root, [ 2 ] ), new Position( root, [ 4 ] ) );
  19. } );
  20. describe( 'wrap', () => {
  21. it( 'should wrap flat range with given element', () => {
  22. let p = new Element( 'p' );
  23. doc.batch().wrap( range, p );
  24. expect( root.getChildCount() ).to.equal( 5 );
  25. expect( root.getChild( 0 ).character ).to.equal( 'f' );
  26. expect( root.getChild( 1 ).character ).to.equal( 'o' );
  27. expect( root.getChild( 2 ) ).to.equal( p );
  28. expect( p.getChild( 0 ).character ).to.equal( 'o' );
  29. expect( p.getChild( 1 ).character ).to.equal( 'b' );
  30. expect( root.getChild( 3 ).character ).to.equal( 'a' );
  31. expect( root.getChild( 4 ).character ).to.equal( 'r' );
  32. } );
  33. it( 'should wrap flat range with an element of given name', () => {
  34. doc.batch().wrap( range, 'p' );
  35. expect( root.getChildCount() ).to.equal( 5 );
  36. expect( root.getChild( 0 ).character ).to.equal( 'f' );
  37. expect( root.getChild( 1 ).character ).to.equal( 'o' );
  38. expect( root.getChild( 2 ).name ).to.equal( 'p' );
  39. expect( root.getChild( 2 ).getChild( 0 ).character ).to.equal( 'o' );
  40. expect( root.getChild( 2 ).getChild( 1 ).character ).to.equal( 'b' );
  41. expect( root.getChild( 3 ).character ).to.equal( 'a' );
  42. expect( root.getChild( 4 ).character ).to.equal( 'r' );
  43. } );
  44. it( 'should throw if range to wrap is not flat', () => {
  45. root.insertChildren( 6, [ new Element( 'p', [], 'xyz' ) ] );
  46. let notFlatRange = new Range( new Position( root, [ 3 ] ), new Position( root, [ 6, 2 ] ) );
  47. expect( () => {
  48. doc.batch().wrap( notFlatRange, 'p' );
  49. } ).to.throw( CKEditorError, /^batch-wrap-range-not-flat/ );
  50. } );
  51. it( 'should throw if element to wrap with has children', () => {
  52. let p = new Element( 'p', [], 'a' );
  53. expect( () => {
  54. doc.batch().wrap( range, p );
  55. } ).to.throw( CKEditorError, /^batch-wrap-element-not-empty/ );
  56. } );
  57. it( 'should throw if element to wrap with has children', () => {
  58. let p = new Element( 'p' );
  59. root.insertChildren( 0, p );
  60. expect( () => {
  61. doc.batch().wrap( range, p );
  62. } ).to.throw( CKEditorError, /^batch-wrap-element-attached/ );
  63. } );
  64. it( 'should be chainable', () => {
  65. const batch = doc.batch();
  66. const chain = batch.wrap( range, 'p' );
  67. expect( chain ).to.equal( batch );
  68. } );
  69. } );
  70. } );