node.js 5.2 KB

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