element.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. 'use strict';
  8. const modules = bender.amd.require(
  9. 'document/node',
  10. 'document/nodelist',
  11. 'document/element',
  12. 'document/attribute'
  13. );
  14. describe( 'Element', function() {
  15. let Element, Node, NodeList, Attribute;
  16. before( function() {
  17. Element = modules[ 'document/element' ];
  18. Node = modules[ 'document/node' ];
  19. NodeList = modules[ 'document/nodelist' ];
  20. Attribute = modules[ 'document/attribute' ];
  21. } );
  22. describe( 'constructor', function() {
  23. it( 'should create element without attributes', function() {
  24. var element = new Element( 'elem' );
  25. var parent = new Element( 'parent', [], [ element ] );
  26. expect( element ).to.be.an.instanceof( Node );
  27. expect( element ).to.have.property( 'name' ).that.equals( 'elem' );
  28. expect( element ).to.have.property( 'parent' ).that.equals( parent );
  29. expect( element._getAttrCount() ).to.equal( 0 );
  30. } );
  31. it( 'should create element with attributes', function() {
  32. var attr = new Attribute( 'foo', 'bar' );
  33. var element = new Element( 'elem', [ attr ] );
  34. var parent = new Element( 'parent', [], [ element ] );
  35. expect( element ).to.be.an.instanceof( Node );
  36. expect( element ).to.have.property( 'name' ).that.equals( 'elem' );
  37. expect( element ).to.have.property( 'parent' ).that.equals( parent );
  38. expect( element._getAttrCount() ).to.equal( 1 );
  39. expect( element.getAttr( attr.key ) ).to.equal( attr.value );
  40. } );
  41. it( 'should create element with children', function() {
  42. var element = new Element( 'elem', [], 'foo' );
  43. expect( element ).to.have.property( 'name' ).that.equals( 'elem' );
  44. expect( element.getChildCount() ).to.equal( 3 );
  45. expect( element.getChild( 0 ) ).to.have.property( 'character' ).that.equals( 'f' );
  46. expect( element.getChild( 1 ) ).to.have.property( 'character' ).that.equals( 'o' );
  47. expect( element.getChild( 2 ) ).to.have.property( 'character' ).that.equals( 'o' );
  48. } );
  49. } );
  50. describe( 'insertChildren', function() {
  51. it( 'should add children to the element', function() {
  52. var element = new Element( 'elem', [], [ 'xy' ] );
  53. element.insertChildren( 1, 'foo' );
  54. expect( element ).to.have.property( 'name' ).that.equals( 'elem' );
  55. expect( element.getChildCount() ).to.equal( 5 );
  56. expect( element.getChild( 0 ) ).to.have.property( 'character' ).that.equals( 'x' );
  57. expect( element.getChild( 1 ) ).to.have.property( 'character' ).that.equals( 'f' );
  58. expect( element.getChild( 2 ) ).to.have.property( 'character' ).that.equals( 'o' );
  59. expect( element.getChild( 3 ) ).to.have.property( 'character' ).that.equals( 'o' );
  60. expect( element.getChild( 4 ) ).to.have.property( 'character' ).that.equals( 'y' );
  61. } );
  62. } );
  63. describe( 'removeChildren', function() {
  64. it( 'should remove children from the element and return them as a NodeList', function() {
  65. var element = new Element( 'elem', [], [ 'foobar' ] );
  66. var o = element.getChild( 2 );
  67. var b = element.getChild( 3 );
  68. var a = element.getChild( 4 );
  69. var removed = element.removeChildren( 2, 3 );
  70. expect( element.getChildCount() ).to.equal( 3 );
  71. expect( element.getChild( 0 ) ).to.have.property( 'character' ).that.equals( 'f' );
  72. expect( element.getChild( 1 ) ).to.have.property( 'character' ).that.equals( 'o' );
  73. expect( element.getChild( 2 ) ).to.have.property( 'character' ).that.equals( 'r' );
  74. expect( o ).to.have.property( 'parent' ).that.is.null;
  75. expect( b ).to.have.property( 'parent' ).that.is.null;
  76. expect( a ).to.have.property( 'parent' ).that.is.null;
  77. expect( removed ).to.be.instanceof( NodeList );
  78. expect( removed.length ).to.equal( 3 );
  79. expect( removed.get( 0 ) ).to.equal( o );
  80. expect( removed.get( 1 ) ).to.equal( b );
  81. expect( removed.get( 2 ) ).to.equal( a );
  82. } );
  83. } );
  84. describe( 'getChildIndex', function() {
  85. it( 'should return child index', function() {
  86. var element = new Element( 'elem', [], [ 'bar' ] );
  87. var b = element.getChild( 0 );
  88. var a = element.getChild( 1 );
  89. var r = element.getChild( 2 );
  90. expect( element.getChildIndex( b ) ).to.equal( 0 );
  91. expect( element.getChildIndex( a ) ).to.equal( 1 );
  92. expect( element.getChildIndex( r ) ).to.equal( 2 );
  93. } );
  94. } );
  95. describe( 'getChildCount', function() {
  96. it( 'should return number of children', function() {
  97. var element = new Element( 'elem', [], [ 'bar' ] );
  98. expect( element.getChildCount() ).to.equal( 3 );
  99. } );
  100. } );
  101. } );