paragraph.js 17 KB

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