paragraph.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  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. } );
  126. describe( 'generic block converter (paragraph-like element handling)', () => {
  127. it( 'should convert h1+h2', () => {
  128. const modelFragment = editor.data.parse( '<h1>foo</h1><h2>bar</h2>' );
  129. expect( stringifyModel( modelFragment ) )
  130. .to.equal( '<paragraph>foo</paragraph><paragraph>bar</paragraph>' );
  131. } );
  132. it( 'should convert h1+h2 (in clipboard holder)', () => {
  133. const modelFragment = editor.data.parse( '<h1>foo</h1><h2>bar</h2>', '$clipboardHolder' );
  134. expect( stringifyModel( modelFragment ) )
  135. .to.equal( '<paragraph>foo</paragraph><paragraph>bar</paragraph>' );
  136. } );
  137. it( 'should not convert h1+h2 (in a context which does not allow paragraphs)', () => {
  138. doc.schema.registerItem( 'div' );
  139. doc.schema.registerItem( 'specialRoot' );
  140. doc.schema.allow( { name: 'div', inside: 'specialRoot' } );
  141. doc.schema.allow( { name: '$text', inside: 'div' } );
  142. buildViewConverter().for( editor.data.viewToModel ).fromElement( 'div' ).toElement( 'div' );
  143. const modelFragment = editor.data.parse( '<h1>foo</h1><h2>bar</h2><div>bom</div>', 'specialRoot' );
  144. expect( stringifyModel( modelFragment ) )
  145. .to.equal( '<div>bom</div>' );
  146. } );
  147. it( 'should convert ul,ol>li', () => {
  148. const modelFragment = editor.data.parse( '<ul><li>a</li><li>b</li></ul><ol><li>c</li></ol>' );
  149. expect( stringifyModel( modelFragment ) )
  150. .to.equal( '<paragraph>a</paragraph><paragraph>b</paragraph><paragraph>c</paragraph>' );
  151. } );
  152. it( 'should convert ul,ol>li (in clipboard holder)', () => {
  153. const modelFragment = editor.data.parse( '<ul><li>a</li><li>b</li></ul><ol><li>c</li></ol>', '$clipboardHolder' );
  154. expect( stringifyModel( modelFragment ) )
  155. .to.equal( '<paragraph>a</paragraph><paragraph>b</paragraph><paragraph>c</paragraph>' );
  156. } );
  157. it( 'should convert ul>li>ul>li+li', () => {
  158. const modelFragment = editor.data.parse( '<ul><li>a<ul><li>b</li><li>c</li></ul></li></ul>' );
  159. expect( stringifyModel( modelFragment ) )
  160. .to.equal( '<paragraph>a</paragraph><paragraph>b</paragraph><paragraph>c</paragraph>' );
  161. } );
  162. // "b" is not autoparagraphed because clipboard holder allows text nodes.
  163. // There's a similar integrational test what's going to happen when pasting in paragraph-integration.js.
  164. it( 'should convert ul>li>ul>li+li (in clipboard holder)', () => {
  165. const modelFragment = editor.data.parse( '<ul><li>a<ul><li>b</li><li>c</li></ul></li></ul>', '$clipboardHolder' );
  166. expect( stringifyModel( modelFragment ) )
  167. .to.equal( 'a<paragraph>b</paragraph><paragraph>c</paragraph>' );
  168. } );
  169. it( 'should convert ul>li>p,text', () => {
  170. const modelFragment = editor.data.parse( '<ul><li><p>a</p>b</li></ul>' );
  171. expect( stringifyModel( modelFragment ) )
  172. .to.equal( '<paragraph>a</paragraph><paragraph>b</paragraph>' );
  173. } );
  174. // "b" is not autoparagraphed because clipboard holder allows text nodes.
  175. // There's a similar integrational test what's going to happen when pasting in paragraph-integration.js.
  176. it( 'should convert ul>li>p,text (in clipboard holder)', () => {
  177. const modelFragment = editor.data.parse( '<ul><li><p>a</p>b</li></ul>', '$clipboardHolder' );
  178. expect( stringifyModel( modelFragment ) )
  179. .to.equal( '<paragraph>a</paragraph>b' );
  180. } );
  181. it( 'should convert td', () => {
  182. const modelFragment = editor.data.parse( '<table><tr><td>a</td><td>b</td></tr><tr><td>c</td><td>d</td></tr></table>' );
  183. expect( stringifyModel( modelFragment ) )
  184. .to.equal( '<paragraph>a</paragraph><paragraph>b</paragraph><paragraph>c</paragraph><paragraph>d</paragraph>' );
  185. } );
  186. it( 'should convert td (in clipboardHolder)', () => {
  187. const modelFragment = editor.data.parse(
  188. '<table><tr><td>a</td><td>b</td></tr><tr><td>c</td><td>d</td></tr></table>',
  189. '$clipboardHolder'
  190. );
  191. expect( stringifyModel( modelFragment ) )
  192. .to.equal( '<paragraph>a</paragraph><paragraph>b</paragraph><paragraph>c</paragraph><paragraph>d</paragraph>' );
  193. } );
  194. it( 'should convert li inside converted container', () => {
  195. doc.schema.registerItem( 'div' );
  196. doc.schema.allow( { name: 'div', inside: '$root' } );
  197. doc.schema.allow( { name: 'paragraph', inside: 'div' } );
  198. buildViewConverter().for( editor.data.viewToModel ).fromElement( 'div' ).toElement( 'div' );
  199. const modelFragment = editor.data.parse( '<div><ul><li>foo</li><li>bar</li></ul></div><div>bom<p>bim</p></div>' );
  200. expect( stringifyModel( modelFragment ) )
  201. .to.equal(
  202. '<div><paragraph>foo</paragraph><paragraph>bar</paragraph></div>' +
  203. '<div><paragraph>bom</paragraph><paragraph>bim</paragraph></div>'
  204. );
  205. } );
  206. it( 'should convert li inside disallowed container', () => {
  207. const modelFragment = editor.data.parse( '<div><ul><li>foo</li><li>bar</li></ul></div><div>bom<p>bim</p></div>' );
  208. expect( stringifyModel( modelFragment ) )
  209. .to.equal(
  210. '<paragraph>foo</paragraph><paragraph>bar</paragraph>' +
  211. '<paragraph>bom</paragraph><paragraph>bim</paragraph>'
  212. );
  213. } );
  214. it( 'creates normalized model', () => {
  215. const modelFragment = editor.data.parse( '<h1><span>foo</span><span>bar</span>' );
  216. expect( stringifyModel( modelFragment ) ).to.equal( '<paragraph>foobar</paragraph>' );
  217. expect( modelFragment ).to.be.instanceof( ModelDocumentFragment );
  218. expect( modelFragment.getChild( 0 ).childCount ).to.equal( 1 );
  219. expect( modelFragment.getChild( 0 ).getChild( 0 ) ).to.be.instanceOf( ModelText );
  220. } );
  221. } );
  222. } );
  223. describe( 'editing pipeline conversion', () => {
  224. it( 'should convert paragraph', () => {
  225. setModelData( doc, '<paragraph>foo</paragraph><paragraph>bar</paragraph>' );
  226. expect( getViewData( editor.editing.view, { withoutSelection: true } ) ).to.equal( '<p>foo</p><p>bar</p>' );
  227. } );
  228. } );
  229. describe( 'autoparagraphing on data load', () => {
  230. it( 'wraps text and place selection at the beginning of that paragraph', () => {
  231. editor.setData( 'foo' );
  232. expect( getModelData( doc ) ).to.equal( '<paragraph>[]foo</paragraph>' );
  233. } );
  234. } );
  235. } );