paragraph.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  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. describe( 'Paragraph feature', () => {
  21. let editor, doc;
  22. beforeEach( () => {
  23. return VirtualTestEditor.create( { plugins: [ Paragraph ] } )
  24. .then( newEditor => {
  25. editor = newEditor;
  26. doc = editor.document;
  27. } );
  28. } );
  29. it( 'should be loaded', () => {
  30. expect( editor.plugins.get( Paragraph ) ).to.be.instanceOf( Paragraph );
  31. } );
  32. it( 'should set proper schema rules', () => {
  33. expect( doc.schema.hasItem( 'paragraph' ) ).to.be.true;
  34. expect( doc.schema.check( { name: 'paragraph', inside: '$root' } ) ).to.be.true;
  35. expect( doc.schema.check( { name: '$inline', inside: 'paragraph' } ) ).to.be.true;
  36. } );
  37. it( 'should have a static paragraphLikeElements property', () => {
  38. expect( Paragraph ).to.have.property( 'paragraphLikeElements' );
  39. } );
  40. describe( 'data pipeline conversions', () => {
  41. it( 'should convert paragraph', () => {
  42. editor.setData( '<p>foobar</p>' );
  43. expect( getModelData( doc, { withoutSelection: true } ) ).to.equal( '<paragraph>foobar</paragraph>' );
  44. expect( editor.getData() ).to.equal( '<p>foobar</p>' );
  45. } );
  46. it( 'should convert paragraph only', () => {
  47. editor.setData( '<p>foo<b>baz</b>bar</p>' );
  48. expect( getModelData( doc, { withoutSelection: true } ) ).to.equal( '<paragraph>foobazbar</paragraph>' );
  49. expect( editor.getData() ).to.equal( '<p>foobazbar</p>' );
  50. } );
  51. it( 'should convert multiple paragraphs', () => {
  52. editor.setData( '<p>foo</p><p>baz</p>' );
  53. expect( getModelData( doc, { withoutSelection: true } ) ).to.equal( '<paragraph>foo</paragraph><paragraph>baz</paragraph>' );
  54. expect( editor.getData() ).to.equal( '<p>foo</p><p>baz</p>' );
  55. } );
  56. describe( 'generic text converter (text autoparagraphing)', () => {
  57. it( 'should autoparagraph text', () => {
  58. editor.setData( 'foo' );
  59. expect( getModelData( doc, { withoutSelection: true } ) ).to.equal( '<paragraph>foo</paragraph>' );
  60. expect( editor.getData() ).to.equal( '<p>foo</p>' );
  61. } );
  62. it( 'should autoparagraph any inline element', () => {
  63. editor.document.schema.registerItem( 'span', '$inline' );
  64. editor.document.schema.allow( { name: '$text', inside: '$inline' } );
  65. buildModelConverter().for( editor.editing.modelToView, editor.data.modelToView ).fromElement( 'span' ).toElement( 'span' );
  66. buildViewConverter().for( editor.data.viewToModel ).fromElement( 'span' ).toElement( 'span' );
  67. editor.setData( '<span>foo</span>' );
  68. expect( getModelData( doc, { withoutSelection: true } ) ).to.equal( '<paragraph><span>foo</span></paragraph>' );
  69. expect( editor.getData() ).to.equal( '<p><span>foo</span></p>' );
  70. } );
  71. it( 'should not autoparagraph text (in clipboard holder)', () => {
  72. const modelFragment = editor.data.parse( 'foo', '$clipboardHolder' );
  73. expect( stringifyModel( modelFragment ) )
  74. .to.equal( 'foo' );
  75. } );
  76. it( 'should not autoparagraph text (in a context which does not allow paragraphs', () => {
  77. doc.schema.registerItem( 'specialRoot' );
  78. const modelFragment = editor.data.parse( 'foo', 'specialRoot' );
  79. expect( stringifyModel( modelFragment ) )
  80. .to.equal( '' );
  81. } );
  82. it( 'should autoparagraph text next to allowed element', () => {
  83. doc.schema.registerItem( 'heading1', '$block' );
  84. buildViewConverter().for( editor.data.viewToModel ).fromElement( 'h1' ).toElement( 'heading1' );
  85. const modelFragment = editor.data.parse( '<h1>foo</h1>bar<p>bom</p>' );
  86. expect( stringifyModel( modelFragment ) )
  87. .to.equal( '<heading1>foo</heading1><paragraph>bar</paragraph><paragraph>bom</paragraph>' );
  88. } );
  89. it( 'should autoparagraph 3 inline nodes into one paragraph', () => {
  90. const modelFragment = editor.data.parse( 'foo<b>bar</b>bom' );
  91. expect( stringifyModel( modelFragment ) )
  92. .to.equal( '<paragraph>foobarbom</paragraph>' );
  93. } );
  94. it( 'should not autoparagraph 3 inline nodes (in clipboardHolder)', () => {
  95. const modelFragment = editor.data.parse( 'foo<b>bar</b>bom', '$clipboardHolder' );
  96. expect( stringifyModel( modelFragment ) )
  97. .to.equal( 'foobarbom' );
  98. } );
  99. it( 'should autoparagraph text inside converted container', () => {
  100. doc.schema.registerItem( 'div' );
  101. doc.schema.allow( { name: 'div', inside: '$root' } );
  102. doc.schema.allow( { name: 'paragraph', inside: 'div' } );
  103. buildViewConverter().for( editor.data.viewToModel ).fromElement( 'div' ).toElement( 'div' );
  104. const modelFragment = editor.data.parse( '<div>foo</div><div>bom<p>bim</p></div>' );
  105. expect( stringifyModel( modelFragment ) )
  106. .to.equal(
  107. '<div><paragraph>foo</paragraph></div>' +
  108. '<div><paragraph>bom</paragraph><paragraph>bim</paragraph></div>'
  109. );
  110. } );
  111. it( 'should autoparagraph text inside disallowed element next to allowed element', () => {
  112. doc.schema.registerItem( 'heading1', '$block' );
  113. buildViewConverter().for( editor.data.viewToModel ).fromElement( 'h1' ).toElement( 'heading1' );
  114. const modelFragment = editor.data.parse( '<div><h1>foo</h1>bar</div>' );
  115. expect( stringifyModel( modelFragment ) )
  116. .to.equal( '<heading1>foo</heading1><paragraph>bar</paragraph>' );
  117. } );
  118. it( 'should not autoparagraph text in disallowed element', () => {
  119. doc.schema.registerItem( 'heading1', '$block' );
  120. buildViewConverter().for( editor.data.viewToModel ).fromElement( 'h1' ).toElement( 'heading1' );
  121. const modelFragment = editor.data.parse( '<h1><b>foo</b>bar</h1>' );
  122. expect( stringifyModel( modelFragment ) )
  123. .to.equal( '<heading1>foobar</heading1>' );
  124. } );
  125. it( 'should not fail when text is not allowed in paragraph', () => {
  126. doc.schema.disallow( { name: '$text', inside: [ '$root', 'paragraph' ] } );
  127. const modelFragment = editor.data.parse( 'foo' );
  128. expect( stringifyModel( modelFragment ) ).to.equal( '' );
  129. } );
  130. it( 'creates normalized model', () => {
  131. const modelFragment = editor.data.parse( 'foo<b>bar</b>bom' );
  132. expect( modelFragment ).to.be.instanceof( ModelDocumentFragment );
  133. expect( modelFragment.getChild( 0 ).childCount ).to.equal( 1 );
  134. expect( modelFragment.getChild( 0 ).getChild( 0 ) ).to.be.instanceOf( ModelText );
  135. } );
  136. // This test was taken from the list package.
  137. it( 'does not break when some converter returns nothing', () => {
  138. editor.data.viewToModel.on( 'element:li', ( evt, data, consumable ) => {
  139. consumable.consume( data.input, { name: true } );
  140. }, { priority: 'highest' } );
  141. const modelFragment = editor.data.parse( '<ul><li></li></ul>' );
  142. expect( stringifyModel( modelFragment ) ).to.equal( '' );
  143. } );
  144. describe( 'should not strip attribute elements when autoparagraphing texts', () => {
  145. beforeEach( () => {
  146. doc.schema.allow( { name: '$inline', attributes: [ 'bold' ] } );
  147. buildViewConverter().for( editor.data.viewToModel )
  148. .fromElement( 'b' )
  149. .toAttribute( 'bold', true );
  150. } );
  151. it( 'inside document fragment', () => {
  152. const modelFragment = editor.data.parse( 'foo<b>bar</b>bom' );
  153. expect( stringifyModel( modelFragment ) ).to.equal( '<paragraph>foo<$text bold="true">bar</$text>bom</paragraph>' );
  154. } );
  155. it( 'inside converted element', () => {
  156. doc.schema.registerItem( 'blockquote' );
  157. doc.schema.allow( { name: 'blockquote', inside: '$root' } );
  158. doc.schema.allow( { name: '$block', inside: 'blockquote' } );
  159. buildModelConverter().for( editor.editing.modelToView, editor.data.modelToView )
  160. .fromElement( 'blockquote' )
  161. .toElement( 'blockquote' );
  162. buildViewConverter().for( editor.data.viewToModel ).fromElement( 'blockquote' ).toElement( '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. doc.schema.registerItem( 'div' );
  187. doc.schema.registerItem( 'specialRoot' );
  188. doc.schema.allow( { name: 'div', inside: 'specialRoot' } );
  189. doc.schema.allow( { name: '$text', inside: 'div' } );
  190. buildViewConverter().for( editor.data.viewToModel ).fromElement( 'div' ).toElement( '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. // "b" is not autoparagraphed because clipboard holder allows text nodes.
  223. // There's a similar integrational test what's going to happen when pasting in paragraph-integration.js.
  224. it( 'should convert ul>li>p,text (in clipboard holder)', () => {
  225. const modelFragment = editor.data.parse( '<ul><li><p>a</p>b</li></ul>', '$clipboardHolder' );
  226. expect( stringifyModel( modelFragment ) )
  227. .to.equal( '<paragraph>a</paragraph><paragraph>b</paragraph>' );
  228. } );
  229. it( 'should convert td', () => {
  230. const modelFragment = editor.data.parse( '<table><tr><td>a</td><td>b</td></tr><tr><td>c</td><td>d</td></tr></table>' );
  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. doc.schema.registerItem( 'div' );
  244. doc.schema.allow( { name: 'div', inside: '$root' } );
  245. doc.schema.allow( { name: 'paragraph', inside: 'div' } );
  246. buildViewConverter().for( editor.data.viewToModel ).fromElement( 'div' ).toElement( '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. } );
  270. } );
  271. describe( 'editing pipeline conversion', () => {
  272. it( 'should convert paragraph', () => {
  273. setModelData( doc, '<paragraph>foo</paragraph><paragraph>bar</paragraph>' );
  274. expect( getViewData( editor.editing.view, { withoutSelection: true } ) ).to.equal( '<p>foo</p><p>bar</p>' );
  275. } );
  276. } );
  277. describe( 'autoparagraphing on data load', () => {
  278. it( 'wraps text and place selection at the beginning of that paragraph', () => {
  279. editor.setData( 'foo' );
  280. expect( getModelData( doc ) ).to.equal( '<paragraph>[]foo</paragraph>' );
  281. } );
  282. } );
  283. describe( 'post-fixing empty roots', () => {
  284. it( 'should fix empty roots after editor is initialised', () => {
  285. expect( doc.getRoot().childCount ).to.equal( 1 );
  286. expect( doc.getRoot().getChild( 0 ).is( 'paragraph' ) ).to.be.true;
  287. } );
  288. it( 'should fix roots if it becomes empty', () => {
  289. editor.setData( '<p>Foobar</p>' );
  290. // Since `setData` first removes all contents from editor and then sets content during same enqueue
  291. // changes block, this checks whether fixing empty roots does not kick too early and does not
  292. // fix root if it is not needed.
  293. expect( editor.getData() ).to.equal( '<p>Foobar</p>' );
  294. editor.setData( '' );
  295. expect( doc.getRoot().childCount ).to.equal( 1 );
  296. expect( doc.getRoot().getChild( 0 ).is( 'paragraph' ) ).to.be.true;
  297. } );
  298. it( 'should not fix root if it got content during changesDone event', () => {
  299. // "Autoheading feature".
  300. doc.schema.registerItem( 'heading', '$block' );
  301. buildModelConverter().for( editor.editing.modelToView, editor.data.modelToView )
  302. .fromElement( 'heading' )
  303. .toElement( 'h1' );
  304. doc.on( 'changesDone', () => {
  305. const root = doc.getRoot();
  306. if ( root.isEmpty ) {
  307. doc.enqueueChanges( () => {
  308. doc.batch().insert( ModelPosition.createAt( root ), new ModelElement( 'heading' ) );
  309. } );
  310. }
  311. } );
  312. editor.setData( '' );
  313. expect( doc.getRoot().childCount ).to.equal( 1 );
  314. expect( doc.getRoot().getChild( 0 ).name ).to.equal( 'heading' );
  315. } );
  316. it( 'should not fix root which does not allow paragraph', () => {
  317. doc.schema.disallow( { name: 'paragraph', inside: '$root' } );
  318. editor.setData( '' );
  319. expect( editor.getData() ).to.equal( '' );
  320. } );
  321. } );
  322. describe( 'command', () => {
  323. it( 'should be set in the editor', () => {
  324. expect( editor.commands.get( 'paragraph' ) ).to.be.instanceof( ParagraphCommand );
  325. } );
  326. } );
  327. } );