8
0

paragraph.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  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 buildModelConverter from '@ckeditor/ckeditor5-engine/src/conversion/buildmodelconverter';
  15. import buildViewConverter from '@ckeditor/ckeditor5-engine/src/conversion/buildviewconverter';
  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. buildModelConverter().for( editor.editing.modelToView, editor.data.modelToView ).fromElement( 'span' ).toElement( 'span' );
  68. buildViewConverter().for( editor.data.viewToModel ).fromElement( 'span' ).toElement( '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. buildViewConverter().for( editor.data.viewToModel ).fromElement( 'h1' ).toElement( 'heading1' );
  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. buildViewConverter().for( editor.data.viewToModel ).fromElement( 'div' ).toElement( '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. buildViewConverter().for( editor.data.viewToModel ).fromElement( 'h1' ).toElement( 'heading1' );
  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. buildViewConverter().for( editor.data.viewToModel ).fromElement( 'h1' ).toElement( 'heading1' );
  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.on( 'checkChild', ( evt, args ) => {
  129. const def = model.schema.getDefinition( args[ 1 ] );
  130. if ( args[ 0 ].endsWith( '$root paragraph' ) && def.name == '$text' ) {
  131. evt.stop();
  132. evt.return = false;
  133. }
  134. } );
  135. const modelFragment = editor.data.parse( 'foo' );
  136. expect( stringifyModel( modelFragment ) ).to.equal( '' );
  137. } );
  138. it( 'creates normalized model', () => {
  139. const modelFragment = editor.data.parse( 'foo<b>bar</b>bom' );
  140. expect( modelFragment ).to.be.instanceof( ModelDocumentFragment );
  141. expect( modelFragment.getChild( 0 ).childCount ).to.equal( 1 );
  142. expect( modelFragment.getChild( 0 ).getChild( 0 ) ).to.be.instanceOf( ModelText );
  143. } );
  144. // This test was taken from the list package.
  145. it( 'does not break when some converter returns nothing', () => {
  146. editor.data.viewToModel.on( 'element:li', ( evt, data, consumable ) => {
  147. consumable.consume( data.input, { name: true } );
  148. }, { priority: 'highest' } );
  149. const modelFragment = editor.data.parse( '<ul><li></li></ul>' );
  150. expect( stringifyModel( modelFragment ) ).to.equal( '' );
  151. } );
  152. describe( 'should not strip attribute elements when autoparagraphing texts', () => {
  153. beforeEach( () => {
  154. model.schema.extend( '$text', { allowAttributes: 'bold' } );
  155. buildViewConverter().for( editor.data.viewToModel )
  156. .fromElement( 'b' )
  157. .toAttribute( 'bold', true );
  158. } );
  159. it( 'inside document fragment', () => {
  160. const modelFragment = editor.data.parse( 'foo<b>bar</b>bom' );
  161. expect( stringifyModel( modelFragment ) ).to.equal( '<paragraph>foo<$text bold="true">bar</$text>bom</paragraph>' );
  162. } );
  163. it( 'inside converted element', () => {
  164. model.schema.register( 'blockQuote', { allowIn: '$root' } );
  165. model.schema.extend( '$block', { allowIn: 'blockQuote' } );
  166. buildViewConverter().for( editor.data.viewToModel ).fromElement( 'blockquote' ).toElement( 'blockQuote' );
  167. const modelFragment = editor.data.parse( '<blockquote>foo<b>bar</b>bom</blockquote>' );
  168. expect( stringifyModel( modelFragment ) )
  169. .to.equal( '<blockQuote><paragraph>foo<$text bold="true">bar</$text>bom</paragraph></blockQuote>' );
  170. } );
  171. it( 'inside paragraph-like element', () => {
  172. const modelFragment = editor.data.parse( '<h1>foo</h1><h2><b>bar</b>bom</h2>' );
  173. expect( stringifyModel( modelFragment ) )
  174. .to.equal( '<paragraph>foo</paragraph><paragraph><$text bold="true">bar</$text>bom</paragraph>' );
  175. } );
  176. } );
  177. } );
  178. describe( 'generic block converter (paragraph-like element handling)', () => {
  179. it( 'should convert h1+h2', () => {
  180. const modelFragment = editor.data.parse( '<h1>foo</h1><h2>bar</h2>' );
  181. expect( stringifyModel( modelFragment ) )
  182. .to.equal( '<paragraph>foo</paragraph><paragraph>bar</paragraph>' );
  183. } );
  184. it( 'should convert h1+h2 (in clipboard holder)', () => {
  185. const modelFragment = editor.data.parse( '<h1>foo</h1><h2>bar</h2>', '$clipboardHolder' );
  186. expect( stringifyModel( modelFragment ) )
  187. .to.equal( '<paragraph>foo</paragraph><paragraph>bar</paragraph>' );
  188. } );
  189. it( 'should not convert h1+h2 (in a context which does not allow paragraphs)', () => {
  190. model.schema.register( 'div' );
  191. model.schema.register( 'specialRoot' );
  192. model.schema.extend( 'div', { allowIn: 'specialRoot' } );
  193. model.schema.extend( '$text', { allowIn: 'div' } );
  194. buildViewConverter().for( editor.data.viewToModel ).fromElement( 'div' ).toElement( 'div' );
  195. const modelFragment = editor.data.parse( '<h1>foo</h1><h2>bar</h2><div>bom</div>', 'specialRoot' );
  196. expect( stringifyModel( modelFragment ) )
  197. .to.equal( '<div>bom</div>' );
  198. } );
  199. it( 'should convert ul,ol>li', () => {
  200. const modelFragment = editor.data.parse( '<ul><li>a</li><li>b</li></ul><ol><li>c</li></ol>' );
  201. expect( stringifyModel( modelFragment ) )
  202. .to.equal( '<paragraph>a</paragraph><paragraph>b</paragraph><paragraph>c</paragraph>' );
  203. } );
  204. it( 'should convert ul,ol>li (in clipboard holder)', () => {
  205. const modelFragment = editor.data.parse( '<ul><li>a</li><li>b</li></ul><ol><li>c</li></ol>', '$clipboardHolder' );
  206. expect( stringifyModel( modelFragment ) )
  207. .to.equal( '<paragraph>a</paragraph><paragraph>b</paragraph><paragraph>c</paragraph>' );
  208. } );
  209. it( 'should convert ul>li>ul>li+li', () => {
  210. const modelFragment = editor.data.parse( '<ul><li>a<ul><li>b</li><li>c</li></ul></li></ul>' );
  211. expect( stringifyModel( modelFragment ) )
  212. .to.equal( '<paragraph>a</paragraph><paragraph>b</paragraph><paragraph>c</paragraph>' );
  213. } );
  214. // "b" is not autoparagraphed because clipboard holder allows text nodes.
  215. // There's a similar integrational test what's going to happen when pasting in paragraph-integration.js.
  216. it( 'should convert ul>li>ul>li+li (in clipboard holder)', () => {
  217. const modelFragment = editor.data.parse( '<ul><li>a<ul><li>b</li><li>c</li></ul></li></ul>', '$clipboardHolder' );
  218. expect( stringifyModel( modelFragment ) )
  219. .to.equal( '<paragraph>a</paragraph><paragraph>b</paragraph><paragraph>c</paragraph>' );
  220. } );
  221. it( 'should convert ul>li>p,text', () => {
  222. const modelFragment = editor.data.parse( '<ul><li><p>a</p>b</li></ul>' );
  223. expect( stringifyModel( modelFragment ) )
  224. .to.equal( '<paragraph>a</paragraph><paragraph>b</paragraph>' );
  225. } );
  226. // "b" is not autoparagraphed because clipboard holder allows text nodes.
  227. // There's a similar integrational test what's going to happen when pasting in paragraph-integration.js.
  228. it( 'should convert ul>li>p,text (in clipboard holder)', () => {
  229. const modelFragment = editor.data.parse( '<ul><li><p>a</p>b</li></ul>', '$clipboardHolder' );
  230. expect( stringifyModel( modelFragment ) )
  231. .to.equal( '<paragraph>a</paragraph><paragraph>b</paragraph>' );
  232. } );
  233. it( 'should convert td', () => {
  234. const modelFragment = editor.data.parse(
  235. '<table><tr><td>a</td><td>b</td></tr><tr><td>c</td><td>d</td></tr></table>'
  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 td (in clipboardHolder)', () => {
  241. const modelFragment = editor.data.parse(
  242. '<table><tr><td>a</td><td>b</td></tr><tr><td>c</td><td>d</td></tr></table>',
  243. '$clipboardHolder'
  244. );
  245. expect( stringifyModel( modelFragment ) )
  246. .to.equal( '<paragraph>a</paragraph><paragraph>b</paragraph><paragraph>c</paragraph><paragraph>d</paragraph>' );
  247. } );
  248. it( 'should convert li inside converted container', () => {
  249. model.schema.register( 'div' );
  250. model.schema.extend( 'div', { allowIn: '$root' } );
  251. model.schema.extend( 'paragraph', { allowIn: 'div' } );
  252. buildViewConverter().for( editor.data.viewToModel ).fromElement( 'div' ).toElement( 'div' );
  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. '<div><paragraph>foo</paragraph><paragraph>bar</paragraph></div>' +
  257. '<div><paragraph>bom</paragraph><paragraph>bim</paragraph></div>'
  258. );
  259. } );
  260. it( 'should convert li inside disallowed container', () => {
  261. const modelFragment = editor.data.parse( '<div><ul><li>foo</li><li>bar</li></ul></div><div>bom<p>bim</p></div>' );
  262. expect( stringifyModel( modelFragment ) )
  263. .to.equal(
  264. '<paragraph>foo</paragraph><paragraph>bar</paragraph>' +
  265. '<paragraph>bom</paragraph><paragraph>bim</paragraph>'
  266. );
  267. } );
  268. it( 'creates normalized model', () => {
  269. const modelFragment = editor.data.parse( '<h1><span>foo</span><span>bar</span>' );
  270. expect( stringifyModel( modelFragment ) ).to.equal( '<paragraph>foobar</paragraph>' );
  271. expect( modelFragment ).to.be.instanceof( ModelDocumentFragment );
  272. expect( modelFragment.getChild( 0 ).childCount ).to.equal( 1 );
  273. expect( modelFragment.getChild( 0 ).getChild( 0 ) ).to.be.instanceOf( ModelText );
  274. } );
  275. } );
  276. } );
  277. describe( 'editing pipeline conversion', () => {
  278. it( 'should convert paragraph', () => {
  279. setModelData( model, '<paragraph>foo</paragraph><paragraph>bar</paragraph>' );
  280. expect( getViewData( editor.editing.view, { withoutSelection: true } ) ).to.equal( '<p>foo</p><p>bar</p>' );
  281. } );
  282. } );
  283. describe( 'autoparagraphing on data load', () => {
  284. it( 'wraps text and place selection at the beginning of that paragraph', () => {
  285. editor.setData( 'foo' );
  286. expect( getModelData( model ) ).to.equal( '<paragraph>[]foo</paragraph>' );
  287. } );
  288. } );
  289. describe( 'post-fixing empty roots', () => {
  290. it( 'should fix empty roots after editor is initialised', () => {
  291. expect( doc.getRoot().childCount ).to.equal( 1 );
  292. expect( doc.getRoot().getChild( 0 ).is( 'paragraph' ) ).to.be.true;
  293. } );
  294. it( 'should fix root if it becomes empty', () => {
  295. editor.setData( '<p>Foobar</p>' );
  296. // Since `setData` first removes all contents from editor and then sets content during same enqueue
  297. // change block, the line below checks whether fixing empty roots does not kick too early and does not
  298. // fix root if it is not needed.
  299. expect( editor.getData() ).to.equal( '<p>Foobar</p>' );
  300. model.change( writer => {
  301. writer.remove( ModelRange.createIn( root ) );
  302. } );
  303. expect( doc.getRoot().childCount ).to.equal( 1 );
  304. expect( doc.getRoot().getChild( 0 ).is( 'paragraph' ) ).to.be.true;
  305. } );
  306. it( 'should not fix root if it got content during changesDone event', () => {
  307. // "Autoheading feature".
  308. model.schema.register( 'heading', { inheritAllFrom: '$block' } );
  309. buildModelConverter().for( editor.editing.modelToView, editor.data.modelToView )
  310. .fromElement( 'heading' )
  311. .toElement( 'h1' );
  312. doc.on( 'changesDone', () => {
  313. const root = doc.getRoot();
  314. if ( root.isEmpty ) {
  315. model.change( writer => {
  316. writer.insertElement( 'heading', root );
  317. } );
  318. }
  319. } );
  320. model.change( writer => {
  321. writer.remove( ModelRange.createIn( root ) );
  322. } );
  323. expect( doc.getRoot().childCount ).to.equal( 1 );
  324. expect( doc.getRoot().getChild( 0 ).name ).to.equal( 'heading' );
  325. } );
  326. it( 'should not fix root which does not allow paragraph', () => {
  327. model.schema.on( 'checkChild', ( evt, args ) => {
  328. const def = model.schema.getDefinition( args[ 1 ] );
  329. if ( args[ 0 ].endsWith( '$root' ) && def.name == 'paragraph' ) {
  330. evt.stop();
  331. evt.return = false;
  332. }
  333. } );
  334. model.change( writer => {
  335. writer.remove( ModelRange.createIn( root ) );
  336. } );
  337. expect( editor.getData() ).to.equal( '' );
  338. } );
  339. it( 'should not fix root if change was in transparent batch', () => {
  340. editor.setData( '<p>Foobar</p>' );
  341. model.enqueueChange( 'transparent', writer => {
  342. writer.remove( ModelRange.createIn( root ) );
  343. } );
  344. expect( editor.getData() ).to.equal( '' );
  345. } );
  346. it( 'should fix empty roots in the right batch', () => {
  347. let removeBatch, attributeBatch;
  348. model.enqueueChange( writer => {
  349. removeBatch = writer.batch;
  350. writer.remove( ModelRange.createIn( root ) );
  351. model.enqueueChange( writer => {
  352. attributeBatch = writer.batch;
  353. writer.setAttribute( 'foo', 'bar', root );
  354. } );
  355. } );
  356. expect( Array.from( removeBatch.deltas, delta => delta.type ) ).to.include.members( [ 'insert' ] );
  357. expect( Array.from( attributeBatch.deltas, delta => delta.type ) ).to.not.include.members( [ 'insert' ] );
  358. } );
  359. } );
  360. describe( 'command', () => {
  361. it( 'should be set in the editor', () => {
  362. expect( editor.commands.get( 'paragraph' ) ).to.be.instanceof( ParagraphCommand );
  363. } );
  364. } );
  365. } );