whitespace-handling-integration.js 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. /**
  2. * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  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. } );
  22. } );
  23. afterEach( () => {
  24. return editor.destroy();
  25. } );
  26. it( 'new line at the end of the content is ignored', () => {
  27. editor.setData( '<p>foo</p>\n' );
  28. expect( getData( editor.model, { withoutSelection: true } ) )
  29. .to.equal( '<paragraph>foo</paragraph>' );
  30. expect( editor.getData() ).to.equal( '<p>foo</p>' );
  31. } );
  32. it( 'whitespaces at the end of the content are ignored', () => {
  33. editor.setData( '<p>foo</p>\n\r\n \t' );
  34. expect( getData( editor.model, { withoutSelection: true } ) )
  35. .to.equal( '<paragraph>foo</paragraph>' );
  36. expect( editor.getData() ).to.equal( '<p>foo</p>' );
  37. } );
  38. // Controversial result. See https://github.com/ckeditor/ckeditor5-engine/issues/987.
  39. it( 'nbsp at the end of the content is not ignored', () => {
  40. editor.setData( '<p>foo</p>' );
  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( 'new line at the beginning of the content is ignored', () => {
  46. editor.setData( '\n<p>foo</p>' );
  47. expect( getData( editor.model, { withoutSelection: true } ) )
  48. .to.equal( '<paragraph>foo</paragraph>' );
  49. expect( editor.getData() ).to.equal( '<p>foo</p>' );
  50. } );
  51. it( 'whitespaces at the beginning of the content are ignored', () => {
  52. editor.setData( '\n\n \t<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. // Controversial result. See https://github.com/ckeditor/ckeditor5-engine/issues/987.
  58. it( 'nbsp at the beginning of the content is not ignored', () => {
  59. editor.setData( '<p>foo</p>' );
  60. expect( getData( editor.model, { withoutSelection: true } ) )
  61. .to.equal( '<paragraph>foo</paragraph>' );
  62. expect( editor.getData() ).to.equal( '<p>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', () => {
  78. editor.setData( '<p>foo</p>&nbsp;<p>bar</p>' );
  79. expect( getData( editor.model, { withoutSelection: true } ) )
  80. .to.equal( '<paragraph>foo</paragraph><paragraph>bar</paragraph>' );
  81. expect( editor.getData() ).to.equal( '<p>foo</p><p>bar</p>' );
  82. } );
  83. it( 'new lines inside blocks are ignored', () => {
  84. editor.setData( '<p>\nfoo\n</p>' );
  85. expect( getData( editor.model, { withoutSelection: true } ) )
  86. .to.equal( '<paragraph>foo</paragraph>' );
  87. expect( editor.getData() ).to.equal( '<p>foo</p>' );
  88. } );
  89. it( 'whitespaces inside blocks are ignored', () => {
  90. editor.setData( '<p>\n\n \tfoo\n\n \t</p>' );
  91. expect( getData( editor.model, { withoutSelection: true } ) )
  92. .to.equal( '<paragraph>foo</paragraph>' );
  93. expect( editor.getData() ).to.equal( '<p>foo</p>' );
  94. } );
  95. it( 'nbsp inside blocks are not ignored', () => {
  96. editor.setData( '<p>&nbsp;foo&nbsp;</p>' );
  97. expect( getData( editor.model, { withoutSelection: true } ) )
  98. .to.equal( '<paragraph> foo </paragraph>' );
  99. expect( editor.getData() ).to.equal( '<p>&nbsp;foo&nbsp;</p>' );
  100. } );
  101. it( 'all whitespaces together are ignored', () => {
  102. editor.setData( '\n<p>foo\n\r\n \t</p>\n<p> bar</p>' );
  103. expect( getData( editor.model, { withoutSelection: true } ) )
  104. .to.equal( '<paragraph>foo</paragraph><paragraph>bar</paragraph>' );
  105. expect( editor.getData() ).to.equal( '<p>foo</p><p>bar</p>' );
  106. } );
  107. } );
  108. // https://github.com/ckeditor/ckeditor5/issues/1024
  109. describe( 'whitespaces around <br>s', () => {
  110. beforeEach( () => {
  111. return VirtualTestEditor
  112. .create( { plugins: [ Paragraph, ShiftEnter ] } )
  113. .then( newEditor => {
  114. editor = newEditor;
  115. } );
  116. } );
  117. afterEach( () => {
  118. return editor.destroy();
  119. } );
  120. it( 'single spaces around <br>', () => {
  121. editor.setData( '<p>foo&nbsp;<br>&nbsp;bar</p>' );
  122. expect( getData( editor.model, { withoutSelection: true } ) )
  123. .to.equal( '<paragraph>foo <softBreak></softBreak> bar</paragraph>' );
  124. expect( editor.getData() ).to.equal( '<p>foo&nbsp;<br>&nbsp;bar</p>' );
  125. } );
  126. it( 'single spaces around <br> (normalization)', () => {
  127. editor.setData( '<p>foo&nbsp;<br> bar</p>' );
  128. expect( getData( editor.model, { withoutSelection: true } ) )
  129. .to.equal( '<paragraph>foo <softBreak></softBreak>bar</paragraph>' );
  130. expect( editor.getData() ).to.equal( '<p>foo&nbsp;<br>bar</p>' );
  131. } );
  132. it( 'two spaces before a <br>', () => {
  133. editor.setData( '<p>foo &nbsp;<br>bar</p>' );
  134. expect( getData( editor.model, { withoutSelection: true } ) )
  135. .to.equal( '<paragraph>foo <softBreak></softBreak>bar</paragraph>' );
  136. expect( editor.getData() ).to.equal( '<p>foo &nbsp;<br>bar</p>' );
  137. } );
  138. it( 'two spaces before a <br> (normalization)', () => {
  139. editor.setData( '<p>foo&nbsp; <br>bar</p>' );
  140. expect( getData( editor.model, { withoutSelection: true } ) )
  141. .to.equal( '<paragraph>foo <softBreak></softBreak>bar</paragraph>' );
  142. expect( editor.getData() ).to.equal( '<p>foo &nbsp;<br>bar</p>' );
  143. } );
  144. it( 'two spaces before a <br> (normalization to a model nbsp)', () => {
  145. editor.setData( '<p>foo&nbsp;&nbsp;<br>bar</p>' );
  146. expect( getData( editor.model, { withoutSelection: true } ) )
  147. .to.equal( '<paragraph>foo\u00a0 <softBreak></softBreak>bar</paragraph>' );
  148. expect( editor.getData() ).to.equal( '<p>foo&nbsp;&nbsp;<br>bar</p>' );
  149. } );
  150. it( 'single space after a <br>', () => {
  151. editor.setData( '<p>foo<br>&nbsp;bar</p>' );
  152. expect( getData( editor.model, { withoutSelection: true } ) )
  153. .to.equal( '<paragraph>foo<softBreak></softBreak> bar</paragraph>' );
  154. expect( editor.getData() ).to.equal( '<p>foo<br>&nbsp;bar</p>' );
  155. } );
  156. it( 'single space after a <br> (normalization)', () => {
  157. editor.setData( '<p>foo<br> bar</p>' );
  158. expect( getData( editor.model, { withoutSelection: true } ) )
  159. .to.equal( '<paragraph>foo<softBreak></softBreak>bar</paragraph>' );
  160. expect( editor.getData() ).to.equal( '<p>foo<br>bar</p>' );
  161. } );
  162. it( 'two spaces after a <br>', () => {
  163. editor.setData( '<p>foo<br>&nbsp; bar</p>' );
  164. expect( getData( editor.model, { withoutSelection: true } ) )
  165. .to.equal( '<paragraph>foo<softBreak></softBreak> bar</paragraph>' );
  166. expect( editor.getData() ).to.equal( '<p>foo<br>&nbsp; bar</p>' );
  167. } );
  168. it( 'two spaces after a <br> (normalization)', () => {
  169. editor.setData( '<p>foo<br> &nbsp;bar</p>' );
  170. expect( getData( editor.model, { withoutSelection: true } ) )
  171. .to.equal( '<paragraph>foo<softBreak></softBreak> bar</paragraph>' );
  172. expect( editor.getData() ).to.equal( '<p>foo<br>&nbsp;bar</p>' );
  173. } );
  174. it( 'two spaces after a <br> (normalization to a model nbsp)', () => {
  175. editor.setData( '<p>foo<br>&nbsp;&nbsp;bar</p>' );
  176. expect( getData( editor.model, { withoutSelection: true } ) )
  177. .to.equal( '<paragraph>foo<softBreak></softBreak> \u00a0bar</paragraph>' );
  178. expect( editor.getData() ).to.equal( '<p>foo<br>&nbsp;&nbsp;bar</p>' );
  179. } );
  180. // https://github.com/ckeditor/ckeditor5-engine/issues/1429
  181. // it( 'space between <br>s', () => {
  182. // editor.setData( '<p>foo<br>&nbsp;<br>bar</p>' );
  183. // expect( getData( editor.model, { withoutSelection: true } ) )
  184. // .to.equal( '<paragraph>foo<softBreak></softBreak> <softBreak></softBreak>bar</paragraph>' );
  185. // expect( editor.getData() ).to.equal( '<p>foo<br>&nbsp;<br>bar</p>' );
  186. // } );
  187. it( 'space between <br>s (normalization)', () => {
  188. editor.setData( '<p>foo<br> <br>bar</p>' );
  189. expect( getData( editor.model, { withoutSelection: true } ) )
  190. .to.equal( '<paragraph>foo<softBreak></softBreak><softBreak></softBreak>bar</paragraph>' );
  191. expect( editor.getData() ).to.equal( '<p>foo<br><br>bar</p>' );
  192. } );
  193. it( 'two spaces between <br>s', () => {
  194. editor.setData( '<p>foo<br>&nbsp;&nbsp;<br>bar</p>' );
  195. expect( getData( editor.model, { withoutSelection: true } ) )
  196. .to.equal( '<paragraph>foo<softBreak></softBreak> <softBreak></softBreak>bar</paragraph>' );
  197. expect( editor.getData() ).to.equal( '<p>foo<br>&nbsp;&nbsp;<br>bar</p>' );
  198. } );
  199. } );
  200. } );