8
0

node.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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( 'getAttr', function() {
  81. it( 'should be possible to get attribute by key', function() {
  82. var Element = modules[ 'document/element' ];
  83. var Attribute = modules[ 'document/attribute' ];
  84. var fooAttr = new Attribute( 'foo', true );
  85. var element = new Element( 'foo', [ fooAttr ] );
  86. expect( element.getAttr( 'foo' ).isEqual( fooAttr ) ).to.be.true;
  87. } );
  88. it( 'should return null if attribute was not found by key', function() {
  89. var Element = modules[ 'document/element' ];
  90. var Attribute = modules[ 'document/attribute' ];
  91. var fooAttr = new Attribute( 'foo', true );
  92. var element = new Element( 'foo', [ fooAttr ] );
  93. expect( element.getAttr( 'bar' ) ).to.be.null;
  94. } );
  95. it( 'should be possible to get attribute by object', function() {
  96. var Element = modules[ 'document/element' ];
  97. var Attribute = modules[ 'document/attribute' ];
  98. var fooAttr = new Attribute( 'foo', true );
  99. var foo2Attr = new Attribute( 'foo', true );
  100. var element = new Element( 'foo', [ fooAttr ] );
  101. expect( element.getAttr( foo2Attr ).isEqual( fooAttr ) ).to.be.true;
  102. } );
  103. it( 'should return null if attribute was not found by object', function() {
  104. var Element = modules[ 'document/element' ];
  105. var Attribute = modules[ 'document/attribute' ];
  106. var fooAttr = new Attribute( 'foo', true );
  107. var element = new Element( 'foo' );
  108. expect( element.getAttr( fooAttr ) ).to.be.null;
  109. } );
  110. } );
  111. describe( 'hasAttr', function() {
  112. it( 'should check attribute by key', function() {
  113. var Element = modules[ 'document/element' ];
  114. var Attribute = modules[ 'document/attribute' ];
  115. var fooAttr = new Attribute( 'foo', true );
  116. var element = new Element( 'foo', [ fooAttr ] );
  117. expect( element.hasAttr( 'foo' ) ).to.be.true;
  118. } );
  119. it( 'should return false if attribute was not found by key', function() {
  120. var Element = modules[ 'document/element' ];
  121. var Attribute = modules[ 'document/attribute' ];
  122. var fooAttr = new Attribute( 'foo', true );
  123. var element = new Element( 'foo', [ fooAttr ] );
  124. expect( element.hasAttr( 'bar' ) ).to.be.false;
  125. } );
  126. it( 'should check attribute by object', function() {
  127. var Element = modules[ 'document/element' ];
  128. var Attribute = modules[ 'document/attribute' ];
  129. var fooAttr = new Attribute( 'foo', true );
  130. var foo2Attr = new Attribute( 'foo', true );
  131. var element = new Element( 'foo', [ fooAttr ] );
  132. expect( element.hasAttr( foo2Attr ) ).to.be.true;
  133. } );
  134. it( 'should return false if attribute was not found by object', function() {
  135. var Element = modules[ 'document/element' ];
  136. var Attribute = modules[ 'document/attribute' ];
  137. var fooAttr = new Attribute( 'foo', true );
  138. var element = new Element( 'foo' );
  139. expect( element.hasAttr( fooAttr ) ).to.be.false;
  140. } );
  141. it( 'should create proper JSON string using toJSON method', function() {
  142. var Element = modules[ 'document/element' ];
  143. var Character = modules[ 'document/character' ];
  144. var NodeList = modules[ 'document/nodelist' ];
  145. var b = new Character( 'b' );
  146. var foo = new Element( 'foo', [], [ b ] );
  147. var parsedFoo = JSON.parse( JSON.stringify( foo ) );
  148. var parsedBar = JSON.parse( JSON.stringify( b ) );
  149. parsedFoo._children = new NodeList( parsedFoo._children );
  150. expect( parsedFoo ).to.be.deep.equals( {
  151. name: 'foo',
  152. parent: null,
  153. attrs: [],
  154. _children: new NodeList( parsedFoo._children )
  155. } );
  156. expect( parsedBar ).to.be.deep.equals( {
  157. character: 'b',
  158. parent: 'foo',
  159. attrs: []
  160. } );
  161. } );
  162. } );