basic-styles.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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.chrome.word2016.html';
  16. import italicStartingText from '../data/integration/basic-styles/italic-starting-text/input.chrome.word2016.html';
  17. import underlinedText from '../data/integration/basic-styles/underlined-text/input.chrome.word2016.html';
  18. import strikethroughEndingText from '../data/integration/basic-styles/strikethrough-ending-text/input.chrome.word2016.html';
  19. import multipleStylesSingleLine from '../data/integration/basic-styles/multiple-styles-single-line/input.chrome.word2016.html';
  20. import multipleStylesMultiline from '../data/integration/basic-styles/multiple-styles-multiline/input.chrome.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. describe( 'bold within text', () => {
  41. it( 'pastes in the empty editor', () => {
  42. expectPaste( editor, boldWithinText, '<paragraph>Some text <$text bold="true">with bold</$text>.[]</paragraph>' );
  43. } );
  44. it( 'pastes in the paragraph', () => {
  45. setData( editor.model, '<paragraph>More [] text</paragraph>' );
  46. expectPaste( editor, boldWithinText, '<paragraph>More Some text <$text bold="true">with bold</$text>.[] text</paragraph>' );
  47. } );
  48. it( 'pastes in the different block context', () => {
  49. setData( editor.model, '<heading1>More [] text</heading1>' );
  50. expectPaste( editor, boldWithinText, '<heading1>More Some text <$text bold="true">with bold</$text>.[] text</heading1>' );
  51. } );
  52. it( 'pastes in the inline styling context', () => {
  53. setData( editor.model, '<paragraph><$text italic="true">Ita[]lic</$text></paragraph>' );
  54. expectPaste( editor, boldWithinText, '<paragraph><$text italic="true">Ita</$text>Some text ' +
  55. '<$text bold="true">with bold</$text>.[]<$text italic="true">lic</$text></paragraph>' );
  56. } );
  57. it( 'pastes inside another bold element', () => {
  58. setData( editor.model, '<paragraph><$text bold="true">Ita[]lic</$text></paragraph>' );
  59. expectPaste( editor, boldWithinText, '<paragraph><$text bold="true">Ita</$text>Some text ' +
  60. '<$text bold="true">with bold</$text>.[]<$text bold="true">lic</$text></paragraph>' );
  61. } );
  62. } );
  63. // Pastes: '<i>Italic</i> text.'
  64. describe( 'italic starting text', () => {
  65. it( 'pastes in the empty editor', () => {
  66. expectPaste( editor, italicStartingText, '<paragraph><$text italic="true">Italic</$text> text.[]</paragraph>' );
  67. } );
  68. it( 'pastes in the paragraph', () => {
  69. setData( editor.model, '<paragraph>More [] text</paragraph>' );
  70. expectPaste( editor, italicStartingText, '<paragraph>More <$text italic="true">Italic</$text> text.[] text</paragraph>' );
  71. } );
  72. it( 'pastes in the different block context', () => {
  73. setData( editor.model, '<heading1>More [] text</heading1>' );
  74. expectPaste( editor, italicStartingText, '<heading1>More <$text italic="true">Italic</$text> text.[] text</heading1>' );
  75. } );
  76. it( 'pastes in the inline styling context', () => {
  77. setData( editor.model, '<paragraph><$text bold="true">Bol[]d</$text></paragraph>' );
  78. expectPaste( editor, italicStartingText, '<paragraph><$text bold="true">Bol</$text><$text italic="true">' +
  79. 'Italic</$text> text.[]<$text bold="true">d</$text></paragraph>' );
  80. } );
  81. it( 'pastes inside another italic element', () => {
  82. setData( editor.model, '<paragraph><$text italic="true">Italic[]</$text></paragraph>' );
  83. expectPaste( editor, italicStartingText, '<paragraph><$text italic="true">ItalicItalic</$text> text.[]</paragraph>' );
  84. } );
  85. } );
  86. // Pastes: '<u>Whole text underlined</u>'
  87. describe( 'underlined text', () => {
  88. it( 'pastes in the empty editor', () => {
  89. expectPaste( editor, underlinedText, '<paragraph><$text underline="true">Whole text underlined[]</$text></paragraph>' );
  90. } );
  91. it( 'pastes in the paragraph', () => {
  92. setData( editor.model, '<paragraph>More [] text</paragraph>' );
  93. expectPaste( editor, underlinedText, '<paragraph>More <$text underline="true">Whole text underlined[]' +
  94. '</$text> text</paragraph>' );
  95. } );
  96. it( 'pastes in the different block context', () => {
  97. setData( editor.model, '<heading1>More [] text</heading1>' );
  98. expectPaste( editor, underlinedText, '<heading1>More <$text underline="true">Whole text underlined[]</$text> text</heading1>' );
  99. } );
  100. it( 'pastes in the inline styling context', () => {
  101. setData( editor.model, '<paragraph><$text bold="true">Bol[]d</$text></paragraph>' );
  102. expectPaste( editor, underlinedText, '<paragraph><$text bold="true">Bol</$text><$text underline="true">' +
  103. 'Whole text underlined[]</$text><$text bold="true">d</$text></paragraph>' );
  104. } );
  105. it( 'pastes inside another underlined element', () => {
  106. setData( editor.model, '<paragraph><$text underline="true">Under []line</$text></paragraph>' );
  107. expectPaste( editor, underlinedText, '<paragraph><$text underline="true">Under Whole text underlined[]' +
  108. 'line</$text></paragraph>' );
  109. } );
  110. } );
  111. // Pastes: 'Text <s>incorrect</s>'
  112. describe( 'strikethrough ending text', () => {
  113. it( 'pastes in the empty editor', () => {
  114. expectPaste( editor, strikethroughEndingText, '<paragraph>Text <$text strikethrough="true">incorrect[]</$text></paragraph>' );
  115. } );
  116. it( 'pastes in the paragraph', () => {
  117. setData( editor.model, '<paragraph>More [] text</paragraph>' );
  118. expectPaste( editor, strikethroughEndingText, '<paragraph>More Text <$text strikethrough="true">incorrect[]' +
  119. '</$text> text</paragraph>' );
  120. } );
  121. it( 'pastes in the different block context', () => {
  122. setData( editor.model, '<heading1>More [] text</heading1>' );
  123. expectPaste( editor, strikethroughEndingText, '<heading1>More Text <$text strikethrough="true">incorrect[]' +
  124. '</$text> text</heading1>' );
  125. } );
  126. it( 'pastes in the inline styling context', () => {
  127. setData( editor.model, '<paragraph><$text bold="true">Bol[]d</$text></paragraph>' );
  128. expectPaste( editor, strikethroughEndingText, '<paragraph><$text bold="true">Bol</$text>' +
  129. 'Text <$text strikethrough="true">incorrect[]</$text><$text bold="true">d</$text></paragraph>' );
  130. } );
  131. it( 'pastes inside another strikethrough element', () => {
  132. setData( editor.model, '<paragraph><$text strikethrough="true">[]Foo</$text></paragraph>' );
  133. expectPaste( editor, strikethroughEndingText, '<paragraph>Text <$text strikethrough="true">incorrect[]Foo' +
  134. '</$text></paragraph>' );
  135. } );
  136. } );
  137. // Pastes: 'Text <b><u>containi<s>ng</s></u></b><s><u> multi</u>ple</s><i>styling</i>.'
  138. describe( 'mulitple styles single line', () => {
  139. it( 'pastes in the empty editor', () => {
  140. expectPaste( editor, multipleStylesSingleLine, '<paragraph>Text ' +
  141. '<$text bold="true" underline="true">containi</$text>' +
  142. '<$text bold="true" strikethrough="true" underline="true">ng</$text>' +
  143. '<$text strikethrough="true" underline="true"> multi</$text>' +
  144. '<$text strikethrough="true">ple </$text>' +
  145. '<$text italic="true">styling</$text>.[]</paragraph>' );
  146. } );
  147. it( 'pastes in the paragraph', () => {
  148. setData( editor.model, '<paragraph>More [] text</paragraph>' );
  149. expectPaste( editor, multipleStylesSingleLine, '<paragraph>More Text ' +
  150. '<$text bold="true" underline="true">containi</$text>' +
  151. '<$text bold="true" strikethrough="true" underline="true">ng</$text>' +
  152. '<$text strikethrough="true" underline="true"> multi</$text>' +
  153. '<$text strikethrough="true">ple </$text>' +
  154. '<$text italic="true">styling</$text>.[] text</paragraph>' );
  155. } );
  156. it( 'pastes in the different block context', () => {
  157. setData( editor.model, '<heading1>More [] text</heading1>' );
  158. expectPaste( editor, multipleStylesSingleLine, '<heading1>More Text ' +
  159. '<$text bold="true" underline="true">containi</$text>' +
  160. '<$text bold="true" strikethrough="true" underline="true">ng</$text>' +
  161. '<$text strikethrough="true" underline="true"> multi</$text>' +
  162. '<$text strikethrough="true">ple </$text>' +
  163. '<$text italic="true">styling</$text>.[] text</heading1>' );
  164. } );
  165. it( 'pastes in the inline styling context', () => {
  166. setData( editor.model, '<paragraph><$text bold="true">Bol[]d</$text></paragraph>' );
  167. expectPaste( editor, multipleStylesSingleLine, '<paragraph><$text bold="true">Bol</$text>Text ' +
  168. '<$text bold="true" underline="true">containi</$text>' +
  169. '<$text bold="true" strikethrough="true" underline="true">ng</$text>' +
  170. '<$text strikethrough="true" underline="true"> multi</$text>' +
  171. '<$text strikethrough="true">ple </$text>' +
  172. '<$text italic="true">styling</$text>.[]' +
  173. '<$text bold="true">d</$text></paragraph>' );
  174. } );
  175. } );
  176. // Pastes:
  177. // <p>Line <b>bold</b> and <i>italics</i></p>
  178. // <p>Line <b><u>foo</u></b><u> bar</u></p>
  179. // <p><s>Third</s> line <b>styling, <i>space on e</i>nd </b></p>
  180. describe( 'mulitple styles multiline', () => {
  181. it( 'pastes in the empty editor', () => {
  182. expectPaste( editor, multipleStylesMultiline, '<paragraph>Line ' +
  183. '<$text bold="true">bold</$text> and <$text italic="true">italics</$text></paragraph>' +
  184. '<paragraph>Line <$text bold="true" underline="true">foo</$text><$text underline="true"> bar</$text></paragraph>' +
  185. '<paragraph><$text strikethrough="true">Third</$text> line <$text bold="true">styling, </$text>' +
  186. '<$text bold="true" italic="true">space on e</$text>' +
  187. '<$text bold="true">nd []</$text></paragraph>' ); // The last space '...nd </b></p>' goes missing.
  188. } );
  189. it( 'pastes in the paragraph', () => {
  190. setData( editor.model, '<paragraph>More [] text</paragraph>' );
  191. expectPaste( editor, multipleStylesMultiline, '<paragraph>More Line ' +
  192. '<$text bold="true">bold</$text> and <$text italic="true">italics</$text></paragraph>' +
  193. '<paragraph>Line <$text bold="true" underline="true">foo</$text><$text underline="true"> bar</$text></paragraph>' +
  194. '<paragraph><$text strikethrough="true">Third</$text> line <$text bold="true">styling, </$text>' +
  195. '<$text bold="true" italic="true">space on e</$text>' +
  196. '<$text bold="true">nd []</$text> text</paragraph>' ); // The last space '...nd </b></p>' goes missing.
  197. } );
  198. it( 'pastes in the different block context', () => {
  199. setData( editor.model, '<heading1>More [] text</heading1>' );
  200. expectPaste( editor, multipleStylesMultiline, '<heading1>More Line ' +
  201. '<$text bold="true">bold</$text> and <$text italic="true">italics</$text></heading1>' +
  202. '<paragraph>Line <$text bold="true" underline="true">foo</$text><$text underline="true"> bar</$text></paragraph>' +
  203. '<paragraph><$text strikethrough="true">Third</$text> line <$text bold="true">styling, </$text>' +
  204. '<$text bold="true" italic="true">space on e</$text>' +
  205. '<$text bold="true">nd []</$text> text</paragraph>' ); // The last space '...nd </b></p>' goes missing.
  206. } );
  207. it( 'pastes in the inline styling context', () => {
  208. setData( editor.model, '<paragraph><$text bold="true">Bol[]d</$text></paragraph>' );
  209. expectPaste( editor, multipleStylesMultiline, '<paragraph><$text bold="true">Bol</$text>Line ' +
  210. '<$text bold="true">bold</$text> and <$text italic="true">italics</$text></paragraph>' +
  211. '<paragraph>Line <$text bold="true" underline="true">foo</$text><$text underline="true"> bar</$text></paragraph>' +
  212. '<paragraph><$text strikethrough="true">Third</$text> line <$text bold="true">styling, </$text>' +
  213. '<$text bold="true" italic="true">space on e</$text>' +
  214. '<$text bold="true">nd []d</$text></paragraph>' ); // The last space '...nd </b></p>' goes missing.
  215. } );
  216. } );
  217. } );
  218. function expectPaste( editor, input, output ) {
  219. pasteHtml( editor, input );
  220. expect( getData( editor.model ) ).to.equal( output );
  221. }
  222. function pasteHtml( editor, html ) {
  223. editor.editing.view.document.fire( 'paste', {
  224. dataTransfer: createDataTransfer( { 'text/html': html } ),
  225. preventDefault() {}
  226. } );
  227. }
  228. function createDataTransfer( data ) {
  229. return {
  230. getData( type ) {
  231. return data[ type ];
  232. }
  233. };
  234. }