8
0

attributeelement.js 8.4 KB

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