element.js 4.4 KB

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