8
0

node.js 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. /**
  2. * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* bender-tags: treemodel */
  6. 'use strict';
  7. import coreTestUtils from '/tests/core/_utils/utils.js';
  8. import Attribute from '/ckeditor5/core/treemodel/attribute.js';
  9. import Element from '/ckeditor5/core/treemodel/element.js';
  10. import CKEditorError from '/ckeditor5/core/ckeditorerror.js';
  11. import Character from '/ckeditor5/core/treemodel/character.js';
  12. const getIteratorCount = coreTestUtils.getIteratorCount;
  13. describe( 'Node', () => {
  14. let root;
  15. let one, two, three;
  16. let charB, charA, charR, img, attrEle;
  17. let attrFooBar;
  18. before( () => {
  19. charB = new Character( 'b' );
  20. charA = new Character( 'a' );
  21. img = new Element( 'img' );
  22. charR = new Character( 'r' );
  23. one = new Element( 'one' );
  24. two = new Element( 'two', null, [ charB, charA, img, charR ] );
  25. three = new Element( 'three' );
  26. root = new Element( null, null, [ one, two, three ] );
  27. attrFooBar = new Attribute( 'foo', 'bar' );
  28. } );
  29. beforeEach( () => {
  30. attrEle = new Element( 'element' );
  31. } );
  32. describe( 'should have a correct property', () => {
  33. it( 'depth', () => {
  34. expect( root ).to.have.property( 'depth' ).that.equals( 0 );
  35. expect( one ).to.have.property( 'depth' ).that.equals( 1 );
  36. expect( two ).to.have.property( 'depth' ).that.equals( 1 );
  37. expect( three ).to.have.property( 'depth' ).that.equals( 1 );
  38. expect( charB ).to.have.property( 'depth' ).that.equals( 2 );
  39. expect( charA ).to.have.property( 'depth' ).that.equals( 2 );
  40. expect( img ).to.have.property( 'depth' ).that.equals( 2 );
  41. expect( charR ).to.have.property( 'depth' ).that.equals( 2 );
  42. } );
  43. it( 'root', () => {
  44. expect( root ).to.have.property( 'root' ).that.equals( root );
  45. expect( one ).to.have.property( 'root' ).that.equals( root );
  46. expect( two ).to.have.property( 'root' ).that.equals( root );
  47. expect( three ).to.have.property( 'root' ).that.equals( root );
  48. expect( charB ).to.have.property( 'root' ).that.equals( root );
  49. expect( charA ).to.have.property( 'root' ).that.equals( root );
  50. expect( img ).to.have.property( 'root' ).that.equals( root );
  51. expect( charR ).to.have.property( 'root' ).that.equals( root );
  52. } );
  53. it( 'nextSibling', () => {
  54. expect( root ).to.have.property( 'nextSibling' ).that.is.null;
  55. expect( one ).to.have.property( 'nextSibling' ).that.equals( two );
  56. expect( two ).to.have.property( 'nextSibling' ).that.equals( three );
  57. expect( three ).to.have.property( 'nextSibling' ).that.is.null;
  58. expect( charB ).to.have.property( 'nextSibling' ).that.equals( charA );
  59. expect( charA ).to.have.property( 'nextSibling' ).that.equals( img );
  60. expect( img ).to.have.property( 'nextSibling' ).that.equals( charR );
  61. expect( charR ).to.have.property( 'nextSibling' ).that.is.null;
  62. } );
  63. it( 'previousSibling', () => {
  64. expect( root ).to.have.property( 'previousSibling' ).that.is.expect;
  65. expect( one ).to.have.property( 'previousSibling' ).that.is.null;
  66. expect( two ).to.have.property( 'previousSibling' ).that.equals( one );
  67. expect( three ).to.have.property( 'previousSibling' ).that.equals( two );
  68. expect( charB ).to.have.property( 'previousSibling' ).that.is.null;
  69. expect( charA ).to.have.property( 'previousSibling' ).that.equals( charB );
  70. expect( img ).to.have.property( 'previousSibling' ).that.equals( charA );
  71. expect( charR ).to.have.property( 'previousSibling' ).that.equals( img );
  72. } );
  73. } );
  74. describe( 'constructor', () => {
  75. it( 'should copy attributes list, not pass by reference', () => {
  76. let attrs = [ new Attribute( 'attr', true ) ];
  77. let foo = new Element( 'foo', attrs );
  78. let bar = new Element( 'bar', attrs );
  79. foo.removeAttr( 'attr' );
  80. expect( getIteratorCount( foo.getAttrs() ) ).to.equal( 0 );
  81. expect( getIteratorCount( bar.getAttrs() ) ).to.equal( 1 );
  82. } );
  83. } );
  84. it( 'should create proper JSON string using toJSON method', () => {
  85. let b = new Character( 'b' );
  86. let foo = new Element( 'foo', [], [ b ] );
  87. let parsedFoo = JSON.parse( JSON.stringify( foo ) );
  88. let parsedBar = JSON.parse( JSON.stringify( b ) );
  89. expect( parsedFoo.parent ).to.equal( null );
  90. expect( parsedBar.parent ).to.equal( 'foo' );
  91. } );
  92. describe( 'getIndex', () => {
  93. it( 'should return null if the parent is null', () => {
  94. expect( root.getIndex() ).to.be.null;
  95. } );
  96. it( 'should return index in the parent', () => {
  97. expect( one.getIndex() ).to.equal( 0 );
  98. expect( two.getIndex() ).to.equal( 1 );
  99. expect( three.getIndex() ).to.equal( 2 );
  100. expect( charB.getIndex() ).to.equal( 0 );
  101. expect( charA.getIndex() ).to.equal( 1 );
  102. expect( img.getIndex() ).to.equal( 2 );
  103. expect( charR.getIndex() ).to.equal( 3 );
  104. } );
  105. it( 'should throw an error if parent does not contains element', () => {
  106. let f = new Character( 'f' );
  107. let bar = new Element( 'bar', [], [] );
  108. f.parent = bar;
  109. expect(
  110. () => {
  111. f.getIndex();
  112. }
  113. ).to.throw( CKEditorError, /node-not-found-in-parent/ );
  114. } );
  115. } );
  116. describe( 'getPath', () => {
  117. it( 'should return proper path', () => {
  118. expect( root.getPath() ).to.deep.equal( [] );
  119. expect( one.getPath() ).to.deep.equal( [ 0 ] );
  120. expect( two.getPath() ).to.deep.equal( [ 1 ] );
  121. expect( three.getPath() ).to.deep.equal( [ 2 ] );
  122. expect( charB.getPath() ).to.deep.equal( [ 1, 0 ] );
  123. expect( charA.getPath() ).to.deep.equal( [ 1, 1 ] );
  124. expect( img.getPath() ).to.deep.equal( [ 1, 2 ] );
  125. expect( charR.getPath() ).to.deep.equal( [ 1, 3 ] );
  126. } );
  127. } );
  128. // Testing integration with attributes list.
  129. // Tests copied from AttributeList tests.
  130. // Some cases were omitted.
  131. describe( 'setAttr', () => {
  132. it( 'should insert an attribute', () => {
  133. attrEle.setAttr( attrFooBar );
  134. expect( getIteratorCount( attrEle.getAttrs() ) ).to.equal( 1 );
  135. expect( attrEle.getAttr( attrFooBar.key ) ).to.equal( attrFooBar.value );
  136. } );
  137. } );
  138. describe( 'setAttrsTo', () => {
  139. it( 'should remove all attributes and set passed ones', () => {
  140. attrEle.setAttr( attrFooBar );
  141. let attrs = [ new Attribute( 'abc', true ), new Attribute( 'xyz', false ) ];
  142. attrEle.setAttrsTo( attrs );
  143. expect( getIteratorCount( attrEle.getAttrs() ) ).to.equal( 2 );
  144. expect( attrEle.getAttr( 'foo' ) ).to.be.null;
  145. expect( attrEle.getAttr( 'abc' ) ).to.be.true;
  146. expect( attrEle.getAttr( 'xyz' ) ).to.be.false;
  147. } );
  148. } );
  149. describe( 'getAttr', () => {
  150. beforeEach( () => {
  151. attrEle = new Element( 'e', [ attrFooBar ] );
  152. } );
  153. it( 'should return attribute value if key of previously set attribute has been passed', () => {
  154. expect( attrEle.getAttr( 'foo' ) ).to.equal( attrFooBar.value );
  155. } );
  156. it( 'should return null if attribute with given key has not been found', () => {
  157. expect( attrEle.getAttr( 'bar' ) ).to.be.null;
  158. } );
  159. } );
  160. describe( 'removeAttr', () => {
  161. it( 'should remove an attribute', () => {
  162. let attrA = new Attribute( 'a', 'A' );
  163. let attrB = new Attribute( 'b', 'B' );
  164. let attrC = new Attribute( 'c', 'C' );
  165. attrEle.setAttr( attrA );
  166. attrEle.setAttr( attrB );
  167. attrEle.setAttr( attrC );
  168. attrEle.removeAttr( attrB.key );
  169. expect( getIteratorCount( attrEle.getAttrs() ) ).to.equal( 2 );
  170. expect( attrEle.getAttr( attrA.key ) ).to.equal( attrA.value );
  171. expect( attrEle.getAttr( attrC.key ) ).to.equal( attrC.value );
  172. expect( attrEle.getAttr( attrB.key ) ).to.be.null;
  173. } );
  174. } );
  175. describe( 'hasAttr', () => {
  176. it( 'should check attribute by key', () => {
  177. attrEle.setAttr( attrFooBar );
  178. expect( attrEle.hasAttr( 'foo' ) ).to.be.true;
  179. } );
  180. it( 'should return false if attribute was not found by key', () => {
  181. expect( attrEle.hasAttr( 'bar' ) ).to.be.false;
  182. } );
  183. it( 'should check attribute by object', () => {
  184. attrEle.setAttr( attrFooBar );
  185. expect( attrEle.hasAttr( attrFooBar ) ).to.be.true;
  186. } );
  187. it( 'should return false if attribute was not found by object', () => {
  188. expect( attrEle.hasAttr( attrFooBar ) ).to.be.false;
  189. } );
  190. } );
  191. describe( 'getAttrs', () => {
  192. it( 'should return all set attributes', () => {
  193. let attrA = new Attribute( 'a', 'A' );
  194. let attrB = new Attribute( 'b', 'B' );
  195. let attrC = new Attribute( 'c', 'C' );
  196. attrEle.setAttrsTo( [
  197. attrA,
  198. attrB,
  199. attrC
  200. ] );
  201. attrEle.removeAttr( attrB.key );
  202. expect( [ attrA, attrC ] ).to.deep.equal( Array.from( attrEle.getAttrs() ) );
  203. } );
  204. } );
  205. } );