8
0

node.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. /**
  2. * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import Document from '../../src/model/document';
  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 CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
  11. import count from '@ckeditor/ckeditor5-utils/src/count';
  12. describe( 'Node', () => {
  13. let doc, root, node;
  14. let one, two, three;
  15. let textBA, textR, img;
  16. before( () => {
  17. node = new Node();
  18. one = new Element( 'one' );
  19. two = new Element( 'two', null, [ new Text( 'ba' ), new Element( 'img' ), new Text( 'r' ) ] );
  20. textBA = two.getChild( 0 );
  21. img = two.getChild( 1 );
  22. textR = two.getChild( 2 );
  23. three = new Element( 'three' );
  24. doc = new Document();
  25. root = doc.createRoot();
  26. root.appendChildren( [ one, two, three ] );
  27. } );
  28. describe( 'should have a correct property', () => {
  29. it( 'root', () => {
  30. expect( root ).to.have.property( 'root' ).that.equals( root );
  31. expect( one ).to.have.property( 'root' ).that.equals( root );
  32. expect( two ).to.have.property( 'root' ).that.equals( root );
  33. expect( three ).to.have.property( 'root' ).that.equals( root );
  34. expect( textBA ).to.have.property( 'root' ).that.equals( root );
  35. expect( img ).to.have.property( 'root' ).that.equals( root );
  36. expect( textR ).to.have.property( 'root' ).that.equals( root );
  37. expect( node ).to.have.property( 'root' ).that.equals( node );
  38. } );
  39. it( 'nextSibling', () => {
  40. expect( root ).to.have.property( 'nextSibling' ).that.is.null;
  41. expect( one ).to.have.property( 'nextSibling' ).that.equals( two );
  42. expect( two ).to.have.property( 'nextSibling' ).that.equals( three );
  43. expect( three ).to.have.property( 'nextSibling' ).that.is.null;
  44. expect( textBA ).to.have.property( 'nextSibling' ).that.deep.equals( img );
  45. expect( img ).to.have.property( 'nextSibling' ).that.deep.equals( textR );
  46. expect( textR ).to.have.property( 'nextSibling' ).that.is.null;
  47. expect( node ).to.have.property( 'nextSibling' ).that.is.null;
  48. } );
  49. it( 'previousSibling', () => {
  50. expect( root ).to.have.property( 'previousSibling' ).that.is.null;
  51. expect( one ).to.have.property( 'previousSibling' ).that.is.null;
  52. expect( two ).to.have.property( 'previousSibling' ).that.equals( one );
  53. expect( three ).to.have.property( 'previousSibling' ).that.equals( two );
  54. expect( textBA ).to.have.property( 'previousSibling' ).that.is.null;
  55. expect( img ).to.have.property( 'previousSibling' ).that.deep.equals( textBA );
  56. expect( textR ).to.have.property( 'previousSibling' ).that.deep.equals( img );
  57. expect( node ).to.have.property( 'previousSibling' ).that.is.null;
  58. } );
  59. it( 'document', () => {
  60. expect( root ).to.have.property( 'document' ).that.equals( doc );
  61. expect( one ).to.have.property( 'document' ).that.equals( doc );
  62. expect( two ).to.have.property( 'document' ).that.equals( doc );
  63. expect( three ).to.have.property( 'document' ).that.equals( doc );
  64. expect( textBA ).to.have.property( 'document' ).that.equals( doc );
  65. expect( img ).to.have.property( 'document' ).that.equals( doc );
  66. expect( textR ).to.have.property( 'document' ).that.equals( doc );
  67. expect( node ).to.have.property( 'document' ).that.is.null;
  68. // DocumentFragment does not have document property, so node's document property should be null.
  69. let docFrag = new DocumentFragment();
  70. docFrag.appendChildren( node );
  71. expect( node ).to.have.property( 'document' ).that.is.null;
  72. } );
  73. } );
  74. describe( 'constructor()', () => {
  75. it( 'should create empty attribute list if no parameters were passed', () => {
  76. expect( count( node.getAttributes() ) ).to.equal( 0 );
  77. } );
  78. it( 'should initialize attribute list with passed attributes', () => {
  79. let foo = new Node( { foo: true, bar: false } );
  80. expect( count( foo.getAttributes() ) ).to.equal( 2 );
  81. expect( foo.getAttribute( 'foo' ) ).to.equal( true );
  82. expect( foo.getAttribute( 'bar' ) ).to.equal( false );
  83. } );
  84. } );
  85. describe( 'getIndex', () => {
  86. it( 'should return null if the parent is null', () => {
  87. expect( root.index ).to.be.null;
  88. } );
  89. it( 'should return index in the parent', () => {
  90. expect( one.index ).to.equal( 0 );
  91. expect( two.index ).to.equal( 1 );
  92. expect( three.index ).to.equal( 2 );
  93. expect( textBA.index ).to.equal( 0 );
  94. expect( img.index ).to.equal( 1 );
  95. expect( textR.index ).to.equal( 2 );
  96. } );
  97. it( 'should throw an error if parent does not contain element', () => {
  98. node.parent = new Element( 'parent' );
  99. expect(
  100. () => {
  101. node.index;
  102. }
  103. ).to.throw( CKEditorError, /model-node-not-found-in-parent/ );
  104. } );
  105. } );
  106. describe( 'is', () => {
  107. it( 'should return true for node', () => {
  108. expect( node.is( 'node' ) ).to.be.true;
  109. } );
  110. it( 'should return false for other accept values', () => {
  111. expect( node.is( 'text' ) ).to.be.false;
  112. expect( node.is( 'textProxy' ) ).to.be.false;
  113. expect( node.is( 'element' ) ).to.be.false;
  114. expect( node.is( 'rootElement' ) ).to.be.false;
  115. expect( node.is( 'documentFragment' ) ).to.be.false;
  116. } );
  117. } );
  118. describe( 'clone', () => {
  119. it( 'should return a copy of cloned node', () => {
  120. let node = new Node( { foo: 'bar' } );
  121. let copy = node.clone();
  122. expect( copy ).not.to.equal( node );
  123. expect( Array.from( copy.getAttributes() ) ).to.deep.equal( Array.from( node.getAttributes() ) );
  124. } );
  125. } );
  126. describe( 'remove', () => {
  127. it( 'should remove node from it\'s parent', () => {
  128. let element = new Element( 'p' );
  129. element.appendChildren( node );
  130. node.remove();
  131. expect( element.childCount ).to.equal( 0 );
  132. expect( node.parent ).to.be.null;
  133. } );
  134. it( 'should throw if node does not have a parent', () => {
  135. expect( () => {
  136. node.remove();
  137. } ).to.throw;
  138. } );
  139. } );
  140. describe( 'startOffset', () => {
  141. it( 'should return null if the parent is null', () => {
  142. expect( root.startOffset ).to.be.null;
  143. } );
  144. it( 'should return offset in the parent', () => {
  145. expect( one.startOffset ).to.equal( 0 );
  146. expect( two.startOffset ).to.equal( 1 );
  147. expect( three.startOffset ).to.equal( 2 );
  148. expect( textBA.startOffset ).to.equal( 0 );
  149. expect( img.startOffset ).to.equal( 2 );
  150. expect( textR.startOffset ).to.equal( 3 );
  151. } );
  152. it( 'should throw an error if parent does not contain element', () => {
  153. node.parent = new Element( 'parent' );
  154. expect(
  155. () => {
  156. node.startOffset;
  157. }
  158. ).to.throw( CKEditorError, /model-node-not-found-in-parent/ );
  159. } );
  160. } );
  161. describe( 'endOffset', () => {
  162. it( 'should return null if the parent is null', () => {
  163. expect( root.endOffset ).to.be.null;
  164. } );
  165. it( 'should return offset at which the node ends', () => {
  166. expect( one.endOffset ).to.equal( 1 );
  167. expect( two.endOffset ).to.equal( 2 );
  168. expect( three.endOffset ).to.equal( 3 );
  169. expect( textBA.endOffset ).to.equal( 2 );
  170. expect( img.endOffset ).to.equal( 3 );
  171. expect( textR.endOffset ).to.equal( 4 );
  172. } );
  173. } );
  174. describe( 'getPath', () => {
  175. it( 'should return proper path', () => {
  176. expect( root.getPath() ).to.deep.equal( [] );
  177. expect( one.getPath() ).to.deep.equal( [ 0 ] );
  178. expect( two.getPath() ).to.deep.equal( [ 1 ] );
  179. expect( three.getPath() ).to.deep.equal( [ 2 ] );
  180. expect( textBA.getPath() ).to.deep.equal( [ 1, 0 ] );
  181. expect( img.getPath() ).to.deep.equal( [ 1, 2 ] );
  182. expect( textR.getPath() ).to.deep.equal( [ 1, 3 ] );
  183. } );
  184. } );
  185. describe( 'getAncestors', () => {
  186. it( 'should return proper array of ancestor nodes', () => {
  187. expect( root.getAncestors() ).to.deep.equal( [] );
  188. expect( two.getAncestors() ).to.deep.equal( [ root ] );
  189. expect( textBA.getAncestors() ).to.deep.equal( [ root, two ] );
  190. } );
  191. it( 'should include itself if includeNode option is set to true', () => {
  192. expect( root.getAncestors( { includeNode: true } ) ).to.deep.equal( [ root ] );
  193. expect( two.getAncestors( { includeNode: true } ) ).to.deep.equal( [ root, two ] );
  194. expect( textBA.getAncestors( { includeNode: true } ) ).to.deep.equal( [ root, two, textBA ] );
  195. expect( img.getAncestors( { includeNode: true } ) ).to.deep.equal( [ root, two, img ] );
  196. expect( textR.getAncestors( { includeNode: true } ) ).to.deep.equal( [ root, two, textR ] );
  197. } );
  198. it( 'should reverse order if parentFirst option is set to true', () => {
  199. expect( root.getAncestors( { includeNode: true, parentFirst: true } ) ).to.deep.equal( [ root ] );
  200. expect( two.getAncestors( { includeNode: true, parentFirst: true } ) ).to.deep.equal( [ two, root ] );
  201. expect( textBA.getAncestors( { includeNode: true, parentFirst: true } ) ).to.deep.equal( [ textBA, two, root ] );
  202. expect( img.getAncestors( { includeNode: true, parentFirst: true } ) ).to.deep.equal( [ img, two, root ] );
  203. expect( textR.getAncestors( { includeNode: true, parentFirst: true } ) ).to.deep.equal( [ textR, two, root ] );
  204. } );
  205. } );
  206. describe( 'attributes interface', () => {
  207. let node = new Node( { foo: 'bar' } );
  208. describe( 'hasAttribute', () => {
  209. it( 'should return true if element contains attribute with given key', () => {
  210. expect( node.hasAttribute( 'foo' ) ).to.be.true;
  211. } );
  212. it( 'should return false if element does not contain attribute with given key', () => {
  213. expect( node.hasAttribute( 'bar' ) ).to.be.false;
  214. } );
  215. } );
  216. describe( 'getAttribute', () => {
  217. it( 'should return attribute value for given key if element contains given attribute', () => {
  218. expect( node.getAttribute( 'foo' ) ).to.equal( 'bar' );
  219. } );
  220. it( 'should return undefined if element does not contain given attribute', () => {
  221. expect( node.getAttribute( 'bar' ) ).to.be.undefined;
  222. } );
  223. } );
  224. describe( 'getAttributes', () => {
  225. it( 'should return an iterator that iterates over all attributes set on the element', () => {
  226. expect( Array.from( node.getAttributes() ) ).to.deep.equal( [ [ 'foo', 'bar' ] ] );
  227. } );
  228. } );
  229. describe( 'setAttribute', () => {
  230. it( 'should set given attribute on the element', () => {
  231. node.setAttribute( 'foo', 'bar' );
  232. expect( node.getAttribute( 'foo' ) ).to.equal( 'bar' );
  233. } );
  234. } );
  235. describe( 'setAttributesTo', () => {
  236. it( 'should remove all attributes set on element and set the given ones', () => {
  237. node.setAttribute( 'abc', 'xyz' );
  238. node.setAttributesTo( { foo: 'bar' } );
  239. expect( node.getAttribute( 'foo' ) ).to.equal( 'bar' );
  240. expect( node.getAttribute( 'abc' ) ).to.be.undefined;
  241. } );
  242. } );
  243. describe( 'removeAttribute', () => {
  244. it( 'should remove attribute set on the element and return true', () => {
  245. node.setAttribute( 'foo', 'bar' );
  246. let result = node.removeAttribute( 'foo' );
  247. expect( node.getAttribute( 'foo' ) ).to.be.undefined;
  248. expect( result ).to.be.true;
  249. } );
  250. it( 'should return false if element does not contain given attribute', () => {
  251. let result = node.removeAttribute( 'foo' );
  252. expect( result ).to.be.false;
  253. } );
  254. } );
  255. describe( 'clearAttributes', () => {
  256. it( 'should remove all attributes from the element', () => {
  257. node.setAttribute( 'foo', 'bar' );
  258. node.setAttribute( 'abc', 'xyz' );
  259. node.clearAttributes();
  260. expect( node.getAttribute( 'foo' ) ).to.be.undefined;
  261. expect( node.getAttribute( 'abc' ) ).to.be.undefined;
  262. } );
  263. } );
  264. } );
  265. } );