element.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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.attrs.getValue( 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 o = element.getChild( 2 );
  57. let b = element.getChild( 3 );
  58. let a = element.getChild( 4 );
  59. let removed = element.removeChildren( 2, 3 );
  60. expect( element.getChildCount() ).to.equal( 3 );
  61. expect( element.getChild( 0 ) ).to.have.property( 'character' ).that.equals( 'f' );
  62. expect( element.getChild( 1 ) ).to.have.property( 'character' ).that.equals( 'o' );
  63. expect( element.getChild( 2 ) ).to.have.property( 'character' ).that.equals( 'r' );
  64. expect( o ).to.have.property( 'parent' ).that.is.null;
  65. expect( b ).to.have.property( 'parent' ).that.is.null;
  66. expect( a ).to.have.property( 'parent' ).that.is.null;
  67. expect( removed ).to.be.instanceof( NodeList );
  68. expect( removed.length ).to.equal( 3 );
  69. expect( removed.get( 0 ) ).to.equal( o );
  70. expect( removed.get( 1 ) ).to.equal( b );
  71. expect( removed.get( 2 ) ).to.equal( a );
  72. } );
  73. } );
  74. describe( 'getChildIndex', () => {
  75. it( 'should return child index', () => {
  76. let element = new Element( 'elem', [], [ 'bar' ] );
  77. let b = element.getChild( 0 );
  78. let a = element.getChild( 1 );
  79. let r = element.getChild( 2 );
  80. expect( element.getChildIndex( b ) ).to.equal( 0 );
  81. expect( element.getChildIndex( a ) ).to.equal( 1 );
  82. expect( element.getChildIndex( r ) ).to.equal( 2 );
  83. } );
  84. } );
  85. describe( 'getChildCount', () => {
  86. it( 'should return number of children', () => {
  87. let element = new Element( 'elem', [], [ 'bar' ] );
  88. expect( element.getChildCount() ).to.equal( 3 );
  89. } );
  90. } );
  91. } );