paragraph.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. /**
  2. * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import Paragraph from 'ckeditor5-paragraph/src/paragraph';
  6. import VirtualTestEditor from 'ckeditor5-core/tests/_utils/virtualtesteditor';
  7. import {
  8. getData as getModelData,
  9. setData as setModelData,
  10. stringify as stringifyModel
  11. } from 'ckeditor5-engine/src/dev-utils/model';
  12. import { getData as getViewData } from 'ckeditor5-engine/src/dev-utils/view';
  13. import buildViewConverter from 'ckeditor5-engine/src/conversion/buildviewconverter';
  14. import ModelDocumentFragment from 'ckeditor5-engine/src/model/documentfragment';
  15. import ModelText from 'ckeditor5-engine/src/model/text';
  16. describe( 'Paragraph feature', () => {
  17. let editor, doc;
  18. beforeEach( () => {
  19. return VirtualTestEditor.create( {
  20. plugins: [ Paragraph ]
  21. } )
  22. .then( newEditor => {
  23. editor = newEditor;
  24. doc = editor.document;
  25. } );
  26. } );
  27. it( 'should be loaded', () => {
  28. expect( editor.plugins.get( Paragraph ) ).to.be.instanceOf( Paragraph );
  29. } );
  30. it( 'should set proper schema rules', () => {
  31. expect( doc.schema.hasItem( 'paragraph' ) ).to.be.true;
  32. expect( doc.schema.check( { name: 'paragraph', inside: '$root' } ) ).to.be.true;
  33. expect( doc.schema.check( { name: '$inline', inside: 'paragraph' } ) ).to.be.true;
  34. } );
  35. it( 'should have a static paragraphLikeElements property', () => {
  36. expect( Paragraph ).to.have.property( 'paragraphLikeElements' );
  37. } );
  38. describe( 'data pipeline conversions', () => {
  39. it( 'should convert paragraph', () => {
  40. editor.setData( '<p>foobar</p>' );
  41. expect( getModelData( doc, { withoutSelection: true } ) ).to.equal( '<paragraph>foobar</paragraph>' );
  42. expect( editor.getData() ).to.equal( '<p>foobar</p>' );
  43. } );
  44. it( 'should convert paragraph only', () => {
  45. editor.setData( '<p>foo<b>baz</b>bar</p>' );
  46. expect( getModelData( doc, { withoutSelection: true } ) ).to.equal( '<paragraph>foobazbar</paragraph>' );
  47. expect( editor.getData() ).to.equal( '<p>foobazbar</p>' );
  48. } );
  49. it( 'should convert multiple paragraphs', () => {
  50. editor.setData( '<p>foo</p><p>baz</p>' );
  51. expect( getModelData( doc, { withoutSelection: true } ) ).to.equal( '<paragraph>foo</paragraph><paragraph>baz</paragraph>' );
  52. expect( editor.getData() ).to.equal( '<p>foo</p><p>baz</p>' );
  53. } );
  54. describe( 'generic text converter (text autoparagraphing)', () => {
  55. it( 'should autoparagraph text', () => {
  56. editor.setData( 'foo' );
  57. expect( getModelData( doc, { withoutSelection: true } ) ).to.equal( '<paragraph>foo</paragraph>' );
  58. expect( editor.getData() ).to.equal( '<p>foo</p>' );
  59. } );
  60. it( 'should not autoparagraph text (in clipboard holder)', () => {
  61. const modelFragment = editor.data.parse( 'foo', '$clipboardHolder' );
  62. expect( stringifyModel( modelFragment ) )
  63. .to.equal( 'foo' );
  64. } );
  65. it( 'should not autoparagraph text (in a context which does not allow paragraphs', () => {
  66. doc.schema.registerItem( 'specialRoot' );
  67. const modelFragment = editor.data.parse( 'foo', 'specialRoot' );
  68. expect( stringifyModel( modelFragment ) )
  69. .to.equal( '' );
  70. } );
  71. it( 'should autoparagraph text next to allowed element', () => {
  72. doc.schema.registerItem( 'heading1', '$block' );
  73. buildViewConverter().for( editor.data.viewToModel ).fromElement( 'h1' ).toElement( 'heading1' );
  74. const modelFragment = editor.data.parse( '<h1>foo</h1>bar<p>bom</p>' );
  75. expect( stringifyModel( modelFragment ) )
  76. .to.equal( '<heading1>foo</heading1><paragraph>bar</paragraph><paragraph>bom</paragraph>' );
  77. } );
  78. it( 'should autoparagraph 3 inline nodes into one paragraph', () => {
  79. const modelFragment = editor.data.parse( 'foo<b>bar</b>bom' );
  80. expect( stringifyModel( modelFragment ) )
  81. .to.equal( '<paragraph>foobarbom</paragraph>' );
  82. } );
  83. it( 'should not autoparagraph 3 inline nodes (in clipboardHolder)', () => {
  84. const modelFragment = editor.data.parse( 'foo<b>bar</b>bom', '$clipboardHolder' );
  85. expect( stringifyModel( modelFragment ) )
  86. .to.equal( 'foobarbom' );
  87. } );
  88. it( 'should autoparagraph text inside converted container', () => {
  89. doc.schema.registerItem( 'div' );
  90. doc.schema.allow( { name: 'div', inside: '$root' } );
  91. doc.schema.allow( { name: 'paragraph', inside: 'div' } );
  92. buildViewConverter().for( editor.data.viewToModel ).fromElement( 'div' ).toElement( 'div' );
  93. const modelFragment = editor.data.parse( '<div>foo</div><div>bom<p>bim</p></div>' );
  94. expect( stringifyModel( modelFragment ) )
  95. .to.equal(
  96. '<div><paragraph>foo</paragraph></div>' +
  97. '<div><paragraph>bom</paragraph><paragraph>bim</paragraph></div>'
  98. );
  99. } );
  100. it( 'should autoparagraph text inside disallowed element next to allowed element', () => {
  101. doc.schema.registerItem( 'heading1', '$block' );
  102. buildViewConverter().for( editor.data.viewToModel ).fromElement( 'h1' ).toElement( 'heading1' );
  103. const modelFragment = editor.data.parse( '<div><h1>foo</h1>bar</div>' );
  104. expect( stringifyModel( modelFragment ) )
  105. .to.equal( '<heading1>foo</heading1><paragraph>bar</paragraph>' );
  106. } );
  107. it( 'should not autoparagraph text in disallowed element', () => {
  108. doc.schema.registerItem( 'heading1', '$block' );
  109. buildViewConverter().for( editor.data.viewToModel ).fromElement( 'h1' ).toElement( 'heading1' );
  110. const modelFragment = editor.data.parse( '<h1><b>foo</b>bar</h1>' );
  111. expect( stringifyModel( modelFragment ) )
  112. .to.equal( '<heading1>foobar</heading1>' );
  113. } );
  114. it( 'should not fail when text is not allowed in paragraph', () => {
  115. doc.schema.disallow( { name: '$text', inside: [ '$root', 'paragraph' ] } );
  116. const modelFragment = editor.data.parse( 'foo' );
  117. expect( stringifyModel( modelFragment ) ).to.equal( '' );
  118. } );
  119. it( 'creates normalized model', () => {
  120. const modelFragment = editor.data.parse( 'foo<b>bar</b>bom' );
  121. expect( modelFragment ).to.be.instanceof( ModelDocumentFragment );
  122. expect( modelFragment.getChild( 0 ).childCount ).to.equal( 1 );
  123. expect( modelFragment.getChild( 0 ).getChild( 0 ) ).to.be.instanceOf( ModelText );
  124. } );
  125. it( 'does not break converting inline elements', () => {
  126. doc.schema.allow( { name: '$inline', attributes: [ 'bold' ] } );
  127. buildViewConverter().for( editor.data.viewToModel )
  128. .fromElement( 'b' )
  129. .toAttribute( 'bold', true );
  130. const modelFragment = editor.data.parse( 'foo<b>bar</b>bom' );
  131. // The result of this test is wrong due to https://github.com/ckeditor/ckeditor5-paragraph/issues/10.
  132. // It's meant to catch the odd situation in mergeSubsequentParagraphs when data.output may be an array
  133. // for a while
  134. expect( stringifyModel( modelFragment ) ).to.equal( '<paragraph>foobarbom</paragraph>' );
  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. } );
  145. describe( 'generic block converter (paragraph-like element handling)', () => {
  146. it( 'should convert h1+h2', () => {
  147. const modelFragment = editor.data.parse( '<h1>foo</h1><h2>bar</h2>' );
  148. expect( stringifyModel( modelFragment ) )
  149. .to.equal( '<paragraph>foo</paragraph><paragraph>bar</paragraph>' );
  150. } );
  151. it( 'should convert h1+h2 (in clipboard holder)', () => {
  152. const modelFragment = editor.data.parse( '<h1>foo</h1><h2>bar</h2>', '$clipboardHolder' );
  153. expect( stringifyModel( modelFragment ) )
  154. .to.equal( '<paragraph>foo</paragraph><paragraph>bar</paragraph>' );
  155. } );
  156. it( 'should not convert h1+h2 (in a context which does not allow paragraphs)', () => {
  157. doc.schema.registerItem( 'div' );
  158. doc.schema.registerItem( 'specialRoot' );
  159. doc.schema.allow( { name: 'div', inside: 'specialRoot' } );
  160. doc.schema.allow( { name: '$text', inside: 'div' } );
  161. buildViewConverter().for( editor.data.viewToModel ).fromElement( 'div' ).toElement( 'div' );
  162. const modelFragment = editor.data.parse( '<h1>foo</h1><h2>bar</h2><div>bom</div>', 'specialRoot' );
  163. expect( stringifyModel( modelFragment ) )
  164. .to.equal( '<div>bom</div>' );
  165. } );
  166. it( 'should convert ul,ol>li', () => {
  167. const modelFragment = editor.data.parse( '<ul><li>a</li><li>b</li></ul><ol><li>c</li></ol>' );
  168. expect( stringifyModel( modelFragment ) )
  169. .to.equal( '<paragraph>a</paragraph><paragraph>b</paragraph><paragraph>c</paragraph>' );
  170. } );
  171. it( 'should convert ul,ol>li (in clipboard holder)', () => {
  172. const modelFragment = editor.data.parse( '<ul><li>a</li><li>b</li></ul><ol><li>c</li></ol>', '$clipboardHolder' );
  173. expect( stringifyModel( modelFragment ) )
  174. .to.equal( '<paragraph>a</paragraph><paragraph>b</paragraph><paragraph>c</paragraph>' );
  175. } );
  176. it( 'should convert ul>li>ul>li+li', () => {
  177. const modelFragment = editor.data.parse( '<ul><li>a<ul><li>b</li><li>c</li></ul></li></ul>' );
  178. expect( stringifyModel( modelFragment ) )
  179. .to.equal( '<paragraph>a</paragraph><paragraph>b</paragraph><paragraph>c</paragraph>' );
  180. } );
  181. // "b" is not autoparagraphed because clipboard holder allows text nodes.
  182. // There's a similar integrational test what's going to happen when pasting in paragraph-integration.js.
  183. it( 'should convert ul>li>ul>li+li (in clipboard holder)', () => {
  184. const modelFragment = editor.data.parse( '<ul><li>a<ul><li>b</li><li>c</li></ul></li></ul>', '$clipboardHolder' );
  185. expect( stringifyModel( modelFragment ) )
  186. .to.equal( 'a<paragraph>b</paragraph><paragraph>c</paragraph>' );
  187. } );
  188. it( 'should convert ul>li>p,text', () => {
  189. const modelFragment = editor.data.parse( '<ul><li><p>a</p>b</li></ul>' );
  190. expect( stringifyModel( modelFragment ) )
  191. .to.equal( '<paragraph>a</paragraph><paragraph>b</paragraph>' );
  192. } );
  193. // "b" is not autoparagraphed because clipboard holder allows text nodes.
  194. // There's a similar integrational test what's going to happen when pasting in paragraph-integration.js.
  195. it( 'should convert ul>li>p,text (in clipboard holder)', () => {
  196. const modelFragment = editor.data.parse( '<ul><li><p>a</p>b</li></ul>', '$clipboardHolder' );
  197. expect( stringifyModel( modelFragment ) )
  198. .to.equal( '<paragraph>a</paragraph>b' );
  199. } );
  200. it( 'should convert td', () => {
  201. const modelFragment = editor.data.parse( '<table><tr><td>a</td><td>b</td></tr><tr><td>c</td><td>d</td></tr></table>' );
  202. expect( stringifyModel( modelFragment ) )
  203. .to.equal( '<paragraph>a</paragraph><paragraph>b</paragraph><paragraph>c</paragraph><paragraph>d</paragraph>' );
  204. } );
  205. it( 'should convert td (in clipboardHolder)', () => {
  206. const modelFragment = editor.data.parse(
  207. '<table><tr><td>a</td><td>b</td></tr><tr><td>c</td><td>d</td></tr></table>',
  208. '$clipboardHolder'
  209. );
  210. expect( stringifyModel( modelFragment ) )
  211. .to.equal( '<paragraph>a</paragraph><paragraph>b</paragraph><paragraph>c</paragraph><paragraph>d</paragraph>' );
  212. } );
  213. it( 'should convert li inside converted container', () => {
  214. doc.schema.registerItem( 'div' );
  215. doc.schema.allow( { name: 'div', inside: '$root' } );
  216. doc.schema.allow( { name: 'paragraph', inside: 'div' } );
  217. buildViewConverter().for( editor.data.viewToModel ).fromElement( 'div' ).toElement( 'div' );
  218. const modelFragment = editor.data.parse( '<div><ul><li>foo</li><li>bar</li></ul></div><div>bom<p>bim</p></div>' );
  219. expect( stringifyModel( modelFragment ) )
  220. .to.equal(
  221. '<div><paragraph>foo</paragraph><paragraph>bar</paragraph></div>' +
  222. '<div><paragraph>bom</paragraph><paragraph>bim</paragraph></div>'
  223. );
  224. } );
  225. it( 'should convert li inside disallowed container', () => {
  226. const modelFragment = editor.data.parse( '<div><ul><li>foo</li><li>bar</li></ul></div><div>bom<p>bim</p></div>' );
  227. expect( stringifyModel( modelFragment ) )
  228. .to.equal(
  229. '<paragraph>foo</paragraph><paragraph>bar</paragraph>' +
  230. '<paragraph>bom</paragraph><paragraph>bim</paragraph>'
  231. );
  232. } );
  233. it( 'creates normalized model', () => {
  234. const modelFragment = editor.data.parse( '<h1><span>foo</span><span>bar</span>' );
  235. expect( stringifyModel( modelFragment ) ).to.equal( '<paragraph>foobar</paragraph>' );
  236. expect( modelFragment ).to.be.instanceof( ModelDocumentFragment );
  237. expect( modelFragment.getChild( 0 ).childCount ).to.equal( 1 );
  238. expect( modelFragment.getChild( 0 ).getChild( 0 ) ).to.be.instanceOf( ModelText );
  239. } );
  240. } );
  241. } );
  242. describe( 'editing pipeline conversion', () => {
  243. it( 'should convert paragraph', () => {
  244. setModelData( doc, '<paragraph>foo</paragraph><paragraph>bar</paragraph>' );
  245. expect( getViewData( editor.editing.view, { withoutSelection: true } ) ).to.equal( '<p>foo</p><p>bar</p>' );
  246. } );
  247. } );
  248. describe( 'autoparagraphing on data load', () => {
  249. it( 'wraps text and place selection at the beginning of that paragraph', () => {
  250. editor.setData( 'foo' );
  251. expect( getModelData( doc ) ).to.equal( '<paragraph>[]foo</paragraph>' );
  252. } );
  253. } );
  254. } );