8
0

element.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  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( 'getNodeByPath', () => {
  113. it( 'should return this node if path is empty', () => {
  114. const element = new Element( 'elem' );
  115. expect( element.getNodeByPath( [] ) ).to.equal( element );
  116. } );
  117. it( 'should return a descendant of this node', () => {
  118. const image = new Element( 'image' );
  119. const element = new Element( 'elem', [], [
  120. new Element( 'elem', [], [
  121. new Text( 'foo' ),
  122. image
  123. ] )
  124. ] );
  125. expect( element.getNodeByPath( [ 0, 1 ] ) ).to.equal( image );
  126. } );
  127. } );
  128. describe( 'getChildIndex', () => {
  129. it( 'should return child index', () => {
  130. let element = new Element( 'elem', [], [ new Element( 'p' ), new Text( 'bar' ), new Element( 'h' ) ] );
  131. let p = element.getChild( 0 );
  132. let bar = element.getChild( 1 );
  133. let h = element.getChild( 2 );
  134. expect( element.getChildIndex( p ) ).to.equal( 0 );
  135. expect( element.getChildIndex( bar ) ).to.equal( 1 );
  136. expect( element.getChildIndex( h ) ).to.equal( 2 );
  137. } );
  138. } );
  139. describe( 'getChildStartOffset', () => {
  140. it( 'should return child offset', () => {
  141. let element = new Element( 'elem', [], [ new Element( 'p' ), new Text( 'bar' ), new Element( 'h' ) ] );
  142. let p = element.getChild( 0 );
  143. let bar = element.getChild( 1 );
  144. let h = element.getChild( 2 );
  145. expect( element.getChildStartOffset( p ) ).to.equal( 0 );
  146. expect( element.getChildStartOffset( bar ) ).to.equal( 1 );
  147. expect( element.getChildStartOffset( h ) ).to.equal( 4 );
  148. } );
  149. } );
  150. describe( 'getChildCount', () => {
  151. it( 'should return number of children', () => {
  152. let element = new Element( 'elem', [], new Text( 'bar' ) );
  153. expect( element.childCount ).to.equal( 1 );
  154. } );
  155. } );
  156. describe( 'getMaxOffset', () => {
  157. it( 'should return offset number after the last child', () => {
  158. let element = new Element( 'elem', [], [ new Element( 'p' ), new Text( 'bar' ), new Element( 'h' ) ] );
  159. expect( element.maxOffset ).to.equal( 5 );
  160. } );
  161. } );
  162. describe( 'isEmpty', () => {
  163. it( 'checks whether element has no children', () => {
  164. expect( new Element( 'a' ).isEmpty ).to.be.true;
  165. expect( new Element( 'a', null, new Text( 'x' ) ).isEmpty ).to.be.false;
  166. } );
  167. } );
  168. describe( 'offsetToIndex', () => {
  169. let element;
  170. beforeEach( () => {
  171. element = new Element( 'elem', [], [ new Element( 'p' ), new Text( 'bar' ), new Element( 'h' ) ] );
  172. } );
  173. it( 'should return index of a node that occupies given offset in this element', () => {
  174. expect( element.offsetToIndex( 0 ) ).to.equal( 0 );
  175. expect( element.offsetToIndex( 1 ) ).to.equal( 1 );
  176. expect( element.offsetToIndex( 2 ) ).to.equal( 1 );
  177. expect( element.offsetToIndex( 3 ) ).to.equal( 1 );
  178. expect( element.offsetToIndex( 4 ) ).to.equal( 2 );
  179. } );
  180. it( 'should throw if given offset is too high or too low', () => {
  181. expect( () => {
  182. element.offsetToIndex( -1 );
  183. } ).to.throw( CKEditorError, /nodelist-offset-out-of-bounds/ );
  184. expect( () => {
  185. element.offsetToIndex( 55 );
  186. } ).to.throw( CKEditorError, /nodelist-offset-out-of-bounds/ );
  187. } );
  188. it( 'should return length if given offset is equal to maxOffset', () => {
  189. expect( element.offsetToIndex( 5 ) ).to.equal( 3 );
  190. } );
  191. } );
  192. describe( 'toJSON', () => {
  193. it( 'should serialize empty element', () => {
  194. let element = new Element( 'one' );
  195. expect( jsonParseStringify( element ) ).to.deep.equal( { name: 'one' } );
  196. } );
  197. it( 'should serialize element with attributes', () => {
  198. let element = new Element( 'one', { foo: true, bar: false } );
  199. expect( jsonParseStringify( element ) ).to.deep.equal( {
  200. attributes: [ [ 'foo', true ], [ 'bar', false ] ],
  201. name: 'one'
  202. } );
  203. } );
  204. it( 'should serialize node with children', () => {
  205. let img = new Element( 'img' );
  206. let one = new Element( 'one' );
  207. let two = new Element( 'two', null, [ new Text( 'ba' ), img, new Text( 'r' ) ] );
  208. let three = new Element( 'three' );
  209. let node = new Element( null, null, [ one, two, three ] );
  210. expect( jsonParseStringify( node ) ).to.deep.equal( {
  211. children: [
  212. { name: 'one' },
  213. {
  214. children: [
  215. { data: 'ba' },
  216. { name: 'img' },
  217. { data: 'r' }
  218. ],
  219. name: 'two'
  220. },
  221. { name: 'three' }
  222. ],
  223. name: null
  224. } );
  225. } );
  226. } );
  227. describe( 'fromJSON', () => {
  228. it( 'should create element without attributes', () => {
  229. const el = new Element( 'el' );
  230. let serialized = jsonParseStringify( el );
  231. let deserialized = Element.fromJSON( serialized );
  232. expect( deserialized.parent ).to.be.null;
  233. expect( deserialized.name ).to.equal( 'el' );
  234. expect( deserialized.childCount ).to.equal( 0 );
  235. } );
  236. it( 'should create element with attributes', () => {
  237. const el = new Element( 'el', { foo: true } );
  238. let serialized = jsonParseStringify( el );
  239. let deserialized = Element.fromJSON( serialized );
  240. expect( deserialized.parent ).to.be.null;
  241. expect( deserialized.name ).to.equal( 'el' );
  242. expect( deserialized.childCount ).to.equal( 0 );
  243. expect( deserialized.hasAttribute( 'foo' ) ).to.be.true;
  244. expect( deserialized.getAttribute( 'foo' ) ).to.be.true;
  245. } );
  246. it( 'should create element with children', () => {
  247. const p = new Element( 'p' );
  248. const text = new Text( 'foo' );
  249. const el = new Element( 'el', null, [ p, text ] );
  250. let serialized = jsonParseStringify( el );
  251. let deserialized = Element.fromJSON( serialized );
  252. expect( deserialized.parent ).to.be.null;
  253. expect( deserialized.name ).to.equal( 'el' );
  254. expect( deserialized.childCount ).to.equal( 2 );
  255. expect( deserialized.getChild( 0 ).name ).to.equal( 'p' );
  256. expect( deserialized.getChild( 0 ).parent ).to.equal( deserialized );
  257. expect( deserialized.getChild( 1 ).data ).to.equal( 'foo' );
  258. expect( deserialized.getChild( 1 ).parent ).to.equal( deserialized );
  259. } );
  260. } );
  261. } );