node.js 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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. /* bender-include: ../_tools/tools.js */
  7. 'use strict';
  8. const getIteratorCount = bender.tools.core.getIteratorCount;
  9. const modules = bender.amd.require(
  10. 'document/element',
  11. 'document/character',
  12. 'document/attribute',
  13. 'document/nodelist',
  14. 'ckeditorerror'
  15. );
  16. describe( 'Node', () => {
  17. let Element, Character, Attribute, NodeList, CKEditorError;
  18. let root;
  19. let one, two, three;
  20. let charB, charA, charR, img;
  21. before( () => {
  22. Element = modules[ 'document/element' ];
  23. Character = modules[ 'document/character' ];
  24. Attribute = modules[ 'document/attribute' ];
  25. NodeList = modules[ 'document/nodelist' ];
  26. CKEditorError = modules.ckeditorerror;
  27. charB = new Character( 'b' );
  28. charA = new Character( 'a' );
  29. img = new Element( 'img' );
  30. charR = new Character( 'r' );
  31. one = new Element( 'one' );
  32. two = new Element( 'two', null, [ charB, charA, img, charR ] );
  33. three = new Element( 'three' );
  34. root = new Element( null, null, [ one, two, three ] );
  35. } );
  36. describe( 'should have a correct property', () => {
  37. it( 'depth', () => {
  38. expect( root ).to.have.property( 'depth' ).that.equals( 0 );
  39. expect( one ).to.have.property( 'depth' ).that.equals( 1 );
  40. expect( two ).to.have.property( 'depth' ).that.equals( 1 );
  41. expect( three ).to.have.property( 'depth' ).that.equals( 1 );
  42. expect( charB ).to.have.property( 'depth' ).that.equals( 2 );
  43. expect( charA ).to.have.property( 'depth' ).that.equals( 2 );
  44. expect( img ).to.have.property( 'depth' ).that.equals( 2 );
  45. expect( charR ).to.have.property( 'depth' ).that.equals( 2 );
  46. } );
  47. it( 'root', () => {
  48. expect( root ).to.have.property( 'root' ).that.equals( root );
  49. expect( one ).to.have.property( 'root' ).that.equals( root );
  50. expect( two ).to.have.property( 'root' ).that.equals( root );
  51. expect( three ).to.have.property( 'root' ).that.equals( root );
  52. expect( charB ).to.have.property( 'root' ).that.equals( root );
  53. expect( charA ).to.have.property( 'root' ).that.equals( root );
  54. expect( img ).to.have.property( 'root' ).that.equals( root );
  55. expect( charR ).to.have.property( 'root' ).that.equals( root );
  56. } );
  57. it( 'nextSibling', () => {
  58. expect( root ).to.have.property( 'nextSibling' ).that.is.null;
  59. expect( one ).to.have.property( 'nextSibling' ).that.equals( two );
  60. expect( two ).to.have.property( 'nextSibling' ).that.equals( three );
  61. expect( three ).to.have.property( 'nextSibling' ).that.is.null;
  62. expect( charB ).to.have.property( 'nextSibling' ).that.equals( charA );
  63. expect( charA ).to.have.property( 'nextSibling' ).that.equals( img );
  64. expect( img ).to.have.property( 'nextSibling' ).that.equals( charR );
  65. expect( charR ).to.have.property( 'nextSibling' ).that.is.null;
  66. } );
  67. it( 'previousSibling', () => {
  68. expect( root ).to.have.property( 'previousSibling' ).that.is.expect;
  69. expect( one ).to.have.property( 'previousSibling' ).that.is.null;
  70. expect( two ).to.have.property( 'previousSibling' ).that.equals( one );
  71. expect( three ).to.have.property( 'previousSibling' ).that.equals( two );
  72. expect( charB ).to.have.property( 'previousSibling' ).that.is.null;
  73. expect( charA ).to.have.property( 'previousSibling' ).that.equals( charB );
  74. expect( img ).to.have.property( 'previousSibling' ).that.equals( charA );
  75. expect( charR ).to.have.property( 'previousSibling' ).that.equals( img );
  76. } );
  77. } );
  78. describe( 'constructor', () => {
  79. it( 'should copy attributes, not pass by reference', () => {
  80. let attrs = [ new Attribute( 'attr', true ) ];
  81. let foo = new Element( 'foo', attrs );
  82. let bar = new Element( 'bar', attrs );
  83. foo.removeAttr( 'attr' );
  84. expect( getIteratorCount( foo.getAttrs() ) ).to.equal( 0 );
  85. expect( getIteratorCount( bar.getAttrs() ) ).to.equal( 1 );
  86. } );
  87. } );
  88. describe( 'getAttr', () => {
  89. let fooAttr, element;
  90. beforeEach( () => {
  91. fooAttr = new Attribute( 'foo', true );
  92. element = new Element( 'foo', [ fooAttr ] );
  93. } );
  94. it( 'should be possible to get attribute by key', () => {
  95. expect( element.getAttr( 'foo' ) ).to.equal( fooAttr.value );
  96. } );
  97. it( 'should return null if attribute was not found by key', () => {
  98. expect( element.getAttr( 'bar' ) ).to.be.null;
  99. } );
  100. } );
  101. describe( 'setAttr', () => {
  102. it( 'should insert an attribute', () => {
  103. let element = new Element( 'elem' );
  104. let attr = new Attribute( 'foo', 'bar' );
  105. element.setAttr( attr );
  106. expect( getIteratorCount( element.getAttrs() ) ).to.equal( 1 );
  107. expect( element.getAttr( attr.key ) ).to.equal( attr.value );
  108. } );
  109. it( 'should overwrite attribute with the same key', () => {
  110. let oldAttr = new Attribute( 'foo', 'bar' );
  111. let newAttr = new Attribute( 'foo', 'bar' );
  112. let element = new Element( 'elem', [ oldAttr ] );
  113. element.setAttr( newAttr );
  114. expect( getIteratorCount( element.getAttrs() ) ).to.equal( 1 );
  115. expect( element.getAttr( newAttr.key ) ).to.equal( newAttr.value );
  116. } );
  117. } );
  118. describe( 'removeAttr', () => {
  119. it( 'should remove an attribute', () => {
  120. let attrA = new Attribute( 'a', 'A' );
  121. let attrB = new Attribute( 'b', 'b' );
  122. let attrC = new Attribute( 'c', 'C' );
  123. let element = new Element( 'elem', [ attrA, attrB, attrC ] );
  124. element.removeAttr( attrB.key );
  125. expect( getIteratorCount( element.getAttrs() ) ).to.equal( 2 );
  126. expect( element.getAttr( attrA.key ) ).to.equal( attrA.value );
  127. expect( element.getAttr( attrC.key ) ).to.equal( attrC.value );
  128. expect( element.getAttr( attrB.key ) ).to.be.null;
  129. } );
  130. } );
  131. describe( 'hasAttr', () => {
  132. it( 'should check attribute by key', () => {
  133. let fooAttr = new Attribute( 'foo', true );
  134. let element = new Element( 'foo', [ fooAttr ] );
  135. expect( element.hasAttr( 'foo' ) ).to.be.true;
  136. } );
  137. it( 'should return false if attribute was not found by key', () => {
  138. let fooAttr = new Attribute( 'foo', true );
  139. let element = new Element( 'foo', [ fooAttr ] );
  140. expect( element.hasAttr( 'bar' ) ).to.be.false;
  141. } );
  142. it( 'should check attribute by object', () => {
  143. let fooAttr = new Attribute( 'foo', true );
  144. let foo2Attr = new Attribute( 'foo', true );
  145. let element = new Element( 'foo', [ fooAttr ] );
  146. expect( element.hasAttr( foo2Attr ) ).to.be.true;
  147. } );
  148. it( 'should return false if attribute was not found by object', () => {
  149. let fooAttr = new Attribute( 'foo', true );
  150. let element = new Element( 'foo' );
  151. expect( element.hasAttr( fooAttr ) ).to.be.false;
  152. } );
  153. it( 'should create proper JSON string using toJSON method', () => {
  154. let b = new Character( 'b' );
  155. let foo = new Element( 'foo', [], [ b ] );
  156. let parsedFoo = JSON.parse( JSON.stringify( foo ) );
  157. let parsedBar = JSON.parse( JSON.stringify( b ) );
  158. expect( parsedFoo.parent ).to.equal( null );
  159. expect( parsedBar.parent ).to.equal( 'foo' );
  160. } );
  161. } );
  162. describe( 'getAttrs', () => {
  163. it( 'should allows to get attribute count', () => {
  164. let element = new Element( 'foo', [
  165. new Attribute( 1, true ),
  166. new Attribute( 2, true ),
  167. new Attribute( 3, true )
  168. ] );
  169. expect( getIteratorCount( element.getAttrs() ) ).to.equal( 3 );
  170. } );
  171. it( 'should allows to copy attributes', () => {
  172. let element = new Element( 'foo', [ new Attribute( 'x', true ) ] );
  173. let copy = new Element( 'bar', element.getAttrs() );
  174. expect( copy.getAttr( 'x' ) ).to.be.true;
  175. } );
  176. } );
  177. describe( 'getIndex', () => {
  178. it( 'should return null if the parent is null', () => {
  179. expect( root.getIndex() ).to.be.null;
  180. } );
  181. it( 'should return index in the parent', () => {
  182. expect( one.getIndex() ).to.equal( 0 );
  183. expect( two.getIndex() ).to.equal( 1 );
  184. expect( three.getIndex() ).to.equal( 2 );
  185. expect( charB.getIndex() ).to.equal( 0 );
  186. expect( charA.getIndex() ).to.equal( 1 );
  187. expect( img.getIndex() ).to.equal( 2 );
  188. expect( charR.getIndex() ).to.equal( 3 );
  189. } );
  190. it( 'should throw an error if parent does not contains element', () => {
  191. let f = new Character( 'f' );
  192. let bar = new Element( 'bar', [], [] );
  193. f.parent = bar;
  194. expect(
  195. () => {
  196. f.getIndex();
  197. }
  198. ).to.throw( CKEditorError, /node-not-found-in-parent/ );
  199. } );
  200. } );
  201. describe( 'getPath', () => {
  202. it( 'should return proper path', () => {
  203. expect( root.getPath() ).to.deep.equal( [] );
  204. expect( one.getPath() ).to.deep.equal( [ 0 ] );
  205. expect( two.getPath() ).to.deep.equal( [ 1 ] );
  206. expect( three.getPath() ).to.deep.equal( [ 2 ] );
  207. expect( charB.getPath() ).to.deep.equal( [ 1, 0 ] );
  208. expect( charA.getPath() ).to.deep.equal( [ 1, 1 ] );
  209. expect( img.getPath() ).to.deep.equal( [ 1, 2 ] );
  210. expect( charR.getPath() ).to.deep.equal( [ 1, 3 ] );
  211. } );
  212. } );
  213. } );