paragraph.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  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.hasItem( 'paragraph' ) ).to.be.true;
  36. expect( model.schema.check( { name: 'paragraph', inside: '$root' } ) ).to.be.true;
  37. expect( model.schema.check( { name: '$inline', 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.registerItem( 'span', '$inline' );
  66. editor.model.schema.allow( { name: '$text', inside: '$inline' } );
  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.registerItem( '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.registerItem( 'heading1', '$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.registerItem( 'div' );
  103. model.schema.allow( { name: 'div', inside: '$root' } );
  104. model.schema.allow( { name: 'paragraph', inside: '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.registerItem( 'heading1', '$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.registerItem( 'heading1', '$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.allow( { name: '$inline', attributes: [ '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.registerItem( 'blockquote' );
  159. model.schema.allow( { name: 'blockquote', inside: '$root' } );
  160. model.schema.allow( { name: '$block', inside: 'blockquote' } );
  161. buildModelConverter().for( editor.editing.modelToView, editor.data.modelToView )
  162. .fromElement( 'blockquote' )
  163. .toElement( 'blockquote' );
  164. buildViewConverter().for( editor.data.viewToModel ).fromElement( 'blockquote' ).toElement( 'blockquote' );
  165. const modelFragment = editor.data.parse( '<blockquote>foo<b>bar</b>bom</blockquote>' );
  166. expect( stringifyModel( modelFragment ) )
  167. .to.equal( '<blockquote><paragraph>foo<$text bold="true">bar</$text>bom</paragraph></blockquote>' );
  168. } );
  169. it( 'inside paragraph-like element', () => {
  170. const modelFragment = editor.data.parse( '<h1>foo</h1><h2><b>bar</b>bom</h2>' );
  171. expect( stringifyModel( modelFragment ) )
  172. .to.equal( '<paragraph>foo</paragraph><paragraph><$text bold="true">bar</$text>bom</paragraph>' );
  173. } );
  174. } );
  175. } );
  176. describe( 'generic block converter (paragraph-like element handling)', () => {
  177. it( 'should convert h1+h2', () => {
  178. const modelFragment = editor.data.parse( '<h1>foo</h1><h2>bar</h2>' );
  179. expect( stringifyModel( modelFragment ) )
  180. .to.equal( '<paragraph>foo</paragraph><paragraph>bar</paragraph>' );
  181. } );
  182. it( 'should convert h1+h2 (in clipboard holder)', () => {
  183. const modelFragment = editor.data.parse( '<h1>foo</h1><h2>bar</h2>', '$clipboardHolder' );
  184. expect( stringifyModel( modelFragment ) )
  185. .to.equal( '<paragraph>foo</paragraph><paragraph>bar</paragraph>' );
  186. } );
  187. it( 'should not convert h1+h2 (in a context which does not allow paragraphs)', () => {
  188. model.schema.registerItem( 'div' );
  189. model.schema.registerItem( 'specialRoot' );
  190. model.schema.allow( { name: 'div', inside: 'specialRoot' } );
  191. model.schema.allow( { name: '$text', inside: 'div' } );
  192. buildViewConverter().for( editor.data.viewToModel ).fromElement( 'div' ).toElement( 'div' );
  193. const modelFragment = editor.data.parse( '<h1>foo</h1><h2>bar</h2><div>bom</div>', 'specialRoot' );
  194. expect( stringifyModel( modelFragment ) )
  195. .to.equal( '<div>bom</div>' );
  196. } );
  197. it( 'should convert ul,ol>li', () => {
  198. const modelFragment = editor.data.parse( '<ul><li>a</li><li>b</li></ul><ol><li>c</li></ol>' );
  199. expect( stringifyModel( modelFragment ) )
  200. .to.equal( '<paragraph>a</paragraph><paragraph>b</paragraph><paragraph>c</paragraph>' );
  201. } );
  202. it( 'should convert ul,ol>li (in clipboard holder)', () => {
  203. const modelFragment = editor.data.parse( '<ul><li>a</li><li>b</li></ul><ol><li>c</li></ol>', '$clipboardHolder' );
  204. expect( stringifyModel( modelFragment ) )
  205. .to.equal( '<paragraph>a</paragraph><paragraph>b</paragraph><paragraph>c</paragraph>' );
  206. } );
  207. it( 'should convert ul>li>ul>li+li', () => {
  208. const modelFragment = editor.data.parse( '<ul><li>a<ul><li>b</li><li>c</li></ul></li></ul>' );
  209. expect( stringifyModel( modelFragment ) )
  210. .to.equal( '<paragraph>a</paragraph><paragraph>b</paragraph><paragraph>c</paragraph>' );
  211. } );
  212. // "b" is not autoparagraphed because clipboard holder allows text nodes.
  213. // There's a similar integrational test what's going to happen when pasting in paragraph-integration.js.
  214. it( 'should convert ul>li>ul>li+li (in clipboard holder)', () => {
  215. const modelFragment = editor.data.parse( '<ul><li>a<ul><li>b</li><li>c</li></ul></li></ul>', '$clipboardHolder' );
  216. expect( stringifyModel( modelFragment ) )
  217. .to.equal( '<paragraph>a</paragraph><paragraph>b</paragraph><paragraph>c</paragraph>' );
  218. } );
  219. it( 'should convert ul>li>p,text', () => {
  220. const modelFragment = editor.data.parse( '<ul><li><p>a</p>b</li></ul>' );
  221. expect( stringifyModel( modelFragment ) )
  222. .to.equal( '<paragraph>a</paragraph><paragraph>b</paragraph>' );
  223. } );
  224. // "b" is not autoparagraphed because clipboard holder allows text nodes.
  225. // There's a similar integrational test what's going to happen when pasting in paragraph-integration.js.
  226. it( 'should convert ul>li>p,text (in clipboard holder)', () => {
  227. const modelFragment = editor.data.parse( '<ul><li><p>a</p>b</li></ul>', '$clipboardHolder' );
  228. expect( stringifyModel( modelFragment ) )
  229. .to.equal( '<paragraph>a</paragraph><paragraph>b</paragraph>' );
  230. } );
  231. it( 'should convert td', () => {
  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. );
  235. expect( stringifyModel( modelFragment ) )
  236. .to.equal( '<paragraph>a</paragraph><paragraph>b</paragraph><paragraph>c</paragraph><paragraph>d</paragraph>' );
  237. } );
  238. it( 'should convert td (in clipboardHolder)', () => {
  239. const modelFragment = editor.data.parse(
  240. '<table><tr><td>a</td><td>b</td></tr><tr><td>c</td><td>d</td></tr></table>',
  241. '$clipboardHolder'
  242. );
  243. expect( stringifyModel( modelFragment ) )
  244. .to.equal( '<paragraph>a</paragraph><paragraph>b</paragraph><paragraph>c</paragraph><paragraph>d</paragraph>' );
  245. } );
  246. it( 'should convert li inside converted container', () => {
  247. model.schema.registerItem( 'div' );
  248. model.schema.allow( { name: 'div', inside: '$root' } );
  249. model.schema.allow( { name: 'paragraph', inside: 'div' } );
  250. buildViewConverter().for( editor.data.viewToModel ).fromElement( 'div' ).toElement( 'div' );
  251. const modelFragment = editor.data.parse( '<div><ul><li>foo</li><li>bar</li></ul></div><div>bom<p>bim</p></div>' );
  252. expect( stringifyModel( modelFragment ) )
  253. .to.equal(
  254. '<div><paragraph>foo</paragraph><paragraph>bar</paragraph></div>' +
  255. '<div><paragraph>bom</paragraph><paragraph>bim</paragraph></div>'
  256. );
  257. } );
  258. it( 'should convert li inside disallowed container', () => {
  259. const modelFragment = editor.data.parse( '<div><ul><li>foo</li><li>bar</li></ul></div><div>bom<p>bim</p></div>' );
  260. expect( stringifyModel( modelFragment ) )
  261. .to.equal(
  262. '<paragraph>foo</paragraph><paragraph>bar</paragraph>' +
  263. '<paragraph>bom</paragraph><paragraph>bim</paragraph>'
  264. );
  265. } );
  266. it( 'creates normalized model', () => {
  267. const modelFragment = editor.data.parse( '<h1><span>foo</span><span>bar</span>' );
  268. expect( stringifyModel( modelFragment ) ).to.equal( '<paragraph>foobar</paragraph>' );
  269. expect( modelFragment ).to.be.instanceof( ModelDocumentFragment );
  270. expect( modelFragment.getChild( 0 ).childCount ).to.equal( 1 );
  271. expect( modelFragment.getChild( 0 ).getChild( 0 ) ).to.be.instanceOf( ModelText );
  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. // changes 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 if it got content during changesDone event', () => {
  305. // "Autoheading feature".
  306. model.schema.registerItem( 'heading', '$block' );
  307. buildModelConverter().for( editor.editing.modelToView, editor.data.modelToView )
  308. .fromElement( 'heading' )
  309. .toElement( 'h1' );
  310. doc.on( 'changesDone', () => {
  311. const root = doc.getRoot();
  312. if ( root.isEmpty ) {
  313. model.change( writer => {
  314. writer.insertElement( 'heading', root );
  315. } );
  316. }
  317. } );
  318. model.change( writer => {
  319. writer.remove( ModelRange.createIn( root ) );
  320. } );
  321. expect( doc.getRoot().childCount ).to.equal( 1 );
  322. expect( doc.getRoot().getChild( 0 ).name ).to.equal( 'heading' );
  323. } );
  324. it( 'should not fix root which does not allow paragraph', () => {
  325. model.schema.disallow( { name: 'paragraph', inside: '$root' } );
  326. model.change( writer => {
  327. writer.remove( ModelRange.createIn( root ) );
  328. } );
  329. expect( editor.getData() ).to.equal( '' );
  330. } );
  331. it( 'should not fix root if change was in transparent batch', () => {
  332. editor.setData( '<p>Foobar</p>' );
  333. model.enqueueChange( 'transparent', writer => {
  334. writer.remove( ModelRange.createIn( root ) );
  335. } );
  336. expect( editor.getData() ).to.equal( '' );
  337. } );
  338. } );
  339. describe( 'command', () => {
  340. it( 'should be set in the editor', () => {
  341. expect( editor.commands.get( 'paragraph' ) ).to.be.instanceof( ParagraphCommand );
  342. } );
  343. } );
  344. } );