8
0

paragraph.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. /**
  2. * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import Paragraph from '../src/paragraph';
  6. import ParagraphCommand from '../src/paragraphcommand';
  7. import VirtualTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/virtualtesteditor';
  8. import {
  9. getData as getModelData,
  10. setData as setModelData,
  11. stringify as stringifyModel
  12. } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  13. import { getData as getViewData } from '@ckeditor/ckeditor5-engine/src/dev-utils/view';
  14. import buildViewConverter from '@ckeditor/ckeditor5-engine/src/conversion/buildviewconverter';
  15. import ModelDocumentFragment from '@ckeditor/ckeditor5-engine/src/model/documentfragment';
  16. import ModelText from '@ckeditor/ckeditor5-engine/src/model/text';
  17. describe( 'Paragraph feature', () => {
  18. let editor, doc;
  19. beforeEach( () => {
  20. return VirtualTestEditor.create( {
  21. plugins: [ Paragraph ]
  22. } )
  23. .then( newEditor => {
  24. editor = newEditor;
  25. doc = editor.document;
  26. } );
  27. } );
  28. it( 'should be loaded', () => {
  29. expect( editor.plugins.get( Paragraph ) ).to.be.instanceOf( Paragraph );
  30. } );
  31. it( 'should set proper schema rules', () => {
  32. expect( doc.schema.hasItem( 'paragraph' ) ).to.be.true;
  33. expect( doc.schema.check( { name: 'paragraph', inside: '$root' } ) ).to.be.true;
  34. expect( doc.schema.check( { name: '$inline', inside: 'paragraph' } ) ).to.be.true;
  35. } );
  36. it( 'should have a static paragraphLikeElements property', () => {
  37. expect( Paragraph ).to.have.property( 'paragraphLikeElements' );
  38. } );
  39. describe( 'data pipeline conversions', () => {
  40. it( 'should convert paragraph', () => {
  41. editor.setData( '<p>foobar</p>' );
  42. expect( getModelData( doc, { withoutSelection: true } ) ).to.equal( '<paragraph>foobar</paragraph>' );
  43. expect( editor.getData() ).to.equal( '<p>foobar</p>' );
  44. } );
  45. it( 'should convert paragraph only', () => {
  46. editor.setData( '<p>foo<b>baz</b>bar</p>' );
  47. expect( getModelData( doc, { withoutSelection: true } ) ).to.equal( '<paragraph>foobazbar</paragraph>' );
  48. expect( editor.getData() ).to.equal( '<p>foobazbar</p>' );
  49. } );
  50. it( 'should convert multiple paragraphs', () => {
  51. editor.setData( '<p>foo</p><p>baz</p>' );
  52. expect( getModelData( doc, { withoutSelection: true } ) ).to.equal( '<paragraph>foo</paragraph><paragraph>baz</paragraph>' );
  53. expect( editor.getData() ).to.equal( '<p>foo</p><p>baz</p>' );
  54. } );
  55. describe( 'generic text converter (text autoparagraphing)', () => {
  56. it( 'should autoparagraph text', () => {
  57. editor.setData( 'foo' );
  58. expect( getModelData( doc, { withoutSelection: true } ) ).to.equal( '<paragraph>foo</paragraph>' );
  59. expect( editor.getData() ).to.equal( '<p>foo</p>' );
  60. } );
  61. it( 'should not autoparagraph text (in clipboard holder)', () => {
  62. const modelFragment = editor.data.parse( 'foo', '$clipboardHolder' );
  63. expect( stringifyModel( modelFragment ) )
  64. .to.equal( 'foo' );
  65. } );
  66. it( 'should not autoparagraph text (in a context which does not allow paragraphs', () => {
  67. doc.schema.registerItem( 'specialRoot' );
  68. const modelFragment = editor.data.parse( 'foo', 'specialRoot' );
  69. expect( stringifyModel( modelFragment ) )
  70. .to.equal( '' );
  71. } );
  72. it( 'should autoparagraph text next to allowed element', () => {
  73. doc.schema.registerItem( 'heading1', '$block' );
  74. buildViewConverter().for( editor.data.viewToModel ).fromElement( 'h1' ).toElement( 'heading1' );
  75. const modelFragment = editor.data.parse( '<h1>foo</h1>bar<p>bom</p>' );
  76. expect( stringifyModel( modelFragment ) )
  77. .to.equal( '<heading1>foo</heading1><paragraph>bar</paragraph><paragraph>bom</paragraph>' );
  78. } );
  79. it( 'should autoparagraph 3 inline nodes into one paragraph', () => {
  80. const modelFragment = editor.data.parse( 'foo<b>bar</b>bom' );
  81. expect( stringifyModel( modelFragment ) )
  82. .to.equal( '<paragraph>foobarbom</paragraph>' );
  83. } );
  84. it( 'should not autoparagraph 3 inline nodes (in clipboardHolder)', () => {
  85. const modelFragment = editor.data.parse( 'foo<b>bar</b>bom', '$clipboardHolder' );
  86. expect( stringifyModel( modelFragment ) )
  87. .to.equal( 'foobarbom' );
  88. } );
  89. it( 'should autoparagraph text inside converted container', () => {
  90. doc.schema.registerItem( 'div' );
  91. doc.schema.allow( { name: 'div', inside: '$root' } );
  92. doc.schema.allow( { name: 'paragraph', inside: 'div' } );
  93. buildViewConverter().for( editor.data.viewToModel ).fromElement( 'div' ).toElement( 'div' );
  94. const modelFragment = editor.data.parse( '<div>foo</div><div>bom<p>bim</p></div>' );
  95. expect( stringifyModel( modelFragment ) )
  96. .to.equal(
  97. '<div><paragraph>foo</paragraph></div>' +
  98. '<div><paragraph>bom</paragraph><paragraph>bim</paragraph></div>'
  99. );
  100. } );
  101. it( 'should autoparagraph text inside disallowed element next to allowed element', () => {
  102. doc.schema.registerItem( 'heading1', '$block' );
  103. buildViewConverter().for( editor.data.viewToModel ).fromElement( 'h1' ).toElement( 'heading1' );
  104. const modelFragment = editor.data.parse( '<div><h1>foo</h1>bar</div>' );
  105. expect( stringifyModel( modelFragment ) )
  106. .to.equal( '<heading1>foo</heading1><paragraph>bar</paragraph>' );
  107. } );
  108. it( 'should not autoparagraph text in disallowed element', () => {
  109. doc.schema.registerItem( 'heading1', '$block' );
  110. buildViewConverter().for( editor.data.viewToModel ).fromElement( 'h1' ).toElement( 'heading1' );
  111. const modelFragment = editor.data.parse( '<h1><b>foo</b>bar</h1>' );
  112. expect( stringifyModel( modelFragment ) )
  113. .to.equal( '<heading1>foobar</heading1>' );
  114. } );
  115. it( 'should not fail when text is not allowed in paragraph', () => {
  116. doc.schema.disallow( { name: '$text', inside: [ '$root', 'paragraph' ] } );
  117. const modelFragment = editor.data.parse( 'foo' );
  118. expect( stringifyModel( modelFragment ) ).to.equal( '' );
  119. } );
  120. it( 'creates normalized model', () => {
  121. const modelFragment = editor.data.parse( 'foo<b>bar</b>bom' );
  122. expect( modelFragment ).to.be.instanceof( ModelDocumentFragment );
  123. expect( modelFragment.getChild( 0 ).childCount ).to.equal( 1 );
  124. expect( modelFragment.getChild( 0 ).getChild( 0 ) ).to.be.instanceOf( ModelText );
  125. } );
  126. it( 'does not break converting inline elements', () => {
  127. doc.schema.allow( { name: '$inline', attributes: [ 'bold' ] } );
  128. buildViewConverter().for( editor.data.viewToModel )
  129. .fromElement( 'b' )
  130. .toAttribute( 'bold', true );
  131. const modelFragment = editor.data.parse( 'foo<b>bar</b>bom' );
  132. // The result of this test is wrong due to https://github.com/ckeditor/ckeditor5-paragraph/issues/10.
  133. // It's meant to catch the odd situation in mergeSubsequentParagraphs when data.output may be an array
  134. // for a while
  135. expect( stringifyModel( modelFragment ) ).to.equal( '<paragraph>foobarbom</paragraph>' );
  136. } );
  137. // This test was taken from the list package.
  138. it( 'does not break when some converter returns nothing', () => {
  139. editor.data.viewToModel.on( 'element:li', ( evt, data, consumable ) => {
  140. consumable.consume( data.input, { name: true } );
  141. }, { priority: 'highest' } );
  142. const modelFragment = editor.data.parse( '<ul><li></li></ul>' );
  143. expect( stringifyModel( modelFragment ) ).to.equal( '' );
  144. } );
  145. } );
  146. describe( 'generic block converter (paragraph-like element handling)', () => {
  147. it( 'should convert h1+h2', () => {
  148. const modelFragment = editor.data.parse( '<h1>foo</h1><h2>bar</h2>' );
  149. expect( stringifyModel( modelFragment ) )
  150. .to.equal( '<paragraph>foo</paragraph><paragraph>bar</paragraph>' );
  151. } );
  152. it( 'should convert h1+h2 (in clipboard holder)', () => {
  153. const modelFragment = editor.data.parse( '<h1>foo</h1><h2>bar</h2>', '$clipboardHolder' );
  154. expect( stringifyModel( modelFragment ) )
  155. .to.equal( '<paragraph>foo</paragraph><paragraph>bar</paragraph>' );
  156. } );
  157. it( 'should not convert h1+h2 (in a context which does not allow paragraphs)', () => {
  158. doc.schema.registerItem( 'div' );
  159. doc.schema.registerItem( 'specialRoot' );
  160. doc.schema.allow( { name: 'div', inside: 'specialRoot' } );
  161. doc.schema.allow( { name: '$text', inside: 'div' } );
  162. buildViewConverter().for( editor.data.viewToModel ).fromElement( 'div' ).toElement( 'div' );
  163. const modelFragment = editor.data.parse( '<h1>foo</h1><h2>bar</h2><div>bom</div>', 'specialRoot' );
  164. expect( stringifyModel( modelFragment ) )
  165. .to.equal( '<div>bom</div>' );
  166. } );
  167. it( 'should convert ul,ol>li', () => {
  168. const modelFragment = editor.data.parse( '<ul><li>a</li><li>b</li></ul><ol><li>c</li></ol>' );
  169. expect( stringifyModel( modelFragment ) )
  170. .to.equal( '<paragraph>a</paragraph><paragraph>b</paragraph><paragraph>c</paragraph>' );
  171. } );
  172. it( 'should convert ul,ol>li (in clipboard holder)', () => {
  173. const modelFragment = editor.data.parse( '<ul><li>a</li><li>b</li></ul><ol><li>c</li></ol>', '$clipboardHolder' );
  174. expect( stringifyModel( modelFragment ) )
  175. .to.equal( '<paragraph>a</paragraph><paragraph>b</paragraph><paragraph>c</paragraph>' );
  176. } );
  177. it( 'should convert ul>li>ul>li+li', () => {
  178. const modelFragment = editor.data.parse( '<ul><li>a<ul><li>b</li><li>c</li></ul></li></ul>' );
  179. expect( stringifyModel( modelFragment ) )
  180. .to.equal( '<paragraph>a</paragraph><paragraph>b</paragraph><paragraph>c</paragraph>' );
  181. } );
  182. // "b" is not autoparagraphed because clipboard holder allows text nodes.
  183. // There's a similar integrational test what's going to happen when pasting in paragraph-integration.js.
  184. it( 'should convert ul>li>ul>li+li (in clipboard holder)', () => {
  185. const modelFragment = editor.data.parse( '<ul><li>a<ul><li>b</li><li>c</li></ul></li></ul>', '$clipboardHolder' );
  186. expect( stringifyModel( modelFragment ) )
  187. .to.equal( 'a<paragraph>b</paragraph><paragraph>c</paragraph>' );
  188. } );
  189. it( 'should convert ul>li>p,text', () => {
  190. const modelFragment = editor.data.parse( '<ul><li><p>a</p>b</li></ul>' );
  191. expect( stringifyModel( modelFragment ) )
  192. .to.equal( '<paragraph>a</paragraph><paragraph>b</paragraph>' );
  193. } );
  194. // "b" is not autoparagraphed because clipboard holder allows text nodes.
  195. // There's a similar integrational test what's going to happen when pasting in paragraph-integration.js.
  196. it( 'should convert ul>li>p,text (in clipboard holder)', () => {
  197. const modelFragment = editor.data.parse( '<ul><li><p>a</p>b</li></ul>', '$clipboardHolder' );
  198. expect( stringifyModel( modelFragment ) )
  199. .to.equal( '<paragraph>a</paragraph>b' );
  200. } );
  201. it( 'should convert td', () => {
  202. const modelFragment = editor.data.parse( '<table><tr><td>a</td><td>b</td></tr><tr><td>c</td><td>d</td></tr></table>' );
  203. expect( stringifyModel( modelFragment ) )
  204. .to.equal( '<paragraph>a</paragraph><paragraph>b</paragraph><paragraph>c</paragraph><paragraph>d</paragraph>' );
  205. } );
  206. it( 'should convert td (in clipboardHolder)', () => {
  207. const modelFragment = editor.data.parse(
  208. '<table><tr><td>a</td><td>b</td></tr><tr><td>c</td><td>d</td></tr></table>',
  209. '$clipboardHolder'
  210. );
  211. expect( stringifyModel( modelFragment ) )
  212. .to.equal( '<paragraph>a</paragraph><paragraph>b</paragraph><paragraph>c</paragraph><paragraph>d</paragraph>' );
  213. } );
  214. it( 'should convert li inside converted container', () => {
  215. doc.schema.registerItem( 'div' );
  216. doc.schema.allow( { name: 'div', inside: '$root' } );
  217. doc.schema.allow( { name: 'paragraph', inside: 'div' } );
  218. buildViewConverter().for( editor.data.viewToModel ).fromElement( 'div' ).toElement( 'div' );
  219. const modelFragment = editor.data.parse( '<div><ul><li>foo</li><li>bar</li></ul></div><div>bom<p>bim</p></div>' );
  220. expect( stringifyModel( modelFragment ) )
  221. .to.equal(
  222. '<div><paragraph>foo</paragraph><paragraph>bar</paragraph></div>' +
  223. '<div><paragraph>bom</paragraph><paragraph>bim</paragraph></div>'
  224. );
  225. } );
  226. it( 'should convert li inside disallowed container', () => {
  227. const modelFragment = editor.data.parse( '<div><ul><li>foo</li><li>bar</li></ul></div><div>bom<p>bim</p></div>' );
  228. expect( stringifyModel( modelFragment ) )
  229. .to.equal(
  230. '<paragraph>foo</paragraph><paragraph>bar</paragraph>' +
  231. '<paragraph>bom</paragraph><paragraph>bim</paragraph>'
  232. );
  233. } );
  234. it( 'creates normalized model', () => {
  235. const modelFragment = editor.data.parse( '<h1><span>foo</span><span>bar</span>' );
  236. expect( stringifyModel( modelFragment ) ).to.equal( '<paragraph>foobar</paragraph>' );
  237. expect( modelFragment ).to.be.instanceof( ModelDocumentFragment );
  238. expect( modelFragment.getChild( 0 ).childCount ).to.equal( 1 );
  239. expect( modelFragment.getChild( 0 ).getChild( 0 ) ).to.be.instanceOf( ModelText );
  240. } );
  241. } );
  242. } );
  243. describe( 'editing pipeline conversion', () => {
  244. it( 'should convert paragraph', () => {
  245. setModelData( doc, '<paragraph>foo</paragraph><paragraph>bar</paragraph>' );
  246. expect( getViewData( editor.editing.view, { withoutSelection: true } ) ).to.equal( '<p>foo</p><p>bar</p>' );
  247. } );
  248. } );
  249. describe( 'autoparagraphing on data load', () => {
  250. it( 'wraps text and place selection at the beginning of that paragraph', () => {
  251. editor.setData( 'foo' );
  252. expect( getModelData( doc ) ).to.equal( '<paragraph>[]foo</paragraph>' );
  253. } );
  254. } );
  255. describe( 'command', () => {
  256. it( 'should be set in the editor', () => {
  257. expect( editor.commands.get( 'paragraph' ) ).to.be.instanceof( ParagraphCommand );
  258. } );
  259. } );
  260. } );