8
0

paragraph.js 18 KB

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