element.js 6.9 KB

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