8
0

node.js 5.5 KB

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