documentfragment.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /**
  2. * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* bender-tags: treemodel */
  6. 'use strict';
  7. import NodeList from '/ckeditor5/engine/treemodel/nodelist.js';
  8. import Element from '/ckeditor5/engine/treemodel/element.js';
  9. import DocumentFragment from '/ckeditor5/engine/treemodel/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. } );
  24. describe( 'insertChildren', () => {
  25. it( 'should add children to the document fragment', () => {
  26. let frag = new DocumentFragment( 'xy' );
  27. frag.insertChildren( 1, 'foo' );
  28. expect( frag.getChildCount() ).to.equal( 5 );
  29. expect( frag.getChild( 0 ) ).to.have.property( 'character' ).that.equals( 'x' );
  30. expect( frag.getChild( 1 ) ).to.have.property( 'character' ).that.equals( 'f' );
  31. expect( frag.getChild( 2 ) ).to.have.property( 'character' ).that.equals( 'o' );
  32. expect( frag.getChild( 3 ) ).to.have.property( 'character' ).that.equals( 'o' );
  33. expect( frag.getChild( 4 ) ).to.have.property( 'character' ).that.equals( 'y' );
  34. } );
  35. it( 'should accept DocumentFragment as a parameter and clean it after it is added', () => {
  36. let p1 = new Element( 'p' );
  37. let p2 = new Element( 'p' );
  38. let otherFrag = new DocumentFragment( [ p1, p2 ] );
  39. let frag = new DocumentFragment();
  40. frag.insertChildren( 0, otherFrag );
  41. expect( frag.getChildCount() ).to.equal( 2 );
  42. expect( frag.getChild( 0 ) ).to.equal( p1 );
  43. expect( frag.getChild( 1 ) ).to.equal( p2 );
  44. expect( otherFrag.getChildCount() ).to.equal( 0 );
  45. } );
  46. } );
  47. describe( 'appendChildren', () => {
  48. it( 'should add children to the end of the element', () => {
  49. let frag = new DocumentFragment( 'xy' );
  50. frag.appendChildren( 'foo' );
  51. expect( frag.getChildCount() ).to.equal( 5 );
  52. expect( frag.getChild( 0 ) ).to.have.property( 'character' ).that.equals( 'x' );
  53. expect( frag.getChild( 1 ) ).to.have.property( 'character' ).that.equals( 'y' );
  54. expect( frag.getChild( 2 ) ).to.have.property( 'character' ).that.equals( 'f' );
  55. expect( frag.getChild( 3 ) ).to.have.property( 'character' ).that.equals( 'o' );
  56. expect( frag.getChild( 4 ) ).to.have.property( 'character' ).that.equals( 'o' );
  57. } );
  58. } );
  59. describe( 'removeChildren', () => {
  60. it( 'should remove children from the element and return them as a NodeList', () => {
  61. let frag = new DocumentFragment( 'foobar' );
  62. let removed = frag.removeChildren( 2, 3 );
  63. expect( frag.getChildCount() ).to.equal( 3 );
  64. expect( frag.getChild( 0 ) ).to.have.property( 'character' ).that.equals( 'f' );
  65. expect( frag.getChild( 1 ) ).to.have.property( 'character' ).that.equals( 'o' );
  66. expect( frag.getChild( 2 ) ).to.have.property( 'character' ).that.equals( 'r' );
  67. expect( removed ).to.be.instanceof( NodeList );
  68. expect( removed.length ).to.equal( 3 );
  69. expect( removed.get( 0 ).character ).to.equal( 'o' );
  70. expect( removed.get( 1 ).character ).to.equal( 'b' );
  71. expect( removed.get( 2 ).character ).to.equal( 'a' );
  72. } );
  73. it( 'should remove one child when second parameter is not specified', () => {
  74. let frag = new DocumentFragment( 'foo' );
  75. let removed = frag.removeChildren( 2 );
  76. expect( frag.getChildCount() ).to.equal( 2 );
  77. expect( frag.getChild( 0 ) ).to.have.property( 'character' ).that.equals( 'f' );
  78. expect( frag.getChild( 1 ) ).to.have.property( 'character' ).that.equals( 'o' );
  79. expect( removed ).to.be.instanceof( NodeList );
  80. expect( removed.length ).to.equal( 1 );
  81. expect( removed.get( 0 ).character ).to.equal( 'o' );
  82. } );
  83. } );
  84. describe( 'getChildIndex', () => {
  85. it( 'should return child index', () => {
  86. let frag = new DocumentFragment( [ new Element( 'p' ), 'bar', new Element( 'h' ) ] );
  87. let p = frag.getChild( 0 );
  88. let b = frag.getChild( 1 );
  89. let a = frag.getChild( 2 );
  90. let r = frag.getChild( 3 );
  91. let h = frag.getChild( 4 );
  92. expect( frag.getChildIndex( p ) ).to.equal( 0 );
  93. expect( frag.getChildIndex( b ) ).to.equal( 1 );
  94. expect( frag.getChildIndex( a ) ).to.equal( 2 );
  95. expect( frag.getChildIndex( r ) ).to.equal( 3 );
  96. expect( frag.getChildIndex( h ) ).to.equal( 4 );
  97. } );
  98. } );
  99. describe( 'getChildCount', () => {
  100. it( 'should return number of children', () => {
  101. let frag = new DocumentFragment( 'bar' );
  102. expect( frag.getChildCount() ).to.equal( 3 );
  103. } );
  104. } );
  105. } );