node.js 7.4 KB

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