node.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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 nextSibling', function() {
  50. expect( root ).to.have.property( 'nextSibling' ).that.is.null;
  51. expect( one ).to.have.property( 'nextSibling' ).that.equals( two );
  52. expect( two ).to.have.property( 'nextSibling' ).that.equals( three );
  53. expect( three ).to.have.property( 'nextSibling' ).that.is.null;
  54. expect( charB ).to.have.property( 'nextSibling' ).that.equals( charA );
  55. expect( charA ).to.have.property( 'nextSibling' ).that.equals( img );
  56. expect( img ).to.have.property( 'nextSibling' ).that.equals( charR );
  57. expect( charR ).to.have.property( 'nextSibling' ).that.is.null;
  58. } );
  59. it( 'should have proper previousSibling', function() {
  60. expect( root ).to.have.property( 'previousSibling' ).that.is.expect;
  61. expect( one ).to.have.property( 'previousSibling' ).that.is.null;
  62. expect( two ).to.have.property( 'previousSibling' ).that.equals( one );
  63. expect( three ).to.have.property( 'previousSibling' ).that.equals( two );
  64. expect( charB ).to.have.property( 'previousSibling' ).that.is.null;
  65. expect( charA ).to.have.property( 'previousSibling' ).that.equals( charB );
  66. expect( img ).to.have.property( 'previousSibling' ).that.equals( charA );
  67. expect( charR ).to.have.property( 'previousSibling' ).that.equals( img );
  68. } );
  69. } );
  70. describe( 'getAttr', function() {
  71. it( 'should be possible to get attribute by key', function() {
  72. var Element = modules[ 'document/element' ];
  73. var Attribute = modules[ 'document/attribute' ];
  74. var fooAttr = new Attribute( 'foo', true );
  75. var element = new Element( 'foo', [ fooAttr ] );
  76. expect( element.getAttr( 'foo' ).isEqual( fooAttr ) ).to.be.true;
  77. } );
  78. it( 'should return null if attribute was not found 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( 'foo', [ fooAttr ] );
  83. expect( element.getAttr( 'bar' ) ).to.be.null;
  84. } );
  85. it( 'should be possible to get attribute by object', function() {
  86. var Element = modules[ 'document/element' ];
  87. var Attribute = modules[ 'document/attribute' ];
  88. var fooAttr = new Attribute( 'foo', true );
  89. var foo2Attr = new Attribute( 'foo', true );
  90. var element = new Element( 'foo', [ fooAttr ] );
  91. expect( element.getAttr( foo2Attr ).isEqual( fooAttr ) ).to.be.true;
  92. } );
  93. it( 'should return null if attribute was not found by object', function() {
  94. var Element = modules[ 'document/element' ];
  95. var Attribute = modules[ 'document/attribute' ];
  96. var fooAttr = new Attribute( 'foo', true );
  97. var element = new Element( 'foo' );
  98. expect( element.getAttr( fooAttr ) ).to.be.null;
  99. } );
  100. } );
  101. describe( 'hasAttr', function() {
  102. it( 'should check attribute by key', function() {
  103. var Element = modules[ 'document/element' ];
  104. var Attribute = modules[ 'document/attribute' ];
  105. var fooAttr = new Attribute( 'foo', true );
  106. var element = new Element( 'foo', [ fooAttr ] );
  107. expect( element.hasAttr( 'foo' ) ).to.be.true;
  108. } );
  109. it( 'should return false if attribute was not found 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( 'foo', [ fooAttr ] );
  114. expect( element.hasAttr( 'bar' ) ).to.be.false;
  115. } );
  116. it( 'should check attribute by object', function() {
  117. var Element = modules[ 'document/element' ];
  118. var Attribute = modules[ 'document/attribute' ];
  119. var fooAttr = new Attribute( 'foo', true );
  120. var foo2Attr = new Attribute( 'foo', true );
  121. var element = new Element( 'foo', [ fooAttr ] );
  122. expect( element.hasAttr( foo2Attr ) ).to.be.true;
  123. } );
  124. it( 'should return false if attribute was not found by object', function() {
  125. var Element = modules[ 'document/element' ];
  126. var Attribute = modules[ 'document/attribute' ];
  127. var fooAttr = new Attribute( 'foo', true );
  128. var element = new Element( 'foo' );
  129. expect( element.hasAttr( fooAttr ) ).to.be.false;
  130. } );
  131. it( 'should create proper JSON string using toJSON method', function() {
  132. var Element = modules[ 'document/element' ];
  133. var Character = modules[ 'document/character' ];
  134. var NodeList = modules[ 'document/nodelist' ];
  135. var b = new Character( 'b' );
  136. var foo = new Element( 'foo', [], [ b ] );
  137. var parsedFoo = JSON.parse( JSON.stringify( foo ) );
  138. var parsedBar = JSON.parse( JSON.stringify( b ) );
  139. parsedFoo.children = new NodeList( parsedFoo.children );
  140. expect( parsedFoo ).to.be.deep.equals( {
  141. name: 'foo',
  142. parent: null,
  143. attrs: [],
  144. children: new NodeList( parsedFoo.children )
  145. } );
  146. expect( parsedBar ).to.be.deep.equals( {
  147. character: 'b',
  148. parent: 'foo',
  149. attrs: []
  150. } );
  151. } );
  152. } );