8
0

node.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /**
  2. * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* bender-tags: model */
  6. 'use strict';
  7. import Element from '/ckeditor5/engine/model/element.js';
  8. import CKEditorError from '/ckeditor5/utils/ckeditorerror.js';
  9. describe( 'Node', () => {
  10. let root;
  11. let one, two, three;
  12. let charB, charA, charR, img;
  13. before( () => {
  14. img = new Element( 'img' );
  15. one = new Element( 'one' );
  16. two = new Element( 'two', null, [ 'b', 'a', img, 'r' ] );
  17. charB = two.getChild( 0 );
  18. charA = two.getChild( 1 );
  19. charR = two.getChild( 3 );
  20. three = new Element( 'three' );
  21. root = new Element( null, null, [ one, two, three ] );
  22. } );
  23. describe( 'should have a correct property', () => {
  24. it( 'depth', () => {
  25. expect( root ).to.have.property( 'depth' ).that.equals( 0 );
  26. expect( one ).to.have.property( 'depth' ).that.equals( 1 );
  27. expect( two ).to.have.property( 'depth' ).that.equals( 1 );
  28. expect( three ).to.have.property( 'depth' ).that.equals( 1 );
  29. expect( charB ).to.have.property( 'depth' ).that.equals( 2 );
  30. expect( charA ).to.have.property( 'depth' ).that.equals( 2 );
  31. expect( img ).to.have.property( 'depth' ).that.equals( 2 );
  32. expect( charR ).to.have.property( 'depth' ).that.equals( 2 );
  33. } );
  34. it( 'root', () => {
  35. expect( root ).to.have.property( 'root' ).that.equals( root );
  36. expect( one ).to.have.property( 'root' ).that.equals( root );
  37. expect( two ).to.have.property( 'root' ).that.equals( root );
  38. expect( three ).to.have.property( 'root' ).that.equals( root );
  39. expect( img ).to.have.property( 'root' ).that.equals( root );
  40. } );
  41. it( 'nextSibling', () => {
  42. expect( root ).to.have.property( 'nextSibling' ).that.is.null;
  43. expect( one ).to.have.property( 'nextSibling' ).that.equals( two );
  44. expect( two ).to.have.property( 'nextSibling' ).that.equals( three );
  45. expect( three ).to.have.property( 'nextSibling' ).that.is.null;
  46. expect( charB ).to.have.property( 'nextSibling' ).that.deep.equals( charA );
  47. expect( charA ).to.have.property( 'nextSibling' ).that.deep.equals( img );
  48. expect( img ).to.have.property( 'nextSibling' ).that.deep.equals( charR );
  49. expect( charR ).to.have.property( 'nextSibling' ).that.is.null;
  50. } );
  51. it( 'previousSibling', () => {
  52. expect( root ).to.have.property( 'previousSibling' ).that.is.expect;
  53. expect( one ).to.have.property( 'previousSibling' ).that.is.null;
  54. expect( two ).to.have.property( 'previousSibling' ).that.equals( one );
  55. expect( three ).to.have.property( 'previousSibling' ).that.equals( two );
  56. expect( charB ).to.have.property( 'previousSibling' ).that.is.null;
  57. expect( charA ).to.have.property( 'previousSibling' ).that.deep.equals( charB );
  58. expect( img ).to.have.property( 'previousSibling' ).that.deep.equals( charA );
  59. expect( charR ).to.have.property( 'previousSibling' ).that.deep.equals( img );
  60. } );
  61. } );
  62. describe( 'constructor', () => {
  63. it( 'should create empty attribute list if no parameters were passed', () => {
  64. let foo = new Element( 'foo' );
  65. expect( foo._attrs ).to.be.instanceof( Map );
  66. expect( foo._attrs.size ).to.equal( 0 );
  67. } );
  68. it( 'should initialize attribute list with passed attributes', () => {
  69. let attrs = { foo: true, bar: false };
  70. let foo = new Element( 'foo', attrs );
  71. expect( foo._attrs.size ).to.equal( 2 );
  72. expect( foo.getAttribute( 'foo' ) ).to.equal( true );
  73. expect( foo.getAttribute( 'bar' ) ).to.equal( false );
  74. } );
  75. } );
  76. describe( 'getIndex', () => {
  77. it( 'should return null if the parent is null', () => {
  78. expect( root.getIndex() ).to.be.null;
  79. } );
  80. it( 'should return index in the parent', () => {
  81. expect( one.getIndex() ).to.equal( 0 );
  82. expect( two.getIndex() ).to.equal( 1 );
  83. expect( three.getIndex() ).to.equal( 2 );
  84. expect( charB.getIndex() ).to.equal( 0 );
  85. expect( img.getIndex() ).to.equal( 2 );
  86. expect( charR.getIndex() ).to.equal( 3 );
  87. } );
  88. it( 'should throw an error if parent does not contains element', () => {
  89. let e = new Element( 'e' );
  90. let bar = new Element( 'bar', [], [] );
  91. e.parent = bar;
  92. expect(
  93. () => {
  94. e.getIndex();
  95. }
  96. ).to.throw( CKEditorError, /node-not-found-in-parent/ );
  97. } );
  98. } );
  99. describe( 'getPath', () => {
  100. it( 'should return proper path', () => {
  101. expect( root.getPath() ).to.deep.equal( [] );
  102. expect( one.getPath() ).to.deep.equal( [ 0 ] );
  103. expect( two.getPath() ).to.deep.equal( [ 1 ] );
  104. expect( three.getPath() ).to.deep.equal( [ 2 ] );
  105. expect( charB.getPath() ).to.deep.equal( [ 1, 0 ] );
  106. expect( img.getPath() ).to.deep.equal( [ 1, 2 ] );
  107. expect( charR.getPath() ).to.deep.equal( [ 1, 3 ] );
  108. } );
  109. } );
  110. describe( 'attributes interface', () => {
  111. let node = new Element( 'p', { foo: 'bar' } );
  112. describe( 'hasAttribute', () => {
  113. it( 'should return true if element contains attribute with given key', () => {
  114. expect( node.hasAttribute( 'foo' ) ).to.be.true;
  115. } );
  116. it( 'should return false if element does not contain attribute with given key', () => {
  117. expect( node.hasAttribute( 'bar' ) ).to.be.false;
  118. } );
  119. } );
  120. describe( 'getAttribute', () => {
  121. it( 'should return attribute value for given key if element contains given attribute', () => {
  122. expect( node.getAttribute( 'foo' ) ).to.equal( 'bar' );
  123. } );
  124. it( 'should return undefined if element does not contain given attribute', () => {
  125. expect( node.getAttribute( 'bar' ) ).to.be.undefined;
  126. } );
  127. } );
  128. describe( 'getAttributes', () => {
  129. it( 'should return an iterator that iterates over all attributes set on the element', () => {
  130. expect( Array.from( node.getAttributes() ) ).to.deep.equal( [ [ 'foo', 'bar' ] ] );
  131. } );
  132. } );
  133. } );
  134. } );