attributeelement.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /**
  2. * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import AttributeElement from '../../src/view/attributeelement';
  6. import Element from '../../src/view/element';
  7. import { parse } from '../../src/dev-utils/view';
  8. describe( 'AttributeElement', () => {
  9. describe( 'constructor()', () => {
  10. it( 'should create element with default priority', () => {
  11. const el = new AttributeElement( 'strong' );
  12. expect( el ).to.be.an.instanceof( AttributeElement );
  13. expect( el ).to.be.an.instanceof( Element );
  14. expect( el ).to.have.property( 'name' ).that.equals( 'strong' );
  15. expect( el ).to.have.property( 'priority' ).that.equals( AttributeElement.DEFAULT_PRIORITY );
  16. } );
  17. } );
  18. describe( 'is', () => {
  19. let el;
  20. before( () => {
  21. el = new AttributeElement( 'span' );
  22. } );
  23. it( 'should return true for attributeElement/element, also with correct name and element name', () => {
  24. expect( el.is( 'attributeElement' ) ).to.be.true;
  25. expect( el.is( 'attributeElement', 'span' ) ).to.be.true;
  26. expect( el.is( 'element' ) ).to.be.true;
  27. expect( el.is( 'element', 'span' ) ).to.be.true;
  28. expect( el.is( 'span' ) ).to.be.true;
  29. } );
  30. it( 'should return false for other accept values', () => {
  31. expect( el.is( 'attributeElement', 'p' ) ).to.be.false;
  32. expect( el.is( 'element', 'p' ) ).to.be.false;
  33. expect( el.is( 'p' ) ).to.be.false;
  34. expect( el.is( 'text' ) ).to.be.false;
  35. expect( el.is( 'textProxy' ) ).to.be.false;
  36. expect( el.is( 'containerElement' ) ).to.be.false;
  37. expect( el.is( 'uiElement' ) ).to.be.false;
  38. expect( el.is( 'emptyElement' ) ).to.be.false;
  39. expect( el.is( 'rootElement' ) ).to.be.false;
  40. expect( el.is( 'documentFragment' ) ).to.be.false;
  41. } );
  42. } );
  43. describe( '_clone()', () => {
  44. it( 'should clone element with priority', () => {
  45. const el = new AttributeElement( 'b' );
  46. el._priority = 7;
  47. const clone = el._clone();
  48. expect( clone ).to.not.equal( el );
  49. expect( clone.name ).to.equal( el.name );
  50. expect( clone.priority ).to.equal( el.priority );
  51. } );
  52. } );
  53. describe( 'isSimilar', () => {
  54. it( 'should return true if priorities are the same', () => {
  55. const b1 = new AttributeElement( 'b' );
  56. b1._priority = 7;
  57. const b2 = new AttributeElement( 'b' );
  58. b2._priority = 7;
  59. expect( b1.isSimilar( b2 ) ).to.be.true;
  60. } );
  61. it( 'should return false if priorities are different', () => {
  62. const b1 = new AttributeElement( 'b' );
  63. b1._priority = 7;
  64. const b2 = new AttributeElement( 'b' ); // default priority
  65. expect( b1.isSimilar( b2 ) ).to.be.false;
  66. } );
  67. it( 'should return true if ids are the same even if other properties are different', () => {
  68. const element1 = new AttributeElement( 'b' );
  69. element1._id = 'xyz';
  70. const element2 = new AttributeElement( 'b', { foo: 'bar' } );
  71. element2._id = 'xyz';
  72. const element3 = new AttributeElement( 'span' );
  73. element3._id = 'xyz';
  74. expect( element1.isSimilar( element2 ) ).to.be.true;
  75. expect( element1.isSimilar( element3 ) ).to.be.true;
  76. } );
  77. it( 'should return false if ids are different even if other properties are same', () => {
  78. const element1 = new AttributeElement( 'span', { foo: 'bar' } );
  79. element1._priority = 3;
  80. element1._id = 'foo';
  81. const element2 = new AttributeElement( 'span', { foo: 'bar' } );
  82. element2._priority = 3;
  83. element2._id = 'bar';
  84. expect( element1.isSimilar( element2 ) ).to.be.false;
  85. } );
  86. } );
  87. describe( 'getFillerOffset', () => {
  88. it( 'should return position 0 if it is the only element in the container', () => {
  89. const { selection } = parse( '<container:p><attribute:b>[]</attribute:b></container:p>' );
  90. const attribute = selection.getFirstPosition().parent;
  91. expect( attribute.getFillerOffset() ).to.equals( 0 );
  92. } );
  93. it( 'should return position 0 if it is the only nested element in the container', () => {
  94. const { selection } = parse(
  95. '<container:p><attribute:b><attribute:i>[]</attribute:i></attribute:b></container:p>' );
  96. const attribute = selection.getFirstPosition().parent;
  97. expect( attribute.getFillerOffset() ).to.equals( 0 );
  98. } );
  99. it( 'should return null if element contains another element', () => {
  100. const attribute = parse( '<attribute:b><attribute:i></attribute:i></attribute:b>' );
  101. expect( attribute.getFillerOffset() ).to.be.null;
  102. } );
  103. it( 'should return null if element contains text', () => {
  104. const attribute = parse( '<attribute:b>text</attribute:b>' );
  105. expect( attribute.getFillerOffset() ).to.be.null;
  106. } );
  107. it( 'should return null if container element contains text', () => {
  108. const { selection } = parse( '<container:p><attribute:b>[]</attribute:b>foo</container:p>' );
  109. const attribute = selection.getFirstPosition().parent;
  110. expect( attribute.getFillerOffset() ).to.be.null;
  111. } );
  112. it( 'should return null if it is the parent contains text', () => {
  113. const { selection } = parse(
  114. '<container:p><attribute:b><attribute:i>[]</attribute:i>foo</attribute:b></container:p>' );
  115. const attribute = selection.getFirstPosition().parent;
  116. expect( attribute.getFillerOffset() ).to.be.null;
  117. } );
  118. it( 'should return null if there is no parent container element', () => {
  119. const { selection } = parse( '<attribute:b><attribute:i>[]</attribute:i>foo</attribute:b>' );
  120. const attribute = selection.getFirstPosition().parent;
  121. expect( attribute.getFillerOffset() ).to.be.null;
  122. } );
  123. it( 'should return null if there is no parent', () => {
  124. const attribute = new AttributeElement( 'b' );
  125. expect( attribute.getFillerOffset() ).to.be.null;
  126. } );
  127. it( 'should return offset after all children if it is the only nested element in the container and has UIElement inside', () => {
  128. const { selection } = parse(
  129. '<container:p><attribute:b><attribute:i>[]<ui:span></ui:span></attribute:i></attribute:b></container:p>' );
  130. const attribute = selection.getFirstPosition().parent;
  131. expect( attribute.getFillerOffset() ).to.equal( 1 );
  132. } );
  133. it( 'should return offset after all children if there is no parent container element and has UIElement inside', () => {
  134. const { selection } = parse( '<attribute:b>[]<ui:span></ui:span><ui:span></ui:span></attribute:b>' );
  135. const attribute = selection.getFirstPosition().parent;
  136. expect( attribute.getFillerOffset() ).to.equal( 2 );
  137. } );
  138. } );
  139. } );