element.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. import Attribute from '/ckeditor5/core/treemodel/attribute.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 = new Attribute( '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.getAttributeValue( 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. } );
  53. describe( 'removeChildren', () => {
  54. it( 'should remove children from the element and return them as a NodeList', () => {
  55. let element = new Element( 'elem', [], [ 'foobar' ] );
  56. let removed = element.removeChildren( 2, 3 );
  57. expect( element.getChildCount() ).to.equal( 3 );
  58. expect( element.getChild( 0 ) ).to.have.property( 'character' ).that.equals( 'f' );
  59. expect( element.getChild( 1 ) ).to.have.property( 'character' ).that.equals( 'o' );
  60. expect( element.getChild( 2 ) ).to.have.property( 'character' ).that.equals( 'r' );
  61. expect( removed ).to.be.instanceof( NodeList );
  62. expect( removed.length ).to.equal( 3 );
  63. expect( removed.get( 0 ).character ).to.equal( 'o' );
  64. expect( removed.get( 1 ).character ).to.equal( 'b' );
  65. expect( removed.get( 2 ).character ).to.equal( 'a' );
  66. } );
  67. } );
  68. describe( 'getChildIndex', () => {
  69. it( 'should return child index', () => {
  70. let element = new Element( 'elem', [], [ new Element( 'p' ), 'bar', new Element( 'h' ) ] );
  71. let p = element.getChild( 0 );
  72. let b = element.getChild( 1 );
  73. let a = element.getChild( 2 );
  74. let r = element.getChild( 3 );
  75. let h = element.getChild( 4 );
  76. expect( element.getChildIndex( p ) ).to.equal( 0 );
  77. expect( element.getChildIndex( b ) ).to.equal( 1 );
  78. expect( element.getChildIndex( a ) ).to.equal( 2 );
  79. expect( element.getChildIndex( r ) ).to.equal( 3 );
  80. expect( element.getChildIndex( h ) ).to.equal( 4 );
  81. } );
  82. } );
  83. describe( 'getChildCount', () => {
  84. it( 'should return number of children', () => {
  85. let element = new Element( 'elem', [], [ 'bar' ] );
  86. expect( element.getChildCount() ).to.equal( 3 );
  87. } );
  88. } );
  89. describe( 'attributes interface', () => {
  90. let node;
  91. let attr = new Attribute( 'foo', 'bar' );
  92. beforeEach( () => {
  93. node = new Element();
  94. } );
  95. describe( 'setAttribute', () => {
  96. it( 'should set given attribute on the element', () => {
  97. node.setAttribute( attr );
  98. expect( node.getAttributeValue( 'foo' ) ).to.equal( 'bar' );
  99. } );
  100. it( 'should be chainable', () => {
  101. let chain = node.setAttribute( attr );
  102. expect( chain ).to.equal( node );
  103. } );
  104. } );
  105. describe( 'setAttributesTo', () => {
  106. it( 'should remove all attributes set on element and set the given ones', () => {
  107. let attr2 = new Attribute( 'abc', 'xyz' );
  108. node.setAttribute( attr2 );
  109. node.setAttributesTo( [ attr ] );
  110. expect( node.getAttributeValue( 'foo' ) ).to.equal( 'bar' );
  111. expect( node.getAttributeValue( 'abc' ) ).to.be.null;
  112. } );
  113. } );
  114. describe( 'removeAttribute', () => {
  115. it( 'should remove attribute set on the element and return true', () => {
  116. node.setAttribute( attr );
  117. let result = node.removeAttribute( 'foo' );
  118. expect( node.getAttributeValue( 'foo' ) ).to.be.null;
  119. expect( result ).to.be.true;
  120. } );
  121. it( 'should return false if element does not contain given attribute', () => {
  122. let result = node.removeAttribute( 'foo' );
  123. expect( result ).to.be.false;
  124. } );
  125. } );
  126. describe( 'clearAttributes', () => {
  127. it( 'should remove all attributes from the element', () => {
  128. node.setAttribute( attr );
  129. node.setAttribute( new Attribute( 'abc', 'xyz' ) );
  130. node.clearAttributes();
  131. expect( node.getAttributeValue( 'foo' ) ).to.be.null;
  132. expect( node.getAttributeValue( 'abc' ) ).to.be.null;
  133. } );
  134. } );
  135. } );
  136. } );