element.js 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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 Node from '/ckeditor5/engine/model/node.js';
  7. import Element from '/ckeditor5/engine/model/element.js';
  8. import Text from '/ckeditor5/engine/model/text.js';
  9. import { jsonParseStringify } from '/tests/engine/model/_utils/utils.js';
  10. import count from '/ckeditor5/utils/count.js';
  11. describe( 'Element', () => {
  12. describe( 'constructor', () => {
  13. it( 'should create empty element', () => {
  14. let element = new Element( 'elem' );
  15. expect( element ).to.be.an.instanceof( Node );
  16. expect( element ).to.have.property( 'name' ).that.equals( 'elem' );
  17. expect( count( element.getAttributes() ) ).to.equal( 0 );
  18. expect( count( element.getChildren() ) ).to.equal( 0 );
  19. } );
  20. it( 'should create element with attributes', () => {
  21. let element = new Element( 'elem', { foo: 'bar' } );
  22. expect( count( element.getAttributes() ) ).to.equal( 1 );
  23. expect( element.getAttribute( 'foo' ) ).to.equal( 'bar' );
  24. } );
  25. it( 'should create element with children', () => {
  26. let element = new Element( 'elem', [], new Text( 'foo' ) );
  27. expect( element.getChildCount() ).to.equal( 1 );
  28. expect( element.getMaxOffset() ).to.equal( 3 );
  29. expect( element.getChild( 0 ).data ).to.equal( 'foo' );
  30. } );
  31. } );
  32. describe( 'clone', () => {
  33. it( 'should return an element with same name, attributes and same instances of children if clone was not deep', () => {
  34. let p = new Element( 'p' );
  35. let foo = new Text( 'foo' );
  36. let element = new Element( 'elem', { bold: true, italic: true }, [ p, foo ] );
  37. let copy = element.clone();
  38. expect( copy.name ).to.equal( 'elem' );
  39. expect( Array.from( copy.getAttributes() ) ).to.deep.equal( [ [ 'bold', true ], [ 'italic', true ] ] );
  40. expect( Array.from( copy.getChildren() ) ).to.deep.equal( [ p, foo ] );
  41. } );
  42. it( 'should clone children, if clone is deep', () => {
  43. let p = new Element( 'p' );
  44. let foo = new Text( 'foo' );
  45. let element = new Element( 'elem', { bold: true, italic: true }, [ p, foo ] );
  46. let copy = element.clone( true );
  47. expect( copy.name ).to.equal( 'elem' );
  48. expect( Array.from( copy.getAttributes() ) ).to.deep.equal( [ [ 'bold', true ], [ 'italic', true ] ] );
  49. expect( copy.getChildCount() ).to.equal( 2 );
  50. expect( copy.getChild( 0 ) ).not.to.equal( p );
  51. expect( copy.getChild( 1 ) ).not.to.equal( foo );
  52. expect( copy.getChild( 0 ).name ).to.equal( 'p' );
  53. expect( copy.getChild( 1 ).data ).to.equal( 'foo' );
  54. } );
  55. } );
  56. describe( 'insertChildren', () => {
  57. it( 'should add a child to the element', () => {
  58. let element = new Element( 'elem', [], new Text( 'xy' ) );
  59. element.insertChildren( 1, new Text( 'foo' ) );
  60. expect( element.getChildCount() ).to.equal( 2 );
  61. expect( element.getMaxOffset() ).to.equal( 5 );
  62. expect( element.getChild( 0 ).data ).to.equal( 'xy' );
  63. expect( element.getChild( 1 ).data ).to.equal( 'foo' );
  64. } );
  65. it( 'should add children to the element', () => {
  66. let element = new Element( 'elem' );
  67. element.insertChildren( 0, [ new Element( 'image' ), new Text( 'xy' ), new Element( 'list' ) ] );
  68. expect( element.getChildCount() ).to.equal( 3 );
  69. expect( element.getMaxOffset() ).to.equal( 4 );
  70. expect( element.getChild( 0 ).name ).to.equal( 'image' );
  71. expect( element.getChild( 1 ).data ).to.equal( 'xy' );
  72. expect( element.getChild( 2 ).name ).to.equal( 'list' );
  73. } );
  74. } );
  75. describe( 'appendChildren', () => {
  76. it( 'should use insertChildren to add children at the end of the element', () => {
  77. let element = new Element( 'elem', [], new Text( 'xy' ) );
  78. sinon.spy( element, 'insertChildren' );
  79. let text = new Text( 'foo' );
  80. element.appendChildren( text );
  81. expect( element.insertChildren.calledWithExactly( 0, text ) );
  82. } );
  83. } );
  84. describe( 'removeChildren', () => {
  85. it( 'should remove children from the element and return them as an array', () => {
  86. let element = new Element( 'elem', [], [ new Text( 'foobar' ), new Element( 'image' ) ] );
  87. let removed = element.removeChildren( 1, 1 );
  88. expect( element.getChildCount() ).to.equal( 1 );
  89. expect( element.getMaxOffset() ).to.equal( 6 );
  90. expect( element.getChild( 0 ).data ).to.equal( 'foobar' );
  91. expect( removed.length ).to.equal( 1 );
  92. expect( removed[ 0 ].name ).to.equal( 'image' );
  93. } );
  94. it( 'should remove one child when second parameter is not specified', () => {
  95. let element = new Element( 'element', [], [ new Text( 'foo' ), new Element( 'image' ) ] );
  96. let removed = element.removeChildren( 0 );
  97. expect( element.getChildCount() ).to.equal( 1 );
  98. expect( element.getMaxOffset() ).to.equal( 1 );
  99. expect( element.getChild( 0 ).name ).to.equal( 'image' );
  100. expect( removed.length ).to.equal( 1 );
  101. expect( removed[ 0 ].data ).to.equal( 'foo' );
  102. } );
  103. } );
  104. describe( 'getChildIndex', () => {
  105. it( 'should return child index', () => {
  106. let element = new Element( 'elem', [], [ new Element( 'p' ), new Text( 'bar' ), new Element( 'h' ) ] );
  107. let p = element.getChild( 0 );
  108. let bar = element.getChild( 1 );
  109. let h = element.getChild( 2 );
  110. expect( element.getChildIndex( p ) ).to.equal( 0 );
  111. expect( element.getChildIndex( bar ) ).to.equal( 1 );
  112. expect( element.getChildIndex( h ) ).to.equal( 2 );
  113. } );
  114. } );
  115. describe( 'getChildStartOffset', () => {
  116. it( 'should return child offset', () => {
  117. let element = new Element( 'elem', [], [ new Element( 'p' ), new Text( 'bar' ), new Element( 'h' ) ] );
  118. let p = element.getChild( 0 );
  119. let bar = element.getChild( 1 );
  120. let h = element.getChild( 2 );
  121. expect( element.getChildStartOffset( p ) ).to.equal( 0 );
  122. expect( element.getChildStartOffset( bar ) ).to.equal( 1 );
  123. expect( element.getChildStartOffset( h ) ).to.equal( 4 );
  124. } );
  125. } );
  126. describe( 'getChildCount', () => {
  127. it( 'should return number of children', () => {
  128. let element = new Element( 'elem', [], new Text( 'bar' ) );
  129. expect( element.getChildCount() ).to.equal( 1 );
  130. } );
  131. } );
  132. describe( 'getMaxOffset', () => {
  133. it( 'should return offset number after the last child', () => {
  134. let element = new Element( 'elem', [], [ new Element( 'p' ), new Text( 'bar' ), new Element( 'h' ) ] );
  135. expect( element.getMaxOffset() ).to.equal( 5 );
  136. } );
  137. } );
  138. describe( 'isEmpty', () => {
  139. it( 'checks whether element has no children', () => {
  140. expect( new Element( 'a' ).isEmpty() ).to.be.true;
  141. expect( new Element( 'a', null, new Text( 'x' ) ).isEmpty() ).to.be.false;
  142. } );
  143. } );
  144. describe( 'offsetToIndex', () => {
  145. let element;
  146. beforeEach( () => {
  147. element = new Element( 'elem', [], [ new Element( 'p' ), new Text( 'bar' ), new Element( 'h' ) ] );
  148. } );
  149. it( 'should return index of a node that occupies given offset in this element', () => {
  150. expect( element.offsetToIndex( 0 ) ).to.equal( 0 );
  151. expect( element.offsetToIndex( 1 ) ).to.equal( 1 );
  152. expect( element.offsetToIndex( 2 ) ).to.equal( 1 );
  153. expect( element.offsetToIndex( 3 ) ).to.equal( 1 );
  154. expect( element.offsetToIndex( 4 ) ).to.equal( 2 );
  155. } );
  156. it( 'should return 0 if offset is too low', () => {
  157. expect( element.offsetToIndex( -1 ) ).to.equal( 0 );
  158. } );
  159. it( 'should return element\'s child count if offset is too high', () => {
  160. expect( element.offsetToIndex( 5 ) ).to.equal( 3 );
  161. expect( element.offsetToIndex( 33 ) ).to.equal( 3 );
  162. } );
  163. } );
  164. describe( 'toJSON', () => {
  165. it( 'should serialize empty element', () => {
  166. let element = new Element( 'one' );
  167. expect( jsonParseStringify( element ) ).to.deep.equal( { name: 'one' } );
  168. } );
  169. it( 'should serialize element with attributes', () => {
  170. let element = new Element( 'one', { foo: true, bar: false } );
  171. expect( jsonParseStringify( element ) ).to.deep.equal( {
  172. attributes: [ [ 'foo', true ], [ 'bar', false ] ],
  173. name: 'one'
  174. } );
  175. } );
  176. it( 'should serialize node with children', () => {
  177. let img = new Element( 'img' );
  178. let one = new Element( 'one' );
  179. let two = new Element( 'two', null, [ new Text( 'ba' ), img, new Text( 'r' ) ] );
  180. let three = new Element( 'three' );
  181. let node = new Element( null, null, [ one, two, three ] );
  182. expect( jsonParseStringify( node ) ).to.deep.equal( {
  183. children: [
  184. { name: 'one' },
  185. {
  186. children: [
  187. { data: 'ba' },
  188. { name: 'img' },
  189. { data: 'r' }
  190. ],
  191. name: 'two'
  192. },
  193. { name: 'three' }
  194. ],
  195. name: null
  196. } );
  197. } );
  198. } );
  199. describe( 'fromJSON', () => {
  200. it( 'should create element without attributes', () => {
  201. const el = new Element( 'el' );
  202. let serialized = jsonParseStringify( el );
  203. let deserialized = Element.fromJSON( serialized );
  204. expect( deserialized.parent ).to.be.null;
  205. expect( deserialized.name ).to.equal( 'el' );
  206. expect( deserialized.getChildCount() ).to.equal( 0 );
  207. } );
  208. it( 'should create element with attributes', () => {
  209. const el = new Element( 'el', { foo: true } );
  210. let serialized = jsonParseStringify( el );
  211. let deserialized = Element.fromJSON( serialized );
  212. expect( deserialized.parent ).to.be.null;
  213. expect( deserialized.name ).to.equal( 'el' );
  214. expect( deserialized.getChildCount() ).to.equal( 0 );
  215. expect( deserialized.hasAttribute( 'foo' ) ).to.be.true;
  216. expect( deserialized.getAttribute( 'foo' ) ).to.be.true;
  217. } );
  218. it( 'should create element with children', () => {
  219. const p = new Element( 'p' );
  220. const text = new Text( 'foo' );
  221. const el = new Element( 'el', null, [ p, text ] );
  222. let serialized = jsonParseStringify( el );
  223. let deserialized = Element.fromJSON( serialized );
  224. expect( deserialized.parent ).to.be.null;
  225. expect( deserialized.name ).to.equal( 'el' );
  226. expect( deserialized.getChildCount() ).to.equal( 2 );
  227. expect( deserialized.getChild( 0 ).name ).to.equal( 'p' );
  228. expect( deserialized.getChild( 0 ).parent ).to.equal( deserialized );
  229. expect( deserialized.getChild( 1 ).data ).to.equal( 'foo' );
  230. expect( deserialized.getChild( 1 ).parent ).to.equal( deserialized );
  231. } );
  232. } );
  233. } );