attributeelement.js 8.2 KB

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