paragraph.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  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 ModelElement from '@ckeditor/ckeditor5-engine/src/model/element';
  18. import ModelText from '@ckeditor/ckeditor5-engine/src/model/text';
  19. import ModelPosition from '@ckeditor/ckeditor5-engine/src/model/position';
  20. import ModelRange from '@ckeditor/ckeditor5-engine/src/model/range';
  21. describe( 'Paragraph feature', () => {
  22. let editor, doc, root;
  23. beforeEach( () => {
  24. return VirtualTestEditor
  25. .create( { plugins: [ Paragraph ] } )
  26. .then( newEditor => {
  27. editor = newEditor;
  28. doc = editor.document;
  29. root = doc.getRoot();
  30. } );
  31. } );
  32. it( 'should be loaded', () => {
  33. expect( editor.plugins.get( Paragraph ) ).to.be.instanceOf( Paragraph );
  34. } );
  35. it( 'should set proper schema rules', () => {
  36. expect( doc.schema.hasItem( 'paragraph' ) ).to.be.true;
  37. expect( doc.schema.check( { name: 'paragraph', inside: '$root' } ) ).to.be.true;
  38. expect( doc.schema.check( { name: '$inline', inside: 'paragraph' } ) ).to.be.true;
  39. } );
  40. it( 'should have a static paragraphLikeElements property', () => {
  41. expect( Paragraph ).to.have.property( 'paragraphLikeElements' );
  42. } );
  43. describe( 'data pipeline conversions', () => {
  44. it( 'should convert paragraph', () => {
  45. editor.setData( '<p>foobar</p>' );
  46. expect( getModelData( doc, { withoutSelection: true } ) ).to.equal( '<paragraph>foobar</paragraph>' );
  47. expect( editor.getData() ).to.equal( '<p>foobar</p>' );
  48. } );
  49. it( 'should convert paragraph only', () => {
  50. editor.setData( '<p>foo<b>baz</b>bar</p>' );
  51. expect( getModelData( doc, { withoutSelection: true } ) ).to.equal( '<paragraph>foobazbar</paragraph>' );
  52. expect( editor.getData() ).to.equal( '<p>foobazbar</p>' );
  53. } );
  54. it( 'should convert multiple paragraphs', () => {
  55. editor.setData( '<p>foo</p><p>baz</p>' );
  56. expect( getModelData( doc, { withoutSelection: true } ) ).to.equal( '<paragraph>foo</paragraph><paragraph>baz</paragraph>' );
  57. expect( editor.getData() ).to.equal( '<p>foo</p><p>baz</p>' );
  58. } );
  59. describe( 'generic text converter (text autoparagraphing)', () => {
  60. it( 'should autoparagraph text', () => {
  61. editor.setData( 'foo' );
  62. expect( getModelData( doc, { withoutSelection: true } ) ).to.equal( '<paragraph>foo</paragraph>' );
  63. expect( editor.getData() ).to.equal( '<p>foo</p>' );
  64. } );
  65. it( 'should autoparagraph any inline element', () => {
  66. editor.document.schema.registerItem( 'span', '$inline' );
  67. editor.document.schema.allow( { name: '$text', inside: '$inline' } );
  68. buildModelConverter().for( editor.editing.modelToView, editor.data.modelToView ).fromElement( 'span' ).toElement( 'span' );
  69. buildViewConverter().for( editor.data.viewToModel ).fromElement( 'span' ).toElement( 'span' );
  70. editor.setData( '<span>foo</span>' );
  71. expect( getModelData( doc, { withoutSelection: true } ) ).to.equal( '<paragraph><span>foo</span></paragraph>' );
  72. expect( editor.getData() ).to.equal( '<p><span>foo</span></p>' );
  73. } );
  74. it( 'should not autoparagraph text (in clipboard holder)', () => {
  75. const modelFragment = editor.data.parse( 'foo', '$clipboardHolder' );
  76. expect( stringifyModel( modelFragment ) )
  77. .to.equal( 'foo' );
  78. } );
  79. it( 'should not autoparagraph text (in a context which does not allow paragraphs', () => {
  80. doc.schema.registerItem( 'specialRoot' );
  81. const modelFragment = editor.data.parse( 'foo', 'specialRoot' );
  82. expect( stringifyModel( modelFragment ) )
  83. .to.equal( '' );
  84. } );
  85. it( 'should autoparagraph text next to allowed element', () => {
  86. doc.schema.registerItem( 'heading1', '$block' );
  87. buildViewConverter().for( editor.data.viewToModel ).fromElement( 'h1' ).toElement( 'heading1' );
  88. const modelFragment = editor.data.parse( '<h1>foo</h1>bar<p>bom</p>' );
  89. expect( stringifyModel( modelFragment ) )
  90. .to.equal( '<heading1>foo</heading1><paragraph>bar</paragraph><paragraph>bom</paragraph>' );
  91. } );
  92. it( 'should autoparagraph 3 inline nodes into one paragraph', () => {
  93. const modelFragment = editor.data.parse( 'foo<b>bar</b>bom' );
  94. expect( stringifyModel( modelFragment ) )
  95. .to.equal( '<paragraph>foobarbom</paragraph>' );
  96. } );
  97. it( 'should not autoparagraph 3 inline nodes (in clipboardHolder)', () => {
  98. const modelFragment = editor.data.parse( 'foo<b>bar</b>bom', '$clipboardHolder' );
  99. expect( stringifyModel( modelFragment ) )
  100. .to.equal( 'foobarbom' );
  101. } );
  102. it( 'should autoparagraph text inside converted container', () => {
  103. doc.schema.registerItem( 'div' );
  104. doc.schema.allow( { name: 'div', inside: '$root' } );
  105. doc.schema.allow( { name: 'paragraph', inside: 'div' } );
  106. buildViewConverter().for( editor.data.viewToModel ).fromElement( 'div' ).toElement( 'div' );
  107. const modelFragment = editor.data.parse( '<div>foo</div><div>bom<p>bim</p></div>' );
  108. expect( stringifyModel( modelFragment ) )
  109. .to.equal(
  110. '<div><paragraph>foo</paragraph></div>' +
  111. '<div><paragraph>bom</paragraph><paragraph>bim</paragraph></div>'
  112. );
  113. } );
  114. it( 'should autoparagraph text inside disallowed element next to allowed element', () => {
  115. doc.schema.registerItem( 'heading1', '$block' );
  116. buildViewConverter().for( editor.data.viewToModel ).fromElement( 'h1' ).toElement( 'heading1' );
  117. const modelFragment = editor.data.parse( '<div><h1>foo</h1>bar</div>' );
  118. expect( stringifyModel( modelFragment ) )
  119. .to.equal( '<heading1>foo</heading1><paragraph>bar</paragraph>' );
  120. } );
  121. it( 'should not autoparagraph text in disallowed element', () => {
  122. doc.schema.registerItem( 'heading1', '$block' );
  123. buildViewConverter().for( editor.data.viewToModel ).fromElement( 'h1' ).toElement( 'heading1' );
  124. const modelFragment = editor.data.parse( '<h1><b>foo</b>bar</h1>' );
  125. expect( stringifyModel( modelFragment ) )
  126. .to.equal( '<heading1>foobar</heading1>' );
  127. } );
  128. it( 'should not fail when text is not allowed in paragraph', () => {
  129. doc.schema.disallow( { name: '$text', inside: [ '$root', 'paragraph' ] } );
  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.viewToModel.on( 'element:li', ( evt, data, consumable ) => {
  142. 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. doc.schema.allow( { name: '$inline', attributes: [ 'bold' ] } );
  150. buildViewConverter().for( editor.data.viewToModel )
  151. .fromElement( 'b' )
  152. .toAttribute( 'bold', true );
  153. } );
  154. it( 'inside document fragment', () => {
  155. const modelFragment = editor.data.parse( 'foo<b>bar</b>bom' );
  156. expect( stringifyModel( modelFragment ) ).to.equal( '<paragraph>foo<$text bold="true">bar</$text>bom</paragraph>' );
  157. } );
  158. it( 'inside converted element', () => {
  159. doc.schema.registerItem( 'blockquote' );
  160. doc.schema.allow( { name: 'blockquote', inside: '$root' } );
  161. doc.schema.allow( { name: '$block', inside: 'blockquote' } );
  162. buildModelConverter().for( editor.editing.modelToView, editor.data.modelToView )
  163. .fromElement( 'blockquote' )
  164. .toElement( 'blockquote' );
  165. buildViewConverter().for( editor.data.viewToModel ).fromElement( 'blockquote' ).toElement( 'blockquote' );
  166. const modelFragment = editor.data.parse( '<blockquote>foo<b>bar</b>bom</blockquote>' );
  167. expect( stringifyModel( modelFragment ) )
  168. .to.equal( '<blockquote><paragraph>foo<$text bold="true">bar</$text>bom</paragraph></blockquote>' );
  169. } );
  170. it( 'inside paragraph-like element', () => {
  171. const modelFragment = editor.data.parse( '<h1>foo</h1><h2><b>bar</b>bom</h2>' );
  172. expect( stringifyModel( modelFragment ) )
  173. .to.equal( '<paragraph>foo</paragraph><paragraph><$text bold="true">bar</$text>bom</paragraph>' );
  174. } );
  175. } );
  176. } );
  177. describe( 'generic block converter (paragraph-like element handling)', () => {
  178. it( 'should convert h1+h2', () => {
  179. const modelFragment = editor.data.parse( '<h1>foo</h1><h2>bar</h2>' );
  180. expect( stringifyModel( modelFragment ) )
  181. .to.equal( '<paragraph>foo</paragraph><paragraph>bar</paragraph>' );
  182. } );
  183. it( 'should convert h1+h2 (in clipboard holder)', () => {
  184. const modelFragment = editor.data.parse( '<h1>foo</h1><h2>bar</h2>', '$clipboardHolder' );
  185. expect( stringifyModel( modelFragment ) )
  186. .to.equal( '<paragraph>foo</paragraph><paragraph>bar</paragraph>' );
  187. } );
  188. it( 'should not convert h1+h2 (in a context which does not allow paragraphs)', () => {
  189. doc.schema.registerItem( 'div' );
  190. doc.schema.registerItem( 'specialRoot' );
  191. doc.schema.allow( { name: 'div', inside: 'specialRoot' } );
  192. doc.schema.allow( { name: '$text', inside: 'div' } );
  193. buildViewConverter().for( editor.data.viewToModel ).fromElement( 'div' ).toElement( 'div' );
  194. const modelFragment = editor.data.parse( '<h1>foo</h1><h2>bar</h2><div>bom</div>', 'specialRoot' );
  195. expect( stringifyModel( modelFragment ) )
  196. .to.equal( '<div>bom</div>' );
  197. } );
  198. it( 'should convert ul,ol>li', () => {
  199. const modelFragment = editor.data.parse( '<ul><li>a</li><li>b</li></ul><ol><li>c</li></ol>' );
  200. expect( stringifyModel( modelFragment ) )
  201. .to.equal( '<paragraph>a</paragraph><paragraph>b</paragraph><paragraph>c</paragraph>' );
  202. } );
  203. it( 'should convert ul,ol>li (in clipboard holder)', () => {
  204. const modelFragment = editor.data.parse( '<ul><li>a</li><li>b</li></ul><ol><li>c</li></ol>', '$clipboardHolder' );
  205. expect( stringifyModel( modelFragment ) )
  206. .to.equal( '<paragraph>a</paragraph><paragraph>b</paragraph><paragraph>c</paragraph>' );
  207. } );
  208. it( 'should convert ul>li>ul>li+li', () => {
  209. const modelFragment = editor.data.parse( '<ul><li>a<ul><li>b</li><li>c</li></ul></li></ul>' );
  210. expect( stringifyModel( modelFragment ) )
  211. .to.equal( '<paragraph>a</paragraph><paragraph>b</paragraph><paragraph>c</paragraph>' );
  212. } );
  213. // "b" is not autoparagraphed because clipboard holder allows text nodes.
  214. // There's a similar integrational test what's going to happen when pasting in paragraph-integration.js.
  215. it( 'should convert ul>li>ul>li+li (in clipboard holder)', () => {
  216. const modelFragment = editor.data.parse( '<ul><li>a<ul><li>b</li><li>c</li></ul></li></ul>', '$clipboardHolder' );
  217. expect( stringifyModel( modelFragment ) )
  218. .to.equal( '<paragraph>a</paragraph><paragraph>b</paragraph><paragraph>c</paragraph>' );
  219. } );
  220. it( 'should convert ul>li>p,text', () => {
  221. const modelFragment = editor.data.parse( '<ul><li><p>a</p>b</li></ul>' );
  222. expect( stringifyModel( modelFragment ) )
  223. .to.equal( '<paragraph>a</paragraph><paragraph>b</paragraph>' );
  224. } );
  225. // "b" is not autoparagraphed because clipboard holder allows text nodes.
  226. // There's a similar integrational test what's going to happen when pasting in paragraph-integration.js.
  227. it( 'should convert ul>li>p,text (in clipboard holder)', () => {
  228. const modelFragment = editor.data.parse( '<ul><li><p>a</p>b</li></ul>', '$clipboardHolder' );
  229. expect( stringifyModel( modelFragment ) )
  230. .to.equal( '<paragraph>a</paragraph><paragraph>b</paragraph>' );
  231. } );
  232. it( 'should convert td', () => {
  233. const modelFragment = editor.data.parse( '<table><tr><td>a</td><td>b</td></tr><tr><td>c</td><td>d</td></tr></table>' );
  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. doc.schema.registerItem( 'div' );
  247. doc.schema.allow( { name: 'div', inside: '$root' } );
  248. doc.schema.allow( { name: 'paragraph', inside: '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( doc, '<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( doc ) ).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. // changes 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. doc.enqueueChanges( () => {
  298. doc.batch().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. doc.schema.registerItem( 'heading', '$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. doc.enqueueChanges( () => {
  313. doc.batch().insert( ModelPosition.createAt( root ), new ModelElement( 'heading' ) );
  314. } );
  315. }
  316. } );
  317. doc.enqueueChanges( () => {
  318. doc.batch().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. doc.schema.disallow( { name: 'paragraph', inside: '$root' } );
  325. doc.enqueueChanges( () => {
  326. doc.batch().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. doc.enqueueChanges( () => {
  333. doc.batch( 'transparent' ).remove( ModelRange.createIn( root ) );
  334. } );
  335. expect( editor.getData() ).to.equal( '' );
  336. } );
  337. } );
  338. describe( 'command', () => {
  339. it( 'should be set in the editor', () => {
  340. expect( editor.commands.get( 'paragraph' ) ).to.be.instanceof( ParagraphCommand );
  341. } );
  342. } );
  343. } );