wrapdelta.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. /* bender-include: ../../_tools/tools.js */
  7. 'use strict';
  8. const modules = bender.amd.require(
  9. 'core/treemodel/document',
  10. 'core/treemodel/position',
  11. 'core/treemodel/range',
  12. 'core/treemodel/element',
  13. 'core/ckeditorerror'
  14. );
  15. describe( 'Batch', () => {
  16. let Document, Position, Range, Element, CKEditorError;
  17. let doc, root, range;
  18. before( () => {
  19. Document = modules[ 'core/treemodel/document' ];
  20. Position = modules[ 'core/treemodel/position' ];
  21. Range = modules[ 'core/treemodel/range' ];
  22. Element = modules[ 'core/treemodel/element' ];
  23. CKEditorError = modules[ 'core/ckeditorerror' ];
  24. } );
  25. beforeEach( () => {
  26. doc = new Document();
  27. root = doc.createRoot( 'root' );
  28. root.insertChildren( 0, 'foobar' );
  29. range = new Range( new Position( root, [ 2 ] ), new Position( root, [ 4 ] ) );
  30. } );
  31. describe( 'wrap', () => {
  32. it( 'should wrap flat range with given element', () => {
  33. let p = new Element( 'p' );
  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 ) ).to.equal( p );
  39. expect( p.getChild( 0 ).character ).to.equal( 'o' );
  40. expect( p.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 wrap flat range with an element of given name', () => {
  45. doc.batch().wrap( range, 'p' );
  46. expect( root.getChildCount() ).to.equal( 5 );
  47. expect( root.getChild( 0 ).character ).to.equal( 'f' );
  48. expect( root.getChild( 1 ).character ).to.equal( 'o' );
  49. expect( root.getChild( 2 ).name ).to.equal( 'p' );
  50. expect( root.getChild( 2 ).getChild( 0 ).character ).to.equal( 'o' );
  51. expect( root.getChild( 2 ).getChild( 1 ).character ).to.equal( 'b' );
  52. expect( root.getChild( 3 ).character ).to.equal( 'a' );
  53. expect( root.getChild( 4 ).character ).to.equal( 'r' );
  54. } );
  55. it( 'should throw if range to wrap is not flat', () => {
  56. root.insertChildren( 6, [ new Element( 'p', [], 'xyz' ) ] );
  57. let notFlatRange = new Range( new Position( root, [ 3 ] ), new Position( root, [ 6, 2 ] ) );
  58. expect( () => {
  59. doc.batch().wrap( notFlatRange, 'p' );
  60. } ).to.throw( CKEditorError, /^batch-wrap-range-not-flat/ );
  61. } );
  62. it( 'should throw if element to wrap with has children', () => {
  63. let p = new Element( 'p', [], 'a' );
  64. expect( () => {
  65. doc.batch().wrap( range, p );
  66. } ).to.throw( CKEditorError, /^batch-wrap-element-not-empty/ );
  67. } );
  68. it( 'should throw if element to wrap with has children', () => {
  69. let p = new Element( 'p' );
  70. root.insertChildren( 0, p );
  71. expect( () => {
  72. doc.batch().wrap( range, p );
  73. } ).to.throw( CKEditorError, /^batch-wrap-element-attached/ );
  74. } );
  75. it( 'should be chainable', () => {
  76. const batch = doc.batch();
  77. const chain = batch.wrap( range, 'p' );
  78. expect( chain ).to.equal( batch );
  79. } );
  80. } );
  81. } );