paragraph.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. /**
  2. * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import Paragraph from 'ckeditor5/paragraph/paragraph.js';
  6. import VirtualTestEditor from 'tests/core/_utils/virtualtesteditor.js';
  7. import {
  8. getData as getModelData,
  9. setData as setModelData,
  10. stringify as stringifyModel
  11. } from 'ckeditor5/engine/dev-utils/model.js';
  12. import { getData as getViewData } from 'ckeditor5/engine/dev-utils/view.js';
  13. import buildViewConverter from 'ckeditor5/engine/conversion/buildviewconverter.js';
  14. describe( 'Paragraph feature', () => {
  15. let editor, doc;
  16. beforeEach( () => {
  17. return VirtualTestEditor.create( {
  18. plugins: [ Paragraph ]
  19. } )
  20. .then( newEditor => {
  21. editor = newEditor;
  22. doc = editor.document;
  23. } );
  24. } );
  25. it( 'should be loaded', () => {
  26. expect( editor.plugins.get( Paragraph ) ).to.be.instanceOf( Paragraph );
  27. } );
  28. it( 'should set proper schema rules', () => {
  29. expect( doc.schema.hasItem( 'paragraph' ) ).to.be.true;
  30. expect( doc.schema.check( { name: 'paragraph', inside: '$root' } ) ).to.be.true;
  31. expect( doc.schema.check( { name: '$inline', inside: 'paragraph' } ) ).to.be.true;
  32. } );
  33. it( 'should have a static paragraphLikeElements property', () => {
  34. expect( Paragraph ).to.have.property( 'paragraphLikeElements' );
  35. } );
  36. describe( 'data pipeline conversions', () => {
  37. it( 'should convert paragraph', () => {
  38. editor.setData( '<p>foobar</p>' );
  39. expect( getModelData( doc, { withoutSelection: true } ) ).to.equal( '<paragraph>foobar</paragraph>' );
  40. expect( editor.getData() ).to.equal( '<p>foobar</p>' );
  41. } );
  42. it( 'should convert paragraph only', () => {
  43. editor.setData( '<p>foo<b>baz</b>bar</p>' );
  44. expect( getModelData( doc, { withoutSelection: true } ) ).to.equal( '<paragraph>foobazbar</paragraph>' );
  45. expect( editor.getData() ).to.equal( '<p>foobazbar</p>' );
  46. } );
  47. it( 'should convert multiple paragraphs', () => {
  48. editor.setData( '<p>foo</p><p>baz</p>' );
  49. expect( getModelData( doc, { withoutSelection: true } ) ).to.equal( '<paragraph>foo</paragraph><paragraph>baz</paragraph>' );
  50. expect( editor.getData() ).to.equal( '<p>foo</p><p>baz</p>' );
  51. } );
  52. describe( 'generic text converter (text autoparagraphing)', () => {
  53. it( 'should autoparagraph text', () => {
  54. editor.setData( 'foo' );
  55. expect( getModelData( doc, { withoutSelection: true } ) ).to.equal( '<paragraph>foo</paragraph>' );
  56. expect( editor.getData() ).to.equal( '<p>foo</p>' );
  57. } );
  58. it( 'should not autoparagraph text (in clipboard holder)', () => {
  59. const modelFragment = editor.data.parse( 'foo', '$clipboardHolder' );
  60. expect( stringifyModel( modelFragment ) )
  61. .to.equal( 'foo' );
  62. } );
  63. it( 'should autoparagraph text next to allowed element', () => {
  64. doc.schema.registerItem( 'heading1', '$block' );
  65. buildViewConverter().for( editor.data.viewToModel ).fromElement( 'h1' ).toElement( 'heading1' );
  66. const modelFragment = editor.data.parse( '<h1>foo</h1>bar<p>bom</p>' );
  67. expect( stringifyModel( modelFragment ) )
  68. .to.equal( '<heading1>foo</heading1><paragraph>bar</paragraph><paragraph>bom</paragraph>' );
  69. } );
  70. it( 'should autoparagraph 3 inline inline nodes into one paragraph', () => {
  71. const modelFragment = editor.data.parse( 'foo<b>bar</b>bom' );
  72. expect( stringifyModel( modelFragment ) )
  73. .to.equal( '<paragraph>foobarbom</paragraph>' );
  74. } );
  75. it( 'should not autoparagraph 3 inline inline nodes (in clipboardHolder)', () => {
  76. const modelFragment = editor.data.parse( 'foo<b>bar</b>bom', '$clipboardHolder' );
  77. expect( stringifyModel( modelFragment ) )
  78. .to.equal( 'foobarbom' );
  79. } );
  80. it( 'should autoparagraph text inside converted container', () => {
  81. doc.schema.registerItem( 'div' );
  82. doc.schema.allow( { name: 'div', inside: '$root' } );
  83. doc.schema.allow( { name: 'paragraph', inside: 'div' } );
  84. buildViewConverter().for( editor.data.viewToModel ).fromElement( 'div' ).toElement( 'div' );
  85. const modelFragment = editor.data.parse( '<div>foo</div><div>bom<p>bim</p></div>' );
  86. expect( stringifyModel( modelFragment ) )
  87. .to.equal(
  88. '<div><paragraph>foo</paragraph></div>' +
  89. '<div><paragraph>bom</paragraph><paragraph>bim</paragraph></div>'
  90. );
  91. } );
  92. it( 'should autoparagraph text inside disallowed element next to allowed element', () => {
  93. doc.schema.registerItem( 'heading1', '$block' );
  94. buildViewConverter().for( editor.data.viewToModel ).fromElement( 'h1' ).toElement( 'heading1' );
  95. const modelFragment = editor.data.parse( '<div><h1>foo</h1>bar</div>' );
  96. expect( stringifyModel( modelFragment ) )
  97. .to.equal( '<heading1>foo</heading1><paragraph>bar</paragraph>' );
  98. } );
  99. it( 'should not autoparagraph text in disallowed element', () => {
  100. doc.schema.registerItem( 'heading1', '$block' );
  101. buildViewConverter().for( editor.data.viewToModel ).fromElement( 'h1' ).toElement( 'heading1' );
  102. const modelFragment = editor.data.parse( '<h1><b>foo</b>bar</h1>' );
  103. expect( stringifyModel( modelFragment ) )
  104. .to.equal( '<heading1>foobar</heading1>' );
  105. } );
  106. } );
  107. describe( 'generic block converter (paragraph-like element handling)', () => {
  108. it( 'should convert h1+h2', () => {
  109. const modelFragment = editor.data.parse( '<h1>foo</h1><h2>bar</h2>' );
  110. expect( stringifyModel( modelFragment ) )
  111. .to.equal( '<paragraph>foo</paragraph><paragraph>bar</paragraph>' );
  112. } );
  113. it( 'should convert h1+h2 (in clipboard holder)', () => {
  114. const modelFragment = editor.data.parse( '<h1>foo</h1><h2>bar</h2>', '$clipboardHolder' );
  115. expect( stringifyModel( modelFragment ) )
  116. .to.equal( '<paragraph>foo</paragraph><paragraph>bar</paragraph>' );
  117. } );
  118. it( 'should convert ul,ol>li', () => {
  119. const modelFragment = editor.data.parse( '<ul><li>a</li><li>b</li></ul><ol><li>c</li></ol>' );
  120. expect( stringifyModel( modelFragment ) )
  121. .to.equal( '<paragraph>a</paragraph><paragraph>b</paragraph><paragraph>c</paragraph>' );
  122. } );
  123. it( 'should convert ul,ol>li (in clipboard holder)', () => {
  124. const modelFragment = editor.data.parse( '<ul><li>a</li><li>b</li></ul><ol><li>c</li></ol>', '$clipboardHolder' );
  125. expect( stringifyModel( modelFragment ) )
  126. .to.equal( '<paragraph>a</paragraph><paragraph>b</paragraph><paragraph>c</paragraph>' );
  127. } );
  128. it( 'should convert ul>li>ul>li+li', () => {
  129. const modelFragment = editor.data.parse( '<ul><li>a<ul><li>b</li><li>c</li></ul></li></ul>' );
  130. expect( stringifyModel( modelFragment ) )
  131. .to.equal( '<paragraph>a</paragraph><paragraph>b</paragraph><paragraph>c</paragraph>' );
  132. } );
  133. // "b" is not autoparagraphed because clipboard holder allows text nodes.
  134. // There's a similar integrational test what's going to happen when pasting in paragraph-integration.js.
  135. it( 'should convert ul>li>ul>li+li (in clipboard holder)', () => {
  136. const modelFragment = editor.data.parse( '<ul><li>a<ul><li>b</li><li>c</li></ul></li></ul>', '$clipboardHolder' );
  137. expect( stringifyModel( modelFragment ) )
  138. .to.equal( 'a<paragraph>b</paragraph><paragraph>c</paragraph>' );
  139. } );
  140. it( 'should convert ul>li>p,text', () => {
  141. const modelFragment = editor.data.parse( '<ul><li><p>a</p>b</li></ul>' );
  142. expect( stringifyModel( modelFragment ) )
  143. .to.equal( '<paragraph>a</paragraph><paragraph>b</paragraph>' );
  144. } );
  145. // "b" is not autoparagraphed because clipboard holder allows text nodes.
  146. // There's a similar integrational test what's going to happen when pasting in paragraph-integration.js.
  147. it( 'should convert ul>li>p,text (in clipboard holder)', () => {
  148. const modelFragment = editor.data.parse( '<ul><li><p>a</p>b</li></ul>', '$clipboardHolder' );
  149. expect( stringifyModel( modelFragment ) )
  150. .to.equal( '<paragraph>a</paragraph>b' );
  151. } );
  152. it( 'should convert td', () => {
  153. const modelFragment = editor.data.parse( '<table><tr><td>a</td><td>b</td></tr><tr><td>c</td><td>d</td></tr></table>' );
  154. expect( stringifyModel( modelFragment ) )
  155. .to.equal( '<paragraph>a</paragraph><paragraph>b</paragraph><paragraph>c</paragraph><paragraph>d</paragraph>' );
  156. } );
  157. it( 'should convert td (in clipboardHolder)', () => {
  158. const modelFragment = editor.data.parse(
  159. '<table><tr><td>a</td><td>b</td></tr><tr><td>c</td><td>d</td></tr></table>',
  160. '$clipboardHolder'
  161. );
  162. expect( stringifyModel( modelFragment ) )
  163. .to.equal( '<paragraph>a</paragraph><paragraph>b</paragraph><paragraph>c</paragraph><paragraph>d</paragraph>' );
  164. } );
  165. it( 'should convert li inside converted container', () => {
  166. doc.schema.registerItem( 'div' );
  167. doc.schema.allow( { name: 'div', inside: '$root' } );
  168. doc.schema.allow( { name: 'paragraph', inside: 'div' } );
  169. buildViewConverter().for( editor.data.viewToModel ).fromElement( 'div' ).toElement( 'div' );
  170. const modelFragment = editor.data.parse( '<div><ul><li>foo</li><li>bar</li></ul></div><div>bom<p>bim</p></div>' );
  171. expect( stringifyModel( modelFragment ) )
  172. .to.equal(
  173. '<div><paragraph>foo</paragraph><paragraph>bar</paragraph></div>' +
  174. '<div><paragraph>bom</paragraph><paragraph>bim</paragraph></div>'
  175. );
  176. } );
  177. it( 'should convert li inside disallowed container', () => {
  178. const modelFragment = editor.data.parse( '<div><ul><li>foo</li><li>bar</li></ul></div><div>bom<p>bim</p></div>' );
  179. expect( stringifyModel( modelFragment ) )
  180. .to.equal(
  181. '<paragraph>foo</paragraph><paragraph>bar</paragraph>' +
  182. '<paragraph>bom</paragraph><paragraph>bim</paragraph>'
  183. );
  184. } );
  185. } );
  186. } );
  187. describe( 'editing pipeline conversion', () => {
  188. it( 'should convert paragraph', () => {
  189. setModelData( doc, '<paragraph>foo</paragraph><paragraph>bar</paragraph>' );
  190. expect( getViewData( editor.editing.view, { withoutSelection: true } ) ).to.equal( '<p>foo</p><p>bar</p>' );
  191. } );
  192. } );
  193. describe( 'autoparagraphing on data load', () => {
  194. it( 'wraps text and place selection at the beginning of that paragraph', () => {
  195. editor.setData( 'foo' );
  196. expect( getModelData( doc ) ).to.equal( '<paragraph>[]foo</paragraph>' );
  197. } );
  198. } );
  199. } );