8
0

attributeelement.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. /**
  2. * @license Copyright (c) 2003-2019, 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. import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
  9. describe( 'AttributeElement', () => {
  10. describe( 'constructor()', () => {
  11. it( 'should create element with default priority', () => {
  12. const el = new AttributeElement( 'strong' );
  13. expect( el ).to.be.an.instanceof( AttributeElement );
  14. expect( el ).to.be.an.instanceof( Element );
  15. expect( el ).to.have.property( 'name' ).that.equals( 'strong' );
  16. expect( el ).to.have.property( 'priority' ).that.equals( AttributeElement.DEFAULT_PRIORITY );
  17. } );
  18. } );
  19. describe( 'is', () => {
  20. let el;
  21. before( () => {
  22. el = new AttributeElement( 'span' );
  23. } );
  24. it( 'should return true for attributeElement/element, also with correct name and element name', () => {
  25. expect( el.is( 'attributeElement' ) ).to.be.true;
  26. expect( el.is( 'attributeElement', 'span' ) ).to.be.true;
  27. expect( el.is( 'element' ) ).to.be.true;
  28. expect( el.is( 'element', 'span' ) ).to.be.true;
  29. expect( el.is( 'span' ) ).to.be.true;
  30. } );
  31. it( 'should return false for other accept values', () => {
  32. expect( el.is( 'attributeElement', 'p' ) ).to.be.false;
  33. expect( el.is( 'element', 'p' ) ).to.be.false;
  34. expect( el.is( 'p' ) ).to.be.false;
  35. expect( el.is( 'text' ) ).to.be.false;
  36. expect( el.is( 'textProxy' ) ).to.be.false;
  37. expect( el.is( 'containerElement' ) ).to.be.false;
  38. expect( el.is( 'uiElement' ) ).to.be.false;
  39. expect( el.is( 'emptyElement' ) ).to.be.false;
  40. expect( el.is( 'rootElement' ) ).to.be.false;
  41. expect( el.is( 'documentFragment' ) ).to.be.false;
  42. } );
  43. } );
  44. describe( '_clone()', () => {
  45. it( 'should clone element with priority', () => {
  46. const el = new AttributeElement( 'b' );
  47. el._priority = 7;
  48. const clone = el._clone();
  49. expect( clone ).to.not.equal( el );
  50. expect( clone.name ).to.equal( el.name );
  51. expect( clone.priority ).to.equal( el.priority );
  52. } );
  53. } );
  54. describe( 'isSimilar', () => {
  55. it( 'should return true if priorities are the same', () => {
  56. const b1 = new AttributeElement( 'b' );
  57. b1._priority = 7;
  58. const b2 = new AttributeElement( 'b' );
  59. b2._priority = 7;
  60. expect( b1.isSimilar( b2 ) ).to.be.true;
  61. } );
  62. it( 'should return false if priorities are different', () => {
  63. const b1 = new AttributeElement( 'b' );
  64. b1._priority = 7;
  65. const b2 = new AttributeElement( 'b' ); // default priority
  66. expect( b1.isSimilar( b2 ) ).to.be.false;
  67. } );
  68. it( 'should return true if ids are the same even if other properties are different', () => {
  69. const element1 = new AttributeElement( 'b' );
  70. element1._id = 'xyz';
  71. const element2 = new AttributeElement( 'b', { foo: 'bar' } );
  72. element2._id = 'xyz';
  73. const element3 = new AttributeElement( 'span' );
  74. element3._id = 'xyz';
  75. expect( element1.isSimilar( element2 ) ).to.be.true;
  76. expect( element1.isSimilar( element3 ) ).to.be.true;
  77. } );
  78. it( 'should return false if ids are different even if other properties are same', () => {
  79. const element1 = new AttributeElement( 'span', { foo: 'bar' } );
  80. element1._priority = 3;
  81. element1._id = 'foo';
  82. const element2 = new AttributeElement( 'span', { foo: 'bar' } );
  83. element2._priority = 3;
  84. element2._id = 'bar';
  85. expect( element1.isSimilar( element2 ) ).to.be.false;
  86. } );
  87. } );
  88. // More tests are available in DowncastWriter tests.
  89. describe( 'getElementsWithSameId', () => {
  90. it( 'should return a copy of _clonesGroup set', () => {
  91. const attributeA = new AttributeElement( 'b' );
  92. const attributeB = new AttributeElement( 'b' );
  93. attributeA._id = 'foo';
  94. attributeB._id = 'foo';
  95. attributeA._clonesGroup = attributeB._clonesGroup = new Set( [ attributeA, attributeB ] );
  96. expect( attributeA.getElementsWithSameId() ).to.deep.equal( attributeA._clonesGroup );
  97. expect( attributeA.getElementsWithSameId() ).not.to.equal( attributeA._clonesGroup );
  98. expect( attributeA.getElementsWithSameId() ).to.deep.equal( attributeB.getElementsWithSameId() );
  99. } );
  100. it( 'should throw if attribute element has no id', () => {
  101. const attribute = new AttributeElement( 'b' );
  102. expect( () => {
  103. attribute.getElementsWithSameId();
  104. } ).to.throw( CKEditorError, /attribute-element-get-elements-with-same-id-no-id/ );
  105. } );
  106. } );
  107. describe( 'getFillerOffset', () => {
  108. it( 'should return position 0 if it is the only element in the container', () => {
  109. const { selection } = parse( '<container:p><attribute:b>[]</attribute:b></container:p>' );
  110. const attribute = selection.getFirstPosition().parent;
  111. expect( attribute.getFillerOffset() ).to.equals( 0 );
  112. } );
  113. it( 'should return position 0 if it is the only nested element in the container', () => {
  114. const { selection } = parse(
  115. '<container:p><attribute:b><attribute:i>[]</attribute:i></attribute:b></container:p>' );
  116. const attribute = selection.getFirstPosition().parent;
  117. expect( attribute.getFillerOffset() ).to.equals( 0 );
  118. } );
  119. it( 'should return null if element contains another element', () => {
  120. const attribute = parse( '<attribute:b><attribute:i></attribute:i></attribute:b>' );
  121. expect( attribute.getFillerOffset() ).to.be.null;
  122. } );
  123. it( 'should return null if element contains text', () => {
  124. const attribute = parse( '<attribute:b>text</attribute:b>' );
  125. expect( attribute.getFillerOffset() ).to.be.null;
  126. } );
  127. it( 'should return null if container element contains text', () => {
  128. const { selection } = parse( '<container:p><attribute:b>[]</attribute:b>foo</container:p>' );
  129. const attribute = selection.getFirstPosition().parent;
  130. expect( attribute.getFillerOffset() ).to.be.null;
  131. } );
  132. it( 'should return null if it is the parent contains text', () => {
  133. const { selection } = parse(
  134. '<container:p><attribute:b><attribute:i>[]</attribute:i>foo</attribute:b></container:p>' );
  135. const attribute = selection.getFirstPosition().parent;
  136. expect( attribute.getFillerOffset() ).to.be.null;
  137. } );
  138. it( 'should return null if there is no parent container element', () => {
  139. const { selection } = parse( '<attribute:b><attribute:i>[]</attribute:i>foo</attribute:b>' );
  140. const attribute = selection.getFirstPosition().parent;
  141. expect( attribute.getFillerOffset() ).to.be.null;
  142. } );
  143. it( 'should return null if there is no parent', () => {
  144. const attribute = new AttributeElement( 'b' );
  145. expect( attribute.getFillerOffset() ).to.be.null;
  146. } );
  147. it( 'should return offset after all children if it is the only nested element in the container and has UIElement inside', () => {
  148. const { selection } = parse(
  149. '<container:p><attribute:b><attribute:i>[]<ui:span></ui:span></attribute:i></attribute:b></container:p>' );
  150. const attribute = selection.getFirstPosition().parent;
  151. expect( attribute.getFillerOffset() ).to.equal( 1 );
  152. } );
  153. it( 'should return offset after all children if there is no parent container element and has UIElement inside', () => {
  154. const { selection } = parse( '<attribute:b>[]<ui:span></ui:span><ui:span></ui:span></attribute:b>' );
  155. const attribute = selection.getFirstPosition().parent;
  156. expect( attribute.getFillerOffset() ).to.equal( 2 );
  157. } );
  158. } );
  159. } );