documentfragment.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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 Element from '/ckeditor5/engine/model/element.js';
  8. import Text from '/ckeditor5/engine/model/text.js';
  9. import DocumentFragment from '/ckeditor5/engine/model/documentfragment.js';
  10. import { jsonParseStringify } from '/tests/engine/model/_utils/utils.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. expect( frag.getMaxOffset() ).to.equal( 0 );
  17. } );
  18. it( 'should create document fragment with children', () => {
  19. let frag = new DocumentFragment( [ new Text( 'xx' ), new Element( 'p' ), new Text( 'yy' ) ] );
  20. expect( frag.getChildCount() ).to.equal( 3 );
  21. expect( frag.getMaxOffset() ).to.equal( 5 );
  22. expect( frag.getChild( 0 ) ).to.have.property( 'data' ).that.equals( 'xx' );
  23. expect( frag.getChild( 1 ) ).to.have.property( 'name' ).that.equals( 'p' );
  24. expect( frag.getChild( 2 ) ).to.have.property( 'data' ).that.equals( 'yy' );
  25. } );
  26. it( 'should have root property, equal to itself', () => {
  27. let frag = new DocumentFragment();
  28. expect( frag ).to.have.property( 'root' ).that.equals( frag );
  29. } );
  30. } );
  31. describe( 'isEmpty', () => {
  32. it( 'should return true if document fragment has no children', () => {
  33. let frag = new DocumentFragment();
  34. expect( frag.isEmpty() ).to.be.true;
  35. } );
  36. it( 'should return false if document fragment has children', () => {
  37. let frag = new DocumentFragment( new Text( 'a' ) );
  38. expect( frag.isEmpty() ).to.be.false;
  39. } );
  40. } );
  41. describe( 'getPath', () => {
  42. it( 'should return empty array', () => {
  43. let frag = new DocumentFragment( [ new Text( 'x' ), new Element( 'p' ), new Text( 'y' ) ] );
  44. expect( frag.getPath() ).to.deep.equal( [] );
  45. } );
  46. } );
  47. describe( 'insertChildren', () => {
  48. it( 'should add children to the document fragment', () => {
  49. let frag = new DocumentFragment( new Text( 'xy' ) );
  50. frag.insertChildren( 1, new Text( 'foo' ) );
  51. expect( frag.getChildCount() ).to.equal( 2 );
  52. expect( frag.getMaxOffset() ).to.equal( 5 );
  53. expect( frag.getChild( 0 ) ).to.have.property( 'data' ).that.equals( 'xy' );
  54. expect( frag.getChild( 1 ) ).to.have.property( 'data' ).that.equals( 'foo' );
  55. } );
  56. } );
  57. describe( 'appendChildren', () => {
  58. it( 'should add children to the end of the element', () => {
  59. let frag = new DocumentFragment( new Text( 'xy' ) );
  60. frag.appendChildren( new Text( 'foo' ) );
  61. expect( frag.getChildCount() ).to.equal( 2 );
  62. expect( frag.getMaxOffset() ).to.equal( 5 );
  63. expect( frag.getChild( 0 ) ).to.have.property( 'data' ).that.equals( 'xy' );
  64. expect( frag.getChild( 1 ) ).to.have.property( 'data' ).that.equals( 'foo' );
  65. } );
  66. } );
  67. describe( 'removeChildren', () => {
  68. it( 'should remove children from the element and return them as an array', () => {
  69. let frag = new DocumentFragment( [ new Text( 'foobar' ), new Element( 'image' ) ] );
  70. let removed = frag.removeChildren( 1, 1 );
  71. expect( frag.getChildCount() ).to.equal( 1 );
  72. expect( frag.getMaxOffset() ).to.equal( 6 );
  73. expect( frag.getChild( 0 ) ).to.have.property( 'data' ).that.equals( 'foobar' );
  74. expect( removed.length ).to.equal( 1 );
  75. expect( removed[ 0 ].name ).to.equal( 'image' );
  76. } );
  77. it( 'should remove one child when second parameter is not specified', () => {
  78. let frag = new DocumentFragment( [ new Text( 'foo' ), new Element( 'image' ) ] );
  79. let removed = frag.removeChildren( 0 );
  80. expect( frag.getChildCount() ).to.equal( 1 );
  81. expect( frag.getMaxOffset() ).to.equal( 1 );
  82. expect( frag.getChild( 0 ).name ).to.equal( 'image' );
  83. expect( removed.length ).to.equal( 1 );
  84. expect( removed[ 0 ].data ).to.equal( 'foo' );
  85. } );
  86. } );
  87. describe( 'getChildIndex', () => {
  88. it( 'should return child index', () => {
  89. let frag = new DocumentFragment( [ new Element( 'p' ), new Text( 'bar' ), new Element( 'h' ) ] );
  90. let p = frag.getChild( 0 );
  91. let textBAR = frag.getChild( 1 );
  92. let h = frag.getChild( 2 );
  93. expect( frag.getChildIndex( p ) ).to.equal( 0 );
  94. expect( frag.getChildIndex( textBAR ) ).to.equal( 1 );
  95. expect( frag.getChildIndex( h ) ).to.equal( 2 );
  96. } );
  97. } );
  98. describe( 'getChildStartOffset', () => {
  99. it( 'should return child start offset', () => {
  100. let frag = new DocumentFragment( [ new Element( 'p' ), new Text( 'bar' ), new Element( 'h' ) ] );
  101. let p = frag.getChild( 0 );
  102. let textBAR = frag.getChild( 1 );
  103. let h = frag.getChild( 2 );
  104. expect( frag.getChildStartOffset( p ) ).to.equal( 0 );
  105. expect( frag.getChildStartOffset( textBAR ) ).to.equal( 1 );
  106. expect( frag.getChildStartOffset( h ) ).to.equal( 4 );
  107. } );
  108. it( 'should return null if node is not a child of that document fragment', () => {
  109. let frag = new DocumentFragment( [ new Element( 'p' ), new Text( 'bar' ), new Element( 'h' ) ] );
  110. let p = new Element( 'p' );
  111. expect( frag.getChildStartOffset( p ) ).to.equal( null );
  112. } );
  113. } );
  114. describe( 'getChildCount', () => {
  115. it( 'should return number of children nodes', () => {
  116. let frag = new DocumentFragment( new Text( 'bar' ) );
  117. expect( frag.getChildCount() ).to.equal( 1 );
  118. } );
  119. } );
  120. describe( 'getMaxOffset', () => {
  121. it( 'should return offset after the last children', () => {
  122. let frag = new DocumentFragment( new Text( 'bar' ) );
  123. expect( frag.getMaxOffset() ).to.equal( 3 );
  124. } );
  125. } );
  126. describe( 'toJSON', () => {
  127. it( 'should serialize empty document fragment', () => {
  128. let frag = new DocumentFragment();
  129. expect( jsonParseStringify( frag ) ).to.deep.equal( [] );
  130. } );
  131. it( 'should serialize document fragment with children', () => {
  132. let img = new Element( 'img' );
  133. let one = new Element( 'one' );
  134. let two = new Element( 'two', null, [ new Text( 'ba' ), img, new Text( 'r' ) ] );
  135. let three = new Element( 'three' );
  136. let frag = new DocumentFragment( [ one, two, three ] );
  137. expect( jsonParseStringify( frag ) ).to.deep.equal( [
  138. { name: 'one' },
  139. {
  140. name: 'two',
  141. children: [
  142. { data: 'ba' },
  143. { name: 'img' },
  144. { data: 'r' }
  145. ]
  146. },
  147. { name: 'three' }
  148. ] );
  149. } );
  150. } );
  151. describe( 'fromJSON', () => {
  152. it( 'should create document fragment without children', () => {
  153. const frag = new DocumentFragment();
  154. let serialized = jsonParseStringify( frag );
  155. let deserialized = DocumentFragment.fromJSON( serialized );
  156. expect( deserialized.isEmpty() ).to.be.true;
  157. } );
  158. it( 'should create element with children', () => {
  159. const p = new Element( 'p' );
  160. const foo = new Text( 'foo' );
  161. const frag = new DocumentFragment( [ p, foo ] );
  162. let serialized = jsonParseStringify( frag );
  163. let deserialized = DocumentFragment.fromJSON( serialized );
  164. expect( deserialized.getChildCount() ).to.equal( 2 );
  165. expect( deserialized.getChild( 0 ).name ).to.equal( 'p' );
  166. expect( deserialized.getChild( 0 ).parent ).to.equal( deserialized );
  167. expect( deserialized.getChild( 1 ).data ).to.equal( 'foo' );
  168. expect( deserialized.getChild( 1 ).parent ).to.equal( deserialized );
  169. } );
  170. } );
  171. } );