8
0

node.js 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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. var modules = bender.amd.require(
  8. 'document/element',
  9. 'document/character',
  10. 'document/attribute',
  11. 'document/nodelist' );
  12. describe( 'tree', function() {
  13. var Element, Character;
  14. var root;
  15. var one, two, three;
  16. var charB, charA, charR, img;
  17. before( function() {
  18. Element = modules[ 'document/element' ];
  19. Character = modules[ 'document/character' ];
  20. charB = new Character( 'b' );
  21. charA = new Character( 'a' );
  22. img = new Element( 'img' );
  23. charR = new Character( 'r' );
  24. one = new Element( 'one' );
  25. two = new Element( 'two', null, [ charB, charA, img, charR ] );
  26. three = new Element( 'three' );
  27. root = new Element( null, null, [ one, two, three ] );
  28. } );
  29. it( 'should have proper positionInParent', function() {
  30. expect( root ).to.have.property( 'positionInParent' ).that.is.null;
  31. expect( one ).to.have.property( 'positionInParent' ).that.equals( 0 );
  32. expect( two ).to.have.property( 'positionInParent' ).that.equals( 1 );
  33. expect( three ).to.have.property( 'positionInParent' ).that.equals( 2 );
  34. expect( charB ).to.have.property( 'positionInParent' ).that.equals( 0 );
  35. expect( charA ).to.have.property( 'positionInParent' ).that.equals( 1 );
  36. expect( img ).to.have.property( 'positionInParent' ).that.equals( 2 );
  37. expect( charR ).to.have.property( 'positionInParent' ).that.equals( 3 );
  38. } );
  39. it( 'should have proper depth', function() {
  40. expect( root ).to.have.property( 'depth' ).that.equals( 0 );
  41. expect( one ).to.have.property( 'depth' ).that.equals( 1 );
  42. expect( two ).to.have.property( 'depth' ).that.equals( 1 );
  43. expect( three ).to.have.property( 'depth' ).that.equals( 1 );
  44. expect( charB ).to.have.property( 'depth' ).that.equals( 2 );
  45. expect( charA ).to.have.property( 'depth' ).that.equals( 2 );
  46. expect( img ).to.have.property( 'depth' ).that.equals( 2 );
  47. expect( charR ).to.have.property( 'depth' ).that.equals( 2 );
  48. } );
  49. it( 'should have proper root', function() {
  50. expect( root ).to.have.property( 'root' ).that.equals( root );
  51. expect( one ).to.have.property( 'root' ).that.equals( root );
  52. expect( two ).to.have.property( 'root' ).that.equals( root );
  53. expect( three ).to.have.property( 'root' ).that.equals( root );
  54. expect( charB ).to.have.property( 'root' ).that.equals( root );
  55. expect( charA ).to.have.property( 'root' ).that.equals( root );
  56. expect( img ).to.have.property( 'root' ).that.equals( root );
  57. expect( charR ).to.have.property( 'root' ).that.equals( root );
  58. } );
  59. it( 'should have proper nextSibling', function() {
  60. expect( root ).to.have.property( 'nextSibling' ).that.is.null;
  61. expect( one ).to.have.property( 'nextSibling' ).that.equals( two );
  62. expect( two ).to.have.property( 'nextSibling' ).that.equals( three );
  63. expect( three ).to.have.property( 'nextSibling' ).that.is.null;
  64. expect( charB ).to.have.property( 'nextSibling' ).that.equals( charA );
  65. expect( charA ).to.have.property( 'nextSibling' ).that.equals( img );
  66. expect( img ).to.have.property( 'nextSibling' ).that.equals( charR );
  67. expect( charR ).to.have.property( 'nextSibling' ).that.is.null;
  68. } );
  69. it( 'should have proper previousSibling', function() {
  70. expect( root ).to.have.property( 'previousSibling' ).that.is.expect;
  71. expect( one ).to.have.property( 'previousSibling' ).that.is.null;
  72. expect( two ).to.have.property( 'previousSibling' ).that.equals( one );
  73. expect( three ).to.have.property( 'previousSibling' ).that.equals( two );
  74. expect( charB ).to.have.property( 'previousSibling' ).that.is.null;
  75. expect( charA ).to.have.property( 'previousSibling' ).that.equals( charB );
  76. expect( img ).to.have.property( 'previousSibling' ).that.equals( charA );
  77. expect( charR ).to.have.property( 'previousSibling' ).that.equals( img );
  78. } );
  79. } );
  80. describe( 'constructor', function() {
  81. it( 'should copy attributes, not pass by reference', function() {
  82. var Element = modules[ 'document/element' ];
  83. var Attribute = modules[ 'document/attribute' ];
  84. var attrs = [ new Attribute( 'attr', true ) ];
  85. var foo = new Element( 'foo', attrs );
  86. var bar = new Element( 'bar', attrs );
  87. foo.removeAttr( 'attr' );
  88. expect( foo._getAttrCount() ).to.equals( 0 );
  89. expect( bar._getAttrCount() ).to.equals( 1 );
  90. } );
  91. } );
  92. describe( 'getAttr', function() {
  93. it( 'should be possible to get attribute by key', function() {
  94. var Element = modules[ 'document/element' ];
  95. var Attribute = modules[ 'document/attribute' ];
  96. var fooAttr = new Attribute( 'foo', true );
  97. var element = new Element( 'foo', [ fooAttr ] );
  98. expect( element.getAttr( 'foo' ) ).to.equals( fooAttr.value );
  99. } );
  100. it( 'should return null if attribute was not found by key', function() {
  101. var Element = modules[ 'document/element' ];
  102. var Attribute = modules[ 'document/attribute' ];
  103. var fooAttr = new Attribute( 'foo', true );
  104. var element = new Element( 'foo', [ fooAttr ] );
  105. expect( element.getAttr( 'bar' ) ).to.be.null;
  106. } );
  107. } );
  108. describe( 'setAttr', function() {
  109. it( 'should insert an attribute', function() {
  110. var Element = modules[ 'document/element' ];
  111. var Attribute = modules[ 'document/attribute' ];
  112. var element = new Element( 'elem' );
  113. var attr = new Attribute( 'foo', 'bar' );
  114. element.setAttr( attr );
  115. expect( element._getAttrCount() ).to.equals( 1 );
  116. expect( element.getAttr( attr.key ) ).to.equals( attr.value );
  117. } );
  118. it( 'should overwrite attribute with the same key', function() {
  119. var Element = modules[ 'document/element' ];
  120. var Attribute = modules[ 'document/attribute' ];
  121. var oldAttr = new Attribute( 'foo', 'bar' );
  122. var newAttr = new Attribute( 'foo', 'bar' );
  123. var element = new Element( 'elem', [ oldAttr ] );
  124. element.setAttr( newAttr );
  125. expect( element._getAttrCount() ).to.equals( 1 );
  126. expect( element.getAttr( newAttr.key ) ).to.equals( newAttr.value );
  127. } );
  128. } );
  129. describe( 'removeAttr', function() {
  130. it( 'should remove an attribute', function() {
  131. var Element = modules[ 'document/element' ];
  132. var Attribute = modules[ 'document/attribute' ];
  133. var attrA = new Attribute( 'a', 'A' );
  134. var attrB = new Attribute( 'b', 'b' );
  135. var attrC = new Attribute( 'c', 'C' );
  136. var element = new Element( 'elem', [ attrA, attrB, attrC ] );
  137. element.removeAttr( attrB.key );
  138. expect( element._getAttrCount() ).to.equals( 2 );
  139. expect( element.getAttr( attrA.key ) ).to.equals( attrA.value );
  140. expect( element.getAttr( attrC.key ) ).to.equals( attrC.value );
  141. expect( element.getAttr( attrB.key ) ).to.be.null;
  142. } );
  143. } );
  144. describe( 'hasAttr', function() {
  145. it( 'should check attribute by key', function() {
  146. var Element = modules[ 'document/element' ];
  147. var Attribute = modules[ 'document/attribute' ];
  148. var fooAttr = new Attribute( 'foo', true );
  149. var element = new Element( 'foo', [ fooAttr ] );
  150. expect( element.hasAttr( 'foo' ) ).to.be.true;
  151. } );
  152. it( 'should return false if attribute was not found by key', function() {
  153. var Element = modules[ 'document/element' ];
  154. var Attribute = modules[ 'document/attribute' ];
  155. var fooAttr = new Attribute( 'foo', true );
  156. var element = new Element( 'foo', [ fooAttr ] );
  157. expect( element.hasAttr( 'bar' ) ).to.be.false;
  158. } );
  159. it( 'should check attribute by object', function() {
  160. var Element = modules[ 'document/element' ];
  161. var Attribute = modules[ 'document/attribute' ];
  162. var fooAttr = new Attribute( 'foo', true );
  163. var foo2Attr = new Attribute( 'foo', true );
  164. var element = new Element( 'foo', [ fooAttr ] );
  165. expect( element.hasAttr( foo2Attr ) ).to.be.true;
  166. } );
  167. it( 'should return false if attribute was not found by object', function() {
  168. var Element = modules[ 'document/element' ];
  169. var Attribute = modules[ 'document/attribute' ];
  170. var fooAttr = new Attribute( 'foo', true );
  171. var element = new Element( 'foo' );
  172. expect( element.hasAttr( fooAttr ) ).to.be.false;
  173. } );
  174. it( 'should create proper JSON string using toJSON method', function() {
  175. var Element = modules[ 'document/element' ];
  176. var Character = modules[ 'document/character' ];
  177. var b = new Character( 'b' );
  178. var foo = new Element( 'foo', [], [ b ] );
  179. var parsedFoo = JSON.parse( JSON.stringify( foo ) );
  180. var parsedBar = JSON.parse( JSON.stringify( b ) );
  181. expect( parsedFoo.parent ).to.be.equals( null );
  182. expect( parsedBar.parent ).to.be.equals( 'foo' );
  183. } );
  184. } );