8
0

whitespace-handling-integration.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. /**
  2. * @license Copyright (c) 2003-2019, 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. editor.model.schema.register( 'blockQuote', {
  24. allowWhere: '$block',
  25. allowContentOf: '$root'
  26. } );
  27. editor.conversion.elementToElement( { model: 'blockQuote', view: 'blockquote' } );
  28. } );
  29. } );
  30. afterEach( () => {
  31. return editor.destroy();
  32. } );
  33. it( 'new line at the end of the content is ignored', () => {
  34. editor.setData( '<p>foo</p>\n' );
  35. expect( getData( editor.model, { withoutSelection: true } ) )
  36. .to.equal( '<paragraph>foo</paragraph>' );
  37. expect( editor.getData() ).to.equal( '<p>foo</p>' );
  38. } );
  39. it( 'whitespaces at the end of the content are ignored', () => {
  40. editor.setData( '<p>foo</p>\n\r\n \t' );
  41. expect( getData( editor.model, { withoutSelection: true } ) )
  42. .to.equal( '<paragraph>foo</paragraph>' );
  43. expect( editor.getData() ).to.equal( '<p>foo</p>' );
  44. } );
  45. it( 'nbsp at the end of the content is not ignored', () => {
  46. editor.setData( '<p>foo&nbsp;</p>' );
  47. expect( getData( editor.model, { withoutSelection: true } ) )
  48. .to.equal( '<paragraph>foo </paragraph>' );
  49. expect( editor.getData() ).to.equal( '<p>foo&nbsp;</p>' );
  50. } );
  51. it( 'new line at the beginning of the content is ignored', () => {
  52. editor.setData( '\n<p>foo</p>' );
  53. expect( getData( editor.model, { withoutSelection: true } ) )
  54. .to.equal( '<paragraph>foo</paragraph>' );
  55. expect( editor.getData() ).to.equal( '<p>foo</p>' );
  56. } );
  57. it( 'whitespaces at the beginning of the content are ignored', () => {
  58. editor.setData( '\n\n \t<p>foo</p>' );
  59. expect( getData( editor.model, { withoutSelection: true } ) )
  60. .to.equal( '<paragraph>foo</paragraph>' );
  61. expect( editor.getData() ).to.equal( '<p>foo</p>' );
  62. } );
  63. it( 'nbsp at the beginning of the content is not ignored', () => {
  64. editor.setData( '<p>&nbsp;foo</p>' );
  65. expect( getData( editor.model, { withoutSelection: true } ) )
  66. .to.equal( '<paragraph> foo</paragraph>' );
  67. expect( editor.getData() ).to.equal( '<p>&nbsp;foo</p>' );
  68. } );
  69. it( 'new line between blocks is ignored', () => {
  70. editor.setData( '<p>foo</p>\n<p>bar</p>' );
  71. expect( getData( editor.model, { withoutSelection: true } ) )
  72. .to.equal( '<paragraph>foo</paragraph><paragraph>bar</paragraph>' );
  73. expect( editor.getData() ).to.equal( '<p>foo</p><p>bar</p>' );
  74. } );
  75. it( 'whitespaces between blocks are ignored', () => {
  76. editor.setData( '<p>foo</p>\n\n \t<p>bar</p>' );
  77. expect( getData( editor.model, { withoutSelection: true } ) )
  78. .to.equal( '<paragraph>foo</paragraph><paragraph>bar</paragraph>' );
  79. expect( editor.getData() ).to.equal( '<p>foo</p><p>bar</p>' );
  80. } );
  81. it( 'nbsp between blocks is ignored #1', () => {
  82. editor.setData( '<p>foo</p>&nbsp;<p>bar</p>' );
  83. expect( getData( editor.model, { withoutSelection: true } ) )
  84. .to.equal( '<paragraph>foo</paragraph><paragraph>bar</paragraph>' );
  85. expect( editor.getData() ).to.equal( '<p>foo</p><p>bar</p>' );
  86. } );
  87. it( 'nbsp between blocks is ignored #2', () => {
  88. editor.setData( '<table>foo</table>&nbsp;<p>bar</p>' );
  89. expect( getData( editor.model, { withoutSelection: true } ) )
  90. .to.equal( '<paragraph>foo</paragraph><paragraph>bar</paragraph>' );
  91. expect( editor.getData() ).to.equal( '<p>foo</p><p>bar</p>' );
  92. } );
  93. it( 'nbsp between blocks is ignored #3', () => {
  94. editor.setData( '<blockquote>foo</blockquote>&nbsp;<blockquote>bar</blockquote>' );
  95. expect( getData( editor.model, { withoutSelection: true } ) )
  96. .to.equal( '<blockQuote><paragraph>foo</paragraph></blockQuote><blockQuote><paragraph>bar</paragraph></blockQuote>' );
  97. expect( editor.getData() ).to.equal( '<blockquote><p>foo</p></blockquote><blockquote><p>bar</p></blockquote>' );
  98. } );
  99. it( 'new lines inside blocks are ignored', () => {
  100. editor.setData( '<p>\nfoo\n</p>' );
  101. expect( getData( editor.model, { withoutSelection: true } ) )
  102. .to.equal( '<paragraph>foo</paragraph>' );
  103. expect( editor.getData() ).to.equal( '<p>foo</p>' );
  104. } );
  105. it( 'whitespaces inside blocks are ignored', () => {
  106. editor.setData( '<p>\n\n \tfoo\n\n \t</p>' );
  107. expect( getData( editor.model, { withoutSelection: true } ) )
  108. .to.equal( '<paragraph>foo</paragraph>' );
  109. expect( editor.getData() ).to.equal( '<p>foo</p>' );
  110. } );
  111. it( 'nbsp inside blocks are not ignored', () => {
  112. editor.setData( '<p>&nbsp;foo&nbsp;</p>' );
  113. expect( getData( editor.model, { withoutSelection: true } ) )
  114. .to.equal( '<paragraph> foo </paragraph>' );
  115. expect( editor.getData() ).to.equal( '<p>&nbsp;foo&nbsp;</p>' );
  116. } );
  117. it( 'single nbsp inside blocks are ignored', () => {
  118. editor.setData( '<p>&nbsp;</p>' );
  119. expect( getData( editor.model, { withoutSelection: true } ) )
  120. .to.equal( '<paragraph></paragraph>' );
  121. expect( editor.getData() ).to.equal( '' ); // trimmed
  122. } );
  123. it( 'all whitespaces together are ignored', () => {
  124. editor.setData( '\n<p>foo\n\r\n \t</p>\n<p> bar</p>' );
  125. expect( getData( editor.model, { withoutSelection: true } ) )
  126. .to.equal( '<paragraph>foo</paragraph><paragraph>bar</paragraph>' );
  127. expect( editor.getData() ).to.equal( '<p>foo</p><p>bar</p>' );
  128. } );
  129. it( 'nbsp between inline elements inside blocks are not ignored', () => {
  130. editor.setData( '<p><b>foo</b>&nbsp;<b>bar</b></p>' );
  131. expect( getData( editor.model, { withoutSelection: true } ) )
  132. .to.equal( '<paragraph><$text bold="true">foo</$text>\u00A0<$text bold="true">bar</$text></paragraph>' );
  133. expect( editor.getData() ).to.equal( '<p><b>foo</b>&nbsp;<b>bar</b></p>' );
  134. } );
  135. it( 'nbsp inside inline element is not ignored', () => {
  136. editor.setData( '<p>Foo<b>&nbsp;</b></p>' );
  137. expect( getData( editor.model, { withoutSelection: true } ) )
  138. .to.equal( '<paragraph>Foo<$text bold="true"> </$text></paragraph>' );
  139. expect( editor.getData() ).to.equal( '<p>Foo<b>&nbsp;</b></p>' );
  140. } );
  141. } );
  142. // https://github.com/ckeditor/ckeditor5/issues/1024
  143. describe( 'whitespaces around <br>s', () => {
  144. beforeEach( () => {
  145. return VirtualTestEditor
  146. .create( { plugins: [ Paragraph, ShiftEnter ] } )
  147. .then( newEditor => {
  148. editor = newEditor;
  149. } );
  150. } );
  151. afterEach( () => {
  152. return editor.destroy();
  153. } );
  154. it( 'single spaces around <br> #1', () => {
  155. editor.setData( '<p>foo&nbsp;<br>&nbsp;bar</p>' );
  156. expect( getData( editor.model, { withoutSelection: true } ) )
  157. .to.equal( '<paragraph>foo\u00A0<softBreak></softBreak> bar</paragraph>' );
  158. expect( editor.getData() ).to.equal( '<p>foo&nbsp;<br>&nbsp;bar</p>' );
  159. } );
  160. it( 'single spaces around <br> #2', () => {
  161. editor.setData( '<p>foo&nbsp;<br> bar</p>' );
  162. expect( getData( editor.model, { withoutSelection: true } ) )
  163. .to.equal( '<paragraph>foo\u00A0<softBreak></softBreak>bar</paragraph>' );
  164. expect( editor.getData() ).to.equal( '<p>foo&nbsp;<br>bar</p>' );
  165. } );
  166. it( 'two spaces before a <br>', () => {
  167. editor.setData( '<p>foo &nbsp;<br>bar</p>' );
  168. expect( getData( editor.model, { withoutSelection: true } ) )
  169. .to.equal( '<paragraph>foo <softBreak></softBreak>bar</paragraph>' );
  170. expect( editor.getData() ).to.equal( '<p>foo &nbsp;<br>bar</p>' );
  171. } );
  172. it( 'two nbsps before a <br>', () => {
  173. editor.setData( '<p>foo&nbsp;&nbsp;<br>bar</p>' );
  174. expect( getData( editor.model, { withoutSelection: true } ) )
  175. .to.equal( '<paragraph>foo\u00a0 <softBreak></softBreak>bar</paragraph>' );
  176. expect( editor.getData() ).to.equal( '<p>foo&nbsp;&nbsp;<br>bar</p>' );
  177. } );
  178. it( 'single space after a <br>', () => {
  179. editor.setData( '<p>foo<br>&nbsp;bar</p>' );
  180. expect( getData( editor.model, { withoutSelection: true } ) )
  181. .to.equal( '<paragraph>foo<softBreak></softBreak> bar</paragraph>' );
  182. expect( editor.getData() ).to.equal( '<p>foo<br>&nbsp;bar</p>' );
  183. } );
  184. it( 'single space after a <br> (normalization)', () => {
  185. editor.setData( '<p>foo<br> bar</p>' );
  186. expect( getData( editor.model, { withoutSelection: true } ) )
  187. .to.equal( '<paragraph>foo<softBreak></softBreak>bar</paragraph>' );
  188. expect( editor.getData() ).to.equal( '<p>foo<br>bar</p>' );
  189. } );
  190. it( 'two spaces after a <br>', () => {
  191. editor.setData( '<p>foo<br>&nbsp; bar</p>' );
  192. expect( getData( editor.model, { withoutSelection: true } ) )
  193. .to.equal( '<paragraph>foo<softBreak></softBreak> bar</paragraph>' );
  194. expect( editor.getData() ).to.equal( '<p>foo<br>&nbsp; bar</p>' );
  195. } );
  196. it( 'two spaces after a <br> (normalization)', () => {
  197. editor.setData( '<p>foo<br> &nbsp;bar</p>' );
  198. expect( getData( editor.model, { withoutSelection: true } ) )
  199. .to.equal( '<paragraph>foo<softBreak></softBreak> bar</paragraph>' );
  200. expect( editor.getData() ).to.equal( '<p>foo<br>&nbsp;bar</p>' );
  201. } );
  202. it( 'two spaces after a <br> (normalization to a model nbsp)', () => {
  203. editor.setData( '<p>foo<br>&nbsp;&nbsp;bar</p>' );
  204. expect( getData( editor.model, { withoutSelection: true } ) )
  205. .to.equal( '<paragraph>foo<softBreak></softBreak> \u00a0bar</paragraph>' );
  206. expect( editor.getData() ).to.equal( '<p>foo<br>&nbsp;&nbsp;bar</p>' );
  207. } );
  208. // https://github.com/ckeditor/ckeditor5-engine/issues/1429
  209. // it( 'space between <br>s', () => {
  210. // editor.setData( '<p>foo<br>&nbsp;<br>bar</p>' );
  211. // expect( getData( editor.model, { withoutSelection: true } ) )
  212. // .to.equal( '<paragraph>foo<softBreak></softBreak> <softBreak></softBreak>bar</paragraph>' );
  213. // expect( editor.getData() ).to.equal( '<p>foo<br>&nbsp;<br>bar</p>' );
  214. // } );
  215. it( 'space between <br>s (normalization)', () => {
  216. editor.setData( '<p>foo<br> <br>bar</p>' );
  217. expect( getData( editor.model, { withoutSelection: true } ) )
  218. .to.equal( '<paragraph>foo<softBreak></softBreak><softBreak></softBreak>bar</paragraph>' );
  219. expect( editor.getData() ).to.equal( '<p>foo<br><br>bar</p>' );
  220. } );
  221. it( 'two spaces between <br>s', () => {
  222. editor.setData( '<p>foo<br>&nbsp;&nbsp;<br>bar</p>' );
  223. expect( getData( editor.model, { withoutSelection: true } ) )
  224. .to.equal( '<paragraph>foo<softBreak></softBreak> <softBreak></softBreak>bar</paragraph>' );
  225. expect( editor.getData() ).to.equal( '<p>foo<br>&nbsp;&nbsp;<br>bar</p>' );
  226. } );
  227. } );
  228. } );