element.js 14 KB

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