node.js 7.4 KB

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