element.js 4.6 KB

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