8
0

element.js 11 KB

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