node.js 6.7 KB

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