8
0

node.js 7.4 KB

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