8
0

element.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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.childCount ).to.equal( 1 );
  28. expect( element.maxOffset ).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.childCount ).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.childCount ).to.equal( 2 );
  61. expect( element.maxOffset ).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 accept arrays and strings', () => {
  66. let element = new Element( 'elem' );
  67. element.insertChildren( 0, [ new Element( 'image' ), 'xy', new Element( 'list' ) ] );
  68. expect( element.childCount ).to.equal( 3 );
  69. expect( element.maxOffset ).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. it( 'should accept strings', () => {
  75. let element = new Element( 'div' );
  76. element.insertChildren( 0, 'abc' );
  77. expect( element.childCount ).to.equal( 1 );
  78. expect( element.maxOffset ).to.equal( 3 );
  79. expect( element.getChild( 0 ) ).to.have.property( 'data' ).that.equals( 'abc' );
  80. } );
  81. } );
  82. describe( 'appendChildren', () => {
  83. it( 'should use insertChildren to add children at the end of the element', () => {
  84. let element = new Element( 'elem', [], new Text( 'xy' ) );
  85. sinon.spy( element, 'insertChildren' );
  86. let text = new Text( 'foo' );
  87. element.appendChildren( text );
  88. expect( element.insertChildren.calledWithExactly( 0, text ) );
  89. } );
  90. } );
  91. describe( 'removeChildren', () => {
  92. it( 'should remove children from the element and return them as an array', () => {
  93. let element = new Element( 'elem', [], [ new Text( 'foobar' ), new Element( 'image' ) ] );
  94. let removed = element.removeChildren( 1, 1 );
  95. expect( element.childCount ).to.equal( 1 );
  96. expect( element.maxOffset ).to.equal( 6 );
  97. expect( element.getChild( 0 ).data ).to.equal( 'foobar' );
  98. expect( removed.length ).to.equal( 1 );
  99. expect( removed[ 0 ].name ).to.equal( 'image' );
  100. } );
  101. it( 'should remove one child when second parameter is not specified', () => {
  102. let element = new Element( 'element', [], [ new Text( 'foo' ), new Element( 'image' ) ] );
  103. let removed = element.removeChildren( 0 );
  104. expect( element.childCount ).to.equal( 1 );
  105. expect( element.maxOffset ).to.equal( 1 );
  106. expect( element.getChild( 0 ).name ).to.equal( 'image' );
  107. expect( removed.length ).to.equal( 1 );
  108. expect( removed[ 0 ].data ).to.equal( 'foo' );
  109. } );
  110. } );
  111. describe( 'getChildIndex', () => {
  112. it( 'should return child index', () => {
  113. let element = new Element( 'elem', [], [ new Element( 'p' ), new Text( 'bar' ), new Element( 'h' ) ] );
  114. let p = element.getChild( 0 );
  115. let bar = element.getChild( 1 );
  116. let h = element.getChild( 2 );
  117. expect( element.getChildIndex( p ) ).to.equal( 0 );
  118. expect( element.getChildIndex( bar ) ).to.equal( 1 );
  119. expect( element.getChildIndex( h ) ).to.equal( 2 );
  120. } );
  121. } );
  122. describe( 'getChildStartOffset', () => {
  123. it( 'should return child offset', () => {
  124. let element = new Element( 'elem', [], [ new Element( 'p' ), new Text( 'bar' ), new Element( 'h' ) ] );
  125. let p = element.getChild( 0 );
  126. let bar = element.getChild( 1 );
  127. let h = element.getChild( 2 );
  128. expect( element.getChildStartOffset( p ) ).to.equal( 0 );
  129. expect( element.getChildStartOffset( bar ) ).to.equal( 1 );
  130. expect( element.getChildStartOffset( h ) ).to.equal( 4 );
  131. } );
  132. } );
  133. describe( 'getChildCount', () => {
  134. it( 'should return number of children', () => {
  135. let element = new Element( 'elem', [], new Text( 'bar' ) );
  136. expect( element.childCount ).to.equal( 1 );
  137. } );
  138. } );
  139. describe( 'getMaxOffset', () => {
  140. it( 'should return offset number after the last child', () => {
  141. let element = new Element( 'elem', [], [ new Element( 'p' ), new Text( 'bar' ), new Element( 'h' ) ] );
  142. expect( element.maxOffset ).to.equal( 5 );
  143. } );
  144. } );
  145. describe( 'isEmpty', () => {
  146. it( 'checks whether element has no children', () => {
  147. expect( new Element( 'a' ).isEmpty ).to.be.true;
  148. expect( new Element( 'a', null, new Text( 'x' ) ).isEmpty ).to.be.false;
  149. } );
  150. } );
  151. describe( 'offsetToIndex', () => {
  152. let element;
  153. beforeEach( () => {
  154. element = new Element( 'elem', [], [ new Element( 'p' ), new Text( 'bar' ), new Element( 'h' ) ] );
  155. } );
  156. it( 'should return index of a node that occupies given offset in this element', () => {
  157. expect( element.offsetToIndex( 0 ) ).to.equal( 0 );
  158. expect( element.offsetToIndex( 1 ) ).to.equal( 1 );
  159. expect( element.offsetToIndex( 2 ) ).to.equal( 1 );
  160. expect( element.offsetToIndex( 3 ) ).to.equal( 1 );
  161. expect( element.offsetToIndex( 4 ) ).to.equal( 2 );
  162. } );
  163. it( 'should throw if given offset is too high or too low', () => {
  164. expect( () => {
  165. element.offsetToIndex( -1 );
  166. } ).to.throwCKEditorError( /nodelist-offset-out-of-bounds/ );
  167. expect( () => {
  168. element.offsetToIndex( 55 );
  169. } ).to.throwCKEditorError( /nodelist-offset-out-of-bounds/ );
  170. } );
  171. it( 'should return length if given offset is equal to maxOffset', () => {
  172. expect( element.offsetToIndex( 5 ) ).to.equal( 3 );
  173. } );
  174. } );
  175. describe( 'toJSON', () => {
  176. it( 'should serialize empty element', () => {
  177. let element = new Element( 'one' );
  178. expect( jsonParseStringify( element ) ).to.deep.equal( { name: 'one' } );
  179. } );
  180. it( 'should serialize element with attributes', () => {
  181. let element = new Element( 'one', { foo: true, bar: false } );
  182. expect( jsonParseStringify( element ) ).to.deep.equal( {
  183. attributes: [ [ 'foo', true ], [ 'bar', false ] ],
  184. name: 'one'
  185. } );
  186. } );
  187. it( 'should serialize node with children', () => {
  188. let img = new Element( 'img' );
  189. let one = new Element( 'one' );
  190. let two = new Element( 'two', null, [ new Text( 'ba' ), img, new Text( 'r' ) ] );
  191. let three = new Element( 'three' );
  192. let node = new Element( null, null, [ one, two, three ] );
  193. expect( jsonParseStringify( node ) ).to.deep.equal( {
  194. children: [
  195. { name: 'one' },
  196. {
  197. children: [
  198. { data: 'ba' },
  199. { name: 'img' },
  200. { data: 'r' }
  201. ],
  202. name: 'two'
  203. },
  204. { name: 'three' }
  205. ],
  206. name: null
  207. } );
  208. } );
  209. } );
  210. describe( 'fromJSON', () => {
  211. it( 'should create element without attributes', () => {
  212. const el = new Element( 'el' );
  213. let serialized = jsonParseStringify( el );
  214. let deserialized = Element.fromJSON( serialized );
  215. expect( deserialized.parent ).to.be.null;
  216. expect( deserialized.name ).to.equal( 'el' );
  217. expect( deserialized.childCount ).to.equal( 0 );
  218. } );
  219. it( 'should create element with attributes', () => {
  220. const el = new Element( 'el', { foo: true } );
  221. let serialized = jsonParseStringify( el );
  222. let deserialized = Element.fromJSON( serialized );
  223. expect( deserialized.parent ).to.be.null;
  224. expect( deserialized.name ).to.equal( 'el' );
  225. expect( deserialized.childCount ).to.equal( 0 );
  226. expect( deserialized.hasAttribute( 'foo' ) ).to.be.true;
  227. expect( deserialized.getAttribute( 'foo' ) ).to.be.true;
  228. } );
  229. it( 'should create element with children', () => {
  230. const p = new Element( 'p' );
  231. const text = new Text( 'foo' );
  232. const el = new Element( 'el', null, [ p, text ] );
  233. let serialized = jsonParseStringify( el );
  234. let deserialized = Element.fromJSON( serialized );
  235. expect( deserialized.parent ).to.be.null;
  236. expect( deserialized.name ).to.equal( 'el' );
  237. expect( deserialized.childCount ).to.equal( 2 );
  238. expect( deserialized.getChild( 0 ).name ).to.equal( 'p' );
  239. expect( deserialized.getChild( 0 ).parent ).to.equal( deserialized );
  240. expect( deserialized.getChild( 1 ).data ).to.equal( 'foo' );
  241. expect( deserialized.getChild( 1 ).parent ).to.equal( deserialized );
  242. } );
  243. } );
  244. } );