8
0

attributeelement.js 7.8 KB

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