element.js 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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 Node from '/ckeditor5/engine/treemodel/node.js';
  8. import NodeList from '/ckeditor5/engine/treemodel/nodelist.js';
  9. import Element from '/ckeditor5/engine/treemodel/element.js';
  10. import DocumentFragment from '/ckeditor5/engine/treemodel/documentfragment.js';
  11. describe( 'Element', () => {
  12. describe( 'constructor', () => {
  13. it( 'should create element without attributes', () => {
  14. let element = new Element( 'elem' );
  15. let parent = new Element( 'parent', [], [ element ] );
  16. expect( element ).to.be.an.instanceof( Node );
  17. expect( element ).to.have.property( 'name' ).that.equals( 'elem' );
  18. expect( element ).to.have.property( 'parent' ).that.equals( parent );
  19. expect( element._attrs.size ).to.equal( 0 );
  20. } );
  21. it( 'should create element with attributes', () => {
  22. let attr = { foo: 'bar' };
  23. let element = new Element( 'elem', [ attr ] );
  24. let parent = new Element( 'parent', [], [ element ] );
  25. expect( element ).to.be.an.instanceof( Node );
  26. expect( element ).to.have.property( 'name' ).that.equals( 'elem' );
  27. expect( element ).to.have.property( 'parent' ).that.equals( parent );
  28. expect( element._attrs.size ).to.equal( 1 );
  29. expect( element.getAttribute( attr.key ) ).to.equal( attr.value );
  30. } );
  31. it( 'should create element with children', () => {
  32. let element = new Element( 'elem', [], 'foo' );
  33. expect( element ).to.have.property( 'name' ).that.equals( 'elem' );
  34. expect( element.getChildCount() ).to.equal( 3 );
  35. expect( element.getChild( 0 ) ).to.have.property( 'character' ).that.equals( 'f' );
  36. expect( element.getChild( 1 ) ).to.have.property( 'character' ).that.equals( 'o' );
  37. expect( element.getChild( 2 ) ).to.have.property( 'character' ).that.equals( 'o' );
  38. } );
  39. } );
  40. describe( 'insertChildren', () => {
  41. it( 'should add children to the element', () => {
  42. let element = new Element( 'elem', [], [ 'xy' ] );
  43. element.insertChildren( 1, 'foo' );
  44. expect( element ).to.have.property( 'name' ).that.equals( 'elem' );
  45. expect( element.getChildCount() ).to.equal( 5 );
  46. expect( element.getChild( 0 ) ).to.have.property( 'character' ).that.equals( 'x' );
  47. expect( element.getChild( 1 ) ).to.have.property( 'character' ).that.equals( 'f' );
  48. expect( element.getChild( 2 ) ).to.have.property( 'character' ).that.equals( 'o' );
  49. expect( element.getChild( 3 ) ).to.have.property( 'character' ).that.equals( 'o' );
  50. expect( element.getChild( 4 ) ).to.have.property( 'character' ).that.equals( 'y' );
  51. } );
  52. it( 'should accept DocumentFragment as a parameter and clean it after it is added', () => {
  53. let p1 = new Element( 'p' );
  54. let p2 = new Element( 'p' );
  55. let frag = new DocumentFragment( [ p1, p2 ] );
  56. let element = new Element( 'div' );
  57. element.insertChildren( 0, frag );
  58. expect( element.getChildCount() ).to.equal( 2 );
  59. expect( element.getChild( 0 ) ).to.equal( p1 );
  60. expect( element.getChild( 1 ) ).to.equal( p2 );
  61. expect( frag.getChildCount() ).to.equal( 0 );
  62. } );
  63. } );
  64. describe( 'appendChildren', () => {
  65. it( 'should add children to the end of the element', () => {
  66. let element = new Element( 'elem', [], [ 'xy' ] );
  67. element.appendChildren( 'foo' );
  68. expect( element ).to.have.property( 'name' ).that.equals( 'elem' );
  69. expect( element.getChildCount() ).to.equal( 5 );
  70. expect( element.getChild( 0 ) ).to.have.property( 'character' ).that.equals( 'x' );
  71. expect( element.getChild( 1 ) ).to.have.property( 'character' ).that.equals( 'y' );
  72. expect( element.getChild( 2 ) ).to.have.property( 'character' ).that.equals( 'f' );
  73. expect( element.getChild( 3 ) ).to.have.property( 'character' ).that.equals( 'o' );
  74. expect( element.getChild( 4 ) ).to.have.property( 'character' ).that.equals( 'o' );
  75. } );
  76. it( 'should accept DocumentFragment as a parameter and clean it after it is added', () => {
  77. let p1 = new Element( 'p' );
  78. let p2 = new Element( 'p' );
  79. let frag = new DocumentFragment( [ p1, p2 ] );
  80. let element = new Element( 'div' );
  81. element.appendChildren( frag );
  82. expect( element.getChildCount() ).to.equal( 2 );
  83. expect( element.getChild( 0 ) ).to.equal( p1 );
  84. expect( element.getChild( 1 ) ).to.equal( p2 );
  85. expect( frag.getChildCount() ).to.equal( 0 );
  86. } );
  87. } );
  88. describe( 'removeChildren', () => {
  89. it( 'should remove children from the element and return them as a NodeList', () => {
  90. let element = new Element( 'elem', [], [ 'foobar' ] );
  91. let removed = element.removeChildren( 2, 3 );
  92. expect( element.getChildCount() ).to.equal( 3 );
  93. expect( element.getChild( 0 ) ).to.have.property( 'character' ).that.equals( 'f' );
  94. expect( element.getChild( 1 ) ).to.have.property( 'character' ).that.equals( 'o' );
  95. expect( element.getChild( 2 ) ).to.have.property( 'character' ).that.equals( 'r' );
  96. expect( removed ).to.be.instanceof( NodeList );
  97. expect( removed.length ).to.equal( 3 );
  98. expect( removed.get( 0 ).character ).to.equal( 'o' );
  99. expect( removed.get( 1 ).character ).to.equal( 'b' );
  100. expect( removed.get( 2 ).character ).to.equal( 'a' );
  101. } );
  102. it( 'should remove one child when second parameter is not specified', () => {
  103. let element = new Element( 'elem', [], [ 'foo' ] );
  104. let removed = element.removeChildren( 2 );
  105. expect( element.getChildCount() ).to.equal( 2 );
  106. expect( element.getChild( 0 ) ).to.have.property( 'character' ).that.equals( 'f' );
  107. expect( element.getChild( 1 ) ).to.have.property( 'character' ).that.equals( 'o' );
  108. expect( removed ).to.be.instanceof( NodeList );
  109. expect( removed.length ).to.equal( 1 );
  110. expect( removed.get( 0 ).character ).to.equal( 'o' );
  111. } );
  112. } );
  113. describe( 'getChildIndex', () => {
  114. it( 'should return child index', () => {
  115. let element = new Element( 'elem', [], [ new Element( 'p' ), 'bar', new Element( 'h' ) ] );
  116. let p = element.getChild( 0 );
  117. let b = element.getChild( 1 );
  118. let a = element.getChild( 2 );
  119. let r = element.getChild( 3 );
  120. let h = element.getChild( 4 );
  121. expect( element.getChildIndex( p ) ).to.equal( 0 );
  122. expect( element.getChildIndex( b ) ).to.equal( 1 );
  123. expect( element.getChildIndex( a ) ).to.equal( 2 );
  124. expect( element.getChildIndex( r ) ).to.equal( 3 );
  125. expect( element.getChildIndex( h ) ).to.equal( 4 );
  126. } );
  127. } );
  128. describe( 'getChildCount', () => {
  129. it( 'should return number of children', () => {
  130. let element = new Element( 'elem', [], [ 'bar' ] );
  131. expect( element.getChildCount() ).to.equal( 3 );
  132. } );
  133. } );
  134. describe( 'attributes interface', () => {
  135. let node;
  136. beforeEach( () => {
  137. node = new Element();
  138. } );
  139. describe( 'setAttribute', () => {
  140. it( 'should set given attribute on the element', () => {
  141. node.setAttribute( 'foo', 'bar' );
  142. expect( node.getAttribute( 'foo' ) ).to.equal( 'bar' );
  143. } );
  144. } );
  145. describe( 'setAttributesTo', () => {
  146. it( 'should remove all attributes set on element and set the given ones', () => {
  147. node.setAttribute( 'abc', 'xyz' );
  148. node.setAttributesTo( { foo: 'bar' } );
  149. expect( node.getAttribute( 'foo' ) ).to.equal( 'bar' );
  150. expect( node.getAttribute( 'abc' ) ).to.be.undefined;
  151. } );
  152. } );
  153. describe( 'removeAttribute', () => {
  154. it( 'should remove attribute set on the element and return true', () => {
  155. node.setAttribute( 'foo', 'bar' );
  156. let result = node.removeAttribute( 'foo' );
  157. expect( node.getAttribute( 'foo' ) ).to.be.undefined;
  158. expect( result ).to.be.true;
  159. } );
  160. it( 'should return false if element does not contain given attribute', () => {
  161. let result = node.removeAttribute( 'foo' );
  162. expect( result ).to.be.false;
  163. } );
  164. } );
  165. describe( 'clearAttributes', () => {
  166. it( 'should remove all attributes from the element', () => {
  167. node.setAttribute( 'foo', 'bar' );
  168. node.setAttribute( 'abc', 'xyz' );
  169. node.clearAttributes();
  170. expect( node.getAttribute( 'foo' ) ).to.be.undefined;
  171. expect( node.getAttribute( 'abc' ) ).to.be.undefined;
  172. } );
  173. } );
  174. } );
  175. } );