node.js 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. /**
  2. * @license Copyright (c) 2003-2015, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* bender-tags: document */
  6. 'use strict';
  7. const modules = bender.amd.require(
  8. 'document/element',
  9. 'document/character',
  10. 'document/attribute',
  11. 'document/nodelist',
  12. 'ckeditorerror'
  13. );
  14. describe( 'Node', function() {
  15. var Element, Character, Attribute, NodeList, CKEditorError;
  16. var root;
  17. var one, two, three;
  18. var charB, charA, charR, img;
  19. before( function() {
  20. Element = modules[ 'document/element' ];
  21. Character = modules[ 'document/character' ];
  22. Attribute = modules[ 'document/attribute' ];
  23. NodeList = modules[ 'document/nodelist' ];
  24. CKEditorError = modules.ckeditorerror;
  25. charB = new Character( 'b' );
  26. charA = new Character( 'a' );
  27. img = new Element( 'img' );
  28. charR = new Character( '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', function() {
  35. it( 'depth', function() {
  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', function() {
  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( 'nextSibling', function() {
  56. expect( root ).to.have.property( 'nextSibling' ).that.is.null;
  57. expect( one ).to.have.property( 'nextSibling' ).that.equals( two );
  58. expect( two ).to.have.property( 'nextSibling' ).that.equals( three );
  59. expect( three ).to.have.property( 'nextSibling' ).that.is.null;
  60. expect( charB ).to.have.property( 'nextSibling' ).that.equals( charA );
  61. expect( charA ).to.have.property( 'nextSibling' ).that.equals( img );
  62. expect( img ).to.have.property( 'nextSibling' ).that.equals( charR );
  63. expect( charR ).to.have.property( 'nextSibling' ).that.is.null;
  64. } );
  65. it( 'previousSibling', function() {
  66. expect( root ).to.have.property( 'previousSibling' ).that.is.expect;
  67. expect( one ).to.have.property( 'previousSibling' ).that.is.null;
  68. expect( two ).to.have.property( 'previousSibling' ).that.equals( one );
  69. expect( three ).to.have.property( 'previousSibling' ).that.equals( two );
  70. expect( charB ).to.have.property( 'previousSibling' ).that.is.null;
  71. expect( charA ).to.have.property( 'previousSibling' ).that.equals( charB );
  72. expect( img ).to.have.property( 'previousSibling' ).that.equals( charA );
  73. expect( charR ).to.have.property( 'previousSibling' ).that.equals( img );
  74. } );
  75. } );
  76. describe( 'constructor', function() {
  77. it( 'should copy attributes, not pass by reference', function() {
  78. var attrs = [ new Attribute( 'attr', true ) ];
  79. var foo = new Element( 'foo', attrs );
  80. var bar = new Element( 'bar', attrs );
  81. foo.removeAttr( 'attr' );
  82. expect( foo._getAttrCount() ).to.equal( 0 );
  83. expect( bar._getAttrCount() ).to.equal( 1 );
  84. } );
  85. } );
  86. describe( 'getAttr', function() {
  87. var fooAttr, element;
  88. beforeEach( function() {
  89. fooAttr = new Attribute( 'foo', true );
  90. element = new Element( 'foo', [ fooAttr ] );
  91. } );
  92. it( 'should be possible to get attribute by key', function() {
  93. expect( element.getAttr( 'foo' ) ).to.equal( fooAttr.value );
  94. } );
  95. it( 'should return null if attribute was not found by key', function() {
  96. expect( element.getAttr( 'bar' ) ).to.be.null;
  97. } );
  98. } );
  99. describe( 'setAttr', function() {
  100. it( 'should insert an attribute', function() {
  101. var element = new Element( 'elem' );
  102. var attr = new Attribute( 'foo', 'bar' );
  103. element.setAttr( attr );
  104. expect( element._getAttrCount() ).to.equal( 1 );
  105. expect( element.getAttr( attr.key ) ).to.equal( attr.value );
  106. } );
  107. it( 'should overwrite attribute with the same key', function() {
  108. var oldAttr = new Attribute( 'foo', 'bar' );
  109. var newAttr = new Attribute( 'foo', 'bar' );
  110. var element = new Element( 'elem', [ oldAttr ] );
  111. element.setAttr( newAttr );
  112. expect( element._getAttrCount() ).to.equal( 1 );
  113. expect( element.getAttr( newAttr.key ) ).to.equal( newAttr.value );
  114. } );
  115. } );
  116. describe( 'removeAttr', function() {
  117. it( 'should remove an attribute', function() {
  118. var attrA = new Attribute( 'a', 'A' );
  119. var attrB = new Attribute( 'b', 'b' );
  120. var attrC = new Attribute( 'c', 'C' );
  121. var element = new Element( 'elem', [ attrA, attrB, attrC ] );
  122. element.removeAttr( attrB.key );
  123. expect( element._getAttrCount() ).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', function() {
  130. it( 'should check attribute by key', function() {
  131. var fooAttr = new Attribute( 'foo', true );
  132. var 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', function() {
  136. var fooAttr = new Attribute( 'foo', true );
  137. var element = new Element( 'foo', [ fooAttr ] );
  138. expect( element.hasAttr( 'bar' ) ).to.be.false;
  139. } );
  140. it( 'should check attribute by object', function() {
  141. var fooAttr = new Attribute( 'foo', true );
  142. var foo2Attr = new Attribute( 'foo', true );
  143. var 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', function() {
  147. var fooAttr = new Attribute( 'foo', true );
  148. var element = new Element( 'foo' );
  149. expect( element.hasAttr( fooAttr ) ).to.be.false;
  150. } );
  151. it( 'should create proper JSON string using toJSON method', function() {
  152. var b = new Character( 'b' );
  153. var foo = new Element( 'foo', [], [ b ] );
  154. var parsedFoo = JSON.parse( JSON.stringify( foo ) );
  155. var parsedBar = JSON.parse( JSON.stringify( b ) );
  156. expect( parsedFoo.parent ).to.equal( null );
  157. expect( parsedBar.parent ).to.equal( 'foo' );
  158. } );
  159. } );
  160. describe( 'getIndex', function() {
  161. it( 'should return null if the parent is null', function() {
  162. expect( root.getIndex() ).to.be.null;
  163. } );
  164. it( 'should return index in the parent', function() {
  165. expect( one.getIndex() ).to.equal( 0 );
  166. expect( two.getIndex() ).to.equal( 1 );
  167. expect( three.getIndex() ).to.equal( 2 );
  168. expect( charB.getIndex() ).to.equal( 0 );
  169. expect( charA.getIndex() ).to.equal( 1 );
  170. expect( img.getIndex() ).to.equal( 2 );
  171. expect( charR.getIndex() ).to.equal( 3 );
  172. } );
  173. it( 'should throw an error if parent does not contains element', function() {
  174. var f = new Character( 'f' );
  175. var bar = new Element( 'bar', [], [] );
  176. f.parent = bar;
  177. expect(
  178. function() {
  179. f.getIndex();
  180. }
  181. ).to.throw( CKEditorError, /node-not-found-in-parent/ );
  182. } );
  183. } );
  184. describe( 'getPath', function() {
  185. it( 'should return proper path', function() {
  186. expect( root.getPath() ).to.deep.equal( [] );
  187. expect( one.getPath() ).to.deep.equal( [ 0 ] );
  188. expect( two.getPath() ).to.deep.equal( [ 1 ] );
  189. expect( three.getPath() ).to.deep.equal( [ 2 ] );
  190. expect( charB.getPath() ).to.deep.equal( [ 1, 0 ] );
  191. expect( charA.getPath() ).to.deep.equal( [ 1, 1 ] );
  192. expect( img.getPath() ).to.deep.equal( [ 1, 2 ] );
  193. expect( charR.getPath() ).to.deep.equal( [ 1, 3 ] );
  194. } );
  195. } );
  196. } );