element.js 4.4 KB

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