element.js 15 KB

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