documentfragment.js 5.7 KB

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