node.js 8.3 KB

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