attributeelement.js 8.1 KB

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