paragraph.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  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 Paragraph from '../src/paragraph';
  6. import ParagraphCommand from '../src/paragraphcommand';
  7. import InsertParagraphCommand from '../src/insertparagraphcommand';
  8. import VirtualTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/virtualtesteditor';
  9. import {
  10. getData as getModelData,
  11. setData as setModelData,
  12. stringify as stringifyModel
  13. } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  14. import { getData as getViewData } from '@ckeditor/ckeditor5-engine/src/dev-utils/view';
  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 model, editor, doc, root;
  19. beforeEach( () => {
  20. return VirtualTestEditor
  21. .create( { plugins: [ Paragraph ] } )
  22. .then( newEditor => {
  23. editor = newEditor;
  24. model = editor.model;
  25. doc = model.document;
  26. root = doc.getRoot();
  27. } );
  28. } );
  29. it( 'should be loaded', () => {
  30. expect( editor.plugins.get( Paragraph ) ).to.be.instanceOf( Paragraph );
  31. } );
  32. it( 'should set proper schema rules', () => {
  33. expect( model.schema.isRegistered( 'paragraph' ) ).to.be.true;
  34. expect( model.schema.checkChild( [ '$root' ], 'paragraph' ) ).to.be.true;
  35. expect( model.schema.checkChild( [ 'paragraph' ], '$text' ) ).to.be.true;
  36. } );
  37. it( 'should have a static paragraphLikeElements property', () => {
  38. expect( Paragraph ).to.have.property( 'paragraphLikeElements' );
  39. } );
  40. describe( 'data pipeline conversions', () => {
  41. it( 'should convert paragraph', () => {
  42. editor.setData( '<p>foobar</p>' );
  43. expect( getModelData( model, { withoutSelection: true } ) ).to.equal( '<paragraph>foobar</paragraph>' );
  44. expect( editor.getData() ).to.equal( '<p>foobar</p>' );
  45. } );
  46. it( 'should convert paragraph only', () => {
  47. editor.setData( '<p>foo<b>baz</b>bar</p>' );
  48. expect( getModelData( model, { withoutSelection: true } ) ).to.equal( '<paragraph>foobazbar</paragraph>' );
  49. expect( editor.getData() ).to.equal( '<p>foobazbar</p>' );
  50. } );
  51. it( 'should convert multiple paragraphs', () => {
  52. editor.setData( '<p>foo</p><p>baz</p>' );
  53. expect( getModelData( model, { withoutSelection: true } ) ).to.equal( '<paragraph>foo</paragraph><paragraph>baz</paragraph>' );
  54. expect( editor.getData() ).to.equal( '<p>foo</p><p>baz</p>' );
  55. } );
  56. describe( 'generic text converter (text autoparagraphing)', () => {
  57. it( 'should autoparagraph text', () => {
  58. editor.setData( 'foo' );
  59. expect( getModelData( model, { withoutSelection: true } ) ).to.equal( '<paragraph>foo</paragraph>' );
  60. expect( editor.getData() ).to.equal( '<p>foo</p>' );
  61. } );
  62. it( 'should autoparagraph any inline element', () => {
  63. editor.model.schema.register( 'span', { allowWhere: '$text' } );
  64. editor.model.schema.extend( '$text', { allowIn: 'span' } );
  65. editor.conversion.for( 'downcast' ).elementToElement( { model: 'span', view: 'span' } );
  66. editor.conversion.for( 'upcast' ).elementToElement( { model: 'span', view: 'span' } );
  67. editor.setData( '<span>foo</span>' );
  68. expect( getModelData( model, { withoutSelection: true } ) ).to.equal( '<paragraph><span>foo</span></paragraph>' );
  69. expect( editor.getData() ).to.equal( '<p><span>foo</span></p>' );
  70. } );
  71. it( 'should not autoparagraph text (in clipboard holder)', () => {
  72. const modelFragment = editor.data.parse( 'foo', [ '$clipboardHolder' ] );
  73. expect( stringifyModel( modelFragment ) )
  74. .to.equal( 'foo' );
  75. } );
  76. it( 'should not autoparagraph text (in a context which does not allow paragraphs', () => {
  77. model.schema.register( 'specialRoot' );
  78. const modelFragment = editor.data.parse( 'foo', [ 'specialRoot' ] );
  79. expect( stringifyModel( modelFragment ) )
  80. .to.equal( '' );
  81. } );
  82. it( 'should autoparagraph text next to allowed element', () => {
  83. model.schema.register( 'heading1', { inheritAllFrom: '$block' } );
  84. editor.conversion.for( 'upcast' ).elementToElement( { model: 'heading1', view: 'h1' } );
  85. const modelFragment = editor.data.parse( '<h1>foo</h1>bar<p>bom</p>' );
  86. expect( stringifyModel( modelFragment ) )
  87. .to.equal( '<heading1>foo</heading1><paragraph>bar</paragraph><paragraph>bom</paragraph>' );
  88. } );
  89. it( 'should autoparagraph 3 inline nodes into one paragraph', () => {
  90. const modelFragment = editor.data.parse( 'foo<b>bar</b>bom' );
  91. expect( stringifyModel( modelFragment ) )
  92. .to.equal( '<paragraph>foobarbom</paragraph>' );
  93. } );
  94. it( 'should not autoparagraph 3 inline nodes (in clipboardHolder)', () => {
  95. const modelFragment = editor.data.parse( 'foo<b>bar</b>bom', [ '$clipboardHolder' ] );
  96. expect( stringifyModel( modelFragment ) )
  97. .to.equal( 'foobarbom' );
  98. } );
  99. it( 'should autoparagraph text inside converted container', () => {
  100. model.schema.register( 'div' );
  101. model.schema.extend( 'div', { allowIn: '$root' } );
  102. model.schema.extend( 'paragraph', { allowIn: 'div' } );
  103. editor.conversion.for( 'upcast' ).elementToElement( { model: 'div', view: 'div' } );
  104. const modelFragment = editor.data.parse( '<div>foo</div><div>bom<p>bim</p></div>' );
  105. expect( stringifyModel( modelFragment ) )
  106. .to.equal(
  107. '<div><paragraph>foo</paragraph></div>' +
  108. '<div><paragraph>bom</paragraph><paragraph>bim</paragraph></div>'
  109. );
  110. } );
  111. it( 'should autoparagraph text inside disallowed element next to allowed element', () => {
  112. model.schema.register( 'heading1', { inheritAllFrom: '$block' } );
  113. editor.conversion.for( 'upcast' ).elementToElement( { model: 'heading1', view: 'h1' } );
  114. const modelFragment = editor.data.parse( '<div><h1>foo</h1>bar</div>' );
  115. expect( stringifyModel( modelFragment ) )
  116. .to.equal( '<heading1>foo</heading1><paragraph>bar</paragraph>' );
  117. } );
  118. it( 'should not autoparagraph text in disallowed element', () => {
  119. model.schema.register( 'heading1', { inheritAllFrom: '$block' } );
  120. editor.conversion.for( 'upcast' ).elementToElement( { model: 'heading1', view: 'h1' } );
  121. const modelFragment = editor.data.parse( '<h1><b>foo</b>bar</h1>' );
  122. expect( stringifyModel( modelFragment ) )
  123. .to.equal( '<heading1>foobar</heading1>' );
  124. } );
  125. it( 'should not fail when text is not allowed in paragraph', () => {
  126. model.schema.addChildCheck( ( ctx, childDef ) => {
  127. if ( ctx.endsWith( '$root paragraph' ) && childDef.name == '$text' ) {
  128. return false;
  129. }
  130. } );
  131. const modelFragment = editor.data.parse( 'foo' );
  132. expect( stringifyModel( modelFragment ) ).to.equal( '' );
  133. } );
  134. it( 'creates normalized model', () => {
  135. const modelFragment = editor.data.parse( 'foo<b>bar</b>bom' );
  136. expect( modelFragment ).to.be.instanceof( ModelDocumentFragment );
  137. expect( modelFragment.getChild( 0 ).childCount ).to.equal( 1 );
  138. expect( modelFragment.getChild( 0 ).getChild( 0 ) ).to.be.instanceOf( ModelText );
  139. } );
  140. // This test was taken from the list package.
  141. it( 'does not break when some converter returns nothing', () => {
  142. editor.data.upcastDispatcher.on( 'element:li', ( evt, data, conversionApi ) => {
  143. conversionApi.consumable.consume( data.viewItem, { name: true } );
  144. }, { priority: 'highest' } );
  145. const modelFragment = editor.data.parse( '<ul><li></li></ul>' );
  146. expect( stringifyModel( modelFragment ) ).to.equal( '' );
  147. } );
  148. describe( 'should not strip attribute elements when autoparagraphing texts', () => {
  149. beforeEach( () => {
  150. model.schema.extend( '$text', { allowAttributes: 'bold' } );
  151. editor.conversion.for( 'upcast' ).elementToAttribute( { view: 'b', model: 'bold' } );
  152. } );
  153. it( 'inside document fragment', () => {
  154. const modelFragment = editor.data.parse( 'foo<b>bar</b>bom' );
  155. expect( stringifyModel( modelFragment ) ).to.equal( '<paragraph>foo<$text bold="true">bar</$text>bom</paragraph>' );
  156. } );
  157. it( 'inside converted element', () => {
  158. model.schema.register( 'blockQuote', { allowIn: '$root' } );
  159. model.schema.extend( '$block', { allowIn: 'blockQuote' } );
  160. editor.conversion.for( 'upcast' ).elementToElement( { model: 'blockQuote', view: 'blockquote' } );
  161. const modelFragment = editor.data.parse( '<blockquote>foo<b>bar</b>bom</blockquote>' );
  162. expect( stringifyModel( modelFragment ) )
  163. .to.equal( '<blockQuote><paragraph>foo<$text bold="true">bar</$text>bom</paragraph></blockQuote>' );
  164. } );
  165. it( 'inside paragraph-like element', () => {
  166. const modelFragment = editor.data.parse( '<h1>foo</h1><h2><b>bar</b>bom</h2>' );
  167. expect( stringifyModel( modelFragment ) )
  168. .to.equal( '<paragraph>foo</paragraph><paragraph><$text bold="true">bar</$text>bom</paragraph>' );
  169. } );
  170. } );
  171. } );
  172. describe( 'generic block converter (paragraph-like element handling)', () => {
  173. it( 'should convert h1+h2', () => {
  174. const modelFragment = editor.data.parse( '<h1>foo</h1><h2>bar</h2>' );
  175. expect( stringifyModel( modelFragment ) )
  176. .to.equal( '<paragraph>foo</paragraph><paragraph>bar</paragraph>' );
  177. } );
  178. it( 'should convert h1+h2 (in clipboard holder)', () => {
  179. const modelFragment = editor.data.parse( '<h1>foo</h1><h2>bar</h2>', [ '$clipboardHolder' ] );
  180. expect( stringifyModel( modelFragment ) )
  181. .to.equal( '<paragraph>foo</paragraph><paragraph>bar</paragraph>' );
  182. } );
  183. it( 'should not convert h1+h2 (in a context which does not allow paragraphs)', () => {
  184. model.schema.register( 'div' );
  185. model.schema.register( 'specialRoot' );
  186. model.schema.extend( 'div', { allowIn: 'specialRoot' } );
  187. model.schema.extend( '$text', { allowIn: 'div' } );
  188. editor.conversion.for( 'upcast' ).elementToElement( { model: 'div', view: 'div' } );
  189. const modelFragment = editor.data.parse( '<h1>foo</h1><h2>bar</h2><div>bom</div>', [ 'specialRoot' ] );
  190. expect( stringifyModel( modelFragment ) )
  191. .to.equal( '<div>bom</div>' );
  192. } );
  193. it( 'should convert ul,ol>li', () => {
  194. const modelFragment = editor.data.parse( '<ul><li>a</li><li>b</li></ul><ol><li>c</li></ol>' );
  195. expect( stringifyModel( modelFragment ) )
  196. .to.equal( '<paragraph>a</paragraph><paragraph>b</paragraph><paragraph>c</paragraph>' );
  197. } );
  198. it( 'should convert ul,ol>li (in clipboard holder)', () => {
  199. const modelFragment = editor.data.parse( '<ul><li>a</li><li>b</li></ul><ol><li>c</li></ol>', [ '$clipboardHolder' ] );
  200. expect( stringifyModel( modelFragment ) )
  201. .to.equal( '<paragraph>a</paragraph><paragraph>b</paragraph><paragraph>c</paragraph>' );
  202. } );
  203. it( 'should convert ul>li>ul>li+li', () => {
  204. const modelFragment = editor.data.parse( '<ul><li>a<ul><li>b</li><li>c</li></ul></li></ul>' );
  205. expect( stringifyModel( modelFragment ) )
  206. .to.equal( '<paragraph>a</paragraph><paragraph>b</paragraph><paragraph>c</paragraph>' );
  207. } );
  208. // "b" is not autoparagraphed because clipboard holder allows text nodes.
  209. // There's a similar integrational test what's going to happen when pasting in paragraph-integration.js.
  210. it( 'should convert ul>li>ul>li+li (in clipboard holder)', () => {
  211. const modelFragment = editor.data.parse( '<ul><li>a<ul><li>b</li><li>c</li></ul></li></ul>', [ '$clipboardHolder' ] );
  212. expect( stringifyModel( modelFragment ) )
  213. .to.equal( '<paragraph>a</paragraph><paragraph>b</paragraph><paragraph>c</paragraph>' );
  214. } );
  215. it( 'should convert ul>li>p,text', () => {
  216. const modelFragment = editor.data.parse( '<ul><li><p>a</p>b</li></ul>' );
  217. expect( stringifyModel( modelFragment ) )
  218. .to.equal( '<paragraph>a</paragraph><paragraph>b</paragraph>' );
  219. } );
  220. it( 'should convert ul>li>p,text (in clipboard holder)', () => {
  221. const modelFragment = editor.data.parse( '<ul><li><p>a</p>b</li></ul>', [ '$clipboardHolder' ] );
  222. expect( stringifyModel( modelFragment ) )
  223. .to.equal( '<paragraph>a</paragraph><paragraph>b</paragraph>' );
  224. } );
  225. it( 'should convert td', () => {
  226. const modelFragment = editor.data.parse(
  227. '<table><tr><td>a</td><td>b</td></tr><tr><td>c</td><td>d</td></tr></table>'
  228. );
  229. expect( stringifyModel( modelFragment ) )
  230. .to.equal( '<paragraph>a</paragraph><paragraph>b</paragraph><paragraph>c</paragraph><paragraph>d</paragraph>' );
  231. } );
  232. it( 'should convert td (in clipboardHolder)', () => {
  233. const modelFragment = editor.data.parse(
  234. '<table><tr><td>a</td><td>b</td></tr><tr><td>c</td><td>d</td></tr></table>',
  235. [ '$clipboardHolder' ]
  236. );
  237. expect( stringifyModel( modelFragment ) )
  238. .to.equal( '<paragraph>a</paragraph><paragraph>b</paragraph><paragraph>c</paragraph><paragraph>d</paragraph>' );
  239. } );
  240. it( 'should convert li inside converted container', () => {
  241. model.schema.register( 'div' );
  242. model.schema.extend( 'div', { allowIn: '$root' } );
  243. model.schema.extend( 'paragraph', { allowIn: 'div' } );
  244. editor.conversion.for( 'upcast' ).elementToElement( { model: 'div', view: 'div' } );
  245. const modelFragment = editor.data.parse( '<div><ul><li>foo</li><li>bar</li></ul></div><div>bom<p>bim</p></div>' );
  246. expect( stringifyModel( modelFragment ) )
  247. .to.equal(
  248. '<div><paragraph>foo</paragraph><paragraph>bar</paragraph></div>' +
  249. '<div><paragraph>bom</paragraph><paragraph>bim</paragraph></div>'
  250. );
  251. } );
  252. it( 'should convert li inside disallowed container', () => {
  253. const modelFragment = editor.data.parse( '<div><ul><li>foo</li><li>bar</li></ul></div><div>bom<p>bim</p></div>' );
  254. expect( stringifyModel( modelFragment ) )
  255. .to.equal(
  256. '<paragraph>foo</paragraph><paragraph>bar</paragraph>' +
  257. '<paragraph>bom</paragraph><paragraph>bim</paragraph>'
  258. );
  259. } );
  260. it( 'creates normalized model', () => {
  261. const modelFragment = editor.data.parse( '<h1><span>foo</span><span>bar</span>' );
  262. expect( stringifyModel( modelFragment ) ).to.equal( '<paragraph>foobar</paragraph>' );
  263. expect( modelFragment ).to.be.instanceof( ModelDocumentFragment );
  264. expect( modelFragment.getChild( 0 ).childCount ).to.equal( 1 );
  265. expect( modelFragment.getChild( 0 ).getChild( 0 ) ).to.be.instanceOf( ModelText );
  266. } );
  267. it( 'should not convert empty elements', () => {
  268. const modelFragment = editor.data.parse( '<ul><li></li><ul>' );
  269. expect( stringifyModel( modelFragment ) ).to.equal( '' );
  270. } );
  271. } );
  272. } );
  273. describe( 'editing pipeline conversion', () => {
  274. it( 'should convert paragraph', () => {
  275. setModelData( model, '<paragraph>foo</paragraph><paragraph>bar</paragraph>' );
  276. expect( getViewData( editor.editing.view, { withoutSelection: true } ) ).to.equal( '<p>foo</p><p>bar</p>' );
  277. } );
  278. } );
  279. describe( 'autoparagraphing on data load', () => {
  280. it( 'wraps text and place selection at the beginning of that paragraph', () => {
  281. editor.setData( 'foo' );
  282. expect( getModelData( model ) ).to.equal( '<paragraph>[]foo</paragraph>' );
  283. } );
  284. } );
  285. describe( 'post-fixing empty roots', () => {
  286. it( 'should fix empty roots after editor is initialised', () => {
  287. expect( doc.getRoot().childCount ).to.equal( 1 );
  288. expect( doc.getRoot().getChild( 0 ).is( 'paragraph' ) ).to.be.true;
  289. } );
  290. it( 'should fix root if it becomes empty', () => {
  291. editor.setData( '<p>Foobar</p>' );
  292. // Since `setData` first removes all contents from editor and then sets content during same enqueue
  293. // change block, the line below checks whether fixing empty roots does not kick too early and does not
  294. // fix root if it is not needed.
  295. expect( editor.getData() ).to.equal( '<p>Foobar</p>' );
  296. model.change( writer => {
  297. writer.remove( writer.createRangeIn( root ) );
  298. } );
  299. expect( doc.getRoot().childCount ).to.equal( 1 );
  300. expect( doc.getRoot().getChild( 0 ).is( 'paragraph' ) ).to.be.true;
  301. } );
  302. it( 'should not fix root which does not allow paragraph', () => {
  303. model.schema.addChildCheck( ( ctx, childDef ) => {
  304. if ( ctx.endsWith( '$root' ) && childDef.name == 'paragraph' ) {
  305. return false;
  306. }
  307. } );
  308. model.change( writer => {
  309. writer.remove( writer.createRangeIn( root ) );
  310. } );
  311. expect( editor.getData() ).to.equal( '' );
  312. } );
  313. it( 'should fix empty roots in the right batch', () => {
  314. let removeBatch, attributeBatch;
  315. model.enqueueChange( writer => {
  316. removeBatch = writer.batch;
  317. writer.remove( writer.createRangeIn( root ) );
  318. model.enqueueChange( writer => {
  319. attributeBatch = writer.batch;
  320. writer.setAttribute( 'foo', 'bar', root );
  321. } );
  322. } );
  323. expect( Array.from( removeBatch.operations, operation => operation.type ) ).to.include.members( [ 'insert' ] );
  324. expect( Array.from( attributeBatch.operations, operation => operation.type ) ).to.not.include.members( [ 'insert' ] );
  325. } );
  326. } );
  327. describe( 'commands', () => {
  328. it( '"paragraph" command should be registered in the editor', () => {
  329. expect( editor.commands.get( 'paragraph' ) ).to.be.instanceof( ParagraphCommand );
  330. } );
  331. it( '"insertParagraph" command should be registered in the editor', () => {
  332. expect( editor.commands.get( 'insertParagraph' ) ).to.be.instanceof( InsertParagraphCommand );
  333. } );
  334. } );
  335. } );