basic-styles.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. /**
  2. * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* globals document */
  6. import ClassicTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/classictesteditor';
  7. import Clipboard from '@ckeditor/ckeditor5-clipboard/src/clipboard';
  8. import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
  9. import Heading from '@ckeditor/ckeditor5-heading/src/heading';
  10. import Bold from '@ckeditor/ckeditor5-basic-styles/src/bold';
  11. import Italic from '@ckeditor/ckeditor5-basic-styles/src/italic';
  12. import Underline from '@ckeditor/ckeditor5-basic-styles/src/underline';
  13. import Strikethrough from '@ckeditor/ckeditor5-basic-styles/src/strikethrough';
  14. import { setData, getData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  15. import boldWithinText from '../data/integration/basic-styles/bold-within-text/input.word2016.html';
  16. import italicStartingText from '../data/integration/basic-styles/italic-starting-text/input.word2016.html';
  17. import underlinedText from '../data/integration/basic-styles/underlined-text/input.word2016.html';
  18. import strikethroughEndingText from '../data/integration/basic-styles/strikethrough-ending-text/input.word2016.html';
  19. import multipleStylesSingleLine from '../data/integration/basic-styles/multiple-styles-single-line/input.word2016.html';
  20. import multipleStylesMultiline from '../data/integration/basic-styles/multiple-styles-multiline/input.word2016.html';
  21. describe( 'Basic Styles – integration', () => {
  22. let element, editor;
  23. before( () => {
  24. element = document.createElement( 'div' );
  25. document.body.appendChild( element );
  26. return ClassicTestEditor
  27. .create( element, { plugins: [ Clipboard, Paragraph, Heading, Bold, Italic, Underline, Strikethrough ] } )
  28. .then( editorInstance => {
  29. editor = editorInstance;
  30. } );
  31. } );
  32. beforeEach( () => {
  33. setData( editor.model, '<paragraph>[]</paragraph>' );
  34. } );
  35. after( () => {
  36. editor.destroy();
  37. element.remove();
  38. } );
  39. // Pastes: 'Some text <b>with bold</b>.'
  40. // Input same for: Chrome, Firefox and Edge.
  41. describe( 'bold within text', () => {
  42. it( 'pastes in the empty editor', () => {
  43. expectPaste( editor, boldWithinText, '<paragraph>Some text <$text bold="true">with bold</$text>.[]</paragraph>' );
  44. } );
  45. it( 'pastes in the paragraph', () => {
  46. setData( editor.model, '<paragraph>More [] text</paragraph>' );
  47. expectPaste( editor, boldWithinText, '<paragraph>More Some text <$text bold="true">with bold</$text>.[] text</paragraph>' );
  48. } );
  49. it( 'pastes in the different block context', () => {
  50. setData( editor.model, '<heading1>More [] text</heading1>' );
  51. expectPaste( editor, boldWithinText, '<heading1>More Some text <$text bold="true">with bold</$text>.[] text</heading1>' );
  52. } );
  53. it( 'pastes in the inline styling context', () => {
  54. setData( editor.model, '<paragraph><$text italic="true">Ita[]lic</$text></paragraph>' );
  55. expectPaste( editor, boldWithinText, '<paragraph><$text italic="true">Ita</$text>Some text ' +
  56. '<$text bold="true">with bold</$text>.[]<$text italic="true">lic</$text></paragraph>' );
  57. } );
  58. it( 'pastes inside another bold element', () => {
  59. setData( editor.model, '<paragraph><$text bold="true">Ita[]lic</$text></paragraph>' );
  60. expectPaste( editor, boldWithinText, '<paragraph><$text bold="true">Ita</$text>Some text ' +
  61. '<$text bold="true">with bold</$text>.[]<$text bold="true">lic</$text></paragraph>' );
  62. } );
  63. } );
  64. // Pastes: '<i>Italic</i> text.'
  65. // Input same for: Chrome, Firefox and Edge.
  66. describe( 'italic starting text', () => {
  67. it( 'pastes in the empty editor', () => {
  68. expectPaste( editor, italicStartingText, '<paragraph><$text italic="true">Italic</$text> text.[]</paragraph>' );
  69. } );
  70. it( 'pastes in the paragraph', () => {
  71. setData( editor.model, '<paragraph>More [] text</paragraph>' );
  72. expectPaste( editor, italicStartingText, '<paragraph>More <$text italic="true">Italic</$text> text.[] text</paragraph>' );
  73. } );
  74. it( 'pastes in the different block context', () => {
  75. setData( editor.model, '<heading1>More [] text</heading1>' );
  76. expectPaste( editor, italicStartingText, '<heading1>More <$text italic="true">Italic</$text> text.[] text</heading1>' );
  77. } );
  78. it( 'pastes in the inline styling context', () => {
  79. setData( editor.model, '<paragraph><$text bold="true">Bol[]d</$text></paragraph>' );
  80. expectPaste( editor, italicStartingText, '<paragraph><$text bold="true">Bol</$text><$text italic="true">' +
  81. 'Italic</$text> text.[]<$text bold="true">d</$text></paragraph>' );
  82. } );
  83. it( 'pastes inside another italic element', () => {
  84. setData( editor.model, '<paragraph><$text italic="true">Italic[]</$text></paragraph>' );
  85. expectPaste( editor, italicStartingText, '<paragraph><$text italic="true">ItalicItalic</$text> text.[]</paragraph>' );
  86. } );
  87. } );
  88. // Pastes: '<u>Whole text underlined</u>'
  89. // Input same for: Chrome, Firefox and Edge.
  90. describe( 'underlined text', () => {
  91. it( 'pastes in the empty editor', () => {
  92. expectPaste( editor, underlinedText, '<paragraph><$text underline="true">Whole text underlined[]</$text></paragraph>' );
  93. } );
  94. it( 'pastes in the paragraph', () => {
  95. setData( editor.model, '<paragraph>More [] text</paragraph>' );
  96. expectPaste( editor, underlinedText, '<paragraph>More <$text underline="true">Whole text underlined[]' +
  97. '</$text> text</paragraph>' );
  98. } );
  99. it( 'pastes in the different block context', () => {
  100. setData( editor.model, '<heading1>More [] text</heading1>' );
  101. expectPaste( editor, underlinedText, '<heading1>More <$text underline="true">Whole text underlined[]</$text> text</heading1>' );
  102. } );
  103. it( 'pastes in the inline styling context', () => {
  104. setData( editor.model, '<paragraph><$text bold="true">Bol[]d</$text></paragraph>' );
  105. expectPaste( editor, underlinedText, '<paragraph><$text bold="true">Bol</$text><$text underline="true">' +
  106. 'Whole text underlined[]</$text><$text bold="true">d</$text></paragraph>' );
  107. } );
  108. it( 'pastes inside another underlined element', () => {
  109. setData( editor.model, '<paragraph><$text underline="true">Under []line</$text></paragraph>' );
  110. expectPaste( editor, underlinedText, '<paragraph><$text underline="true">Under Whole text underlined[]' +
  111. 'line</$text></paragraph>' );
  112. } );
  113. } );
  114. // Pastes: 'Text <s>incorrect</s>'
  115. // Input same for: Chrome, Firefox and Edge.
  116. describe( 'strikethrough ending text', () => {
  117. it( 'pastes in the empty editor', () => {
  118. expectPaste( editor, strikethroughEndingText, '<paragraph>Text <$text strikethrough="true">incorrect[]</$text></paragraph>' );
  119. } );
  120. it( 'pastes in the paragraph', () => {
  121. setData( editor.model, '<paragraph>More [] text</paragraph>' );
  122. expectPaste( editor, strikethroughEndingText, '<paragraph>More Text <$text strikethrough="true">incorrect[]' +
  123. '</$text> text</paragraph>' );
  124. } );
  125. it( 'pastes in the different block context', () => {
  126. setData( editor.model, '<heading1>More [] text</heading1>' );
  127. expectPaste( editor, strikethroughEndingText, '<heading1>More Text <$text strikethrough="true">incorrect[]' +
  128. '</$text> text</heading1>' );
  129. } );
  130. it( 'pastes in the inline styling context', () => {
  131. setData( editor.model, '<paragraph><$text bold="true">Bol[]d</$text></paragraph>' );
  132. expectPaste( editor, strikethroughEndingText, '<paragraph><$text bold="true">Bol</$text>' +
  133. 'Text <$text strikethrough="true">incorrect[]</$text><$text bold="true">d</$text></paragraph>' );
  134. } );
  135. it( 'pastes inside another strikethrough element', () => {
  136. setData( editor.model, '<paragraph><$text strikethrough="true">[]Foo</$text></paragraph>' );
  137. expectPaste( editor, strikethroughEndingText, '<paragraph>Text <$text strikethrough="true">incorrect[]Foo' +
  138. '</$text></paragraph>' );
  139. } );
  140. } );
  141. // Pastes: 'Text <b><u>containi<s>ng</s></u></b><s><u> multi</u>ple</s><i>styling</i>.'
  142. // Input same for: Chrome, Firefox and Edge.
  143. describe( 'mulitple styles single line', () => {
  144. it( 'pastes in the empty editor', () => {
  145. expectPaste( editor, multipleStylesSingleLine, '<paragraph>Text ' +
  146. '<$text bold="true" underline="true">containi</$text>' +
  147. '<$text bold="true" strikethrough="true" underline="true">ng</$text>' +
  148. '<$text strikethrough="true" underline="true"> multi</$text>' +
  149. '<$text strikethrough="true">ple </$text>' +
  150. '<$text italic="true">styling</$text>.[]</paragraph>' );
  151. } );
  152. it( 'pastes in the paragraph', () => {
  153. setData( editor.model, '<paragraph>More [] text</paragraph>' );
  154. expectPaste( editor, multipleStylesSingleLine, '<paragraph>More Text ' +
  155. '<$text bold="true" underline="true">containi</$text>' +
  156. '<$text bold="true" strikethrough="true" underline="true">ng</$text>' +
  157. '<$text strikethrough="true" underline="true"> multi</$text>' +
  158. '<$text strikethrough="true">ple </$text>' +
  159. '<$text italic="true">styling</$text>.[] text</paragraph>' );
  160. } );
  161. it( 'pastes in the different block context', () => {
  162. setData( editor.model, '<heading1>More [] text</heading1>' );
  163. expectPaste( editor, multipleStylesSingleLine, '<heading1>More Text ' +
  164. '<$text bold="true" underline="true">containi</$text>' +
  165. '<$text bold="true" strikethrough="true" underline="true">ng</$text>' +
  166. '<$text strikethrough="true" underline="true"> multi</$text>' +
  167. '<$text strikethrough="true">ple </$text>' +
  168. '<$text italic="true">styling</$text>.[] text</heading1>' );
  169. } );
  170. it( 'pastes in the inline styling context', () => {
  171. setData( editor.model, '<paragraph><$text bold="true">Bol[]d</$text></paragraph>' );
  172. expectPaste( editor, multipleStylesSingleLine, '<paragraph><$text bold="true">Bol</$text>Text ' +
  173. '<$text bold="true" underline="true">containi</$text>' +
  174. '<$text bold="true" strikethrough="true" underline="true">ng</$text>' +
  175. '<$text strikethrough="true" underline="true"> multi</$text>' +
  176. '<$text strikethrough="true">ple </$text>' +
  177. '<$text italic="true">styling</$text>.[]' +
  178. '<$text bold="true">d</$text></paragraph>' );
  179. } );
  180. } );
  181. // Pastes:
  182. // <p>Line <b>bold</b> and <i>italics</i></p>
  183. // <p>Line <b><u>foo</u></b><u> bar</u></p>
  184. // <p><s>Third</s> line <b>styling, <i>space on e</i>nd </b></p>
  185. // Input same for: Chrome, Firefox and Edge.
  186. describe( 'mulitple styles multiline', () => {
  187. it( 'pastes in the empty editor', () => {
  188. expectPaste( editor, multipleStylesMultiline, '<paragraph>Line ' +
  189. '<$text bold="true">bold</$text> and <$text italic="true">italics</$text></paragraph>' +
  190. '<paragraph>Line <$text bold="true" underline="true">foo</$text><$text underline="true"> bar</$text></paragraph>' +
  191. '<paragraph><$text strikethrough="true">Third</$text> line <$text bold="true">styling, </$text>' +
  192. '<$text bold="true" italic="true">space on e</$text>' +
  193. '<$text bold="true">nd []</$text></paragraph>' ); // The last space '...nd </b></p>' goes missing.
  194. } );
  195. it( 'pastes in the paragraph', () => {
  196. setData( editor.model, '<paragraph>More [] text</paragraph>' );
  197. expectPaste( editor, multipleStylesMultiline, '<paragraph>More Line ' +
  198. '<$text bold="true">bold</$text> and <$text italic="true">italics</$text></paragraph>' +
  199. '<paragraph>Line <$text bold="true" underline="true">foo</$text><$text underline="true"> bar</$text></paragraph>' +
  200. '<paragraph><$text strikethrough="true">Third</$text> line <$text bold="true">styling, </$text>' +
  201. '<$text bold="true" italic="true">space on e</$text>' +
  202. '<$text bold="true">nd []</$text> text</paragraph>' ); // The last space '...nd </b></p>' goes missing.
  203. } );
  204. it( 'pastes in the different block context', () => {
  205. setData( editor.model, '<heading1>More [] text</heading1>' );
  206. expectPaste( editor, multipleStylesMultiline, '<heading1>More Line ' +
  207. '<$text bold="true">bold</$text> and <$text italic="true">italics</$text></heading1>' +
  208. '<paragraph>Line <$text bold="true" underline="true">foo</$text><$text underline="true"> bar</$text></paragraph>' +
  209. '<paragraph><$text strikethrough="true">Third</$text> line <$text bold="true">styling, </$text>' +
  210. '<$text bold="true" italic="true">space on e</$text>' +
  211. '<$text bold="true">nd []</$text> text</paragraph>' ); // The last space '...nd </b></p>' goes missing.
  212. } );
  213. it( 'pastes in the inline styling context', () => {
  214. setData( editor.model, '<paragraph><$text bold="true">Bol[]d</$text></paragraph>' );
  215. expectPaste( editor, multipleStylesMultiline, '<paragraph><$text bold="true">Bol</$text>Line ' +
  216. '<$text bold="true">bold</$text> and <$text italic="true">italics</$text></paragraph>' +
  217. '<paragraph>Line <$text bold="true" underline="true">foo</$text><$text underline="true"> bar</$text></paragraph>' +
  218. '<paragraph><$text strikethrough="true">Third</$text> line <$text bold="true">styling, </$text>' +
  219. '<$text bold="true" italic="true">space on e</$text>' +
  220. '<$text bold="true">nd []d</$text></paragraph>' ); // The last space '...nd </b></p>' goes missing.
  221. } );
  222. } );
  223. } );
  224. function expectPaste( editor, input, output ) {
  225. pasteHtml( editor, input );
  226. expect( getData( editor.model ) ).to.equal( output );
  227. }
  228. function pasteHtml( editor, html ) {
  229. editor.editing.view.document.fire( 'paste', {
  230. dataTransfer: createDataTransfer( { 'text/html': html } ),
  231. preventDefault() {}
  232. } );
  233. }
  234. function createDataTransfer( data ) {
  235. return {
  236. getData( type ) {
  237. return data[ type ];
  238. }
  239. };
  240. }