node.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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. describe( 'getIndex', () => {
  78. it( 'should return null if the parent is null', () => {
  79. expect( root.getIndex() ).to.be.null;
  80. } );
  81. it( 'should return index in the parent', () => {
  82. expect( one.getIndex() ).to.equal( 0 );
  83. expect( two.getIndex() ).to.equal( 1 );
  84. expect( three.getIndex() ).to.equal( 2 );
  85. expect( charB.getIndex() ).to.equal( 0 );
  86. expect( img.getIndex() ).to.equal( 2 );
  87. expect( charR.getIndex() ).to.equal( 3 );
  88. } );
  89. it( 'should throw an error if parent does not contains element', () => {
  90. let e = new Element( 'e' );
  91. let bar = new Element( 'bar', [], [] );
  92. e.parent = bar;
  93. expect(
  94. () => {
  95. e.getIndex();
  96. }
  97. ).to.throw( CKEditorError, /node-not-found-in-parent/ );
  98. } );
  99. } );
  100. describe( 'getPath', () => {
  101. it( 'should return proper path', () => {
  102. expect( root.getPath() ).to.deep.equal( [] );
  103. expect( one.getPath() ).to.deep.equal( [ 0 ] );
  104. expect( two.getPath() ).to.deep.equal( [ 1 ] );
  105. expect( three.getPath() ).to.deep.equal( [ 2 ] );
  106. expect( charB.getPath() ).to.deep.equal( [ 1, 0 ] );
  107. expect( img.getPath() ).to.deep.equal( [ 1, 2 ] );
  108. expect( charR.getPath() ).to.deep.equal( [ 1, 3 ] );
  109. } );
  110. } );
  111. describe( 'attributes interface', () => {
  112. let node = new Element( 'p', { foo: 'bar' } );
  113. describe( 'hasAttribute', () => {
  114. it( 'should return true if element contains attribute with given key', () => {
  115. expect( node.hasAttribute( 'foo' ) ).to.be.true;
  116. } );
  117. it( 'should return false if element does not contain attribute with given key', () => {
  118. expect( node.hasAttribute( 'bar' ) ).to.be.false;
  119. } );
  120. } );
  121. describe( 'getAttribute', () => {
  122. it( 'should return attribute value for given key if element contains given attribute', () => {
  123. expect( node.getAttribute( 'foo' ) ).to.equal( 'bar' );
  124. } );
  125. it( 'should return undefined if element does not contain given attribute', () => {
  126. expect( node.getAttribute( 'bar' ) ).to.be.undefined;
  127. } );
  128. } );
  129. describe( 'getAttributes', () => {
  130. it( 'should return an iterator that iterates over all attributes set on the element', () => {
  131. expect( Array.from( node.getAttributes() ) ).to.deep.equal( [ [ 'foo', 'bar' ] ] );
  132. } );
  133. } );
  134. } );
  135. describe( 'toJSON', () => {
  136. it( 'should serialize empty node', () => {
  137. let node = new Element( 'one' );
  138. expect( treeModelTestUtils.jsonParseStringify( node ) ).to.deep.equal( {
  139. _attrs: [],
  140. _children: {
  141. _indexMap: [],
  142. _nodes: []
  143. },
  144. name: 'one'
  145. } );
  146. } );
  147. it( 'should serialize node with attributes', () => {
  148. let node = new Element( 'one', { foo: true, bar: false } );
  149. expect( treeModelTestUtils.jsonParseStringify( node ) ).to.deep.equal( {
  150. _attrs: [ [ 'foo', true ], [ 'bar', false ] ],
  151. _children: {
  152. _indexMap: [],
  153. _nodes: []
  154. },
  155. name: 'one'
  156. } );
  157. } );
  158. it( 'should serialize node with children', () => {
  159. expect( treeModelTestUtils.jsonParseStringify( root ) ).to.deep.equal( {
  160. _attrs: [],
  161. _children: {
  162. _indexMap: [ 0, 1, 2 ],
  163. _nodes: [
  164. { _attrs: [], _children: { _indexMap: [], _nodes: [] }, name: 'one' },
  165. {
  166. _attrs: [],
  167. _children: {
  168. _indexMap: [ 0, 0, 1, 2 ],
  169. _nodes: [
  170. { _attrs: [], text: 'ba' },
  171. { _attrs: [], _children: { _indexMap: [], _nodes: [] }, name: 'img' },
  172. { _attrs: [], text: 'r' }
  173. ]
  174. },
  175. name: 'two'
  176. },
  177. { _attrs: [], _children: { _indexMap: [], _nodes: [] }, name: 'three' }
  178. ]
  179. },
  180. name: null
  181. } );
  182. } );
  183. } );
  184. } );