node.js 5.6 KB

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