node.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  1. /**
  2. * @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  4. */
  5. import Model from '../../src/model/model';
  6. import DocumentFragment from '../../src/model/documentfragment';
  7. import Node from '../../src/model/node';
  8. import Element from '../../src/model/element';
  9. import Text from '../../src/model/text';
  10. import count from '@ckeditor/ckeditor5-utils/src/count';
  11. import { expectToThrowCKEditorError } from '@ckeditor/ckeditor5-utils/tests/_utils/utils';
  12. describe( 'Node', () => {
  13. let doc, root, node,
  14. one, two, three,
  15. textBA, textR, img;
  16. beforeEach( () => {
  17. const model = new Model();
  18. node = new Node();
  19. one = new Element( 'one' );
  20. two = new Element( 'two', null, [ new Text( 'ba' ), new Element( 'img' ), new Text( 'r' ) ] );
  21. textBA = two.getChild( 0 );
  22. img = two.getChild( 1 );
  23. textR = two.getChild( 2 );
  24. three = new Element( 'three' );
  25. doc = model.document;
  26. root = doc.createRoot();
  27. root._appendChild( [ one, two, three ] );
  28. } );
  29. describe( 'should have a correct property', () => {
  30. it( 'root', () => {
  31. expect( root ).to.have.property( 'root' ).that.equals( root );
  32. expect( one ).to.have.property( 'root' ).that.equals( root );
  33. expect( two ).to.have.property( 'root' ).that.equals( root );
  34. expect( three ).to.have.property( 'root' ).that.equals( root );
  35. expect( textBA ).to.have.property( 'root' ).that.equals( root );
  36. expect( img ).to.have.property( 'root' ).that.equals( root );
  37. expect( textR ).to.have.property( 'root' ).that.equals( root );
  38. expect( node ).to.have.property( 'root' ).that.equals( node );
  39. } );
  40. it( 'nextSibling', () => {
  41. expect( root ).to.have.property( 'nextSibling' ).that.is.null;
  42. expect( one ).to.have.property( 'nextSibling' ).that.equals( two );
  43. expect( two ).to.have.property( 'nextSibling' ).that.equals( three );
  44. expect( three ).to.have.property( 'nextSibling' ).that.is.null;
  45. expect( textBA ).to.have.property( 'nextSibling' ).that.deep.equals( img );
  46. expect( img ).to.have.property( 'nextSibling' ).that.deep.equals( textR );
  47. expect( textR ).to.have.property( 'nextSibling' ).that.is.null;
  48. expect( node ).to.have.property( 'nextSibling' ).that.is.null;
  49. } );
  50. it( 'previousSibling', () => {
  51. expect( root ).to.have.property( 'previousSibling' ).that.is.null;
  52. expect( one ).to.have.property( 'previousSibling' ).that.is.null;
  53. expect( two ).to.have.property( 'previousSibling' ).that.equals( one );
  54. expect( three ).to.have.property( 'previousSibling' ).that.equals( two );
  55. expect( textBA ).to.have.property( 'previousSibling' ).that.is.null;
  56. expect( img ).to.have.property( 'previousSibling' ).that.deep.equals( textBA );
  57. expect( textR ).to.have.property( 'previousSibling' ).that.deep.equals( img );
  58. expect( node ).to.have.property( 'previousSibling' ).that.is.null;
  59. } );
  60. it( 'document', () => {
  61. expect( root ).to.have.property( 'document' ).that.equals( doc );
  62. expect( one ).to.have.property( 'document' ).that.equals( doc );
  63. expect( two ).to.have.property( 'document' ).that.equals( doc );
  64. expect( three ).to.have.property( 'document' ).that.equals( doc );
  65. expect( textBA ).to.have.property( 'document' ).that.equals( doc );
  66. expect( img ).to.have.property( 'document' ).that.equals( doc );
  67. expect( textR ).to.have.property( 'document' ).that.equals( doc );
  68. expect( node ).to.have.property( 'document' ).that.is.null;
  69. // DocumentFragment does not have document property, so node's document property should be null.
  70. const docFrag = new DocumentFragment();
  71. docFrag._appendChild( node );
  72. expect( node ).to.have.property( 'document' ).that.is.null;
  73. } );
  74. } );
  75. describe( 'constructor()', () => {
  76. it( 'should create empty attribute list if no parameters were passed', () => {
  77. expect( count( node.getAttributes() ) ).to.equal( 0 );
  78. } );
  79. it( 'should initialize attribute list with passed attributes', () => {
  80. const foo = new Node( { foo: true, bar: false } );
  81. expect( count( foo.getAttributes() ) ).to.equal( 2 );
  82. expect( foo.getAttribute( 'foo' ) ).to.equal( true );
  83. expect( foo.getAttribute( 'bar' ) ).to.equal( false );
  84. } );
  85. } );
  86. describe( 'getIndex()', () => {
  87. it( 'should return null if the parent is null', () => {
  88. expect( root.index ).to.be.null;
  89. } );
  90. it( 'should return index in the parent', () => {
  91. expect( one.index ).to.equal( 0 );
  92. expect( two.index ).to.equal( 1 );
  93. expect( three.index ).to.equal( 2 );
  94. expect( textBA.index ).to.equal( 0 );
  95. expect( img.index ).to.equal( 1 );
  96. expect( textR.index ).to.equal( 2 );
  97. } );
  98. it( 'should throw an error if parent does not contain element', () => {
  99. node.parent = new Element( 'parent' );
  100. expectToThrowCKEditorError( () => {
  101. node.index;
  102. }, /model-node-not-found-in-parent/, node.parent );
  103. } );
  104. } );
  105. describe( '_clone()', () => {
  106. it( 'should return a copy of cloned node', () => {
  107. const node = new Node( { foo: 'bar' } );
  108. const copy = node._clone();
  109. expect( copy ).not.to.equal( node );
  110. expect( Array.from( copy.getAttributes() ) ).to.deep.equal( Array.from( node.getAttributes() ) );
  111. } );
  112. } );
  113. describe( '_remove()', () => {
  114. it( 'should remove node from it\'s parent', () => {
  115. const element = new Element( 'p' );
  116. element._appendChild( node );
  117. node._remove();
  118. expect( element.childCount ).to.equal( 0 );
  119. expect( node.parent ).to.be.null;
  120. } );
  121. it( 'should throw if node does not have a parent', () => {
  122. expect( () => {
  123. node._remove();
  124. } ).to.throw;
  125. } );
  126. } );
  127. describe( 'is()', () => {
  128. it( 'should return true for node', () => {
  129. expect( node.is( 'node' ) ).to.be.true;
  130. } );
  131. } );
  132. describe( 'startOffset', () => {
  133. it( 'should return null if the parent is null', () => {
  134. expect( root.startOffset ).to.be.null;
  135. } );
  136. it( 'should return offset in the parent', () => {
  137. expect( one.startOffset ).to.equal( 0 );
  138. expect( two.startOffset ).to.equal( 1 );
  139. expect( three.startOffset ).to.equal( 2 );
  140. expect( textBA.startOffset ).to.equal( 0 );
  141. expect( img.startOffset ).to.equal( 2 );
  142. expect( textR.startOffset ).to.equal( 3 );
  143. } );
  144. it( 'should throw an error if parent does not contain element', () => {
  145. node.parent = new Element( 'parent' );
  146. expectToThrowCKEditorError( () => {
  147. node.startOffset;
  148. }, /model-node-not-found-in-parent/, node.parent );
  149. } );
  150. } );
  151. describe( 'endOffset', () => {
  152. it( 'should return null if the parent is null', () => {
  153. expect( root.endOffset ).to.be.null;
  154. } );
  155. it( 'should return offset at which the node ends', () => {
  156. expect( one.endOffset ).to.equal( 1 );
  157. expect( two.endOffset ).to.equal( 2 );
  158. expect( three.endOffset ).to.equal( 3 );
  159. expect( textBA.endOffset ).to.equal( 2 );
  160. expect( img.endOffset ).to.equal( 3 );
  161. expect( textR.endOffset ).to.equal( 4 );
  162. } );
  163. } );
  164. describe( 'getPath()', () => {
  165. it( 'should return proper path', () => {
  166. expect( root.getPath() ).to.deep.equal( [] );
  167. expect( one.getPath() ).to.deep.equal( [ 0 ] );
  168. expect( two.getPath() ).to.deep.equal( [ 1 ] );
  169. expect( three.getPath() ).to.deep.equal( [ 2 ] );
  170. expect( textBA.getPath() ).to.deep.equal( [ 1, 0 ] );
  171. expect( img.getPath() ).to.deep.equal( [ 1, 2 ] );
  172. expect( textR.getPath() ).to.deep.equal( [ 1, 3 ] );
  173. } );
  174. } );
  175. describe( 'getAncestors()', () => {
  176. it( 'should return proper array of ancestor nodes', () => {
  177. expect( root.getAncestors() ).to.deep.equal( [] );
  178. expect( two.getAncestors() ).to.deep.equal( [ root ] );
  179. expect( textBA.getAncestors() ).to.deep.equal( [ root, two ] );
  180. } );
  181. it( 'should include itself if includeSelf option is set to true', () => {
  182. expect( root.getAncestors( { includeSelf: true } ) ).to.deep.equal( [ root ] );
  183. expect( two.getAncestors( { includeSelf: true } ) ).to.deep.equal( [ root, two ] );
  184. expect( textBA.getAncestors( { includeSelf: true } ) ).to.deep.equal( [ root, two, textBA ] );
  185. expect( img.getAncestors( { includeSelf: true } ) ).to.deep.equal( [ root, two, img ] );
  186. expect( textR.getAncestors( { includeSelf: true } ) ).to.deep.equal( [ root, two, textR ] );
  187. } );
  188. it( 'should reverse order if parentFirst option is set to true', () => {
  189. expect( root.getAncestors( { includeSelf: true, parentFirst: true } ) ).to.deep.equal( [ root ] );
  190. expect( two.getAncestors( { includeSelf: true, parentFirst: true } ) ).to.deep.equal( [ two, root ] );
  191. expect( textBA.getAncestors( { includeSelf: true, parentFirst: true } ) ).to.deep.equal( [ textBA, two, root ] );
  192. expect( img.getAncestors( { includeSelf: true, parentFirst: true } ) ).to.deep.equal( [ img, two, root ] );
  193. expect( textR.getAncestors( { includeSelf: true, parentFirst: true } ) ).to.deep.equal( [ textR, two, root ] );
  194. } );
  195. } );
  196. describe( 'getCommonAncestor()', () => {
  197. it( 'should return the parent element for the same node', () => {
  198. expect( img.getCommonAncestor( img ) ).to.equal( two );
  199. } );
  200. it( 'should return the given node for the same node if includeSelf is used', () => {
  201. expect( img.getCommonAncestor( img, { includeSelf: true } ) ).to.equal( img );
  202. } );
  203. it( 'should return null for detached subtrees', () => {
  204. const detached = new Element( 'foo' );
  205. expect( img.getCommonAncestor( detached ) ).to.be.null;
  206. expect( detached.getCommonAncestor( img ) ).to.be.null;
  207. expect( img.getCommonAncestor( detached, { includeSelf: true } ) ).to.be.null;
  208. expect( detached.getCommonAncestor( img, { includeSelf: true } ) ).to.be.null;
  209. } );
  210. it( 'should return null when one of the nodes is a tree root itself', () => {
  211. expect( root.getCommonAncestor( img ) ).to.be.null;
  212. expect( img.getCommonAncestor( root ) ).to.be.null;
  213. expect( root.getCommonAncestor( root ) ).to.be.null;
  214. } );
  215. it( 'should return root when one of the nodes is a tree root itself and includeSelf is used', () => {
  216. expect( root.getCommonAncestor( img, { includeSelf: true } ) ).to.equal( root );
  217. expect( img.getCommonAncestor( root, { includeSelf: true } ) ).to.equal( root );
  218. expect( root.getCommonAncestor( root, { includeSelf: true } ) ).to.equal( root );
  219. } );
  220. it( 'should return parent of the nodes at the same level', () => {
  221. expect( img.getCommonAncestor( textBA ), 1 ).to.equal( two );
  222. expect( textR.getCommonAncestor( textBA ), 2 ).to.equal( two );
  223. expect( img.getCommonAncestor( textBA, { includeSelf: true } ), 3 ).to.equal( two );
  224. expect( textR.getCommonAncestor( textBA, { includeSelf: true } ), 4 ).to.equal( two );
  225. } );
  226. it( 'should return proper element for nodes in different branches and on different levels', () => {
  227. const foo = new Text( 'foo' );
  228. const bar = new Text( 'bar' );
  229. const bom = new Text( 'bom' );
  230. const d = new Element( 'd', null, [ bar ] );
  231. const c = new Element( 'c', null, [ foo, d ] );
  232. const b = new Element( 'b', null, [ c ] );
  233. const e = new Element( 'e', null, [ bom ] );
  234. const a = new Element( 'a', null, [ b, e ] );
  235. // <a><b><c>foo<d>bar</d></c></b><e>bom</e></a>
  236. expect( bar.getCommonAncestor( foo ), 1 ).to.equal( c );
  237. expect( foo.getCommonAncestor( d ), 2 ).to.equal( c );
  238. expect( c.getCommonAncestor( b ), 3 ).to.equal( a );
  239. expect( bom.getCommonAncestor( d ), 4 ).to.equal( a );
  240. expect( b.getCommonAncestor( bom ), 5 ).to.equal( a );
  241. expect( b.getCommonAncestor( bar ), 6 ).to.equal( a );
  242. expect( bar.getCommonAncestor( foo, { includeSelf: true } ), 11 ).to.equal( c );
  243. expect( foo.getCommonAncestor( d, { includeSelf: true } ), 12 ).to.equal( c );
  244. expect( c.getCommonAncestor( b, { includeSelf: true } ), 13 ).to.equal( b );
  245. expect( bom.getCommonAncestor( d, { includeSelf: true } ), 14 ).to.equal( a );
  246. expect( b.getCommonAncestor( bom, { includeSelf: true } ), 15 ).to.equal( a );
  247. expect( b.getCommonAncestor( bar, { includeSelf: true } ), 16 ).to.equal( b );
  248. } );
  249. it( 'should return document fragment', () => {
  250. const foo = new Text( 'foo' );
  251. const bar = new Text( 'bar' );
  252. const df = new DocumentFragment( [ foo, bar ] );
  253. expect( foo.getCommonAncestor( bar ) ).to.equal( df );
  254. } );
  255. } );
  256. describe( 'isBefore()', () => {
  257. // Model is: <root><one></one><two>ba<img></img>r</two><three></three>
  258. it( 'should return true if the element is before given element', () => {
  259. expect( one.isBefore( two ) ).to.be.true;
  260. expect( one.isBefore( img ) ).to.be.true;
  261. expect( two.isBefore( textBA ) ).to.be.true;
  262. expect( two.isBefore( textR ) ).to.be.true;
  263. expect( two.isBefore( three ) ).to.be.true;
  264. expect( root.isBefore( one ) ).to.be.true;
  265. } );
  266. it( 'should return false if the element is after given element', () => {
  267. expect( two.isBefore( one ) ).to.be.false;
  268. expect( img.isBefore( one ) ).to.be.false;
  269. expect( textBA.isBefore( two ) ).to.be.false;
  270. expect( textR.isBefore( two ) ).to.be.false;
  271. expect( three.isBefore( two ) ).to.be.false;
  272. expect( one.isBefore( root ) ).to.be.false;
  273. } );
  274. it( 'should return false if the same element is given', () => {
  275. expect( one.isBefore( one ) ).to.be.false;
  276. } );
  277. it( 'should return false if elements are in different roots', () => {
  278. const otherRoot = new Element( 'root' );
  279. const otherElement = new Element( 'element' );
  280. otherRoot._appendChild( otherElement );
  281. expect( otherElement.isBefore( three ) ).to.be.false;
  282. } );
  283. } );
  284. describe( 'isAfter()', () => {
  285. // Model is: <root><one></one><two>ba<img></img>r</two><three></three>
  286. it( 'should return true if the element is after given element', () => {
  287. expect( two.isAfter( one ) ).to.be.true;
  288. expect( img.isAfter( one ) ).to.be.true;
  289. expect( textBA.isAfter( two ) ).to.be.true;
  290. expect( textR.isAfter( two ) ).to.be.true;
  291. expect( three.isAfter( two ) ).to.be.true;
  292. expect( one.isAfter( root ) ).to.be.true;
  293. } );
  294. it( 'should return false if the element is before given element', () => {
  295. expect( one.isAfter( two ) ).to.be.false;
  296. expect( one.isAfter( img ) ).to.be.false;
  297. expect( two.isAfter( textBA ) ).to.be.false;
  298. expect( two.isAfter( textR ) ).to.be.false;
  299. expect( two.isAfter( three ) ).to.be.false;
  300. expect( root.isAfter( one ) ).to.be.false;
  301. } );
  302. it( 'should return false if the same element is given', () => {
  303. expect( one.isAfter( one ) ).to.be.false;
  304. } );
  305. it( 'should return false if elements are in different roots', () => {
  306. const otherRoot = new Element( 'root' );
  307. const otherElement = new Element( 'element' );
  308. otherRoot._appendChild( otherElement );
  309. expect( three.isAfter( otherElement ) ).to.be.false;
  310. } );
  311. } );
  312. describe( 'attributes interface', () => {
  313. const node = new Node( { foo: 'bar' } );
  314. describe( 'hasAttribute', () => {
  315. it( 'should return true if element contains attribute with given key', () => {
  316. expect( node.hasAttribute( 'foo' ) ).to.be.true;
  317. } );
  318. it( 'should return false if element does not contain attribute with given key', () => {
  319. expect( node.hasAttribute( 'bar' ) ).to.be.false;
  320. } );
  321. } );
  322. describe( 'getAttribute', () => {
  323. it( 'should return attribute value for given key if element contains given attribute', () => {
  324. expect( node.getAttribute( 'foo' ) ).to.equal( 'bar' );
  325. } );
  326. it( 'should return undefined if element does not contain given attribute', () => {
  327. expect( node.getAttribute( 'bar' ) ).to.be.undefined;
  328. } );
  329. } );
  330. describe( 'getAttributes', () => {
  331. it( 'should return an iterator that iterates over all attributes set on the element', () => {
  332. expect( Array.from( node.getAttributes() ) ).to.deep.equal( [ [ 'foo', 'bar' ] ] );
  333. } );
  334. } );
  335. describe( '_setAttribute', () => {
  336. it( 'should set given attribute on the element', () => {
  337. node._setAttribute( 'foo', 'bar' );
  338. expect( node.getAttribute( 'foo' ) ).to.equal( 'bar' );
  339. } );
  340. } );
  341. describe( '_setAttributesTo', () => {
  342. it( 'should remove all attributes set on element and set the given ones', () => {
  343. node._setAttribute( 'abc', 'xyz' );
  344. node._setAttributesTo( { foo: 'bar' } );
  345. expect( node.getAttribute( 'foo' ) ).to.equal( 'bar' );
  346. expect( node.getAttribute( 'abc' ) ).to.be.undefined;
  347. } );
  348. } );
  349. describe( '_removeAttribute', () => {
  350. it( 'should remove attribute set on the element and return true', () => {
  351. node._setAttribute( 'foo', 'bar' );
  352. const result = node._removeAttribute( 'foo' );
  353. expect( node.getAttribute( 'foo' ) ).to.be.undefined;
  354. expect( result ).to.be.true;
  355. } );
  356. it( 'should return false if element does not contain given attribute', () => {
  357. const result = node._removeAttribute( 'foo' );
  358. expect( result ).to.be.false;
  359. } );
  360. } );
  361. describe( '_clearAttributes', () => {
  362. it( 'should remove all attributes from the element', () => {
  363. node._setAttribute( 'foo', 'bar' );
  364. node._setAttribute( 'abc', 'xyz' );
  365. node._clearAttributes();
  366. expect( node.getAttribute( 'foo' ) ).to.be.undefined;
  367. expect( node.getAttribute( 'abc' ) ).to.be.undefined;
  368. } );
  369. } );
  370. } );
  371. } );