8
0

node.js 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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. 'use strict';
  7. import Document from '/ckeditor5/engine/model/document.js';
  8. import DocumentFragment from '/ckeditor5/engine/model/documentfragment.js';
  9. import Node from '/ckeditor5/engine/model/node.js';
  10. import Element from '/ckeditor5/engine/model/element.js';
  11. import Text from '/ckeditor5/engine/model/text.js';
  12. import CKEditorError from '/ckeditor5/utils/ckeditorerror.js';
  13. import count from '/ckeditor5/utils/count.js';
  14. describe( 'Node', () => {
  15. let doc, root, node;
  16. let one, two, three;
  17. let textBA, textR, img;
  18. before( () => {
  19. node = new Node();
  20. one = new Element( 'one' );
  21. two = new Element( 'two', null, [ new Text( 'ba' ), new Element( 'img' ), new Text( 'r' ) ] );
  22. textBA = two.getChild( 0 );
  23. img = two.getChild( 1 );
  24. textR = two.getChild( 2 );
  25. three = new Element( 'three' );
  26. doc = new Document();
  27. root = doc.createRoot();
  28. root.appendChildren( [ one, two, three ] );
  29. } );
  30. describe( 'should have a correct property', () => {
  31. it( 'root', () => {
  32. expect( root ).to.have.property( 'root' ).that.equals( root );
  33. expect( one ).to.have.property( 'root' ).that.equals( root );
  34. expect( two ).to.have.property( 'root' ).that.equals( root );
  35. expect( three ).to.have.property( 'root' ).that.equals( root );
  36. expect( textBA ).to.have.property( 'root' ).that.equals( root );
  37. expect( img ).to.have.property( 'root' ).that.equals( root );
  38. expect( textR ).to.have.property( 'root' ).that.equals( root );
  39. expect( node ).to.have.property( 'root' ).that.equals( node );
  40. } );
  41. it( 'nextSibling', () => {
  42. expect( root ).to.have.property( 'nextSibling' ).that.is.null;
  43. expect( one ).to.have.property( 'nextSibling' ).that.equals( two );
  44. expect( two ).to.have.property( 'nextSibling' ).that.equals( three );
  45. expect( three ).to.have.property( 'nextSibling' ).that.is.null;
  46. expect( textBA ).to.have.property( 'nextSibling' ).that.deep.equals( img );
  47. expect( img ).to.have.property( 'nextSibling' ).that.deep.equals( textR );
  48. expect( textR ).to.have.property( 'nextSibling' ).that.is.null;
  49. expect( node ).to.have.property( 'nextSibling' ).that.is.null;
  50. } );
  51. it( 'previousSibling', () => {
  52. expect( root ).to.have.property( 'previousSibling' ).that.is.null;
  53. expect( one ).to.have.property( 'previousSibling' ).that.is.null;
  54. expect( two ).to.have.property( 'previousSibling' ).that.equals( one );
  55. expect( three ).to.have.property( 'previousSibling' ).that.equals( two );
  56. expect( textBA ).to.have.property( 'previousSibling' ).that.is.null;
  57. expect( img ).to.have.property( 'previousSibling' ).that.deep.equals( textBA );
  58. expect( textR ).to.have.property( 'previousSibling' ).that.deep.equals( img );
  59. expect( node ).to.have.property( 'previousSibling' ).that.is.null;
  60. } );
  61. it( 'document', () => {
  62. expect( root ).to.have.property( 'document' ).that.equals( doc );
  63. expect( one ).to.have.property( 'document' ).that.equals( doc );
  64. expect( two ).to.have.property( 'document' ).that.equals( doc );
  65. expect( three ).to.have.property( 'document' ).that.equals( doc );
  66. expect( textBA ).to.have.property( 'document' ).that.equals( doc );
  67. expect( img ).to.have.property( 'document' ).that.equals( doc );
  68. expect( textR ).to.have.property( 'document' ).that.equals( doc );
  69. expect( node ).to.have.property( 'document' ).that.is.null;
  70. // DocumentFragment does not have document property, so node's document property should be null.
  71. let docFrag = new DocumentFragment();
  72. docFrag.appendChildren( node );
  73. expect( node ).to.have.property( 'document' ).that.is.null;
  74. } );
  75. } );
  76. describe( 'constructor', () => {
  77. it( 'should create empty attribute list if no parameters were passed', () => {
  78. expect( count( node.getAttributes() ) ).to.equal( 0 );
  79. } );
  80. it( 'should initialize attribute list with passed attributes', () => {
  81. let foo = new Node( { foo: true, bar: false } );
  82. expect( count( foo.getAttributes() ) ).to.equal( 2 );
  83. expect( foo.getAttribute( 'foo' ) ).to.equal( true );
  84. expect( foo.getAttribute( 'bar' ) ).to.equal( false );
  85. } );
  86. } );
  87. describe( 'getIndex', () => {
  88. it( 'should return null if the parent is null', () => {
  89. expect( root.getIndex() ).to.be.null;
  90. } );
  91. it( 'should return index in the parent', () => {
  92. expect( one.getIndex() ).to.equal( 0 );
  93. expect( two.getIndex() ).to.equal( 1 );
  94. expect( three.getIndex() ).to.equal( 2 );
  95. expect( textBA.getIndex() ).to.equal( 0 );
  96. expect( img.getIndex() ).to.equal( 1 );
  97. expect( textR.getIndex() ).to.equal( 2 );
  98. } );
  99. it( 'should throw an error if parent does not contain element', () => {
  100. node.parent = new Element( 'parent' );
  101. expect(
  102. () => {
  103. node.getIndex();
  104. }
  105. ).to.throw( CKEditorError, /node-not-found-in-parent/ );
  106. } );
  107. } );
  108. describe( 'clone', () => {
  109. it( 'should return a copy of cloned node', () => {
  110. let node = new Node( { foo: 'bar' } );
  111. let copy = node.clone();
  112. expect( copy ).not.to.equal( node );
  113. expect( Array.from( copy.getAttributes() ) ).to.deep.equal( Array.from( node.getAttributes() ) );
  114. } );
  115. } );
  116. describe( 'remove', () => {
  117. it( 'should remove node from it\'s parent', () => {
  118. let element = new Element( 'p' );
  119. element.appendChildren( node );
  120. node.remove();
  121. expect( element.getChildCount() ).to.equal( 0 );
  122. expect( node.parent ).to.be.null;
  123. } );
  124. it( 'should throw if node does not have a parent', () => {
  125. expect( () => {
  126. node.remove();
  127. } ).to.throw;
  128. } );
  129. } );
  130. describe( 'startOffset', () => {
  131. it( 'should return null if the parent is null', () => {
  132. expect( root.startOffset ).to.be.null;
  133. } );
  134. it( 'should return offset in the parent', () => {
  135. expect( one.startOffset ).to.equal( 0 );
  136. expect( two.startOffset ).to.equal( 1 );
  137. expect( three.startOffset ).to.equal( 2 );
  138. expect( textBA.startOffset ).to.equal( 0 );
  139. expect( img.startOffset ).to.equal( 2 );
  140. expect( textR.startOffset ).to.equal( 3 );
  141. } );
  142. it( 'should throw an error if parent does not contain element', () => {
  143. node.parent = new Element( 'parent' );
  144. expect(
  145. () => {
  146. node.startOffset;
  147. }
  148. ).to.throw( CKEditorError, /node-not-found-in-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( 'attributes interface', () => {
  176. let node = new Node( { foo: 'bar' } );
  177. describe( 'hasAttribute', () => {
  178. it( 'should return true if element contains attribute with given key', () => {
  179. expect( node.hasAttribute( 'foo' ) ).to.be.true;
  180. } );
  181. it( 'should return false if element does not contain attribute with given key', () => {
  182. expect( node.hasAttribute( 'bar' ) ).to.be.false;
  183. } );
  184. } );
  185. describe( 'getAttribute', () => {
  186. it( 'should return attribute value for given key if element contains given attribute', () => {
  187. expect( node.getAttribute( 'foo' ) ).to.equal( 'bar' );
  188. } );
  189. it( 'should return undefined if element does not contain given attribute', () => {
  190. expect( node.getAttribute( 'bar' ) ).to.be.undefined;
  191. } );
  192. } );
  193. describe( 'getAttributes', () => {
  194. it( 'should return an iterator that iterates over all attributes set on the element', () => {
  195. expect( Array.from( node.getAttributes() ) ).to.deep.equal( [ [ 'foo', 'bar' ] ] );
  196. } );
  197. } );
  198. describe( 'setAttribute', () => {
  199. it( 'should set given attribute on the element', () => {
  200. node.setAttribute( 'foo', 'bar' );
  201. expect( node.getAttribute( 'foo' ) ).to.equal( 'bar' );
  202. } );
  203. } );
  204. describe( 'setAttributesTo', () => {
  205. it( 'should remove all attributes set on element and set the given ones', () => {
  206. node.setAttribute( 'abc', 'xyz' );
  207. node.setAttributesTo( { foo: 'bar' } );
  208. expect( node.getAttribute( 'foo' ) ).to.equal( 'bar' );
  209. expect( node.getAttribute( 'abc' ) ).to.be.undefined;
  210. } );
  211. } );
  212. describe( 'removeAttribute', () => {
  213. it( 'should remove attribute set on the element and return true', () => {
  214. node.setAttribute( 'foo', 'bar' );
  215. let result = node.removeAttribute( 'foo' );
  216. expect( node.getAttribute( 'foo' ) ).to.be.undefined;
  217. expect( result ).to.be.true;
  218. } );
  219. it( 'should return false if element does not contain given attribute', () => {
  220. let result = node.removeAttribute( 'foo' );
  221. expect( result ).to.be.false;
  222. } );
  223. } );
  224. describe( 'clearAttributes', () => {
  225. it( 'should remove all attributes from the element', () => {
  226. node.setAttribute( 'foo', 'bar' );
  227. node.setAttribute( 'abc', 'xyz' );
  228. node.clearAttributes();
  229. expect( node.getAttribute( 'foo' ) ).to.be.undefined;
  230. expect( node.getAttribute( 'abc' ) ).to.be.undefined;
  231. } );
  232. } );
  233. } );
  234. } );