paragraph.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  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 buildViewConverter from '@ckeditor/ckeditor5-engine/src/conversion/buildviewconverter';
  15. import buildModelConverter from '@ckeditor/ckeditor5-engine/src/conversion/buildmodelconverter';
  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 not autoparagraph text (in clipboard holder)', () => {
  65. const modelFragment = editor.data.parse( 'foo', '$clipboardHolder' );
  66. expect( stringifyModel( modelFragment ) )
  67. .to.equal( 'foo' );
  68. } );
  69. it( 'should not autoparagraph text (in a context which does not allow paragraphs', () => {
  70. doc.schema.registerItem( 'specialRoot' );
  71. const modelFragment = editor.data.parse( 'foo', 'specialRoot' );
  72. expect( stringifyModel( modelFragment ) )
  73. .to.equal( '' );
  74. } );
  75. it( 'should autoparagraph text next to allowed element', () => {
  76. doc.schema.registerItem( 'heading1', '$block' );
  77. buildViewConverter().for( editor.data.viewToModel ).fromElement( 'h1' ).toElement( 'heading1' );
  78. const modelFragment = editor.data.parse( '<h1>foo</h1>bar<p>bom</p>' );
  79. expect( stringifyModel( modelFragment ) )
  80. .to.equal( '<heading1>foo</heading1><paragraph>bar</paragraph><paragraph>bom</paragraph>' );
  81. } );
  82. it( 'should autoparagraph 3 inline nodes into one paragraph', () => {
  83. const modelFragment = editor.data.parse( 'foo<b>bar</b>bom' );
  84. expect( stringifyModel( modelFragment ) )
  85. .to.equal( '<paragraph>foobarbom</paragraph>' );
  86. } );
  87. it( 'should not autoparagraph 3 inline nodes (in clipboardHolder)', () => {
  88. const modelFragment = editor.data.parse( 'foo<b>bar</b>bom', '$clipboardHolder' );
  89. expect( stringifyModel( modelFragment ) )
  90. .to.equal( 'foobarbom' );
  91. } );
  92. it( 'should autoparagraph text inside converted container', () => {
  93. doc.schema.registerItem( 'div' );
  94. doc.schema.allow( { name: 'div', inside: '$root' } );
  95. doc.schema.allow( { name: 'paragraph', inside: 'div' } );
  96. buildViewConverter().for( editor.data.viewToModel ).fromElement( 'div' ).toElement( 'div' );
  97. const modelFragment = editor.data.parse( '<div>foo</div><div>bom<p>bim</p></div>' );
  98. expect( stringifyModel( modelFragment ) )
  99. .to.equal(
  100. '<div><paragraph>foo</paragraph></div>' +
  101. '<div><paragraph>bom</paragraph><paragraph>bim</paragraph></div>'
  102. );
  103. } );
  104. it( 'should autoparagraph text inside disallowed element next to allowed element', () => {
  105. doc.schema.registerItem( 'heading1', '$block' );
  106. buildViewConverter().for( editor.data.viewToModel ).fromElement( 'h1' ).toElement( 'heading1' );
  107. const modelFragment = editor.data.parse( '<div><h1>foo</h1>bar</div>' );
  108. expect( stringifyModel( modelFragment ) )
  109. .to.equal( '<heading1>foo</heading1><paragraph>bar</paragraph>' );
  110. } );
  111. it( 'should not autoparagraph text in disallowed element', () => {
  112. doc.schema.registerItem( 'heading1', '$block' );
  113. buildViewConverter().for( editor.data.viewToModel ).fromElement( 'h1' ).toElement( 'heading1' );
  114. const modelFragment = editor.data.parse( '<h1><b>foo</b>bar</h1>' );
  115. expect( stringifyModel( modelFragment ) )
  116. .to.equal( '<heading1>foobar</heading1>' );
  117. } );
  118. it( 'should not fail when text is not allowed in paragraph', () => {
  119. doc.schema.disallow( { name: '$text', inside: [ '$root', 'paragraph' ] } );
  120. const modelFragment = editor.data.parse( 'foo' );
  121. expect( stringifyModel( modelFragment ) ).to.equal( '' );
  122. } );
  123. it( 'creates normalized model', () => {
  124. const modelFragment = editor.data.parse( 'foo<b>bar</b>bom' );
  125. expect( modelFragment ).to.be.instanceof( ModelDocumentFragment );
  126. expect( modelFragment.getChild( 0 ).childCount ).to.equal( 1 );
  127. expect( modelFragment.getChild( 0 ).getChild( 0 ) ).to.be.instanceOf( ModelText );
  128. } );
  129. it( 'does not break converting inline elements', () => {
  130. doc.schema.allow( { name: '$inline', attributes: [ 'bold' ] } );
  131. buildViewConverter().for( editor.data.viewToModel )
  132. .fromElement( 'b' )
  133. .toAttribute( 'bold', true );
  134. const modelFragment = editor.data.parse( 'foo<b>bar</b>bom' );
  135. // The result of this test is wrong due to https://github.com/ckeditor/ckeditor5-paragraph/issues/10.
  136. // It's meant to catch the odd situation in mergeSubsequentParagraphs when data.output may be an array
  137. // for a while
  138. expect( stringifyModel( modelFragment ) ).to.equal( '<paragraph>foobarbom</paragraph>' );
  139. } );
  140. // This test was taken from the list package.
  141. it( 'does not break when some converter returns nothing', () => {
  142. editor.data.viewToModel.on( 'element:li', ( evt, data, consumable ) => {
  143. consumable.consume( data.input, { name: true } );
  144. }, { priority: 'highest' } );
  145. const modelFragment = editor.data.parse( '<ul><li></li></ul>' );
  146. expect( stringifyModel( modelFragment ) ).to.equal( '' );
  147. } );
  148. } );
  149. describe( 'generic block converter (paragraph-like element handling)', () => {
  150. it( 'should convert h1+h2', () => {
  151. const modelFragment = editor.data.parse( '<h1>foo</h1><h2>bar</h2>' );
  152. expect( stringifyModel( modelFragment ) )
  153. .to.equal( '<paragraph>foo</paragraph><paragraph>bar</paragraph>' );
  154. } );
  155. it( 'should convert h1+h2 (in clipboard holder)', () => {
  156. const modelFragment = editor.data.parse( '<h1>foo</h1><h2>bar</h2>', '$clipboardHolder' );
  157. expect( stringifyModel( modelFragment ) )
  158. .to.equal( '<paragraph>foo</paragraph><paragraph>bar</paragraph>' );
  159. } );
  160. it( 'should not convert h1+h2 (in a context which does not allow paragraphs)', () => {
  161. doc.schema.registerItem( 'div' );
  162. doc.schema.registerItem( 'specialRoot' );
  163. doc.schema.allow( { name: 'div', inside: 'specialRoot' } );
  164. doc.schema.allow( { name: '$text', inside: 'div' } );
  165. buildViewConverter().for( editor.data.viewToModel ).fromElement( 'div' ).toElement( 'div' );
  166. const modelFragment = editor.data.parse( '<h1>foo</h1><h2>bar</h2><div>bom</div>', 'specialRoot' );
  167. expect( stringifyModel( modelFragment ) )
  168. .to.equal( '<div>bom</div>' );
  169. } );
  170. it( 'should convert ul,ol>li', () => {
  171. const modelFragment = editor.data.parse( '<ul><li>a</li><li>b</li></ul><ol><li>c</li></ol>' );
  172. expect( stringifyModel( modelFragment ) )
  173. .to.equal( '<paragraph>a</paragraph><paragraph>b</paragraph><paragraph>c</paragraph>' );
  174. } );
  175. it( 'should convert ul,ol>li (in clipboard holder)', () => {
  176. const modelFragment = editor.data.parse( '<ul><li>a</li><li>b</li></ul><ol><li>c</li></ol>', '$clipboardHolder' );
  177. expect( stringifyModel( modelFragment ) )
  178. .to.equal( '<paragraph>a</paragraph><paragraph>b</paragraph><paragraph>c</paragraph>' );
  179. } );
  180. it( 'should convert ul>li>ul>li+li', () => {
  181. const modelFragment = editor.data.parse( '<ul><li>a<ul><li>b</li><li>c</li></ul></li></ul>' );
  182. expect( stringifyModel( modelFragment ) )
  183. .to.equal( '<paragraph>a</paragraph><paragraph>b</paragraph><paragraph>c</paragraph>' );
  184. } );
  185. // "b" is not autoparagraphed because clipboard holder allows text nodes.
  186. // There's a similar integrational test what's going to happen when pasting in paragraph-integration.js.
  187. it( 'should convert ul>li>ul>li+li (in clipboard holder)', () => {
  188. const modelFragment = editor.data.parse( '<ul><li>a<ul><li>b</li><li>c</li></ul></li></ul>', '$clipboardHolder' );
  189. expect( stringifyModel( modelFragment ) )
  190. .to.equal( 'a<paragraph>b</paragraph><paragraph>c</paragraph>' );
  191. } );
  192. it( 'should convert ul>li>p,text', () => {
  193. const modelFragment = editor.data.parse( '<ul><li><p>a</p>b</li></ul>' );
  194. expect( stringifyModel( modelFragment ) )
  195. .to.equal( '<paragraph>a</paragraph><paragraph>b</paragraph>' );
  196. } );
  197. // "b" is not autoparagraphed because clipboard holder allows text nodes.
  198. // There's a similar integrational test what's going to happen when pasting in paragraph-integration.js.
  199. it( 'should convert ul>li>p,text (in clipboard holder)', () => {
  200. const modelFragment = editor.data.parse( '<ul><li><p>a</p>b</li></ul>', '$clipboardHolder' );
  201. expect( stringifyModel( modelFragment ) )
  202. .to.equal( '<paragraph>a</paragraph>b' );
  203. } );
  204. it( 'should convert td', () => {
  205. const modelFragment = editor.data.parse( '<table><tr><td>a</td><td>b</td></tr><tr><td>c</td><td>d</td></tr></table>' );
  206. expect( stringifyModel( modelFragment ) )
  207. .to.equal( '<paragraph>a</paragraph><paragraph>b</paragraph><paragraph>c</paragraph><paragraph>d</paragraph>' );
  208. } );
  209. it( 'should convert td (in clipboardHolder)', () => {
  210. const modelFragment = editor.data.parse(
  211. '<table><tr><td>a</td><td>b</td></tr><tr><td>c</td><td>d</td></tr></table>',
  212. '$clipboardHolder'
  213. );
  214. expect( stringifyModel( modelFragment ) )
  215. .to.equal( '<paragraph>a</paragraph><paragraph>b</paragraph><paragraph>c</paragraph><paragraph>d</paragraph>' );
  216. } );
  217. it( 'should convert li inside converted container', () => {
  218. doc.schema.registerItem( 'div' );
  219. doc.schema.allow( { name: 'div', inside: '$root' } );
  220. doc.schema.allow( { name: 'paragraph', inside: 'div' } );
  221. buildViewConverter().for( editor.data.viewToModel ).fromElement( 'div' ).toElement( 'div' );
  222. const modelFragment = editor.data.parse( '<div><ul><li>foo</li><li>bar</li></ul></div><div>bom<p>bim</p></div>' );
  223. expect( stringifyModel( modelFragment ) )
  224. .to.equal(
  225. '<div><paragraph>foo</paragraph><paragraph>bar</paragraph></div>' +
  226. '<div><paragraph>bom</paragraph><paragraph>bim</paragraph></div>'
  227. );
  228. } );
  229. it( 'should convert li inside disallowed container', () => {
  230. const modelFragment = editor.data.parse( '<div><ul><li>foo</li><li>bar</li></ul></div><div>bom<p>bim</p></div>' );
  231. expect( stringifyModel( modelFragment ) )
  232. .to.equal(
  233. '<paragraph>foo</paragraph><paragraph>bar</paragraph>' +
  234. '<paragraph>bom</paragraph><paragraph>bim</paragraph>'
  235. );
  236. } );
  237. it( 'creates normalized model', () => {
  238. const modelFragment = editor.data.parse( '<h1><span>foo</span><span>bar</span>' );
  239. expect( stringifyModel( modelFragment ) ).to.equal( '<paragraph>foobar</paragraph>' );
  240. expect( modelFragment ).to.be.instanceof( ModelDocumentFragment );
  241. expect( modelFragment.getChild( 0 ).childCount ).to.equal( 1 );
  242. expect( modelFragment.getChild( 0 ).getChild( 0 ) ).to.be.instanceOf( ModelText );
  243. } );
  244. } );
  245. } );
  246. describe( 'editing pipeline conversion', () => {
  247. it( 'should convert paragraph', () => {
  248. setModelData( doc, '<paragraph>foo</paragraph><paragraph>bar</paragraph>' );
  249. expect( getViewData( editor.editing.view, { withoutSelection: true } ) ).to.equal( '<p>foo</p><p>bar</p>' );
  250. } );
  251. } );
  252. describe( 'autoparagraphing on data load', () => {
  253. it( 'wraps text and place selection at the beginning of that paragraph', () => {
  254. editor.setData( 'foo' );
  255. expect( getModelData( doc ) ).to.equal( '<paragraph>[]foo</paragraph>' );
  256. } );
  257. } );
  258. describe( 'post-fixing empty roots', () => {
  259. it( 'should fix empty roots after editor is initialised', () => {
  260. expect( doc.getRoot().childCount ).to.equal( 1 );
  261. expect( doc.getRoot().getChild( 0 ).is( 'paragraph' ) ).to.be.true;
  262. } );
  263. it( 'should fix roots if it becomes empty', () => {
  264. editor.setData( '<p>Foobar</p>' );
  265. // Since `setData` first removes all contents from editor and then sets content during same enqueue
  266. // changes block, this checks whether fixing empty roots does not kick too early and does not
  267. // fix root if it is not needed.
  268. expect( editor.getData() ).to.equal( '<p>Foobar</p>' );
  269. editor.setData( '' );
  270. expect( doc.getRoot().childCount ).to.equal( 1 );
  271. expect( doc.getRoot().getChild( 0 ).is( 'paragraph' ) ).to.be.true;
  272. } );
  273. it( 'should not fix root if it got content during changesDone event', () => {
  274. // "Autoheading feature".
  275. doc.schema.registerItem( 'heading', '$block' );
  276. buildModelConverter().for( editor.editing.modelToView, editor.data.modelToView )
  277. .fromElement( 'heading' )
  278. .toElement( 'h1' );
  279. doc.on( 'changesDone', () => {
  280. const root = doc.getRoot();
  281. if ( root.isEmpty ) {
  282. doc.enqueueChanges( () => {
  283. doc.batch().insert( ModelPosition.createAt( root ), new ModelElement( 'heading' ) );
  284. } );
  285. }
  286. } );
  287. editor.setData( '' );
  288. expect( doc.getRoot().childCount ).to.equal( 1 );
  289. expect( doc.getRoot().getChild( 0 ).name ).to.equal( 'heading' );
  290. } );
  291. it( 'should not fix root which does not allow paragraph', () => {
  292. doc.schema.disallow( { name: 'paragraph', inside: '$root' } );
  293. editor.setData( '' );
  294. expect( editor.getData() ).to.equal( '' );
  295. } );
  296. } );
  297. describe( 'command', () => {
  298. it( 'should be set in the editor', () => {
  299. expect( editor.commands.get( 'paragraph' ) ).to.be.instanceof( ParagraphCommand );
  300. } );
  301. } );
  302. } );