node.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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 Element from '/ckeditor5/core/treemodel/element.js';
  8. import CKEditorError from '/ckeditor5/core/ckeditorerror.js';
  9. describe( 'Node', () => {
  10. let root;
  11. let one, two, three;
  12. let charB, charA, charR, img;
  13. before( () => {
  14. img = new Element( 'img' );
  15. one = new Element( 'one' );
  16. two = new Element( 'two', null, [ 'b', 'a', img, 'r' ] );
  17. charB = two.getChild( 0 );
  18. charA = two.getChild( 1 );
  19. charR = two.getChild( 3 );
  20. three = new Element( 'three' );
  21. root = new Element( null, null, [ one, two, three ] );
  22. } );
  23. describe( 'should have a correct property', () => {
  24. it( 'depth', () => {
  25. expect( root ).to.have.property( 'depth' ).that.equals( 0 );
  26. expect( one ).to.have.property( 'depth' ).that.equals( 1 );
  27. expect( two ).to.have.property( 'depth' ).that.equals( 1 );
  28. expect( three ).to.have.property( 'depth' ).that.equals( 1 );
  29. expect( charB ).to.have.property( 'depth' ).that.equals( 2 );
  30. expect( charA ).to.have.property( 'depth' ).that.equals( 2 );
  31. expect( img ).to.have.property( 'depth' ).that.equals( 2 );
  32. expect( charR ).to.have.property( 'depth' ).that.equals( 2 );
  33. } );
  34. it( 'root', () => {
  35. expect( root ).to.have.property( 'root' ).that.equals( root );
  36. expect( one ).to.have.property( 'root' ).that.equals( root );
  37. expect( two ).to.have.property( 'root' ).that.equals( root );
  38. expect( three ).to.have.property( 'root' ).that.equals( root );
  39. expect( img ).to.have.property( 'root' ).that.equals( root );
  40. } );
  41. it( 'nextSibling', () => {
  42. expect( root ).to.have.property( 'nextSibling' ).that.is.null;
  43. expect( one ).to.have.property( 'nextSibling' ).that.equals( two );
  44. expect( two ).to.have.property( 'nextSibling' ).that.equals( three );
  45. expect( three ).to.have.property( 'nextSibling' ).that.is.null;
  46. expect( charB ).to.have.property( 'nextSibling' ).that.deep.equals( charA );
  47. expect( charA ).to.have.property( 'nextSibling' ).that.deep.equals( img );
  48. expect( img ).to.have.property( 'nextSibling' ).that.deep.equals( charR );
  49. expect( charR ).to.have.property( 'nextSibling' ).that.is.null;
  50. } );
  51. it( 'previousSibling', () => {
  52. expect( root ).to.have.property( 'previousSibling' ).that.is.expect;
  53. expect( one ).to.have.property( 'previousSibling' ).that.is.null;
  54. expect( two ).to.have.property( 'previousSibling' ).that.equals( one );
  55. expect( three ).to.have.property( 'previousSibling' ).that.equals( two );
  56. expect( charB ).to.have.property( 'previousSibling' ).that.is.null;
  57. expect( charA ).to.have.property( 'previousSibling' ).that.deep.equals( charB );
  58. expect( img ).to.have.property( 'previousSibling' ).that.deep.equals( charA );
  59. expect( charR ).to.have.property( 'previousSibling' ).that.deep.equals( img );
  60. } );
  61. } );
  62. describe( 'constructor', () => {
  63. it( 'should create empty attribute list if no parameters were passed', () => {
  64. let foo = new Element( 'foo' );
  65. expect( foo._attrs ).to.be.instanceof( Map );
  66. expect( foo._attrs.size ).to.equal( 0 );
  67. } );
  68. it( 'should initialize attribute list with passed attributes', () => {
  69. let attrs = { foo: true, bar: false };
  70. let foo = new Element( 'foo', attrs );
  71. expect( foo._attrs.size ).to.equal( 2 );
  72. expect( foo.getAttribute( 'foo' ) ).to.equal( true );
  73. expect( foo.getAttribute( 'bar' ) ).to.equal( false );
  74. } );
  75. } );
  76. it( 'should create proper JSON string using toJSON method', () => {
  77. let foo = new Element( 'foo', [], [ 'b' ] );
  78. let parsedFoo = JSON.parse( JSON.stringify( foo ) );
  79. expect( parsedFoo.parent ).to.equal( null );
  80. expect( parsedFoo._children._nodes[ 0 ].parent ).to.equal( 'foo' );
  81. } );
  82. describe( 'getIndex', () => {
  83. it( 'should return null if the parent is null', () => {
  84. expect( root.getIndex() ).to.be.null;
  85. } );
  86. it( 'should return index in the parent', () => {
  87. expect( one.getIndex() ).to.equal( 0 );
  88. expect( two.getIndex() ).to.equal( 1 );
  89. expect( three.getIndex() ).to.equal( 2 );
  90. expect( charB.getIndex() ).to.equal( 0 );
  91. expect( img.getIndex() ).to.equal( 2 );
  92. expect( charR.getIndex() ).to.equal( 3 );
  93. } );
  94. it( 'should throw an error if parent does not contains element', () => {
  95. let e = new Element( 'e' );
  96. let bar = new Element( 'bar', [], [] );
  97. e.parent = bar;
  98. expect(
  99. () => {
  100. e.getIndex();
  101. }
  102. ).to.throw( CKEditorError, /node-not-found-in-parent/ );
  103. } );
  104. } );
  105. describe( 'getPath', () => {
  106. it( 'should return proper path', () => {
  107. expect( root.getPath() ).to.deep.equal( [] );
  108. expect( one.getPath() ).to.deep.equal( [ 0 ] );
  109. expect( two.getPath() ).to.deep.equal( [ 1 ] );
  110. expect( three.getPath() ).to.deep.equal( [ 2 ] );
  111. expect( charB.getPath() ).to.deep.equal( [ 1, 0 ] );
  112. expect( img.getPath() ).to.deep.equal( [ 1, 2 ] );
  113. expect( charR.getPath() ).to.deep.equal( [ 1, 3 ] );
  114. } );
  115. } );
  116. describe( 'attributes interface', () => {
  117. let node = new Element( 'p', { foo: 'bar' } );
  118. describe( 'hasAttribute', () => {
  119. it( 'should return true if element contains attribute with given key', () => {
  120. expect( node.hasAttribute( 'foo' ) ).to.be.true;
  121. } );
  122. it( 'should return false if element does not contain attribute with given key', () => {
  123. expect( node.hasAttribute( 'bar' ) ).to.be.false;
  124. } );
  125. } );
  126. describe( 'getAttribute', () => {
  127. it( 'should return attribute value for given key if element contains given attribute', () => {
  128. expect( node.getAttribute( 'foo' ) ).to.equal( 'bar' );
  129. } );
  130. it( 'should return undefined if element does not contain given attribute', () => {
  131. expect( node.getAttribute( 'bar' ) ).to.be.undefined;
  132. } );
  133. } );
  134. describe( 'getAttributes', () => {
  135. it( 'should return an iterator that iterates over all attributes set on the element', () => {
  136. let it = node.getAttributes();
  137. let attrs = [];
  138. let step = it.next();
  139. while ( !step.done ) {
  140. attrs.push( step.value );
  141. step = it.next();
  142. }
  143. expect( attrs ).to.deep.equal( [ [ 'foo', 'bar' ] ] );
  144. } );
  145. } );
  146. } );
  147. } );