fontsizeediting.js 9.0 KB

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