node.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /**
  2. * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* bender-tags: model */
  6. import Element from '/ckeditor5/engine/model/element.js';
  7. import CKEditorError from '/ckeditor5/utils/ckeditorerror.js';
  8. describe( 'Node', () => {
  9. let root;
  10. let one, two, three;
  11. let charB, charA, charR, img;
  12. before( () => {
  13. img = new Element( 'img' );
  14. one = new Element( 'one' );
  15. two = new Element( 'two', null, [ 'b', 'a', img, 'r' ] );
  16. charB = two.getChild( 0 );
  17. charA = two.getChild( 1 );
  18. charR = two.getChild( 3 );
  19. three = new Element( 'three' );
  20. root = new Element( null, null, [ one, two, three ] );
  21. } );
  22. describe( 'should have a correct property', () => {
  23. it( 'depth', () => {
  24. expect( root ).to.have.property( 'depth' ).that.equals( 0 );
  25. expect( one ).to.have.property( 'depth' ).that.equals( 1 );
  26. expect( two ).to.have.property( 'depth' ).that.equals( 1 );
  27. expect( three ).to.have.property( 'depth' ).that.equals( 1 );
  28. expect( charB ).to.have.property( 'depth' ).that.equals( 2 );
  29. expect( charA ).to.have.property( 'depth' ).that.equals( 2 );
  30. expect( img ).to.have.property( 'depth' ).that.equals( 2 );
  31. expect( charR ).to.have.property( 'depth' ).that.equals( 2 );
  32. } );
  33. it( 'root', () => {
  34. expect( root ).to.have.property( 'root' ).that.equals( root );
  35. expect( one ).to.have.property( 'root' ).that.equals( root );
  36. expect( two ).to.have.property( 'root' ).that.equals( root );
  37. expect( three ).to.have.property( 'root' ).that.equals( root );
  38. expect( img ).to.have.property( 'root' ).that.equals( root );
  39. } );
  40. it( 'nextSibling', () => {
  41. expect( root ).to.have.property( 'nextSibling' ).that.is.null;
  42. expect( one ).to.have.property( 'nextSibling' ).that.equals( two );
  43. expect( two ).to.have.property( 'nextSibling' ).that.equals( three );
  44. expect( three ).to.have.property( 'nextSibling' ).that.is.null;
  45. expect( charB ).to.have.property( 'nextSibling' ).that.deep.equals( charA );
  46. expect( charA ).to.have.property( 'nextSibling' ).that.deep.equals( img );
  47. expect( img ).to.have.property( 'nextSibling' ).that.deep.equals( charR );
  48. expect( charR ).to.have.property( 'nextSibling' ).that.is.null;
  49. } );
  50. it( 'previousSibling', () => {
  51. expect( root ).to.have.property( 'previousSibling' ).that.is.expect;
  52. expect( one ).to.have.property( 'previousSibling' ).that.is.null;
  53. expect( two ).to.have.property( 'previousSibling' ).that.equals( one );
  54. expect( three ).to.have.property( 'previousSibling' ).that.equals( two );
  55. expect( charB ).to.have.property( 'previousSibling' ).that.is.null;
  56. expect( charA ).to.have.property( 'previousSibling' ).that.deep.equals( charB );
  57. expect( img ).to.have.property( 'previousSibling' ).that.deep.equals( charA );
  58. expect( charR ).to.have.property( 'previousSibling' ).that.deep.equals( img );
  59. } );
  60. } );
  61. describe( 'constructor', () => {
  62. it( 'should create empty attribute list if no parameters were passed', () => {
  63. let foo = new Element( 'foo' );
  64. expect( foo._attrs ).to.be.instanceof( Map );
  65. expect( foo._attrs.size ).to.equal( 0 );
  66. } );
  67. it( 'should initialize attribute list with passed attributes', () => {
  68. let attrs = { foo: true, bar: false };
  69. let foo = new Element( 'foo', attrs );
  70. expect( foo._attrs.size ).to.equal( 2 );
  71. expect( foo.getAttribute( 'foo' ) ).to.equal( true );
  72. expect( foo.getAttribute( 'bar' ) ).to.equal( false );
  73. } );
  74. } );
  75. describe( 'getIndex', () => {
  76. it( 'should return null if the parent is null', () => {
  77. expect( root.getIndex() ).to.be.null;
  78. } );
  79. it( 'should return index in the parent', () => {
  80. expect( one.getIndex() ).to.equal( 0 );
  81. expect( two.getIndex() ).to.equal( 1 );
  82. expect( three.getIndex() ).to.equal( 2 );
  83. expect( charB.getIndex() ).to.equal( 0 );
  84. expect( img.getIndex() ).to.equal( 2 );
  85. expect( charR.getIndex() ).to.equal( 3 );
  86. } );
  87. it( 'should throw an error if parent does not contains element', () => {
  88. let e = new Element( 'e' );
  89. let bar = new Element( 'bar', [], [] );
  90. e.parent = bar;
  91. expect(
  92. () => {
  93. e.getIndex();
  94. }
  95. ).to.throw( CKEditorError, /node-not-found-in-parent/ );
  96. } );
  97. } );
  98. describe( 'getPath', () => {
  99. it( 'should return proper path', () => {
  100. expect( root.getPath() ).to.deep.equal( [] );
  101. expect( one.getPath() ).to.deep.equal( [ 0 ] );
  102. expect( two.getPath() ).to.deep.equal( [ 1 ] );
  103. expect( three.getPath() ).to.deep.equal( [ 2 ] );
  104. expect( charB.getPath() ).to.deep.equal( [ 1, 0 ] );
  105. expect( img.getPath() ).to.deep.equal( [ 1, 2 ] );
  106. expect( charR.getPath() ).to.deep.equal( [ 1, 3 ] );
  107. } );
  108. } );
  109. describe( 'attributes interface', () => {
  110. let node = new Element( 'p', { foo: 'bar' } );
  111. describe( 'hasAttribute', () => {
  112. it( 'should return true if element contains attribute with given key', () => {
  113. expect( node.hasAttribute( 'foo' ) ).to.be.true;
  114. } );
  115. it( 'should return false if element does not contain attribute with given key', () => {
  116. expect( node.hasAttribute( 'bar' ) ).to.be.false;
  117. } );
  118. } );
  119. describe( 'getAttribute', () => {
  120. it( 'should return attribute value for given key if element contains given attribute', () => {
  121. expect( node.getAttribute( 'foo' ) ).to.equal( 'bar' );
  122. } );
  123. it( 'should return undefined if element does not contain given attribute', () => {
  124. expect( node.getAttribute( 'bar' ) ).to.be.undefined;
  125. } );
  126. } );
  127. describe( 'getAttributes', () => {
  128. it( 'should return an iterator that iterates over all attributes set on the element', () => {
  129. expect( Array.from( node.getAttributes() ) ).to.deep.equal( [ [ 'foo', 'bar' ] ] );
  130. } );
  131. } );
  132. } );
  133. } );