paragraph.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. /**
  2. * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import Paragraph from 'ckeditor5/paragraph/paragraph.js';
  6. import VirtualTestEditor from 'tests/core/_utils/virtualtesteditor.js';
  7. import {
  8. getData as getModelData,
  9. setData as setModelData,
  10. stringify as stringifyModel
  11. } from 'ckeditor5/engine/dev-utils/model.js';
  12. import { getData as getViewData } from 'ckeditor5/engine/dev-utils/view.js';
  13. import buildViewConverter from 'ckeditor5/engine/conversion/buildviewconverter.js';
  14. import ModelDocumentFragment from 'ckeditor5/engine/model/documentfragment.js';
  15. import ModelText from 'ckeditor5/engine/model/text.js';
  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 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 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. } );
  137. describe( 'generic block converter (paragraph-like element handling)', () => {
  138. it( 'should convert h1+h2', () => {
  139. const modelFragment = editor.data.parse( '<h1>foo</h1><h2>bar</h2>' );
  140. expect( stringifyModel( modelFragment ) )
  141. .to.equal( '<paragraph>foo</paragraph><paragraph>bar</paragraph>' );
  142. } );
  143. it( 'should convert h1+h2 (in clipboard holder)', () => {
  144. const modelFragment = editor.data.parse( '<h1>foo</h1><h2>bar</h2>', '$clipboardHolder' );
  145. expect( stringifyModel( modelFragment ) )
  146. .to.equal( '<paragraph>foo</paragraph><paragraph>bar</paragraph>' );
  147. } );
  148. it( 'should not convert h1+h2 (in a context which does not allow paragraphs)', () => {
  149. doc.schema.registerItem( 'div' );
  150. doc.schema.registerItem( 'specialRoot' );
  151. doc.schema.allow( { name: 'div', inside: 'specialRoot' } );
  152. doc.schema.allow( { name: '$text', inside: 'div' } );
  153. buildViewConverter().for( editor.data.viewToModel ).fromElement( 'div' ).toElement( 'div' );
  154. const modelFragment = editor.data.parse( '<h1>foo</h1><h2>bar</h2><div>bom</div>', 'specialRoot' );
  155. expect( stringifyModel( modelFragment ) )
  156. .to.equal( '<div>bom</div>' );
  157. } );
  158. it( 'should convert ul,ol>li', () => {
  159. const modelFragment = editor.data.parse( '<ul><li>a</li><li>b</li></ul><ol><li>c</li></ol>' );
  160. expect( stringifyModel( modelFragment ) )
  161. .to.equal( '<paragraph>a</paragraph><paragraph>b</paragraph><paragraph>c</paragraph>' );
  162. } );
  163. it( 'should convert ul,ol>li (in clipboard holder)', () => {
  164. const modelFragment = editor.data.parse( '<ul><li>a</li><li>b</li></ul><ol><li>c</li></ol>', '$clipboardHolder' );
  165. expect( stringifyModel( modelFragment ) )
  166. .to.equal( '<paragraph>a</paragraph><paragraph>b</paragraph><paragraph>c</paragraph>' );
  167. } );
  168. it( 'should convert ul>li>ul>li+li', () => {
  169. const modelFragment = editor.data.parse( '<ul><li>a<ul><li>b</li><li>c</li></ul></li></ul>' );
  170. expect( stringifyModel( modelFragment ) )
  171. .to.equal( '<paragraph>a</paragraph><paragraph>b</paragraph><paragraph>c</paragraph>' );
  172. } );
  173. // "b" is not autoparagraphed because clipboard holder allows text nodes.
  174. // There's a similar integrational test what's going to happen when pasting in paragraph-integration.js.
  175. it( 'should convert ul>li>ul>li+li (in clipboard holder)', () => {
  176. const modelFragment = editor.data.parse( '<ul><li>a<ul><li>b</li><li>c</li></ul></li></ul>', '$clipboardHolder' );
  177. expect( stringifyModel( modelFragment ) )
  178. .to.equal( 'a<paragraph>b</paragraph><paragraph>c</paragraph>' );
  179. } );
  180. it( 'should convert ul>li>p,text', () => {
  181. const modelFragment = editor.data.parse( '<ul><li><p>a</p>b</li></ul>' );
  182. expect( stringifyModel( modelFragment ) )
  183. .to.equal( '<paragraph>a</paragraph><paragraph>b</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>p,text (in clipboard holder)', () => {
  188. const modelFragment = editor.data.parse( '<ul><li><p>a</p>b</li></ul>', '$clipboardHolder' );
  189. expect( stringifyModel( modelFragment ) )
  190. .to.equal( '<paragraph>a</paragraph>b' );
  191. } );
  192. it( 'should convert td', () => {
  193. const modelFragment = editor.data.parse( '<table><tr><td>a</td><td>b</td></tr><tr><td>c</td><td>d</td></tr></table>' );
  194. expect( stringifyModel( modelFragment ) )
  195. .to.equal( '<paragraph>a</paragraph><paragraph>b</paragraph><paragraph>c</paragraph><paragraph>d</paragraph>' );
  196. } );
  197. it( 'should convert td (in clipboardHolder)', () => {
  198. const modelFragment = editor.data.parse(
  199. '<table><tr><td>a</td><td>b</td></tr><tr><td>c</td><td>d</td></tr></table>',
  200. '$clipboardHolder'
  201. );
  202. expect( stringifyModel( modelFragment ) )
  203. .to.equal( '<paragraph>a</paragraph><paragraph>b</paragraph><paragraph>c</paragraph><paragraph>d</paragraph>' );
  204. } );
  205. it( 'should convert li inside converted container', () => {
  206. doc.schema.registerItem( 'div' );
  207. doc.schema.allow( { name: 'div', inside: '$root' } );
  208. doc.schema.allow( { name: 'paragraph', inside: 'div' } );
  209. buildViewConverter().for( editor.data.viewToModel ).fromElement( 'div' ).toElement( 'div' );
  210. const modelFragment = editor.data.parse( '<div><ul><li>foo</li><li>bar</li></ul></div><div>bom<p>bim</p></div>' );
  211. expect( stringifyModel( modelFragment ) )
  212. .to.equal(
  213. '<div><paragraph>foo</paragraph><paragraph>bar</paragraph></div>' +
  214. '<div><paragraph>bom</paragraph><paragraph>bim</paragraph></div>'
  215. );
  216. } );
  217. it( 'should convert li inside disallowed container', () => {
  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. '<paragraph>foo</paragraph><paragraph>bar</paragraph>' +
  222. '<paragraph>bom</paragraph><paragraph>bim</paragraph>'
  223. );
  224. } );
  225. it( 'creates normalized model', () => {
  226. const modelFragment = editor.data.parse( '<h1><span>foo</span><span>bar</span>' );
  227. expect( stringifyModel( modelFragment ) ).to.equal( '<paragraph>foobar</paragraph>' );
  228. expect( modelFragment ).to.be.instanceof( ModelDocumentFragment );
  229. expect( modelFragment.getChild( 0 ).childCount ).to.equal( 1 );
  230. expect( modelFragment.getChild( 0 ).getChild( 0 ) ).to.be.instanceOf( ModelText );
  231. } );
  232. } );
  233. } );
  234. describe( 'editing pipeline conversion', () => {
  235. it( 'should convert paragraph', () => {
  236. setModelData( doc, '<paragraph>foo</paragraph><paragraph>bar</paragraph>' );
  237. expect( getViewData( editor.editing.view, { withoutSelection: true } ) ).to.equal( '<p>foo</p><p>bar</p>' );
  238. } );
  239. } );
  240. describe( 'autoparagraphing on data load', () => {
  241. it( 'wraps text and place selection at the beginning of that paragraph', () => {
  242. editor.setData( 'foo' );
  243. expect( getModelData( doc ) ).to.equal( '<paragraph>[]foo</paragraph>' );
  244. } );
  245. } );
  246. } );