documentfragment.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /**
  2. * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* bender-tags: model */
  6. import NodeList from '/ckeditor5/engine/model/nodelist.js';
  7. import Element from '/ckeditor5/engine/model/element.js';
  8. import DocumentFragment from '/ckeditor5/engine/model/documentfragment.js';
  9. describe( 'DocumentFragment', () => {
  10. describe( 'constructor', () => {
  11. it( 'should create empty document fragment', () => {
  12. let frag = new DocumentFragment();
  13. expect( frag.getChildCount() ).to.equal( 0 );
  14. } );
  15. it( 'should create document fragment with children', () => {
  16. let frag = new DocumentFragment( [ 'x', new Element( 'p' ), 'y' ] );
  17. expect( frag.getChildCount() ).to.equal( 3 );
  18. expect( frag.getChild( 0 ) ).to.have.property( 'character' ).that.equals( 'x' );
  19. expect( frag.getChild( 1 ) ).to.have.property( 'name' ).that.equals( 'p' );
  20. expect( frag.getChild( 2 ) ).to.have.property( 'character' ).that.equals( 'y' );
  21. } );
  22. it( 'should have root property, equal to itself', () => {
  23. let frag = new DocumentFragment();
  24. expect( frag ).to.have.property( 'root' ).that.equals( frag );
  25. } );
  26. } );
  27. describe( 'iterator', () => {
  28. it( 'should iterate over all nodes added to document fragment', () => {
  29. let frag = new DocumentFragment( [ 'x', new Element( 'p' ), 'y' ] );
  30. let arr = Array.from( frag );
  31. expect( arr.length ).to.equal( 3 );
  32. expect( arr[ 0 ] ).to.have.property( 'character' ).that.equals( 'x' );
  33. expect( arr[ 1 ] ).to.have.property( 'name' ).that.equals( 'p' );
  34. expect( arr[ 2 ] ).to.have.property( 'character' ).that.equals( 'y' );
  35. } );
  36. } );
  37. describe( 'getPath', () => {
  38. it( 'should return empty array', () => {
  39. let frag = new DocumentFragment( [ 'x', new Element( 'p' ), 'y' ] );
  40. expect( frag.getPath() ).to.deep.equal( [] );
  41. } );
  42. } );
  43. describe( 'insertChildren', () => {
  44. it( 'should add children to the document fragment', () => {
  45. let frag = new DocumentFragment( 'xy' );
  46. frag.insertChildren( 1, 'foo' );
  47. expect( frag.getChildCount() ).to.equal( 5 );
  48. expect( frag.getChild( 0 ) ).to.have.property( 'character' ).that.equals( 'x' );
  49. expect( frag.getChild( 1 ) ).to.have.property( 'character' ).that.equals( 'f' );
  50. expect( frag.getChild( 2 ) ).to.have.property( 'character' ).that.equals( 'o' );
  51. expect( frag.getChild( 3 ) ).to.have.property( 'character' ).that.equals( 'o' );
  52. expect( frag.getChild( 4 ) ).to.have.property( 'character' ).that.equals( 'y' );
  53. } );
  54. it( 'should accept DocumentFragment as a parameter and clean it after it is added', () => {
  55. let p1 = new Element( 'p' );
  56. let p2 = new Element( 'p' );
  57. let otherFrag = new DocumentFragment( [ p1, p2 ] );
  58. let frag = new DocumentFragment();
  59. frag.insertChildren( 0, otherFrag );
  60. expect( frag.getChildCount() ).to.equal( 2 );
  61. expect( frag.getChild( 0 ) ).to.equal( p1 );
  62. expect( frag.getChild( 1 ) ).to.equal( p2 );
  63. expect( otherFrag.getChildCount() ).to.equal( 0 );
  64. } );
  65. } );
  66. describe( 'appendChildren', () => {
  67. it( 'should add children to the end of the element', () => {
  68. let frag = new DocumentFragment( 'xy' );
  69. frag.appendChildren( 'foo' );
  70. expect( frag.getChildCount() ).to.equal( 5 );
  71. expect( frag.getChild( 0 ) ).to.have.property( 'character' ).that.equals( 'x' );
  72. expect( frag.getChild( 1 ) ).to.have.property( 'character' ).that.equals( 'y' );
  73. expect( frag.getChild( 2 ) ).to.have.property( 'character' ).that.equals( 'f' );
  74. expect( frag.getChild( 3 ) ).to.have.property( 'character' ).that.equals( 'o' );
  75. expect( frag.getChild( 4 ) ).to.have.property( 'character' ).that.equals( 'o' );
  76. } );
  77. } );
  78. describe( 'removeChildren', () => {
  79. it( 'should remove children from the element and return them as a NodeList', () => {
  80. let frag = new DocumentFragment( 'foobar' );
  81. let removed = frag.removeChildren( 2, 3 );
  82. expect( frag.getChildCount() ).to.equal( 3 );
  83. expect( frag.getChild( 0 ) ).to.have.property( 'character' ).that.equals( 'f' );
  84. expect( frag.getChild( 1 ) ).to.have.property( 'character' ).that.equals( 'o' );
  85. expect( frag.getChild( 2 ) ).to.have.property( 'character' ).that.equals( 'r' );
  86. expect( removed ).to.be.instanceof( NodeList );
  87. expect( removed.length ).to.equal( 3 );
  88. expect( removed.get( 0 ).character ).to.equal( 'o' );
  89. expect( removed.get( 1 ).character ).to.equal( 'b' );
  90. expect( removed.get( 2 ).character ).to.equal( 'a' );
  91. } );
  92. it( 'should remove one child when second parameter is not specified', () => {
  93. let frag = new DocumentFragment( 'foo' );
  94. let removed = frag.removeChildren( 2 );
  95. expect( frag.getChildCount() ).to.equal( 2 );
  96. expect( frag.getChild( 0 ) ).to.have.property( 'character' ).that.equals( 'f' );
  97. expect( frag.getChild( 1 ) ).to.have.property( 'character' ).that.equals( 'o' );
  98. expect( removed ).to.be.instanceof( NodeList );
  99. expect( removed.length ).to.equal( 1 );
  100. expect( removed.get( 0 ).character ).to.equal( 'o' );
  101. } );
  102. } );
  103. describe( 'getChildIndex', () => {
  104. it( 'should return child index', () => {
  105. let frag = new DocumentFragment( [ new Element( 'p' ), 'bar', new Element( 'h' ) ] );
  106. let p = frag.getChild( 0 );
  107. let b = frag.getChild( 1 );
  108. let a = frag.getChild( 2 );
  109. let r = frag.getChild( 3 );
  110. let h = frag.getChild( 4 );
  111. expect( frag.getChildIndex( p ) ).to.equal( 0 );
  112. expect( frag.getChildIndex( b ) ).to.equal( 1 );
  113. expect( frag.getChildIndex( a ) ).to.equal( 2 );
  114. expect( frag.getChildIndex( r ) ).to.equal( 3 );
  115. expect( frag.getChildIndex( h ) ).to.equal( 4 );
  116. } );
  117. } );
  118. describe( 'getChildCount', () => {
  119. it( 'should return number of children', () => {
  120. let frag = new DocumentFragment( 'bar' );
  121. expect( frag.getChildCount() ).to.equal( 3 );
  122. } );
  123. } );
  124. } );