paragraph.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  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( {
  24. plugins: [ Paragraph ]
  25. } )
  26. .then( newEditor => {
  27. editor = newEditor;
  28. doc = editor.document;
  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( doc.schema.hasItem( 'paragraph' ) ).to.be.true;
  36. expect( doc.schema.check( { name: 'paragraph', inside: '$root' } ) ).to.be.true;
  37. expect( doc.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( doc, { 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( doc, { 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( doc, { 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( doc, { 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.document.schema.registerItem( 'span', '$inline' );
  66. editor.document.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( doc, { 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. doc.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. doc.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. doc.schema.registerItem( 'div' );
  103. doc.schema.allow( { name: 'div', inside: '$root' } );
  104. doc.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. doc.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. doc.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. doc.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. doc.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. doc.schema.registerItem( 'blockquote' );
  159. doc.schema.allow( { name: 'blockquote', inside: '$root' } );
  160. doc.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. doc.schema.registerItem( 'div' );
  189. doc.schema.registerItem( 'specialRoot' );
  190. doc.schema.allow( { name: 'div', inside: 'specialRoot' } );
  191. doc.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( '<table><tr><td>a</td><td>b</td></tr><tr><td>c</td><td>d</td></tr></table>' );
  233. expect( stringifyModel( modelFragment ) )
  234. .to.equal( '<paragraph>a</paragraph><paragraph>b</paragraph><paragraph>c</paragraph><paragraph>d</paragraph>' );
  235. } );
  236. it( 'should convert td (in clipboardHolder)', () => {
  237. const modelFragment = editor.data.parse(
  238. '<table><tr><td>a</td><td>b</td></tr><tr><td>c</td><td>d</td></tr></table>',
  239. '$clipboardHolder'
  240. );
  241. expect( stringifyModel( modelFragment ) )
  242. .to.equal( '<paragraph>a</paragraph><paragraph>b</paragraph><paragraph>c</paragraph><paragraph>d</paragraph>' );
  243. } );
  244. it( 'should convert li inside converted container', () => {
  245. doc.schema.registerItem( 'div' );
  246. doc.schema.allow( { name: 'div', inside: '$root' } );
  247. doc.schema.allow( { name: 'paragraph', inside: 'div' } );
  248. buildViewConverter().for( editor.data.viewToModel ).fromElement( 'div' ).toElement( 'div' );
  249. const modelFragment = editor.data.parse( '<div><ul><li>foo</li><li>bar</li></ul></div><div>bom<p>bim</p></div>' );
  250. expect( stringifyModel( modelFragment ) )
  251. .to.equal(
  252. '<div><paragraph>foo</paragraph><paragraph>bar</paragraph></div>' +
  253. '<div><paragraph>bom</paragraph><paragraph>bim</paragraph></div>'
  254. );
  255. } );
  256. it( 'should convert li inside disallowed container', () => {
  257. const modelFragment = editor.data.parse( '<div><ul><li>foo</li><li>bar</li></ul></div><div>bom<p>bim</p></div>' );
  258. expect( stringifyModel( modelFragment ) )
  259. .to.equal(
  260. '<paragraph>foo</paragraph><paragraph>bar</paragraph>' +
  261. '<paragraph>bom</paragraph><paragraph>bim</paragraph>'
  262. );
  263. } );
  264. it( 'creates normalized model', () => {
  265. const modelFragment = editor.data.parse( '<h1><span>foo</span><span>bar</span>' );
  266. expect( stringifyModel( modelFragment ) ).to.equal( '<paragraph>foobar</paragraph>' );
  267. expect( modelFragment ).to.be.instanceof( ModelDocumentFragment );
  268. expect( modelFragment.getChild( 0 ).childCount ).to.equal( 1 );
  269. expect( modelFragment.getChild( 0 ).getChild( 0 ) ).to.be.instanceOf( ModelText );
  270. } );
  271. } );
  272. } );
  273. describe( 'editing pipeline conversion', () => {
  274. it( 'should convert paragraph', () => {
  275. setModelData( doc, '<paragraph>foo</paragraph><paragraph>bar</paragraph>' );
  276. expect( getViewData( editor.editing.view, { withoutSelection: true } ) ).to.equal( '<p>foo</p><p>bar</p>' );
  277. } );
  278. } );
  279. describe( 'autoparagraphing on data load', () => {
  280. it( 'wraps text and place selection at the beginning of that paragraph', () => {
  281. editor.setData( 'foo' );
  282. expect( getModelData( doc ) ).to.equal( '<paragraph>[]foo</paragraph>' );
  283. } );
  284. } );
  285. describe( 'post-fixing empty roots', () => {
  286. it( 'should fix empty roots after editor is initialised', () => {
  287. expect( doc.getRoot().childCount ).to.equal( 1 );
  288. expect( doc.getRoot().getChild( 0 ).is( 'paragraph' ) ).to.be.true;
  289. } );
  290. it( 'should fix roots if it becomes empty', () => {
  291. editor.setData( '<p>Foobar</p>' );
  292. // Since `setData` first removes all contents from editor and then sets content during same enqueue
  293. // changes block, this checks whether fixing empty roots does not kick too early and does not
  294. // fix root if it is not needed.
  295. expect( editor.getData() ).to.equal( '<p>Foobar</p>' );
  296. editor.setData( '' );
  297. expect( doc.getRoot().childCount ).to.equal( 1 );
  298. expect( doc.getRoot().getChild( 0 ).is( 'paragraph' ) ).to.be.true;
  299. } );
  300. it( 'should not fix root if it got content during changesDone event', () => {
  301. // "Autoheading feature".
  302. doc.schema.registerItem( 'heading', '$block' );
  303. buildModelConverter().for( editor.editing.modelToView, editor.data.modelToView )
  304. .fromElement( 'heading' )
  305. .toElement( 'h1' );
  306. doc.on( 'changesDone', () => {
  307. const root = doc.getRoot();
  308. if ( root.isEmpty ) {
  309. doc.enqueueChanges( () => {
  310. doc.batch().insert( ModelPosition.createAt( root ), new ModelElement( 'heading' ) );
  311. } );
  312. }
  313. } );
  314. editor.setData( '' );
  315. expect( doc.getRoot().childCount ).to.equal( 1 );
  316. expect( doc.getRoot().getChild( 0 ).name ).to.equal( 'heading' );
  317. } );
  318. it( 'should not fix root which does not allow paragraph', () => {
  319. doc.schema.disallow( { name: 'paragraph', inside: '$root' } );
  320. editor.setData( '' );
  321. expect( editor.getData() ).to.equal( '' );
  322. } );
  323. } );
  324. describe( 'command', () => {
  325. it( 'should be set in the editor', () => {
  326. expect( editor.commands.get( 'paragraph' ) ).to.be.instanceof( ParagraphCommand );
  327. } );
  328. } );
  329. } );