8
0

node.js 18 KB

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