element.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /**
  2. * @license Copyright (c) 2003-2015, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* jshint expr: true */
  6. /* bender-tags: document */
  7. /* bender-include: ../_tools/tools.js */
  8. 'use strict';
  9. const getIteratorCount = bender.tools.core.getIteratorCount;
  10. const modules = bender.amd.require(
  11. 'document/node',
  12. 'document/nodelist',
  13. 'document/element',
  14. 'document/attribute'
  15. );
  16. describe( 'Element', () => {
  17. let Element, Node, NodeList, Attribute;
  18. before( () => {
  19. Element = modules[ 'document/element' ];
  20. Node = modules[ 'document/node' ];
  21. NodeList = modules[ 'document/nodelist' ];
  22. Attribute = modules[ 'document/attribute' ];
  23. } );
  24. describe( 'constructor', () => {
  25. it( 'should create element without attributes', () => {
  26. let element = new Element( 'elem' );
  27. let parent = new Element( 'parent', [], [ element ] );
  28. expect( element ).to.be.an.instanceof( Node );
  29. expect( element ).to.have.property( 'name' ).that.equals( 'elem' );
  30. expect( element ).to.have.property( 'parent' ).that.equals( parent );
  31. expect( getIteratorCount( element.getAttrs() ) ).to.equal( 0 );
  32. } );
  33. it( 'should create element with attributes', () => {
  34. let attr = new Attribute( 'foo', 'bar' );
  35. let element = new Element( 'elem', [ attr ] );
  36. let parent = new Element( 'parent', [], [ element ] );
  37. expect( element ).to.be.an.instanceof( Node );
  38. expect( element ).to.have.property( 'name' ).that.equals( 'elem' );
  39. expect( element ).to.have.property( 'parent' ).that.equals( parent );
  40. expect( getIteratorCount( element.getAttrs() ) ).to.equal( 1 );
  41. expect( element.getAttr( attr.key ) ).to.equal( attr.value );
  42. } );
  43. it( 'should create element with children', () => {
  44. let element = new Element( 'elem', [], 'foo' );
  45. expect( element ).to.have.property( 'name' ).that.equals( 'elem' );
  46. expect( element.getChildCount() ).to.equal( 3 );
  47. expect( element.getChild( 0 ) ).to.have.property( 'character' ).that.equals( 'f' );
  48. expect( element.getChild( 1 ) ).to.have.property( 'character' ).that.equals( 'o' );
  49. expect( element.getChild( 2 ) ).to.have.property( 'character' ).that.equals( 'o' );
  50. } );
  51. } );
  52. describe( 'insertChildren', () => {
  53. it( 'should add children to the element', () => {
  54. let element = new Element( 'elem', [], [ 'xy' ] );
  55. element.insertChildren( 1, '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( 'f' );
  60. expect( element.getChild( 2 ) ).to.have.property( 'character' ).that.equals( 'o' );
  61. expect( element.getChild( 3 ) ).to.have.property( 'character' ).that.equals( 'o' );
  62. expect( element.getChild( 4 ) ).to.have.property( 'character' ).that.equals( 'y' );
  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 o = element.getChild( 2 );
  69. let b = element.getChild( 3 );
  70. let a = element.getChild( 4 );
  71. let removed = element.removeChildren( 2, 3 );
  72. expect( element.getChildCount() ).to.equal( 3 );
  73. expect( element.getChild( 0 ) ).to.have.property( 'character' ).that.equals( 'f' );
  74. expect( element.getChild( 1 ) ).to.have.property( 'character' ).that.equals( 'o' );
  75. expect( element.getChild( 2 ) ).to.have.property( 'character' ).that.equals( 'r' );
  76. expect( o ).to.have.property( 'parent' ).that.is.null;
  77. expect( b ).to.have.property( 'parent' ).that.is.null;
  78. expect( a ).to.have.property( 'parent' ).that.is.null;
  79. expect( removed ).to.be.instanceof( NodeList );
  80. expect( removed.length ).to.equal( 3 );
  81. expect( removed.get( 0 ) ).to.equal( o );
  82. expect( removed.get( 1 ) ).to.equal( b );
  83. expect( removed.get( 2 ) ).to.equal( a );
  84. } );
  85. } );
  86. describe( 'getChildIndex', () => {
  87. it( 'should return child index', () => {
  88. let element = new Element( 'elem', [], [ 'bar' ] );
  89. let b = element.getChild( 0 );
  90. let a = element.getChild( 1 );
  91. let r = element.getChild( 2 );
  92. expect( element.getChildIndex( b ) ).to.equal( 0 );
  93. expect( element.getChildIndex( a ) ).to.equal( 1 );
  94. expect( element.getChildIndex( r ) ).to.equal( 2 );
  95. } );
  96. } );
  97. describe( 'getChildCount', () => {
  98. it( 'should return number of children', () => {
  99. let element = new Element( 'elem', [], [ 'bar' ] );
  100. expect( element.getChildCount() ).to.equal( 3 );
  101. } );
  102. } );
  103. } );