element.js 10 KB

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