element.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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 coreTestUtils from '/tests/core/_utils/utils.js';
  8. import Node from '/ckeditor5/core/treeview/node.js';
  9. import ViewElement from '/ckeditor5/core/treeview/element.js';
  10. const getIteratorCount = coreTestUtils.getIteratorCount;
  11. describe( 'Element', () => {
  12. describe( 'constructor', () => {
  13. it( 'should create element without attributes', () => {
  14. const elem = new ViewElement( 'p' );
  15. expect( elem ).to.be.an.instanceof( Node );
  16. expect( elem ).to.have.property( 'name' ).that.equals( 'p' );
  17. expect( elem ).to.have.property( 'parent' ).that.is.null;
  18. expect( getIteratorCount( elem.getAttrKeys() ) ).to.equal( 0 );
  19. } );
  20. it( 'should create element with attributes as plain object', () => {
  21. const elem = new ViewElement( 'p', { 'foo': 'bar' } );
  22. expect( elem ).to.have.property( 'name' ).that.equals( 'p' );
  23. expect( getIteratorCount( elem.getAttrKeys() ) ).to.equal( 1 );
  24. expect( elem.getAttr( '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 elem = new ViewElement( 'p', attrs );
  30. expect( elem ).to.have.property( 'name' ).that.equals( 'p' );
  31. expect( getIteratorCount( elem.getAttrKeys() ) ).to.equal( 1 );
  32. expect( elem.getAttr( '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( 'children manipulation methods', () => {
  43. let parent, e1, e2, e3, e4;
  44. beforeEach( () => {
  45. parent = new ViewElement( 'p' );
  46. e1 = new ViewElement( 'e1' );
  47. e2 = new ViewElement( 'e2' );
  48. e3 = new ViewElement( 'e3' );
  49. e4 = new ViewElement( 'e4' );
  50. } );
  51. describe( 'insertion', () => {
  52. it( 'should insert children', () => {
  53. parent.insertChildren( 0, [ e1, e3 ] );
  54. parent.insertChildren( 1, e2 );
  55. expect( parent.getChildCount() ).to.equal( 3 );
  56. expect( parent.getChild( 0 ) ).to.have.property( 'name' ).that.equals( 'e1' );
  57. expect( parent.getChild( 1 ) ).to.have.property( 'name' ).that.equals( 'e2' );
  58. expect( parent.getChild( 2 ) ).to.have.property( 'name' ).that.equals( 'e3' );
  59. } );
  60. it( 'should append children', () => {
  61. parent.insertChildren( 0, e1 );
  62. parent.appendChildren( e2 );
  63. parent.appendChildren( e3 );
  64. expect( parent.getChildCount() ).to.equal( 3 );
  65. expect( parent.getChild( 0 ) ).to.have.property( 'name' ).that.equals( 'e1' );
  66. expect( parent.getChild( 1 ) ).to.have.property( 'name' ).that.equals( 'e2' );
  67. expect( parent.getChild( 2 ) ).to.have.property( 'name' ).that.equals( 'e3' );
  68. } );
  69. } );
  70. describe( 'getChildIndex', () => {
  71. it( 'should return child index', () => {
  72. parent.appendChildren( e1 );
  73. parent.appendChildren( e2 );
  74. parent.appendChildren( e3 );
  75. expect( parent.getChildCount() ).to.equal( 3 );
  76. expect( parent.getChildIndex( e1 ) ).to.equal( 0 );
  77. expect( parent.getChildIndex( e2 ) ).to.equal( 1 );
  78. expect( parent.getChildIndex( e3 ) ).to.equal( 2 );
  79. } );
  80. } );
  81. describe( 'getChildren', () => {
  82. it( 'should renturn children iterator', () => {
  83. parent.appendChildren( e1 );
  84. parent.appendChildren( e2 );
  85. parent.appendChildren( e3 );
  86. const expected = [ e1, e2, e3 ];
  87. let i = 0;
  88. for ( let child of parent.getChildren() ) {
  89. expect( child ).to.equal( expected[ i ] );
  90. i++;
  91. }
  92. expect( i ).to.equal( 3 );
  93. } );
  94. } );
  95. describe( 'removeChildren', () => {
  96. it( 'should remove children', () => {
  97. parent.appendChildren( e1 );
  98. parent.appendChildren( e2 );
  99. parent.appendChildren( e3 );
  100. parent.appendChildren( e4 );
  101. parent.removeChildren( 1, 2 );
  102. expect( parent.getChildCount() ).to.equal( 2 );
  103. expect( parent.getChild( 0 ) ).to.have.property( 'name' ).that.equals( 'e1' );
  104. expect( parent.getChild( 1 ) ).to.have.property( 'name' ).that.equals( 'e4' );
  105. expect( e1.parent ).to.equal( parent );
  106. expect( e2.parent ).to.be.null;
  107. expect( e3.parent ).to.be.null;
  108. expect( e4.parent ).equal( parent );
  109. } );
  110. } );
  111. } );
  112. describe( 'attributes manipulation methods', () => {
  113. let elem;
  114. beforeEach( () => {
  115. elem = new ViewElement( 'p' );
  116. } );
  117. describe( 'getAttr', () => {
  118. it( 'should return attribute', () => {
  119. elem.setAttr( 'foo', 'bar' );
  120. expect( elem.getAttr( 'foo' ) ).to.equal( 'bar' );
  121. expect( elem.getAttr( 'bom' ) ).to.not.be.ok;
  122. } );
  123. } );
  124. describe( 'hasAttr', () => {
  125. it( 'should return true if element has attribute', () => {
  126. elem.setAttr( 'foo', 'bar' );
  127. expect( elem.hasAttr( 'foo' ) ).to.be.true;
  128. expect( elem.hasAttr( 'bom' ) ).to.be.false;
  129. } );
  130. } );
  131. describe( 'getAttrKeys', () => {
  132. it( 'should return keys', () => {
  133. elem.setAttr( 'foo', true );
  134. elem.setAttr( 'bar', true );
  135. const expected = [ 'foo', 'bar' ];
  136. let i = 0;
  137. for ( let child of elem.getAttrKeys() ) {
  138. expect( child ).to.equal( expected[ i ] );
  139. i++;
  140. }
  141. expect( i ).to.equal( 2 );
  142. } );
  143. } );
  144. describe( 'removeAttr', () => {
  145. it( 'should remove attributes', () => {
  146. elem.setAttr( 'foo', true );
  147. expect( elem.hasAttr( 'foo' ) ).to.be.true;
  148. elem.removeAttr( 'foo' );
  149. expect( elem.hasAttr( 'foo' ) ).to.be.false;
  150. expect( getIteratorCount( elem.getAttrKeys() ) ).to.equal( 0 );
  151. } );
  152. } );
  153. } );
  154. } );