element.js 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. /**
  2. * @license Copyright (c) 2003-2015, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* bender-tags: treeview */
  6. 'use strict';
  7. import utils from '/ckeditor5/utils/utils.js';
  8. import Node from '/ckeditor5/core/treeview/node.js';
  9. import ViewElement from '/ckeditor5/core/treeview/element.js';
  10. describe( 'Element', () => {
  11. describe( 'constructor', () => {
  12. it( 'should create element without attributes', () => {
  13. const el = new ViewElement( 'p' );
  14. expect( el ).to.be.an.instanceof( Node );
  15. expect( el ).to.have.property( 'name' ).that.equals( 'p' );
  16. expect( el ).to.have.property( 'parent' ).that.is.null;
  17. expect( utils.count( el.getAttributeKeys() ) ).to.equal( 0 );
  18. } );
  19. it( 'should create element with attributes as plain object', () => {
  20. const el = new ViewElement( 'p', { foo: 'bar' } );
  21. expect( el ).to.have.property( 'name' ).that.equals( 'p' );
  22. expect( utils.count( el.getAttributeKeys() ) ).to.equal( 1 );
  23. expect( el.getAttribute( 'foo' ) ).to.equal( 'bar' );
  24. } );
  25. it( 'should create element with attributes as map', () => {
  26. const attrs = new Map();
  27. attrs.set( 'foo', 'bar' );
  28. const el = new ViewElement( 'p', attrs );
  29. expect( el ).to.have.property( 'name' ).that.equals( 'p' );
  30. expect( utils.count( el.getAttributeKeys() ) ).to.equal( 1 );
  31. expect( el.getAttribute( 'foo' ) ).to.equal( 'bar' );
  32. } );
  33. it( 'should create element with children', () => {
  34. const child = new ViewElement( 'p', { foo: 'bar' } );
  35. const parent = new ViewElement( 'div', [], [ child ] );
  36. expect( parent ).to.have.property( 'name' ).that.equals( 'div' );
  37. expect( parent.getChildCount() ).to.equal( 1 );
  38. expect( parent.getChild( 0 ) ).to.have.property( 'name' ).that.equals( 'p' );
  39. } );
  40. } );
  41. describe( 'clone', () => {
  42. it( 'should clone element', () => {
  43. const el = new ViewElement( 'p', { attr1: 'foo', attr2: 'bar' } );
  44. const clone = el.clone();
  45. expect( clone ).to.not.equal( el );
  46. expect( clone.name ).to.equal( el.name );
  47. expect( clone.getAttribute( 'attr1' ) ).to.equal( 'foo' );
  48. expect( clone.getAttribute( 'attr2' ) ).to.equal( 'bar' );
  49. } );
  50. it( 'should deeply clone element', () => {
  51. const el = new ViewElement( 'p', { attr1: 'foo', attr2: 'bar' }, [
  52. new ViewElement( 'b', { attr: 'baz' } ),
  53. new ViewElement( 'span', { attr: 'qux' } )
  54. ] );
  55. const count = el.getChildCount();
  56. const clone = el.clone( true );
  57. expect( clone ).to.not.equal( el );
  58. expect( clone.name ).to.equal( el.name );
  59. expect( clone.getAttribute( 'attr1' ) ).to.equal( 'foo' );
  60. expect( clone.getAttribute( 'attr2' ) ).to.equal( 'bar' );
  61. expect( clone.getChildCount() ).to.equal( count );
  62. for ( let i = 0; i < count; i++ ) {
  63. const child = el.getChild( i );
  64. const clonedChild = clone.getChild( i );
  65. expect( clonedChild ).to.not.equal( child );
  66. expect( clonedChild.name ).to.equal( child.name );
  67. expect( clonedChild.getAttribute( 'attr' ) ).to.equal( child.getAttribute( 'attr' ) );
  68. }
  69. } );
  70. it( 'shouldn\'t clone any children when deep copy is not performed', () => {
  71. const el = new ViewElement( 'p', { attr1: 'foo', attr2: 'bar' }, [
  72. new ViewElement( 'b', { attr: 'baz' } ),
  73. new ViewElement( 'span', { attr: 'qux' } )
  74. ] );
  75. const clone = el.clone( false );
  76. expect( clone ).to.not.equal( el );
  77. expect( clone.name ).to.equal( el.name );
  78. expect( clone.getAttribute( 'attr1' ) ).to.equal( 'foo' );
  79. expect( clone.getAttribute( 'attr2' ) ).to.equal( 'bar' );
  80. expect( clone.getChildCount() ).to.equal( 0 );
  81. } );
  82. } );
  83. describe( 'isSimilar', () => {
  84. const el = new ViewElement( 'p', { foo: 'bar' } );
  85. it( 'should return false when comparing to non-element', () => {
  86. expect( el.isSimilar( null ) ).to.be.false;
  87. expect( el.isSimilar( {} ) ).to.be.false;
  88. } );
  89. it( 'should return true when the same node is provided', () => {
  90. expect( el.isSimilar( el ) ).to.be.true;
  91. } );
  92. it( 'should return true for element with same attributes and name', () => {
  93. const other = new ViewElement( 'p', { foo: 'bar' } );
  94. expect( el.isSimilar( other ) ).to.be.true;
  95. } );
  96. it( 'sould return false when name is not the same', () => {
  97. const other = el.clone();
  98. other.name = 'div';
  99. expect( el.isSimilar( other ) ).to.be.false;
  100. } );
  101. it( 'should return false when attributes are not the same', () => {
  102. const other1 = el.clone();
  103. const other2 = el.clone();
  104. const other3 = el.clone();
  105. other1.setAttribute( 'baz', 'qux' );
  106. other2.setAttribute( 'foo', 'not-bar' );
  107. other3.removeAttribute( 'foo' );
  108. expect( el.isSimilar( other1 ) ).to.be.false;
  109. expect( el.isSimilar( other2 ) ).to.be.false;
  110. expect( el.isSimilar( other3 ) ).to.be.false;
  111. } );
  112. } );
  113. describe( 'children manipulation methods', () => {
  114. let parent, el1, el2, el3, el4;
  115. beforeEach( () => {
  116. parent = new ViewElement( 'p' );
  117. el1 = new ViewElement( 'el1' );
  118. el2 = new ViewElement( 'el2' );
  119. el3 = new ViewElement( 'el3' );
  120. el4 = new ViewElement( 'el4' );
  121. } );
  122. describe( 'insertion', () => {
  123. it( 'should insert children', () => {
  124. const count1 = parent.insertChildren( 0, [ el1, el3 ] );
  125. const count2 = parent.insertChildren( 1, el2 );
  126. expect( parent.getChildCount() ).to.equal( 3 );
  127. expect( parent.getChild( 0 ) ).to.have.property( 'name' ).that.equals( 'el1' );
  128. expect( parent.getChild( 1 ) ).to.have.property( 'name' ).that.equals( 'el2' );
  129. expect( parent.getChild( 2 ) ).to.have.property( 'name' ).that.equals( 'el3' );
  130. expect( count1 ).to.equal( 2 );
  131. expect( count2 ).to.equal( 1 );
  132. } );
  133. it( 'should append children', () => {
  134. const count1 = parent.insertChildren( 0, el1 );
  135. const count2 = parent.appendChildren( el2 );
  136. const count3 = parent.appendChildren( el3 );
  137. expect( parent.getChildCount() ).to.equal( 3 );
  138. expect( parent.getChild( 0 ) ).to.have.property( 'name' ).that.equals( 'el1' );
  139. expect( parent.getChild( 1 ) ).to.have.property( 'name' ).that.equals( 'el2' );
  140. expect( parent.getChild( 2 ) ).to.have.property( 'name' ).that.equals( 'el3' );
  141. expect( count1 ).to.equal( 1 );
  142. expect( count2 ).to.equal( 1 );
  143. expect( count3 ).to.equal( 1 );
  144. } );
  145. } );
  146. describe( 'getChildIndex', () => {
  147. it( 'should return child index', () => {
  148. parent.appendChildren( el1 );
  149. parent.appendChildren( el2 );
  150. parent.appendChildren( el3 );
  151. expect( parent.getChildCount() ).to.equal( 3 );
  152. expect( parent.getChildIndex( el1 ) ).to.equal( 0 );
  153. expect( parent.getChildIndex( el2 ) ).to.equal( 1 );
  154. expect( parent.getChildIndex( el3 ) ).to.equal( 2 );
  155. } );
  156. } );
  157. describe( 'getChildren', () => {
  158. it( 'should renturn children iterator', () => {
  159. parent.appendChildren( el1 );
  160. parent.appendChildren( el2 );
  161. parent.appendChildren( el3 );
  162. const expected = [ el1, el2, el3 ];
  163. let i = 0;
  164. for ( let child of parent.getChildren() ) {
  165. expect( child ).to.equal( expected[ i ] );
  166. i++;
  167. }
  168. expect( i ).to.equal( 3 );
  169. } );
  170. } );
  171. describe( 'removeChildren', () => {
  172. it( 'should remove children', () => {
  173. parent.appendChildren( el1 );
  174. parent.appendChildren( el2 );
  175. parent.appendChildren( el3 );
  176. parent.appendChildren( el4 );
  177. parent.removeChildren( 1, 2 );
  178. expect( parent.getChildCount() ).to.equal( 2 );
  179. expect( parent.getChild( 0 ) ).to.have.property( 'name' ).that.equals( 'el1' );
  180. expect( parent.getChild( 1 ) ).to.have.property( 'name' ).that.equals( 'el4' );
  181. expect( el1.parent ).to.equal( parent );
  182. expect( el2.parent ).to.be.null;
  183. expect( el3.parent ).to.be.null;
  184. expect( el4.parent ).equal( parent );
  185. } );
  186. } );
  187. } );
  188. describe( 'attributes manipulation methods', () => {
  189. let el;
  190. beforeEach( () => {
  191. el = new ViewElement( 'p' );
  192. } );
  193. describe( 'getAttribute', () => {
  194. it( 'should return attribute', () => {
  195. el.setAttribute( 'foo', 'bar' );
  196. expect( el.getAttribute( 'foo' ) ).to.equal( 'bar' );
  197. expect( el.getAttribute( 'bom' ) ).to.not.be.ok;
  198. } );
  199. } );
  200. describe( 'hasAttribute', () => {
  201. it( 'should return true if element has attribute', () => {
  202. el.setAttribute( 'foo', 'bar' );
  203. expect( el.hasAttribute( 'foo' ) ).to.be.true;
  204. expect( el.hasAttribute( 'bom' ) ).to.be.false;
  205. } );
  206. } );
  207. describe( 'getAttributeKeys', () => {
  208. it( 'should return keys', () => {
  209. el.setAttribute( 'foo', true );
  210. el.setAttribute( 'bar', true );
  211. const expected = [ 'foo', 'bar' ];
  212. let i = 0;
  213. for ( let key of el.getAttributeKeys() ) {
  214. expect( key ).to.equal( expected[ i ] );
  215. i++;
  216. }
  217. expect( i ).to.equal( 2 );
  218. } );
  219. } );
  220. describe( 'removeAttribute', () => {
  221. it( 'should remove attributes', () => {
  222. el.setAttribute( 'foo', true );
  223. expect( el.hasAttribute( 'foo' ) ).to.be.true;
  224. el.removeAttribute( 'foo' );
  225. expect( el.hasAttribute( 'foo' ) ).to.be.false;
  226. expect( utils.count( el.getAttributeKeys() ) ).to.equal( 0 );
  227. } );
  228. } );
  229. } );
  230. } );