fontsizeediting.js 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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 FontSizeEditing from './../../src/fontsize/fontsizeediting';
  6. import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
  7. import VirtualTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/virtualtesteditor';
  8. import { getData as getModelData, setData as setModelData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  9. import { assertCKEditorError } from '@ckeditor/ckeditor5-utils/tests/_utils/utils';
  10. describe( 'FontSizeEditing', () => {
  11. let editor, doc;
  12. beforeEach( () => {
  13. return VirtualTestEditor
  14. .create( {
  15. plugins: [ FontSizeEditing, Paragraph ]
  16. } )
  17. .then( newEditor => {
  18. editor = newEditor;
  19. doc = editor.document;
  20. } );
  21. } );
  22. afterEach( () => {
  23. editor.destroy();
  24. } );
  25. it( 'should have pluginName', () => {
  26. expect( FontSizeEditing.pluginName ).to.equal( 'FontSizeEditing' );
  27. } );
  28. it( 'should set proper schema rules', () => {
  29. expect( editor.model.schema.checkAttribute( [ '$block', '$text' ], 'fontSize' ) ).to.be.true;
  30. expect( editor.model.schema.checkAttribute( [ '$clipboardHolder', '$text' ], 'fontSize' ) ).to.be.true;
  31. expect( editor.model.schema.checkAttribute( [ '$block' ], 'fontSize' ) ).to.be.false;
  32. } );
  33. it( 'should be marked with a formatting property', () => {
  34. expect( editor.model.schema.getAttributeProperties( 'fontSize' ) ).to.include( {
  35. isFormatting: true
  36. } );
  37. } );
  38. it( 'its attribute is marked with a copOnEnter property', () => {
  39. expect( editor.model.schema.getAttributeProperties( 'fontSize' ) ).to.include( {
  40. copyOnEnter: true
  41. } );
  42. } );
  43. describe( 'config', () => {
  44. describe( 'default value', () => {
  45. it( 'should be set', () => {
  46. expect( editor.config.get( 'fontSize.options' ) ).to.deep.equal( [ 'tiny', 'small', 'default', 'big', 'huge' ] );
  47. expect( editor.config.get( 'fontSize.supportAllValues' ) ).to.equal( false );
  48. } );
  49. } );
  50. describe( 'supportAllValues=true', () => {
  51. let editor, doc;
  52. beforeEach( () => {
  53. return VirtualTestEditor
  54. .create( {
  55. plugins: [ FontSizeEditing, Paragraph ],
  56. fontSize: {
  57. supportAllValues: true,
  58. options: [
  59. '6.25%',
  60. '8em',
  61. '10px',
  62. 12,
  63. {
  64. model: 14,
  65. title: '14px'
  66. }
  67. ]
  68. }
  69. } )
  70. .then( newEditor => {
  71. editor = newEditor;
  72. doc = editor.model;
  73. } );
  74. } );
  75. afterEach( () => {
  76. return editor.destroy();
  77. } );
  78. describe( 'editing pipeline conversion', () => {
  79. it( 'should pass fontSize to data', () => {
  80. setModelData( doc, '<paragraph>f<$text fontSize="10px">o</$text>o</paragraph>' );
  81. expect( editor.getData() ).to.equal( '<p>f<span style="font-size:10px;">o</span>o</p>' );
  82. } );
  83. } );
  84. describe( 'data pipeline conversions', () => {
  85. it( 'should convert from an element with defined style when with other styles', () => {
  86. const data = '<p>f<span style="font-family: Other;font-size: 18px">o</span>o</p>';
  87. editor.setData( data );
  88. expect( getModelData( doc ) ).to.equal( '<paragraph>[]f<$text fontSize="18px">o</$text>o</paragraph>' );
  89. expect( editor.getData() ).to.equal( '<p>f<span style="font-size:18px;">o</span>o</p>' );
  90. } );
  91. } );
  92. it( 'should throw an error if used with default configuration of the plugin', () => {
  93. return VirtualTestEditor
  94. .create( {
  95. plugins: [ FontSizeEditing ],
  96. fontSize: {
  97. supportAllValues: true
  98. }
  99. } )
  100. .then(
  101. () => {
  102. throw new Error( 'Supposed to be rejected' );
  103. },
  104. error => {
  105. assertCKEditorError( error, /font-size-invalid-use-of-named-presets/, null, {
  106. presets: [ 'tiny', 'small', 'big', 'huge' ]
  107. } );
  108. }
  109. );
  110. } );
  111. } );
  112. } );
  113. describe( 'editing pipeline conversion', () => {
  114. beforeEach( () => {
  115. return VirtualTestEditor
  116. .create( {
  117. plugins: [ FontSizeEditing, Paragraph ],
  118. fontSize: {
  119. options: [
  120. 'tiny',
  121. 'default',
  122. 18,
  123. {
  124. title: 'My setting',
  125. model: 'my',
  126. view: {
  127. name: 'mark',
  128. styles: { 'font-size': '30px' },
  129. classes: 'my-style'
  130. }
  131. }
  132. ]
  133. }
  134. } )
  135. .then( newEditor => {
  136. editor = newEditor;
  137. doc = editor.model;
  138. } );
  139. } );
  140. it( 'should discard unknown fontSize attribute values', () => {
  141. setModelData( doc, '<paragraph>f<$text fontSize="foo-bar">o</$text>o</paragraph>' );
  142. expect( editor.getData() ).to.equal( '<p>foo</p>' );
  143. } );
  144. it( 'should convert fontSize attribute to predefined named preset', () => {
  145. setModelData( doc, '<paragraph>f<$text fontSize="tiny">o</$text>o</paragraph>' );
  146. expect( editor.getData() ).to.equal( '<p>f<span class="text-tiny">o</span>o</p>' );
  147. } );
  148. it( 'should convert fontSize attribute to predefined pixel size preset', () => {
  149. setModelData( doc, '<paragraph>f<$text fontSize="18px">o</$text>o</paragraph>' );
  150. expect( editor.getData() ).to.equal( '<p>f<span style="font-size:18px;">o</span>o</p>' );
  151. } );
  152. it( 'should convert fontSize attribute from user defined settings', () => {
  153. setModelData( doc, '<paragraph>f<$text fontSize="my">o</$text>o</paragraph>' );
  154. expect( editor.getData() ).to.equal( '<p>f<mark class="my-style" style="font-size:30px;">o</mark>o</p>' );
  155. } );
  156. } );
  157. describe( 'data pipeline conversions', () => {
  158. beforeEach( () => {
  159. return VirtualTestEditor
  160. .create( {
  161. plugins: [ FontSizeEditing, Paragraph ],
  162. fontSize: {
  163. options: [
  164. 'tiny',
  165. 'default',
  166. 18,
  167. {
  168. title: 'My setting',
  169. model: 'my',
  170. view: {
  171. name: 'mark',
  172. styles: { 'font-size': '30px' },
  173. classes: 'my-style'
  174. }
  175. },
  176. {
  177. title: 'Big multiple classes',
  178. model: 'big-multiple',
  179. view: {
  180. name: 'span',
  181. classes: [ 'foo', 'foo-big' ]
  182. }
  183. },
  184. {
  185. title: 'Hybrid',
  186. model: 'complex',
  187. view: {
  188. name: 'span',
  189. classes: [ 'text-complex' ]
  190. },
  191. upcastAlso: [
  192. { name: 'span', styles: { 'font-size': '77em' } },
  193. { name: 'span', attributes: { 'data-size': '77em' } }
  194. ]
  195. }
  196. ]
  197. }
  198. } )
  199. .then( newEditor => {
  200. editor = newEditor;
  201. doc = editor.model;
  202. } );
  203. } );
  204. it( 'should convert from element with defined class', () => {
  205. const data = '<p>f<span class="text-tiny foo bar">o</span>o</p>';
  206. editor.setData( data );
  207. expect( getModelData( doc ) ).to.equal( '<paragraph>[]f<$text fontSize="tiny">o</$text>o</paragraph>' );
  208. expect( editor.getData() ).to.equal( '<p>f<span class="text-tiny">o</span>o</p>' );
  209. } );
  210. it( 'should convert from element with defined multiple classes', () => {
  211. const data = '<p>f<span class="foo foo-big bar">o</span>o</p>';
  212. editor.setData( data );
  213. expect( getModelData( doc ) ).to.equal( '<paragraph>[]f<$text fontSize="big-multiple">o</$text>o</paragraph>' );
  214. expect( editor.getData() ).to.equal( '<p>f<span class="foo foo-big">o</span>o</p>' );
  215. } );
  216. it( 'should convert from element with defined style', () => {
  217. const data = '<p>f<span style="font-size:18px;">o</span>o</p>';
  218. editor.setData( data );
  219. expect( getModelData( doc ) ).to.equal( '<paragraph>[]f<$text fontSize="18px">o</$text>o</paragraph>' );
  220. expect( editor.getData() ).to.equal( data );
  221. } );
  222. it( 'should convert from element with defined style when with other styles', () => {
  223. const data = '<p>f<span style="font-family: serif;font-size: 18px">o</span>o</p>';
  224. editor.setData( data );
  225. expect( getModelData( doc ) ).to.equal( '<paragraph>[]f<$text fontSize="18px">o</$text>o</paragraph>' );
  226. expect( editor.getData() ).to.equal( '<p>f<span style="font-size:18px;">o</span>o</p>' );
  227. } );
  228. it( 'should convert from user defined element', () => {
  229. const data = '<p>f<mark class="my-style" style="font-size:30px;">o</mark>o</p>';
  230. editor.setData( data );
  231. expect( getModelData( doc ) ).to.equal( '<paragraph>[]f<$text fontSize="my">o</$text>o</paragraph>' );
  232. expect( editor.getData() ).to.equal( data );
  233. } );
  234. it( 'should convert from complex definitions', () => {
  235. editor.setData(
  236. '<p>f<span style="font-size: 77em;">o</span>o</p>' +
  237. '<p>b<span data-size="77em">a</span>r</p>' +
  238. '<p>b<span class="text-complex">a</span>z</p>'
  239. );
  240. expect( getModelData( doc ) ).to.equal(
  241. '<paragraph>[]f<$text fontSize="complex">o</$text>o</paragraph>' +
  242. '<paragraph>b<$text fontSize="complex">a</$text>r</paragraph>' +
  243. '<paragraph>b<$text fontSize="complex">a</$text>z</paragraph>'
  244. );
  245. expect( editor.getData() ).to.equal(
  246. '<p>f<span class="text-complex">o</span>o</p>' +
  247. '<p>b<span class="text-complex">a</span>r</p>' +
  248. '<p>b<span class="text-complex">a</span>z</p>'
  249. );
  250. } );
  251. } );
  252. } );