element.js 8.7 KB

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