8
0

node.js 6.9 KB

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