node.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. /**
  2. * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* bender-tags: model */
  6. import Document from '/ckeditor5/engine/model/document.js';
  7. import DocumentFragment from '/ckeditor5/engine/model/documentfragment.js';
  8. import Node from '/ckeditor5/engine/model/node.js';
  9. import Element from '/ckeditor5/engine/model/element.js';
  10. import Text from '/ckeditor5/engine/model/text.js';
  11. import CKEditorError from '/ckeditor5/utils/ckeditorerror.js';
  12. import count from '/ckeditor5/utils/count.js';
  13. describe( 'Node', () => {
  14. let doc, root, node;
  15. let one, two, three;
  16. let textBA, textR, img;
  17. before( () => {
  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 = new Document();
  26. root = doc.createRoot();
  27. root.appendChildren( [ 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. let docFrag = new DocumentFragment();
  71. docFrag.appendChildren( 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. let 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. expect(
  101. () => {
  102. node.index;
  103. }
  104. ).to.throw( CKEditorError, /model-node-not-found-in-parent/ );
  105. } );
  106. } );
  107. describe( 'clone', () => {
  108. it( 'should return a copy of cloned node', () => {
  109. let node = new Node( { foo: 'bar' } );
  110. let copy = node.clone();
  111. expect( copy ).not.to.equal( node );
  112. expect( Array.from( copy.getAttributes() ) ).to.deep.equal( Array.from( node.getAttributes() ) );
  113. } );
  114. } );
  115. describe( 'remove', () => {
  116. it( 'should remove node from it\'s parent', () => {
  117. let element = new Element( 'p' );
  118. element.appendChildren( node );
  119. node.remove();
  120. expect( element.childCount ).to.equal( 0 );
  121. expect( node.parent ).to.be.null;
  122. } );
  123. it( 'should throw if node does not have a parent', () => {
  124. expect( () => {
  125. node.remove();
  126. } ).to.throw;
  127. } );
  128. } );
  129. describe( 'startOffset', () => {
  130. it( 'should return null if the parent is null', () => {
  131. expect( root.startOffset ).to.be.null;
  132. } );
  133. it( 'should return offset in the parent', () => {
  134. expect( one.startOffset ).to.equal( 0 );
  135. expect( two.startOffset ).to.equal( 1 );
  136. expect( three.startOffset ).to.equal( 2 );
  137. expect( textBA.startOffset ).to.equal( 0 );
  138. expect( img.startOffset ).to.equal( 2 );
  139. expect( textR.startOffset ).to.equal( 3 );
  140. } );
  141. it( 'should throw an error if parent does not contain element', () => {
  142. node.parent = new Element( 'parent' );
  143. expect(
  144. () => {
  145. node.startOffset;
  146. }
  147. ).to.throw( CKEditorError, /model-node-not-found-in-parent/ );
  148. } );
  149. } );
  150. describe( 'endOffset', () => {
  151. it( 'should return null if the parent is null', () => {
  152. expect( root.endOffset ).to.be.null;
  153. } );
  154. it( 'should return offset at which the node ends', () => {
  155. expect( one.endOffset ).to.equal( 1 );
  156. expect( two.endOffset ).to.equal( 2 );
  157. expect( three.endOffset ).to.equal( 3 );
  158. expect( textBA.endOffset ).to.equal( 2 );
  159. expect( img.endOffset ).to.equal( 3 );
  160. expect( textR.endOffset ).to.equal( 4 );
  161. } );
  162. } );
  163. describe( 'getPath', () => {
  164. it( 'should return proper path', () => {
  165. expect( root.getPath() ).to.deep.equal( [] );
  166. expect( one.getPath() ).to.deep.equal( [ 0 ] );
  167. expect( two.getPath() ).to.deep.equal( [ 1 ] );
  168. expect( three.getPath() ).to.deep.equal( [ 2 ] );
  169. expect( textBA.getPath() ).to.deep.equal( [ 1, 0 ] );
  170. expect( img.getPath() ).to.deep.equal( [ 1, 2 ] );
  171. expect( textR.getPath() ).to.deep.equal( [ 1, 3 ] );
  172. } );
  173. } );
  174. describe( 'getAncestors', () => {
  175. it( 'should return proper array of ancestor nodes', () => {
  176. expect( root.getAncestors() ).to.deep.equal( [] );
  177. expect( two.getAncestors() ).to.deep.equal( [ root ] );
  178. expect( textBA.getAncestors() ).to.deep.equal( [ root, two ] );
  179. } );
  180. it( 'should include itself if includeNode option is set to true', () => {
  181. expect( root.getAncestors( { includeNode: true } ) ).to.deep.equal( [ root ] );
  182. expect( two.getAncestors( { includeNode: true } ) ).to.deep.equal( [ root, two ] );
  183. expect( textBA.getAncestors( { includeNode: true } ) ).to.deep.equal( [ root, two, textBA ] );
  184. expect( img.getAncestors( { includeNode: true } ) ).to.deep.equal( [ root, two, img ] );
  185. expect( textR.getAncestors( { includeNode: true } ) ).to.deep.equal( [ root, two, textR ] );
  186. } );
  187. it( 'should reverse order if parentFirst option is set to true', () => {
  188. expect( root.getAncestors( { includeNode: true, parentFirst: true } ) ).to.deep.equal( [ root ] );
  189. expect( two.getAncestors( { includeNode: true, parentFirst: true } ) ).to.deep.equal( [ two, root ] );
  190. expect( textBA.getAncestors( { includeNode: true, parentFirst: true } ) ).to.deep.equal( [ textBA, two, root ] );
  191. expect( img.getAncestors( { includeNode: true, parentFirst: true } ) ).to.deep.equal( [ img, two, root ] );
  192. expect( textR.getAncestors( { includeNode: true, parentFirst: true } ) ).to.deep.equal( [ textR, two, root ] );
  193. } );
  194. } );
  195. describe( 'attributes interface', () => {
  196. let node = new Node( { foo: 'bar' } );
  197. describe( 'hasAttribute', () => {
  198. it( 'should return true if element contains attribute with given key', () => {
  199. expect( node.hasAttribute( 'foo' ) ).to.be.true;
  200. } );
  201. it( 'should return false if element does not contain attribute with given key', () => {
  202. expect( node.hasAttribute( 'bar' ) ).to.be.false;
  203. } );
  204. } );
  205. describe( 'getAttribute', () => {
  206. it( 'should return attribute value for given key if element contains given attribute', () => {
  207. expect( node.getAttribute( 'foo' ) ).to.equal( 'bar' );
  208. } );
  209. it( 'should return undefined if element does not contain given attribute', () => {
  210. expect( node.getAttribute( 'bar' ) ).to.be.undefined;
  211. } );
  212. } );
  213. describe( 'getAttributes', () => {
  214. it( 'should return an iterator that iterates over all attributes set on the element', () => {
  215. expect( Array.from( node.getAttributes() ) ).to.deep.equal( [ [ 'foo', 'bar' ] ] );
  216. } );
  217. } );
  218. describe( 'setAttribute', () => {
  219. it( 'should set given attribute on the element', () => {
  220. node.setAttribute( 'foo', 'bar' );
  221. expect( node.getAttribute( 'foo' ) ).to.equal( 'bar' );
  222. } );
  223. } );
  224. describe( 'setAttributesTo', () => {
  225. it( 'should remove all attributes set on element and set the given ones', () => {
  226. node.setAttribute( 'abc', 'xyz' );
  227. node.setAttributesTo( { foo: 'bar' } );
  228. expect( node.getAttribute( 'foo' ) ).to.equal( 'bar' );
  229. expect( node.getAttribute( 'abc' ) ).to.be.undefined;
  230. } );
  231. } );
  232. describe( 'removeAttribute', () => {
  233. it( 'should remove attribute set on the element and return true', () => {
  234. node.setAttribute( 'foo', 'bar' );
  235. let result = node.removeAttribute( 'foo' );
  236. expect( node.getAttribute( 'foo' ) ).to.be.undefined;
  237. expect( result ).to.be.true;
  238. } );
  239. it( 'should return false if element does not contain given attribute', () => {
  240. let result = node.removeAttribute( 'foo' );
  241. expect( result ).to.be.false;
  242. } );
  243. } );
  244. describe( 'clearAttributes', () => {
  245. it( 'should remove all attributes from the element', () => {
  246. node.setAttribute( 'foo', 'bar' );
  247. node.setAttribute( 'abc', 'xyz' );
  248. node.clearAttributes();
  249. expect( node.getAttribute( 'foo' ) ).to.be.undefined;
  250. expect( node.getAttribute( 'abc' ) ).to.be.undefined;
  251. } );
  252. } );
  253. } );
  254. } );