fontsizeediting.js 8.9 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( doc.schema.check( { name: '$inline', attributes: 'fontSize', inside: '$block' } ) ).to.be.true;
  26. expect( doc.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 falsy 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( [] );
  48. } );
  49. } );
  50. it( 'should pass through object definition', () => {
  51. return VirtualTestEditor
  52. .create( {
  53. plugins: [ FontSizeEditing ],
  54. fontSize: {
  55. items: [ { label: '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. label: '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. { label: 'Tiny', model: 'text-tiny', view: { name: 'span', class: 'text-tiny' } },
  84. { label: 'Small', model: 'text-small', view: { name: 'span', class: 'text-small' } },
  85. { label: 'Big', model: 'text-big', view: { name: 'span', class: 'text-big' } },
  86. { label: 'Huge', model: 'text-huge', view: { name: 'span', class: 'text-huge' } }
  87. ] );
  88. } );
  89. } );
  90. } );
  91. describe( 'numeric presets', () => {
  92. it( 'should return generated presets', () => {
  93. return VirtualTestEditor
  94. .create( {
  95. plugins: [ FontSizeEditing ],
  96. fontSize: {
  97. items: [ '10', 12, 'normal', '14.1', 18.3 ]
  98. }
  99. } )
  100. .then( newEditor => {
  101. editor = newEditor;
  102. const plugin = editor.plugins.get( FontSizeEditing );
  103. expect( plugin.configuredItems ).to.deep.equal( [
  104. { label: '10', model: '10', view: { name: 'span', style: { 'font-size': '10px' } } },
  105. { label: '12', model: '12', view: { name: 'span', style: { 'font-size': '12px' } } },
  106. { label: '14', model: '14', view: { name: 'span', style: { 'font-size': '14px' } } },
  107. { label: '18', model: '18', view: { name: 'span', style: { 'font-size': '18px' } } }
  108. ] );
  109. } );
  110. } );
  111. } );
  112. } );
  113. describe( 'editing pipeline conversion', () => {
  114. beforeEach( () => {
  115. return VirtualTestEditor
  116. .create( {
  117. plugins: [ FontSizeEditing, Paragraph ],
  118. fontSize: {
  119. items: [
  120. 'tiny',
  121. 'normal',
  122. 18,
  123. {
  124. label: 'My setting',
  125. model: 'my',
  126. view: {
  127. name: 'mark',
  128. style: 'font-size: 30px',
  129. class: 'my-style'
  130. }
  131. }
  132. ]
  133. }
  134. } )
  135. .then( newEditor => {
  136. editor = newEditor;
  137. doc = editor.document;
  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="text-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="18">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. items: [
  164. 'tiny',
  165. 'normal',
  166. 18,
  167. {
  168. label: 'My setting',
  169. model: 'my',
  170. view: {
  171. name: 'mark',
  172. style: { 'font-size': '30px' },
  173. class: 'my-style'
  174. }
  175. },
  176. {
  177. label: 'Big multiple classes',
  178. model: 'big-multiple',
  179. view: {
  180. name: 'span',
  181. class: [ 'foo', 'foo-big' ]
  182. }
  183. },
  184. {
  185. label: 'Hybrid',
  186. model: 'complex',
  187. view: {
  188. to: {
  189. name: 'span',
  190. class: [ 'text-complex' ]
  191. },
  192. from: [
  193. { name: 'span', style: { 'font-size': '77em' } },
  194. { name: 'span', attribute: { 'data-size': '77em' } }
  195. ]
  196. }
  197. }
  198. ]
  199. }
  200. } )
  201. .then( newEditor => {
  202. editor = newEditor;
  203. doc = editor.document;
  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. } );