element.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. /**
  2. * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import Node from '../../src/model/node';
  6. import Element from '../../src/model/element';
  7. import Text from '../../src/model/text';
  8. import { jsonParseStringify } from '../../tests/model/_utils/utils';
  9. import count from '@ckeditor/ckeditor5-utils/src/count';
  10. import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
  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( 'is', () => {
  33. let element;
  34. before( () => {
  35. element = new Element( 'paragraph' );
  36. } );
  37. it( 'should return true for element, element with same name and element name', () => {
  38. expect( element.is( 'element' ) ).to.be.true;
  39. expect( element.is( 'element', 'paragraph' ) ).to.be.true;
  40. expect( element.is( 'paragraph' ) ).to.be.true;
  41. } );
  42. it( 'should return false for other accept values', () => {
  43. expect( element.is( 'element', 'image' ) ).to.be.false;
  44. expect( element.is( 'image' ) ).to.be.false;
  45. expect( element.is( 'text' ) ).to.be.false;
  46. expect( element.is( 'textProxy' ) ).to.be.false;
  47. expect( element.is( 'documentFragment' ) ).to.be.false;
  48. expect( element.is( 'rootElement' ) ).to.be.false;
  49. } );
  50. } );
  51. describe( 'clone', () => {
  52. it( 'should return an element with same name, attributes and same instances of children if clone was not deep', () => {
  53. let p = new Element( 'p' );
  54. let foo = new Text( 'foo' );
  55. let element = new Element( 'elem', { bold: true, italic: true }, [ p, foo ] );
  56. let copy = element.clone();
  57. expect( copy.name ).to.equal( 'elem' );
  58. expect( Array.from( copy.getAttributes() ) ).to.deep.equal( [ [ 'bold', true ], [ 'italic', true ] ] );
  59. expect( Array.from( copy.getChildren() ) ).to.deep.equal( [ p, foo ] );
  60. } );
  61. it( 'should clone children, if clone is deep', () => {
  62. let p = new Element( 'p' );
  63. let foo = new Text( 'foo' );
  64. let element = new Element( 'elem', { bold: true, italic: true }, [ p, foo ] );
  65. let copy = element.clone( true );
  66. expect( copy.name ).to.equal( 'elem' );
  67. expect( Array.from( copy.getAttributes() ) ).to.deep.equal( [ [ 'bold', true ], [ 'italic', true ] ] );
  68. expect( copy.childCount ).to.equal( 2 );
  69. expect( copy.getChild( 0 ) ).not.to.equal( p );
  70. expect( copy.getChild( 1 ) ).not.to.equal( foo );
  71. expect( copy.getChild( 0 ).name ).to.equal( 'p' );
  72. expect( copy.getChild( 1 ).data ).to.equal( 'foo' );
  73. } );
  74. } );
  75. describe( 'insertChildren', () => {
  76. it( 'should add a child to the element', () => {
  77. let element = new Element( 'elem', [], new Text( 'xy' ) );
  78. element.insertChildren( 1, new Text( 'foo' ) );
  79. expect( element.childCount ).to.equal( 2 );
  80. expect( element.maxOffset ).to.equal( 5 );
  81. expect( element.getChild( 0 ).data ).to.equal( 'xy' );
  82. expect( element.getChild( 1 ).data ).to.equal( 'foo' );
  83. } );
  84. it( 'should accept arrays and strings', () => {
  85. let element = new Element( 'elem' );
  86. element.insertChildren( 0, [ new Element( 'image' ), 'xy', new Element( 'list' ) ] );
  87. expect( element.childCount ).to.equal( 3 );
  88. expect( element.maxOffset ).to.equal( 4 );
  89. expect( element.getChild( 0 ).name ).to.equal( 'image' );
  90. expect( element.getChild( 1 ).data ).to.equal( 'xy' );
  91. expect( element.getChild( 2 ).name ).to.equal( 'list' );
  92. } );
  93. it( 'should accept strings', () => {
  94. let element = new Element( 'div' );
  95. element.insertChildren( 0, 'abc' );
  96. expect( element.childCount ).to.equal( 1 );
  97. expect( element.maxOffset ).to.equal( 3 );
  98. expect( element.getChild( 0 ) ).to.have.property( 'data' ).that.equals( 'abc' );
  99. } );
  100. } );
  101. describe( 'appendChildren', () => {
  102. it( 'should use insertChildren to add children at the end of the element', () => {
  103. let element = new Element( 'elem', [], new Text( 'xy' ) );
  104. sinon.spy( element, 'insertChildren' );
  105. let text = new Text( 'foo' );
  106. element.appendChildren( text );
  107. expect( element.insertChildren.calledWithExactly( 0, text ) );
  108. } );
  109. } );
  110. describe( 'removeChildren', () => {
  111. it( 'should remove children from the element and return them as an array', () => {
  112. let element = new Element( 'elem', [], [ new Text( 'foobar' ), new Element( 'image' ) ] );
  113. let removed = element.removeChildren( 1, 1 );
  114. expect( element.childCount ).to.equal( 1 );
  115. expect( element.maxOffset ).to.equal( 6 );
  116. expect( element.getChild( 0 ).data ).to.equal( 'foobar' );
  117. expect( removed.length ).to.equal( 1 );
  118. expect( removed[ 0 ].name ).to.equal( 'image' );
  119. } );
  120. it( 'should remove one child when second parameter is not specified', () => {
  121. let element = new Element( 'element', [], [ new Text( 'foo' ), new Element( 'image' ) ] );
  122. let removed = element.removeChildren( 0 );
  123. expect( element.childCount ).to.equal( 1 );
  124. expect( element.maxOffset ).to.equal( 1 );
  125. expect( element.getChild( 0 ).name ).to.equal( 'image' );
  126. expect( removed.length ).to.equal( 1 );
  127. expect( removed[ 0 ].data ).to.equal( 'foo' );
  128. } );
  129. } );
  130. describe( 'getNodeByPath', () => {
  131. it( 'should return this node if path is empty', () => {
  132. const element = new Element( 'elem' );
  133. expect( element.getNodeByPath( [] ) ).to.equal( element );
  134. } );
  135. it( 'should return a descendant of this node', () => {
  136. const image = new Element( 'image' );
  137. const element = new Element( 'elem', [], [
  138. new Element( 'elem', [], [
  139. new Text( 'foo' ),
  140. image
  141. ] )
  142. ] );
  143. expect( element.getNodeByPath( [ 0, 1 ] ) ).to.equal( image );
  144. } );
  145. } );
  146. describe( 'getChildIndex', () => {
  147. it( 'should return child index', () => {
  148. let element = new Element( 'elem', [], [ new Element( 'p' ), new Text( 'bar' ), new Element( 'h' ) ] );
  149. let p = element.getChild( 0 );
  150. let bar = element.getChild( 1 );
  151. let h = element.getChild( 2 );
  152. expect( element.getChildIndex( p ) ).to.equal( 0 );
  153. expect( element.getChildIndex( bar ) ).to.equal( 1 );
  154. expect( element.getChildIndex( h ) ).to.equal( 2 );
  155. } );
  156. } );
  157. describe( 'getChildStartOffset', () => {
  158. it( 'should return child offset', () => {
  159. let element = new Element( 'elem', [], [ new Element( 'p' ), new Text( 'bar' ), new Element( 'h' ) ] );
  160. let p = element.getChild( 0 );
  161. let bar = element.getChild( 1 );
  162. let h = element.getChild( 2 );
  163. expect( element.getChildStartOffset( p ) ).to.equal( 0 );
  164. expect( element.getChildStartOffset( bar ) ).to.equal( 1 );
  165. expect( element.getChildStartOffset( h ) ).to.equal( 4 );
  166. } );
  167. } );
  168. describe( 'getChildCount', () => {
  169. it( 'should return number of children', () => {
  170. let element = new Element( 'elem', [], new Text( 'bar' ) );
  171. expect( element.childCount ).to.equal( 1 );
  172. } );
  173. } );
  174. describe( 'getMaxOffset', () => {
  175. it( 'should return offset number after the last child', () => {
  176. let element = new Element( 'elem', [], [ new Element( 'p' ), new Text( 'bar' ), new Element( 'h' ) ] );
  177. expect( element.maxOffset ).to.equal( 5 );
  178. } );
  179. } );
  180. describe( 'isEmpty', () => {
  181. it( 'checks whether element has no children', () => {
  182. expect( new Element( 'a' ).isEmpty ).to.be.true;
  183. expect( new Element( 'a', null, new Text( 'x' ) ).isEmpty ).to.be.false;
  184. } );
  185. } );
  186. describe( 'offsetToIndex', () => {
  187. let element;
  188. beforeEach( () => {
  189. element = new Element( 'elem', [], [ new Element( 'p' ), new Text( 'bar' ), new Element( 'h' ) ] );
  190. } );
  191. it( 'should return index of a node that occupies given offset in this element', () => {
  192. expect( element.offsetToIndex( 0 ) ).to.equal( 0 );
  193. expect( element.offsetToIndex( 1 ) ).to.equal( 1 );
  194. expect( element.offsetToIndex( 2 ) ).to.equal( 1 );
  195. expect( element.offsetToIndex( 3 ) ).to.equal( 1 );
  196. expect( element.offsetToIndex( 4 ) ).to.equal( 2 );
  197. } );
  198. it( 'should throw if given offset is too high or too low', () => {
  199. expect( () => {
  200. element.offsetToIndex( -1 );
  201. } ).to.throw( CKEditorError, /nodelist-offset-out-of-bounds/ );
  202. expect( () => {
  203. element.offsetToIndex( 55 );
  204. } ).to.throw( CKEditorError, /nodelist-offset-out-of-bounds/ );
  205. } );
  206. it( 'should return length if given offset is equal to maxOffset', () => {
  207. expect( element.offsetToIndex( 5 ) ).to.equal( 3 );
  208. } );
  209. } );
  210. describe( 'toJSON', () => {
  211. it( 'should serialize empty element', () => {
  212. let element = new Element( 'one' );
  213. expect( jsonParseStringify( element ) ).to.deep.equal( { name: 'one' } );
  214. } );
  215. it( 'should serialize element with attributes', () => {
  216. let element = new Element( 'one', { foo: true, bar: false } );
  217. expect( jsonParseStringify( element ) ).to.deep.equal( {
  218. attributes: [ [ 'foo', true ], [ 'bar', false ] ],
  219. name: 'one'
  220. } );
  221. } );
  222. it( 'should serialize node with children', () => {
  223. let img = new Element( 'img' );
  224. let one = new Element( 'one' );
  225. let two = new Element( 'two', null, [ new Text( 'ba' ), img, new Text( 'r' ) ] );
  226. let three = new Element( 'three' );
  227. let node = new Element( null, null, [ one, two, three ] );
  228. expect( jsonParseStringify( node ) ).to.deep.equal( {
  229. children: [
  230. { name: 'one' },
  231. {
  232. children: [
  233. { data: 'ba' },
  234. { name: 'img' },
  235. { data: 'r' }
  236. ],
  237. name: 'two'
  238. },
  239. { name: 'three' }
  240. ],
  241. name: null
  242. } );
  243. } );
  244. } );
  245. describe( 'fromJSON', () => {
  246. it( 'should create element without attributes', () => {
  247. const el = new Element( 'el' );
  248. let serialized = jsonParseStringify( el );
  249. let deserialized = Element.fromJSON( serialized );
  250. expect( deserialized.parent ).to.be.null;
  251. expect( deserialized.name ).to.equal( 'el' );
  252. expect( deserialized.childCount ).to.equal( 0 );
  253. } );
  254. it( 'should create element with attributes', () => {
  255. const el = new Element( 'el', { foo: true } );
  256. let serialized = jsonParseStringify( el );
  257. let deserialized = Element.fromJSON( serialized );
  258. expect( deserialized.parent ).to.be.null;
  259. expect( deserialized.name ).to.equal( 'el' );
  260. expect( deserialized.childCount ).to.equal( 0 );
  261. expect( deserialized.hasAttribute( 'foo' ) ).to.be.true;
  262. expect( deserialized.getAttribute( 'foo' ) ).to.be.true;
  263. } );
  264. it( 'should create element with children', () => {
  265. const p = new Element( 'p' );
  266. const text = new Text( 'foo' );
  267. const el = new Element( 'el', null, [ p, text ] );
  268. let serialized = jsonParseStringify( el );
  269. let deserialized = Element.fromJSON( serialized );
  270. expect( deserialized.parent ).to.be.null;
  271. expect( deserialized.name ).to.equal( 'el' );
  272. expect( deserialized.childCount ).to.equal( 2 );
  273. expect( deserialized.getChild( 0 ).name ).to.equal( 'p' );
  274. expect( deserialized.getChild( 0 ).parent ).to.equal( deserialized );
  275. expect( deserialized.getChild( 1 ).data ).to.equal( 'foo' );
  276. expect( deserialized.getChild( 1 ).parent ).to.equal( deserialized );
  277. } );
  278. } );
  279. } );