8
0

element.js 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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. /* bender-include: ../_tools/tools.js */
  7. 'use strict';
  8. const getIteratorCount = bender.tools.core.getIteratorCount;
  9. const modules = bender.amd.require(
  10. 'core/treeview/node',
  11. 'core/treeview/element'
  12. );
  13. describe( 'Element', () => {
  14. let ViewElement, Node;
  15. before( () => {
  16. ViewElement = modules[ 'core/treeview/element' ];
  17. Node = modules[ 'core/treeview/node' ];
  18. } );
  19. describe( 'constructor', () => {
  20. it( 'should create element without attributes', () => {
  21. const elem = new ViewElement( 'p' );
  22. expect( elem ).to.be.an.instanceof( Node );
  23. expect( elem ).to.have.property( 'name' ).that.equals( 'p' );
  24. expect( elem ).to.have.property( 'parent' ).that.is.null;
  25. expect( getIteratorCount( elem.getAttrKeys() ) ).to.equal( 0 );
  26. } );
  27. it( 'should create element with attributes as plain object', () => {
  28. const elem = new ViewElement( 'p', { 'foo': 'bar' } );
  29. expect( elem ).to.have.property( 'name' ).that.equals( 'p' );
  30. expect( getIteratorCount( elem.getAttrKeys() ) ).to.equal( 1 );
  31. expect( elem.getAttr( 'foo' ) ).to.equal( 'bar' );
  32. } );
  33. it( 'should create element with attributes as map', () => {
  34. const attrs = new Map();
  35. attrs.set( 'foo', 'bar' );
  36. const elem = new ViewElement( 'p', attrs );
  37. expect( elem ).to.have.property( 'name' ).that.equals( 'p' );
  38. expect( getIteratorCount( elem.getAttrKeys() ) ).to.equal( 1 );
  39. expect( elem.getAttr( 'foo' ) ).to.equal( 'bar' );
  40. } );
  41. it( 'should create element with children', () => {
  42. const child = new ViewElement( 'p', { 'foo': 'bar' } );
  43. const parent = new ViewElement( 'div', [], [ child ] );
  44. expect( parent ).to.have.property( 'name' ).that.equals( 'div' );
  45. expect( parent.getChildCount() ).to.equal( 1 );
  46. expect( parent.getChild( 0 ) ).to.have.property( 'name' ).that.equals( 'p' );
  47. } );
  48. } );
  49. describe( 'children manipulation methods', () => {
  50. let parent, e1, e2, e3, e4;
  51. beforeEach( () => {
  52. parent = new ViewElement( 'p' );
  53. e1 = new ViewElement( 'e1' );
  54. e2 = new ViewElement( 'e2' );
  55. e3 = new ViewElement( 'e3' );
  56. e4 = new ViewElement( 'e4' );
  57. } );
  58. describe( 'insertion', () => {
  59. it( 'should insert children', () => {
  60. parent.insertChildren( 0, [ e1, e3 ] );
  61. parent.insertChildren( 1, e2 );
  62. expect( parent.getChildCount() ).to.equal( 3 );
  63. expect( parent.getChild( 0 ) ).to.have.property( 'name' ).that.equals( 'e1' );
  64. expect( parent.getChild( 1 ) ).to.have.property( 'name' ).that.equals( 'e2' );
  65. expect( parent.getChild( 2 ) ).to.have.property( 'name' ).that.equals( 'e3' );
  66. } );
  67. it( 'should append children', () => {
  68. parent.insertChildren( 0, e1 );
  69. parent.appendChildren( e2 );
  70. parent.appendChildren( e3 );
  71. expect( parent.getChildCount() ).to.equal( 3 );
  72. expect( parent.getChild( 0 ) ).to.have.property( 'name' ).that.equals( 'e1' );
  73. expect( parent.getChild( 1 ) ).to.have.property( 'name' ).that.equals( 'e2' );
  74. expect( parent.getChild( 2 ) ).to.have.property( 'name' ).that.equals( 'e3' );
  75. } );
  76. } );
  77. describe( 'getChildIndex', () => {
  78. it( 'should return child index', () => {
  79. parent.appendChildren( e1 );
  80. parent.appendChildren( e2 );
  81. parent.appendChildren( e3 );
  82. expect( parent.getChildCount() ).to.equal( 3 );
  83. expect( parent.getChildIndex( e1 ) ).to.equal( 0 );
  84. expect( parent.getChildIndex( e2 ) ).to.equal( 1 );
  85. expect( parent.getChildIndex( e3 ) ).to.equal( 2 );
  86. } );
  87. } );
  88. describe( 'getChildren', () => {
  89. it( 'should renturn children iterator', () => {
  90. parent.appendChildren( e1 );
  91. parent.appendChildren( e2 );
  92. parent.appendChildren( e3 );
  93. const expected = [ e1, e2, e3 ];
  94. let i = 0;
  95. for ( let child of parent.getChildren() ) {
  96. expect( child ).to.equal( expected[ i ] );
  97. i++;
  98. }
  99. expect( i ).to.equal( 3 );
  100. } );
  101. } );
  102. describe( 'removeChildren', () => {
  103. it( 'should remove children', () => {
  104. parent.appendChildren( e1 );
  105. parent.appendChildren( e2 );
  106. parent.appendChildren( e3 );
  107. parent.appendChildren( e4 );
  108. parent.removeChildren( 1, 2 );
  109. expect( parent.getChildCount() ).to.equal( 2 );
  110. expect( parent.getChild( 0 ) ).to.have.property( 'name' ).that.equals( 'e1' );
  111. expect( parent.getChild( 1 ) ).to.have.property( 'name' ).that.equals( 'e4' );
  112. expect( e1.parent ).to.equal( parent );
  113. expect( e2.parent ).to.be.null;
  114. expect( e3.parent ).to.be.null;
  115. expect( e4.parent ).equal( parent );
  116. } );
  117. } );
  118. } );
  119. describe( 'attributes manipulation methods', () => {
  120. let elem;
  121. beforeEach( () => {
  122. elem = new ViewElement( 'p' );
  123. } );
  124. describe( 'getAttr', () => {
  125. it( 'should return attribute', () => {
  126. elem.setAttr( 'foo', 'bar' );
  127. expect( elem.getAttr( 'foo' ) ).to.equal( 'bar' );
  128. expect( elem.getAttr( 'bom' ) ).to.not.be.ok;
  129. } );
  130. } );
  131. describe( 'hasAttr', () => {
  132. it( 'should return true if element has attribute', () => {
  133. elem.setAttr( 'foo', 'bar' );
  134. expect( elem.hasAttr( 'foo' ) ).to.be.true;
  135. expect( elem.hasAttr( 'bom' ) ).to.be.false;
  136. } );
  137. } );
  138. describe( 'getAttrKeys', () => {
  139. it( 'should return keys', () => {
  140. elem.setAttr( 'foo', true );
  141. elem.setAttr( 'bar', true );
  142. const expected = [ 'foo', 'bar' ];
  143. let i = 0;
  144. for ( let child of elem.getAttrKeys() ) {
  145. expect( child ).to.equal( expected[ i ] );
  146. i++;
  147. }
  148. expect( i ).to.equal( 2 );
  149. } );
  150. } );
  151. describe( 'removeAttr', () => {
  152. it( 'should remove attributes', () => {
  153. elem.setAttr( 'foo', true );
  154. expect( elem.hasAttr( 'foo' ) ).to.be.true;
  155. elem.removeAttr( 'foo' );
  156. expect( elem.hasAttr( 'foo' ) ).to.be.false;
  157. expect( getIteratorCount( elem.getAttrKeys() ) ).to.equal( 0 );
  158. } );
  159. } );
  160. describe( 'cloneDomAttrs', () => {
  161. it( 'should clone DOM attributes', () => {
  162. const domElement = document.createElement( 'p' );
  163. domElement.setAttribute( 'foo', '1' );
  164. domElement.setAttribute( 'bar', '2' );
  165. elem.cloneDomAttrs( domElement );
  166. expect( elem.getAttr( 'foo' ) ).to.equal( '1' );
  167. expect( elem.getAttr( 'bar' ) ).to.equal( '2' );
  168. expect( getIteratorCount( elem.getAttrKeys() ) ).to.equal( 2 );
  169. } );
  170. } );
  171. } );
  172. describe( 'DOM-View binding', () => {
  173. describe( 'getCorespondingView', () => {
  174. it( 'should return coresponding view element', () => {
  175. const domElement = document.createElement( 'p' );
  176. const viewElement = new ViewElement( 'p' );
  177. viewElement.bindDomElement( domElement );
  178. expect( ViewElement.getCorespondingView( domElement ) ).to.equal( viewElement );
  179. } );
  180. } );
  181. describe( 'getCorespondingDom', () => {
  182. it( 'should return coresponding DOM element', () => {
  183. const domElement = document.createElement( 'p' );
  184. const viewElement = new ViewElement( 'p' );
  185. viewElement.bindDomElement( domElement );
  186. expect( viewElement.getCorespondingDom() ).to.equal( domElement );
  187. } );
  188. } );
  189. describe( 'createFromDom', () => {
  190. it( 'should create tree of view elements from DOM elements', () => {
  191. const domImg = document.createElement( 'img' );
  192. const domText = document.createTextNode( 'foo' );
  193. const domP = document.createElement( 'p' );
  194. domP.setAttribute( 'class', 'foo' );
  195. domP.appendChild( domImg );
  196. domP.appendChild( domText );
  197. const viewImg = new ViewElement( 'img' );
  198. viewImg.bindDomElement( domImg );
  199. const viewP = ViewElement.createFromDom( domP );
  200. expect( viewP ).to.be.an.instanceof( ViewElement );
  201. expect( viewP.name ).to.equal( 'p' );
  202. expect( viewP.getAttr( 'class' ) ).to.equal( 'foo' );
  203. expect( getIteratorCount( viewP.getAttrKeys() ) ).to.equal( 1 );
  204. expect( viewP.getChildCount() ).to.equal( 2 );
  205. expect( viewP.getChild( 0 ).name ).to.equal( 'img' );
  206. expect( viewP.getChild( 1 ).getText() ).to.equal( 'foo' );
  207. expect( viewP.getCorespondingDom() ).to.not.equal( domP );
  208. expect( viewP.getChild( 0 ).getCorespondingDom() ).to.equal( domImg );
  209. } );
  210. } );
  211. } );
  212. } );