whitespace-handling-integration.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  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 VirtualTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/virtualtesteditor';
  6. import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
  7. import ShiftEnter from '@ckeditor/ckeditor5-enter/src/shiftenter';
  8. import { getData } from '../../../src/dev-utils/model';
  9. // NOTE:
  10. // dev utils' setData() loses white spaces so don't use it for tests here!!!
  11. // https://github.com/ckeditor/ckeditor5-engine/issues/1428
  12. describe( 'DomConverter – whitespace handling – integration', () => {
  13. let editor;
  14. // See https://github.com/ckeditor/ckeditor5-engine/issues/822.
  15. describe( 'normalizing whitespaces around block boundaries (#822)', () => {
  16. beforeEach( () => {
  17. return VirtualTestEditor
  18. .create( { plugins: [ Paragraph ] } )
  19. .then( newEditor => {
  20. editor = newEditor;
  21. editor.model.schema.extend( '$text', { allowAttributes: [ 'bold' ] } );
  22. editor.conversion.attributeToElement( { model: 'bold', view: 'b' } );
  23. } );
  24. } );
  25. afterEach( () => {
  26. return editor.destroy();
  27. } );
  28. it( 'new line at the end of the content is ignored', () => {
  29. editor.setData( '<p>foo</p>\n' );
  30. expect( getData( editor.model, { withoutSelection: true } ) )
  31. .to.equal( '<paragraph>foo</paragraph>' );
  32. expect( editor.getData() ).to.equal( '<p>foo</p>' );
  33. } );
  34. it( 'whitespaces at the end of the content are ignored', () => {
  35. editor.setData( '<p>foo</p>\n\r\n \t' );
  36. expect( getData( editor.model, { withoutSelection: true } ) )
  37. .to.equal( '<paragraph>foo</paragraph>' );
  38. expect( editor.getData() ).to.equal( '<p>foo</p>' );
  39. } );
  40. it( 'nbsp at the end of the content is not ignored', () => {
  41. editor.setData( '<p>foo&nbsp;</p>' );
  42. expect( getData( editor.model, { withoutSelection: true } ) )
  43. .to.equal( '<paragraph>foo </paragraph>' );
  44. expect( editor.getData() ).to.equal( '<p>foo&nbsp;</p>' );
  45. } );
  46. it( 'new line at the beginning of the content is ignored', () => {
  47. editor.setData( '\n<p>foo</p>' );
  48. expect( getData( editor.model, { withoutSelection: true } ) )
  49. .to.equal( '<paragraph>foo</paragraph>' );
  50. expect( editor.getData() ).to.equal( '<p>foo</p>' );
  51. } );
  52. it( 'whitespaces at the beginning of the content are ignored', () => {
  53. editor.setData( '\n\n \t<p>foo</p>' );
  54. expect( getData( editor.model, { withoutSelection: true } ) )
  55. .to.equal( '<paragraph>foo</paragraph>' );
  56. expect( editor.getData() ).to.equal( '<p>foo</p>' );
  57. } );
  58. it( 'nbsp at the beginning of the content is not ignored', () => {
  59. editor.setData( '<p>&nbsp;foo</p>' );
  60. expect( getData( editor.model, { withoutSelection: true } ) )
  61. .to.equal( '<paragraph> foo</paragraph>' );
  62. expect( editor.getData() ).to.equal( '<p>&nbsp;foo</p>' );
  63. } );
  64. it( 'new line between blocks is ignored', () => {
  65. editor.setData( '<p>foo</p>\n<p>bar</p>' );
  66. expect( getData( editor.model, { withoutSelection: true } ) )
  67. .to.equal( '<paragraph>foo</paragraph><paragraph>bar</paragraph>' );
  68. expect( editor.getData() ).to.equal( '<p>foo</p><p>bar</p>' );
  69. } );
  70. it( 'whitespaces between blocks are ignored', () => {
  71. editor.setData( '<p>foo</p>\n\n \t<p>bar</p>' );
  72. expect( getData( editor.model, { withoutSelection: true } ) )
  73. .to.equal( '<paragraph>foo</paragraph><paragraph>bar</paragraph>' );
  74. expect( editor.getData() ).to.equal( '<p>foo</p><p>bar</p>' );
  75. } );
  76. // Controversial result. See https://github.com/ckeditor/ckeditor5-engine/issues/987.
  77. it( 'nbsp between blocks is not ignored (between paragraphs)', () => {
  78. editor.setData( '<p>foo</p>&nbsp;<p>bar</p>' );
  79. expect( getData( editor.model, { withoutSelection: true } ) )
  80. .to.equal( '<paragraph>foo</paragraph><paragraph> </paragraph><paragraph>bar</paragraph>' );
  81. expect( editor.getData() ).to.equal( '<p>foo</p><p>&nbsp;</p><p>bar</p>' );
  82. } );
  83. it( 'nbsp between blocks is not ignored (different blocks)', () => {
  84. editor.model.schema.register( 'block', { inheritAllFrom: '$block' } );
  85. editor.conversion.elementToElement( { model: 'block', view: 'block' } );
  86. editor.setData( '<block>foo</block>&nbsp;<p>bar</p>' );
  87. expect( getData( editor.model, { withoutSelection: true } ) )
  88. .to.equal(
  89. '<block>foo</block>' +
  90. '<paragraph> </paragraph>' +
  91. '<paragraph>bar</paragraph>'
  92. );
  93. expect( editor.getData() )
  94. .to.equal(
  95. '<block>foo</block>' +
  96. '<p>&nbsp;</p>' +
  97. '<p>bar</p>'
  98. );
  99. } );
  100. it( 'new lines inside blocks are ignored', () => {
  101. editor.setData( '<p>\nfoo\n</p>' );
  102. expect( getData( editor.model, { withoutSelection: true } ) )
  103. .to.equal( '<paragraph>foo</paragraph>' );
  104. expect( editor.getData() ).to.equal( '<p>foo</p>' );
  105. } );
  106. it( 'whitespaces inside blocks are ignored', () => {
  107. editor.setData( '<p>\n\n \tfoo\n\n \t</p>' );
  108. expect( getData( editor.model, { withoutSelection: true } ) )
  109. .to.equal( '<paragraph>foo</paragraph>' );
  110. expect( editor.getData() ).to.equal( '<p>foo</p>' );
  111. } );
  112. it( 'nbsp inside blocks are not ignored', () => {
  113. editor.setData( '<p>&nbsp;foo&nbsp;</p>' );
  114. expect( getData( editor.model, { withoutSelection: true } ) )
  115. .to.equal( '<paragraph> foo </paragraph>' );
  116. expect( editor.getData() ).to.equal( '<p>&nbsp;foo&nbsp;</p>' );
  117. } );
  118. it( 'single nbsp inside blocks are ignored', () => {
  119. editor.setData( '<p>&nbsp;</p>' );
  120. expect( getData( editor.model, { withoutSelection: true } ) )
  121. .to.equal( '<paragraph></paragraph>' );
  122. expect( editor.getData() ).to.equal( '' ); // trimmed
  123. } );
  124. it( 'all whitespaces together are ignored', () => {
  125. editor.setData( '\n<p>foo\n\r\n \t</p>\n<p> bar</p>' );
  126. expect( getData( editor.model, { withoutSelection: true } ) )
  127. .to.equal( '<paragraph>foo</paragraph><paragraph>bar</paragraph>' );
  128. expect( editor.getData() ).to.equal( '<p>foo</p><p>bar</p>' );
  129. } );
  130. it( 'nbsp between inline elements is not ignored', () => {
  131. editor.setData( '<p><b>foo</b>&nbsp;<b>bar</b></p>' );
  132. expect( getData( editor.model, { withoutSelection: true } ) )
  133. .to.equal( '<paragraph><$text bold="true">foo</$text>\u00A0<$text bold="true">bar</$text></paragraph>' );
  134. expect( editor.getData() ).to.equal( '<p><b>foo</b>&nbsp;<b>bar</b></p>' );
  135. } );
  136. it( 'nbsp before inline element is not ignored', () => {
  137. editor.setData( '<p>&nbsp;<b>bar</b></p>' );
  138. expect( getData( editor.model, { withoutSelection: true } ) )
  139. .to.equal( '<paragraph> <$text bold="true">bar</$text></paragraph>' );
  140. expect( editor.getData() ).to.equal( '<p>&nbsp;<b>bar</b></p>' );
  141. } );
  142. it( 'nbsp after inline element is not ignored', () => {
  143. editor.setData( '<p><b>bar</b>&nbsp;</p>' );
  144. expect( getData( editor.model, { withoutSelection: true } ) )
  145. .to.equal( '<paragraph><$text bold="true">bar</$text> </paragraph>' );
  146. expect( editor.getData() ).to.equal( '<p><b>bar</b>&nbsp;</p>' );
  147. } );
  148. } );
  149. // https://github.com/ckeditor/ckeditor5/issues/1024
  150. describe( 'whitespaces around <br>s', () => {
  151. beforeEach( () => {
  152. return VirtualTestEditor
  153. .create( { plugins: [ Paragraph, ShiftEnter ] } )
  154. .then( newEditor => {
  155. editor = newEditor;
  156. } );
  157. } );
  158. afterEach( () => {
  159. return editor.destroy();
  160. } );
  161. it( 'single spaces around <br> #1', () => {
  162. editor.setData( '<p>foo&nbsp;<br>&nbsp;bar</p>' );
  163. expect( getData( editor.model, { withoutSelection: true } ) )
  164. .to.equal( '<paragraph>foo\u00A0<softBreak></softBreak> bar</paragraph>' );
  165. expect( editor.getData() ).to.equal( '<p>foo&nbsp;<br>&nbsp;bar</p>' );
  166. } );
  167. it( 'single spaces around <br> #2', () => {
  168. editor.setData( '<p>foo&nbsp;<br> bar</p>' );
  169. expect( getData( editor.model, { withoutSelection: true } ) )
  170. .to.equal( '<paragraph>foo\u00A0<softBreak></softBreak>bar</paragraph>' );
  171. expect( editor.getData() ).to.equal( '<p>foo&nbsp;<br>bar</p>' );
  172. } );
  173. it( 'two spaces before a <br>', () => {
  174. editor.setData( '<p>foo &nbsp;<br>bar</p>' );
  175. expect( getData( editor.model, { withoutSelection: true } ) )
  176. .to.equal( '<paragraph>foo <softBreak></softBreak>bar</paragraph>' );
  177. expect( editor.getData() ).to.equal( '<p>foo &nbsp;<br>bar</p>' );
  178. } );
  179. it( 'two nbsps before a <br>', () => {
  180. editor.setData( '<p>foo&nbsp;&nbsp;<br>bar</p>' );
  181. expect( getData( editor.model, { withoutSelection: true } ) )
  182. .to.equal( '<paragraph>foo\u00a0 <softBreak></softBreak>bar</paragraph>' );
  183. expect( editor.getData() ).to.equal( '<p>foo&nbsp;&nbsp;<br>bar</p>' );
  184. } );
  185. it( 'single space after a <br>', () => {
  186. editor.setData( '<p>foo<br>&nbsp;bar</p>' );
  187. expect( getData( editor.model, { withoutSelection: true } ) )
  188. .to.equal( '<paragraph>foo<softBreak></softBreak> bar</paragraph>' );
  189. expect( editor.getData() ).to.equal( '<p>foo<br>&nbsp;bar</p>' );
  190. } );
  191. it( 'single space after a <br> (normalization)', () => {
  192. editor.setData( '<p>foo<br> bar</p>' );
  193. expect( getData( editor.model, { withoutSelection: true } ) )
  194. .to.equal( '<paragraph>foo<softBreak></softBreak>bar</paragraph>' );
  195. expect( editor.getData() ).to.equal( '<p>foo<br>bar</p>' );
  196. } );
  197. it( 'two spaces after a <br>', () => {
  198. editor.setData( '<p>foo<br>&nbsp; bar</p>' );
  199. expect( getData( editor.model, { withoutSelection: true } ) )
  200. .to.equal( '<paragraph>foo<softBreak></softBreak> bar</paragraph>' );
  201. expect( editor.getData() ).to.equal( '<p>foo<br>&nbsp; bar</p>' );
  202. } );
  203. it( 'two spaces after a <br> (normalization)', () => {
  204. editor.setData( '<p>foo<br> &nbsp;bar</p>' );
  205. expect( getData( editor.model, { withoutSelection: true } ) )
  206. .to.equal( '<paragraph>foo<softBreak></softBreak> bar</paragraph>' );
  207. expect( editor.getData() ).to.equal( '<p>foo<br>&nbsp;bar</p>' );
  208. } );
  209. it( 'two spaces after a <br> (normalization to a model nbsp)', () => {
  210. editor.setData( '<p>foo<br>&nbsp;&nbsp;bar</p>' );
  211. expect( getData( editor.model, { withoutSelection: true } ) )
  212. .to.equal( '<paragraph>foo<softBreak></softBreak> \u00a0bar</paragraph>' );
  213. expect( editor.getData() ).to.equal( '<p>foo<br>&nbsp;&nbsp;bar</p>' );
  214. } );
  215. // https://github.com/ckeditor/ckeditor5-engine/issues/1429
  216. // it( 'space between <br>s', () => {
  217. // editor.setData( '<p>foo<br>&nbsp;<br>bar</p>' );
  218. // expect( getData( editor.model, { withoutSelection: true } ) )
  219. // .to.equal( '<paragraph>foo<softBreak></softBreak> <softBreak></softBreak>bar</paragraph>' );
  220. // expect( editor.getData() ).to.equal( '<p>foo<br>&nbsp;<br>bar</p>' );
  221. // } );
  222. it( 'space between <br>s (normalization)', () => {
  223. editor.setData( '<p>foo<br> <br>bar</p>' );
  224. expect( getData( editor.model, { withoutSelection: true } ) )
  225. .to.equal( '<paragraph>foo<softBreak></softBreak><softBreak></softBreak>bar</paragraph>' );
  226. expect( editor.getData() ).to.equal( '<p>foo<br><br>bar</p>' );
  227. } );
  228. it( 'two spaces between <br>s', () => {
  229. editor.setData( '<p>foo<br>&nbsp;&nbsp;<br>bar</p>' );
  230. expect( getData( editor.model, { withoutSelection: true } ) )
  231. .to.equal( '<paragraph>foo<softBreak></softBreak> <softBreak></softBreak>bar</paragraph>' );
  232. expect( editor.getData() ).to.equal( '<p>foo<br>&nbsp;&nbsp;<br>bar</p>' );
  233. } );
  234. } );
  235. } );