8
0

element.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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/core/treemodel/node.js';
  8. import NodeList from '/ckeditor5/core/treemodel/nodelist.js';
  9. import Element from '/ckeditor5/core/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. } );
  80. describe( 'getChildIndex', () => {
  81. it( 'should return child index', () => {
  82. let element = new Element( 'elem', [], [ new Element( 'p' ), 'bar', new Element( 'h' ) ] );
  83. let p = element.getChild( 0 );
  84. let b = element.getChild( 1 );
  85. let a = element.getChild( 2 );
  86. let r = element.getChild( 3 );
  87. let h = element.getChild( 4 );
  88. expect( element.getChildIndex( p ) ).to.equal( 0 );
  89. expect( element.getChildIndex( b ) ).to.equal( 1 );
  90. expect( element.getChildIndex( a ) ).to.equal( 2 );
  91. expect( element.getChildIndex( r ) ).to.equal( 3 );
  92. expect( element.getChildIndex( h ) ).to.equal( 4 );
  93. } );
  94. } );
  95. describe( 'getChildCount', () => {
  96. it( 'should return number of children', () => {
  97. let element = new Element( 'elem', [], [ 'bar' ] );
  98. expect( element.getChildCount() ).to.equal( 3 );
  99. } );
  100. } );
  101. describe( 'attributes interface', () => {
  102. let node;
  103. beforeEach( () => {
  104. node = new Element();
  105. } );
  106. describe( 'setAttribute', () => {
  107. it( 'should set given attribute on the element', () => {
  108. node.setAttribute( 'foo', 'bar' );
  109. expect( node.getAttribute( 'foo' ) ).to.equal( 'bar' );
  110. } );
  111. } );
  112. describe( 'setAttributesTo', () => {
  113. it( 'should remove all attributes set on element and set the given ones', () => {
  114. node.setAttribute( 'abc', 'xyz' );
  115. node.setAttributesTo( { foo: 'bar' } );
  116. expect( node.getAttribute( 'foo' ) ).to.equal( 'bar' );
  117. expect( node.getAttribute( 'abc' ) ).to.be.undefined;
  118. } );
  119. } );
  120. describe( 'removeAttribute', () => {
  121. it( 'should remove attribute set on the element and return true', () => {
  122. node.setAttribute( 'foo', 'bar' );
  123. let result = node.removeAttribute( 'foo' );
  124. expect( node.getAttribute( 'foo' ) ).to.be.undefined;
  125. expect( result ).to.be.true;
  126. } );
  127. it( 'should return false if element does not contain given attribute', () => {
  128. let result = node.removeAttribute( 'foo' );
  129. expect( result ).to.be.false;
  130. } );
  131. } );
  132. describe( 'clearAttributes', () => {
  133. it( 'should remove all attributes from the element', () => {
  134. node.setAttribute( 'foo', 'bar' );
  135. node.setAttribute( 'abc', 'xyz' );
  136. node.clearAttributes();
  137. expect( node.getAttribute( 'foo' ) ).to.be.undefined;
  138. expect( node.getAttribute( 'abc' ) ).to.be.undefined;
  139. } );
  140. } );
  141. } );
  142. } );