fontfamilyediting.js 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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 FontFamilyEditing from './../../src/fontfamily/fontfamilyediting';
  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. describe( 'FontFamilyEditing', () => {
  10. let editor, doc;
  11. beforeEach( () => {
  12. return VirtualTestEditor
  13. .create( {
  14. plugins: [ FontFamilyEditing, Paragraph ]
  15. } )
  16. .then( newEditor => {
  17. editor = newEditor;
  18. doc = editor.document;
  19. } );
  20. } );
  21. afterEach( () => {
  22. editor.destroy();
  23. } );
  24. it( 'should have pluginName', () => {
  25. expect( FontFamilyEditing.pluginName ).to.equal( 'FontFamilyEditing' );
  26. } );
  27. it( 'should set proper schema rules', () => {
  28. expect( editor.model.schema.checkAttribute( [ '$block', '$text' ], 'fontFamily' ) ).to.be.true;
  29. expect( editor.model.schema.checkAttribute( [ '$clipboardHolder', '$text' ], 'fontFamily' ) ).to.be.true;
  30. expect( editor.model.schema.checkAttribute( [ '$block' ], 'fontFamily' ) ).to.be.false;
  31. } );
  32. it( 'should be marked with a formatting property', () => {
  33. expect( editor.model.schema.getAttributeProperties( 'fontFamily' ) ).to.include( {
  34. isFormatting: true
  35. } );
  36. } );
  37. it( 'its attribute is marked with a copOnEnter property', () => {
  38. expect( editor.model.schema.getAttributeProperties( 'fontFamily' ) ).to.include( {
  39. copyOnEnter: true
  40. } );
  41. } );
  42. describe( 'config', () => {
  43. describe( 'default value', () => {
  44. it( 'should be set', () => {
  45. expect( editor.config.get( 'fontFamily.options' ) ).to.deep.equal( [
  46. 'default',
  47. 'Arial, Helvetica, sans-serif',
  48. 'Courier New, Courier, monospace',
  49. 'Georgia, serif',
  50. 'Lucida Sans Unicode, Lucida Grande, sans-serif',
  51. 'Tahoma, Geneva, sans-serif',
  52. 'Times New Roman, Times, serif',
  53. 'Trebuchet MS, Helvetica, sans-serif',
  54. 'Verdana, Geneva, sans-serif'
  55. ] );
  56. expect( editor.config.get( 'fontFamily.supportAllValues' ) ).to.equal( false );
  57. } );
  58. } );
  59. describe( 'supportAllValues=true', () => {
  60. let editor, doc;
  61. beforeEach( () => {
  62. return VirtualTestEditor
  63. .create( {
  64. plugins: [ FontFamilyEditing, Paragraph ],
  65. fontFamily: {
  66. options: [
  67. 'Arial'
  68. ],
  69. supportAllValues: true
  70. }
  71. } )
  72. .then( newEditor => {
  73. editor = newEditor;
  74. doc = editor.model;
  75. } );
  76. } );
  77. afterEach( () => {
  78. return editor.destroy();
  79. } );
  80. describe( 'editing pipeline conversion', () => {
  81. it( 'should convert unknown fontFamily attribute values', () => {
  82. setModelData( doc, '<paragraph>f<$text fontFamily="foo-bar">o</$text>o</paragraph>' );
  83. expect( editor.getData() ).to.equal( '<p>f<span style="font-family:foo-bar;">o</span>o</p>' );
  84. } );
  85. it( 'should convert defined fontFamily attribute values', () => {
  86. setModelData( doc, '<paragraph>f<$text fontFamily="Arial">o</$text>o</paragraph>' );
  87. expect( editor.getData() ).to.equal( '<p>f<span style="font-family:Arial;">o</span>o</p>' );
  88. } );
  89. } );
  90. describe( 'data pipeline conversions', () => {
  91. it( 'should convert from an element with defined style when with other styles', () => {
  92. const data = '<p>f<span style="font-family: Other;font-size: 18px">o</span>o</p>';
  93. editor.setData( data );
  94. expect( getModelData( doc ) ).to.equal( '<paragraph>[]f<$text fontFamily="Other">o</$text>o</paragraph>' );
  95. expect( editor.getData() ).to.equal( '<p>f<span style="font-family:Other;">o</span>o</p>' );
  96. } );
  97. it( 'should convert from a complex definition', () => {
  98. const data = '<p>f<span style="font-family:Arial,sans-serif;">o</span>o</p>';
  99. editor.setData( data );
  100. expect( getModelData( doc ) ).to.equal( '<paragraph>[]f<$text fontFamily="Arial,sans-serif">o</$text>o</paragraph>' );
  101. expect( editor.getData() ).to.equal( data );
  102. } );
  103. } );
  104. } );
  105. } );
  106. describe( 'editing pipeline conversion', () => {
  107. beforeEach( () => {
  108. return VirtualTestEditor
  109. .create( {
  110. plugins: [ FontFamilyEditing, Paragraph ],
  111. fontFamily: {
  112. options: [
  113. 'Arial',
  114. 'Lucida Sans Unicode, Lucida Grande, sans-serif',
  115. {
  116. title: 'My font',
  117. model: 'my',
  118. view: {
  119. name: 'mark',
  120. classes: 'my-style'
  121. }
  122. }
  123. ]
  124. }
  125. } )
  126. .then( newEditor => {
  127. editor = newEditor;
  128. doc = editor.model;
  129. } );
  130. } );
  131. it( 'should discard unknown fontFamily attribute values', () => {
  132. setModelData( doc, '<paragraph>f<$text fontFamily="foo-bar">o</$text>o</paragraph>' );
  133. expect( editor.getData() ).to.equal( '<p>foo</p>' );
  134. } );
  135. it( 'should convert fontFamily attribute to configured simple preset', () => {
  136. setModelData( doc, '<paragraph>f<$text fontFamily="Arial">o</$text>o</paragraph>' );
  137. expect( editor.getData() ).to.equal( '<p>f<span style="font-family:Arial;">o</span>o</p>' );
  138. } );
  139. it( 'should convert fontFamily attribute to configured complex preset', () => {
  140. setModelData( doc, '<paragraph>f<$text fontFamily="Lucida Sans Unicode">o</$text>o</paragraph>' );
  141. expect( editor.getData() )
  142. .to.equal( '<p>f<span style="font-family:\'Lucida Sans Unicode\', \'Lucida Grande\', sans-serif;">o</span>o</p>' );
  143. } );
  144. it( 'should convert fontFamily attribute from user defined settings', () => {
  145. setModelData( doc, '<paragraph>f<$text fontFamily="my">o</$text>o</paragraph>' );
  146. expect( editor.getData() ).to.equal( '<p>f<mark class="my-style">o</mark>o</p>' );
  147. } );
  148. } );
  149. describe( 'data pipeline conversions', () => {
  150. beforeEach( () => {
  151. return VirtualTestEditor
  152. .create( {
  153. plugins: [ FontFamilyEditing, Paragraph ],
  154. fontFamily: {
  155. options: [
  156. 'Lucida Sans Unicode, Lucida Grande, sans-serif',
  157. {
  158. title: 'My other setting',
  159. model: 'my-other',
  160. view: {
  161. name: 'span',
  162. styles: { 'font-family': 'Other' }
  163. }
  164. },
  165. {
  166. title: 'My setting',
  167. model: 'my',
  168. view: {
  169. name: 'mark',
  170. styles: { 'font-family': 'Verdana' },
  171. classes: 'my-style'
  172. }
  173. },
  174. {
  175. title: 'Hybrid',
  176. model: 'complex',
  177. view: {
  178. name: 'span',
  179. classes: [ 'text-complex' ]
  180. },
  181. upcastAlso: [
  182. { name: 'span', styles: { 'font-family': 'Arial' } },
  183. { name: 'span', styles: { 'font-family': 'Arial,sans-serif' } },
  184. { name: 'span', attributes: { 'data-font': 'Arial' } }
  185. ]
  186. }
  187. ]
  188. }
  189. } )
  190. .then( newEditor => {
  191. editor = newEditor;
  192. doc = editor.model;
  193. } );
  194. } );
  195. it( 'should convert from element with defined style when with other styles', () => {
  196. const data = '<p>f<span style="font-family: Other;font-size: 18px">o</span>o</p>';
  197. editor.setData( data );
  198. expect( getModelData( doc ) ).to.equal( '<paragraph>[]f<$text fontFamily="my-other">o</$text>o</paragraph>' );
  199. expect( editor.getData() ).to.equal( '<p>f<span style="font-family:Other;">o</span>o</p>' );
  200. } );
  201. it( 'should convert from user defined element', () => {
  202. const data = '<p>f<mark class="my-style" style="font-family:Verdana;">o</mark>o</p>';
  203. editor.setData( data );
  204. expect( getModelData( doc ) ).to.equal( '<paragraph>[]f<$text fontFamily="my">o</$text>o</paragraph>' );
  205. expect( editor.getData() ).to.equal( data );
  206. } );
  207. it( 'should convert from complex definitions', () => {
  208. editor.setData(
  209. '<p>f<span style="font-family:Arial;">o</span>o</p>' +
  210. '<p>f<span style="font-family: Arial,sans-serif">o</span>o</p>' +
  211. '<p>b<span data-font="Arial">a</span>r</p>' +
  212. '<p>b<span class="text-complex">a</span>z</p>'
  213. );
  214. expect( getModelData( doc ) ).to.equal(
  215. '<paragraph>[]f<$text fontFamily="complex">o</$text>o</paragraph>' +
  216. '<paragraph>f<$text fontFamily="complex">o</$text>o</paragraph>' +
  217. '<paragraph>b<$text fontFamily="complex">a</$text>r</paragraph>' +
  218. '<paragraph>b<$text fontFamily="complex">a</$text>z</paragraph>'
  219. );
  220. expect( editor.getData() ).to.equal(
  221. '<p>f<span class="text-complex">o</span>o</p>' +
  222. '<p>f<span class="text-complex">o</span>o</p>' +
  223. '<p>b<span class="text-complex">a</span>r</p>' +
  224. '<p>b<span class="text-complex">a</span>z</p>'
  225. );
  226. } );
  227. } );
  228. } );