node.js 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. /**
  2. * @license Copyright (c) 2003-2015, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* bender-tags: treemodel */
  6. /* bender-include: ../_tools/tools.js */
  7. 'use strict';
  8. const getIteratorCount = bender.tools.core.getIteratorCount;
  9. const modules = bender.amd.require(
  10. 'treemodel/element',
  11. 'treemodel/character',
  12. 'treemodel/attribute',
  13. 'treemodel/attributelist',
  14. 'treemodel/nodelist',
  15. 'ckeditorerror'
  16. );
  17. describe( 'Node', () => {
  18. let Element, Character, Attribute, AttributeList, NodeList, CKEditorError;
  19. let root;
  20. let one, two, three;
  21. let charB, charA, charR, img, attrEle;
  22. let attrFooBar;
  23. before( () => {
  24. Element = modules[ 'treemodel/element' ];
  25. Character = modules[ 'treemodel/character' ];
  26. Attribute = modules[ 'treemodel/attribute' ];
  27. AttributeList = modules[ 'treemodel/attributelist' ];
  28. NodeList = modules[ 'treemodel/nodelist' ];
  29. CKEditorError = modules.ckeditorerror;
  30. charB = new Character( 'b' );
  31. charA = new Character( 'a' );
  32. img = new Element( 'img' );
  33. charR = new Character( 'r' );
  34. one = new Element( 'one' );
  35. two = new Element( 'two', null, [ charB, charA, img, charR ] );
  36. three = new Element( 'three' );
  37. root = new Element( null, null, [ one, two, three ] );
  38. attrFooBar = new Attribute( 'foo', 'bar' );
  39. } );
  40. beforeEach( () => {
  41. attrEle = new Element( 'element' );
  42. } );
  43. describe( 'should have a correct property', () => {
  44. it( 'depth', () => {
  45. expect( root ).to.have.property( 'depth' ).that.equals( 0 );
  46. expect( one ).to.have.property( 'depth' ).that.equals( 1 );
  47. expect( two ).to.have.property( 'depth' ).that.equals( 1 );
  48. expect( three ).to.have.property( 'depth' ).that.equals( 1 );
  49. expect( charB ).to.have.property( 'depth' ).that.equals( 2 );
  50. expect( charA ).to.have.property( 'depth' ).that.equals( 2 );
  51. expect( img ).to.have.property( 'depth' ).that.equals( 2 );
  52. expect( charR ).to.have.property( 'depth' ).that.equals( 2 );
  53. } );
  54. it( 'root', () => {
  55. expect( root ).to.have.property( 'root' ).that.equals( root );
  56. expect( one ).to.have.property( 'root' ).that.equals( root );
  57. expect( two ).to.have.property( 'root' ).that.equals( root );
  58. expect( three ).to.have.property( 'root' ).that.equals( root );
  59. expect( charB ).to.have.property( 'root' ).that.equals( root );
  60. expect( charA ).to.have.property( 'root' ).that.equals( root );
  61. expect( img ).to.have.property( 'root' ).that.equals( root );
  62. expect( charR ).to.have.property( 'root' ).that.equals( root );
  63. } );
  64. it( 'nextSibling', () => {
  65. expect( root ).to.have.property( 'nextSibling' ).that.is.null;
  66. expect( one ).to.have.property( 'nextSibling' ).that.equals( two );
  67. expect( two ).to.have.property( 'nextSibling' ).that.equals( three );
  68. expect( three ).to.have.property( 'nextSibling' ).that.is.null;
  69. expect( charB ).to.have.property( 'nextSibling' ).that.equals( charA );
  70. expect( charA ).to.have.property( 'nextSibling' ).that.equals( img );
  71. expect( img ).to.have.property( 'nextSibling' ).that.equals( charR );
  72. expect( charR ).to.have.property( 'nextSibling' ).that.is.null;
  73. } );
  74. it( 'previousSibling', () => {
  75. expect( root ).to.have.property( 'previousSibling' ).that.is.expect;
  76. expect( one ).to.have.property( 'previousSibling' ).that.is.null;
  77. expect( two ).to.have.property( 'previousSibling' ).that.equals( one );
  78. expect( three ).to.have.property( 'previousSibling' ).that.equals( two );
  79. expect( charB ).to.have.property( 'previousSibling' ).that.is.null;
  80. expect( charA ).to.have.property( 'previousSibling' ).that.equals( charB );
  81. expect( img ).to.have.property( 'previousSibling' ).that.equals( charA );
  82. expect( charR ).to.have.property( 'previousSibling' ).that.equals( img );
  83. } );
  84. } );
  85. describe( 'constructor', () => {
  86. it( 'should copy attributes list, not pass by reference', () => {
  87. let attrs = [ new Attribute( 'attr', true ) ];
  88. let foo = new Element( 'foo', attrs );
  89. let bar = new Element( 'bar', attrs );
  90. foo.removeAttr( 'attr' );
  91. expect( getIteratorCount( foo.getAttrs() ) ).to.equal( 0 );
  92. expect( getIteratorCount( bar.getAttrs() ) ).to.equal( 1 );
  93. } );
  94. } );
  95. it( 'should create proper JSON string using toJSON method', () => {
  96. let b = new Character( 'b' );
  97. let foo = new Element( 'foo', [], [ b ] );
  98. let parsedFoo = JSON.parse( JSON.stringify( foo ) );
  99. let parsedBar = JSON.parse( JSON.stringify( b ) );
  100. expect( parsedFoo.parent ).to.equal( null );
  101. expect( parsedBar.parent ).to.equal( 'foo' );
  102. } );
  103. describe( 'getIndex', () => {
  104. it( 'should return null if the parent is null', () => {
  105. expect( root.getIndex() ).to.be.null;
  106. } );
  107. it( 'should return index in the parent', () => {
  108. expect( one.getIndex() ).to.equal( 0 );
  109. expect( two.getIndex() ).to.equal( 1 );
  110. expect( three.getIndex() ).to.equal( 2 );
  111. expect( charB.getIndex() ).to.equal( 0 );
  112. expect( charA.getIndex() ).to.equal( 1 );
  113. expect( img.getIndex() ).to.equal( 2 );
  114. expect( charR.getIndex() ).to.equal( 3 );
  115. } );
  116. it( 'should throw an error if parent does not contains element', () => {
  117. let f = new Character( 'f' );
  118. let bar = new Element( 'bar', [], [] );
  119. f.parent = bar;
  120. expect(
  121. () => {
  122. f.getIndex();
  123. }
  124. ).to.throw( CKEditorError, /node-not-found-in-parent/ );
  125. } );
  126. } );
  127. describe( 'getPath', () => {
  128. it( 'should return proper path', () => {
  129. expect( root.getPath() ).to.deep.equal( [] );
  130. expect( one.getPath() ).to.deep.equal( [ 0 ] );
  131. expect( two.getPath() ).to.deep.equal( [ 1 ] );
  132. expect( three.getPath() ).to.deep.equal( [ 2 ] );
  133. expect( charB.getPath() ).to.deep.equal( [ 1, 0 ] );
  134. expect( charA.getPath() ).to.deep.equal( [ 1, 1 ] );
  135. expect( img.getPath() ).to.deep.equal( [ 1, 2 ] );
  136. expect( charR.getPath() ).to.deep.equal( [ 1, 3 ] );
  137. } );
  138. } );
  139. // Testing integration with attributes list.
  140. // Tests copied from AttributeList tests.
  141. // Some cases were omitted.
  142. describe( 'setAttr', () => {
  143. it( 'should insert an attribute', () => {
  144. attrEle.setAttr( attrFooBar );
  145. expect( getIteratorCount( attrEle.getAttrs() ) ).to.equal( 1 );
  146. expect( attrEle.getAttr( attrFooBar.key ) ).to.equal( attrFooBar.value );
  147. } );
  148. } );
  149. describe( 'setAttrsTo', () => {
  150. it( 'should remove all attributes and set passed ones', () => {
  151. attrEle.setAttr( attrFooBar );
  152. let attrs = [ new Attribute( 'abc', true ), new Attribute( 'xyz', false ) ];
  153. attrEle.setAttrsTo( attrs );
  154. expect( getIteratorCount( attrEle.getAttrs() ) ).to.equal( 2 );
  155. expect( attrEle.getAttr( 'foo' ) ).to.be.null;
  156. expect( attrEle.getAttr( 'abc' ) ).to.be.true;
  157. expect( attrEle.getAttr( 'xyz' ) ).to.be.false;
  158. } );
  159. } );
  160. describe( 'getAttr', () => {
  161. beforeEach( () => {
  162. attrEle = new Element( 'e', [ attrFooBar ] );
  163. } );
  164. it( 'should return attribute value if key of previously set attribute has been passed', () => {
  165. expect( attrEle.getAttr( 'foo' ) ).to.equal( attrFooBar.value );
  166. } );
  167. it( 'should return null if attribute with given key has not been found', () => {
  168. expect( attrEle.getAttr( 'bar' ) ).to.be.null;
  169. } );
  170. } );
  171. describe( 'removeAttr', () => {
  172. it( 'should remove an attribute', () => {
  173. let attrA = new Attribute( 'a', 'A' );
  174. let attrB = new Attribute( 'b', 'B' );
  175. let attrC = new Attribute( 'c', 'C' );
  176. attrEle.setAttr( attrA );
  177. attrEle.setAttr( attrB );
  178. attrEle.setAttr( attrC );
  179. attrEle.removeAttr( attrB.key );
  180. expect( getIteratorCount( attrEle.getAttrs() ) ).to.equal( 2 );
  181. expect( attrEle.getAttr( attrA.key ) ).to.equal( attrA.value );
  182. expect( attrEle.getAttr( attrC.key ) ).to.equal( attrC.value );
  183. expect( attrEle.getAttr( attrB.key ) ).to.be.null;
  184. } );
  185. } );
  186. describe( 'hasAttr', () => {
  187. it( 'should check attribute by key', () => {
  188. attrEle.setAttr( attrFooBar );
  189. expect( attrEle.hasAttr( 'foo' ) ).to.be.true;
  190. } );
  191. it( 'should return false if attribute was not found by key', () => {
  192. expect( attrEle.hasAttr( 'bar' ) ).to.be.false;
  193. } );
  194. it( 'should check attribute by object', () => {
  195. attrEle.setAttr( attrFooBar );
  196. expect( attrEle.hasAttr( attrFooBar ) ).to.be.true;
  197. } );
  198. it( 'should return false if attribute was not found by object', () => {
  199. expect( attrEle.hasAttr( attrFooBar ) ).to.be.false;
  200. } );
  201. } );
  202. describe( 'getAttrs', () => {
  203. it( 'should return all set attributes', () => {
  204. let attrA = new Attribute( 'a', 'A' );
  205. let attrB = new Attribute( 'b', 'B' );
  206. let attrC = new Attribute( 'c', 'C' );
  207. attrEle.setAttrsTo( [
  208. attrA,
  209. attrB,
  210. attrC
  211. ] );
  212. attrEle.removeAttr( attrB.key );
  213. expect( [ attrA, attrC ] ).to.deep.equal( Array.from( attrEle.getAttrs() ) );
  214. } );
  215. } );
  216. } );