node.js 7.0 KB

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