8
0

attributeelement.js 8.4 KB

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