documentfragment.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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 Node from '/ckeditor5/engine/treemodel/node.js';
  8. import NodeList from '/ckeditor5/engine/treemodel/nodelist.js';
  9. import Element from '/ckeditor5/engine/treemodel/element.js';
  10. import DocumentFragment from '/ckeditor5/engine/treemodel/documentfragment.js';
  11. describe( 'DocumentFragment', () => {
  12. describe( 'constructor', () => {
  13. it( 'should create empty document fragment', () => {
  14. let frag = new DocumentFragment();
  15. expect( frag.getChildCount() ).to.equal( 0 );
  16. } );
  17. it( 'should create document fragment with children', () => {
  18. let frag = new DocumentFragment( [ 'x', new Element( 'p' ), 'y' ] );
  19. expect( frag.getChildCount() ).to.equal( 3 );
  20. expect( frag.getChild( 0 ) ).to.have.property( 'character' ).that.equals( 'x' );
  21. expect( frag.getChild( 1 ) ).to.have.property( 'name' ).that.equals( 'p' );
  22. expect( frag.getChild( 2 ) ).to.have.property( 'character' ).that.equals( 'y' );
  23. } );
  24. } );
  25. describe( 'insertChildren', () => {
  26. it( 'should add children to the document fragment', () => {
  27. let frag = new DocumentFragment( 'xy' );
  28. frag.insertChildren( 1, 'foo' );
  29. expect( frag.getChildCount() ).to.equal( 5 );
  30. expect( frag.getChild( 0 ) ).to.have.property( 'character' ).that.equals( 'x' );
  31. expect( frag.getChild( 1 ) ).to.have.property( 'character' ).that.equals( 'f' );
  32. expect( frag.getChild( 2 ) ).to.have.property( 'character' ).that.equals( 'o' );
  33. expect( frag.getChild( 3 ) ).to.have.property( 'character' ).that.equals( 'o' );
  34. expect( frag.getChild( 4 ) ).to.have.property( 'character' ).that.equals( 'y' );
  35. } );
  36. } );
  37. describe( 'appendChildren', () => {
  38. it( 'should add children to the end of the element', () => {
  39. let frag = new DocumentFragment( 'xy' );
  40. frag.appendChildren( 'foo' );
  41. expect( frag.getChildCount() ).to.equal( 5 );
  42. expect( frag.getChild( 0 ) ).to.have.property( 'character' ).that.equals( 'x' );
  43. expect( frag.getChild( 1 ) ).to.have.property( 'character' ).that.equals( 'y' );
  44. expect( frag.getChild( 2 ) ).to.have.property( 'character' ).that.equals( 'f' );
  45. expect( frag.getChild( 3 ) ).to.have.property( 'character' ).that.equals( 'o' );
  46. expect( frag.getChild( 4 ) ).to.have.property( 'character' ).that.equals( 'o' );
  47. } );
  48. } );
  49. describe( 'removeChildren', () => {
  50. it( 'should remove children from the element and return them as a NodeList', () => {
  51. let frag = new DocumentFragment( 'foobar' );
  52. let removed = frag.removeChildren( 2, 3 );
  53. expect( frag.getChildCount() ).to.equal( 3 );
  54. expect( frag.getChild( 0 ) ).to.have.property( 'character' ).that.equals( 'f' );
  55. expect( frag.getChild( 1 ) ).to.have.property( 'character' ).that.equals( 'o' );
  56. expect( frag.getChild( 2 ) ).to.have.property( 'character' ).that.equals( 'r' );
  57. expect( removed ).to.be.instanceof( NodeList );
  58. expect( removed.length ).to.equal( 3 );
  59. expect( removed.get( 0 ).character ).to.equal( 'o' );
  60. expect( removed.get( 1 ).character ).to.equal( 'b' );
  61. expect( removed.get( 2 ).character ).to.equal( 'a' );
  62. } );
  63. it( 'should remove one child when second parameter is not specified', () => {
  64. let frag = new DocumentFragment( 'foo' );
  65. let removed = frag.removeChildren( 2 );
  66. expect( frag.getChildCount() ).to.equal( 2 );
  67. expect( frag.getChild( 0 ) ).to.have.property( 'character' ).that.equals( 'f' );
  68. expect( frag.getChild( 1 ) ).to.have.property( 'character' ).that.equals( 'o' );
  69. expect( removed ).to.be.instanceof( NodeList );
  70. expect( removed.length ).to.equal( 1 );
  71. expect( removed.get( 0 ).character ).to.equal( 'o' );
  72. } );
  73. } );
  74. describe( 'getChildIndex', () => {
  75. it( 'should return child index', () => {
  76. let frag = new DocumentFragment( [ new Element( 'p' ), 'bar', new Element( 'h' ) ] );
  77. let p = frag.getChild( 0 );
  78. let b = frag.getChild( 1 );
  79. let a = frag.getChild( 2 );
  80. let r = frag.getChild( 3 );
  81. let h = frag.getChild( 4 );
  82. expect( frag.getChildIndex( p ) ).to.equal( 0 );
  83. expect( frag.getChildIndex( b ) ).to.equal( 1 );
  84. expect( frag.getChildIndex( a ) ).to.equal( 2 );
  85. expect( frag.getChildIndex( r ) ).to.equal( 3 );
  86. expect( frag.getChildIndex( h ) ).to.equal( 4 );
  87. } );
  88. } );
  89. describe( 'getChildCount', () => {
  90. it( 'should return number of children', () => {
  91. let frag = new DocumentFragment( 'bar' );
  92. expect( frag.getChildCount() ).to.equal( 3 );
  93. } );
  94. } );
  95. } );