paragraph.js 17 KB

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