paragraph.js 18 KB

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