paragraph.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. describe( 'Paragraph feature', () => {
  15. let editor, doc;
  16. beforeEach( () => {
  17. return VirtualTestEditor.create( {
  18. plugins: [ Paragraph ]
  19. } )
  20. .then( newEditor => {
  21. editor = newEditor;
  22. doc = editor.document;
  23. } );
  24. } );
  25. it( 'should be loaded', () => {
  26. expect( editor.plugins.get( Paragraph ) ).to.be.instanceOf( Paragraph );
  27. } );
  28. it( 'should set proper schema rules', () => {
  29. expect( doc.schema.hasItem( 'paragraph' ) ).to.be.true;
  30. expect( doc.schema.check( { name: 'paragraph', inside: '$root' } ) ).to.be.true;
  31. expect( doc.schema.check( { name: '$inline', inside: 'paragraph' } ) ).to.be.true;
  32. } );
  33. it( 'should have a static paragraphLikeElements property', () => {
  34. expect( Paragraph ).to.have.property( 'paragraphLikeElements' );
  35. } );
  36. describe( 'data pipeline conversions', () => {
  37. it( 'should convert paragraph', () => {
  38. editor.setData( '<p>foobar</p>' );
  39. expect( getModelData( doc, { withoutSelection: true } ) ).to.equal( '<paragraph>foobar</paragraph>' );
  40. expect( editor.getData() ).to.equal( '<p>foobar</p>' );
  41. } );
  42. it( 'should convert paragraph only', () => {
  43. editor.setData( '<p>foo<b>baz</b>bar</p>' );
  44. expect( getModelData( doc, { withoutSelection: true } ) ).to.equal( '<paragraph>foobazbar</paragraph>' );
  45. expect( editor.getData() ).to.equal( '<p>foobazbar</p>' );
  46. } );
  47. it( 'should convert multiple paragraphs', () => {
  48. editor.setData( '<p>foo</p><p>baz</p>' );
  49. expect( getModelData( doc, { withoutSelection: true } ) ).to.equal( '<paragraph>foo</paragraph><paragraph>baz</paragraph>' );
  50. expect( editor.getData() ).to.equal( '<p>foo</p><p>baz</p>' );
  51. } );
  52. describe( 'generic block converter (autoparagraphing)', () => {
  53. it( 'should autoparagraph text', () => {
  54. editor.setData( 'foo' );
  55. expect( getModelData( doc, { withoutSelection: true } ) ).to.equal( '<paragraph>foo</paragraph>' );
  56. expect( editor.getData() ).to.equal( '<p>foo</p>' );
  57. } );
  58. it( 'should autoparagraph text next to allowed element', () => {
  59. doc.schema.registerItem( 'heading1', '$block' );
  60. buildViewConverter().for( editor.data.viewToModel ).fromElement( 'h1' ).toElement( 'heading1' );
  61. const modelFragment = editor.data.parse( '<h1>foo</h1>bar<p>bom</p>' );
  62. expect( stringifyModel( modelFragment ) )
  63. .to.equal( '<heading1>foo</heading1><paragraph>bar</paragraph><paragraph>bom</paragraph>' );
  64. } );
  65. it( 'should autoparagraph 3 inline things into one paragraph', () => {
  66. const modelFragment = editor.data.parse( 'foo<b>bar</b>bom' );
  67. expect( stringifyModel( modelFragment ) )
  68. .to.equal( '<paragraph>foobarbom</paragraph>' );
  69. } );
  70. it( 'should autoparagraph h1, h2...', () => {
  71. const modelFragment = editor.data.parse( '<h1>foo</h1><h2>bar</h2>' );
  72. expect( stringifyModel( modelFragment ) )
  73. .to.equal( '<paragraph>foo</paragraph><paragraph>bar</paragraph>' );
  74. } );
  75. it( 'should autoparagraph ul > li', () => {
  76. const modelFragment = editor.data.parse( '<ul><li>foo</li><li>bar</li></ul>' );
  77. expect( stringifyModel( modelFragment ) )
  78. .to.equal( '<paragraph>foo</paragraph><paragraph>bar</paragraph>' );
  79. } );
  80. it( 'should autoparagraph tr', () => {
  81. const modelFragment = editor.data.parse( '<table><tr><td>a</td><td>b</td></tr><tr><td>c</td><td>d</td></tr></table>' );
  82. expect( stringifyModel( modelFragment ) )
  83. .to.equal( '<paragraph>ab</paragraph><paragraph>cd</paragraph>' );
  84. } );
  85. it( 'should autoparagraph inside some container', () => {
  86. doc.schema.registerItem( 'div' );
  87. doc.schema.allow( { name: '$inline', attributes: [ 'bold' ] } );
  88. doc.schema.allow( { name: 'div', inside: '$root' } );
  89. doc.schema.allow( { name: 'paragraph', inside: 'div' } );
  90. buildViewConverter().for( editor.data.viewToModel )
  91. .fromElement( 'b' )
  92. .toAttribute( 'bold', true );
  93. buildViewConverter().for( editor.data.viewToModel ).fromElement( 'div' ).toElement( 'div' );
  94. const modelFragment = editor.data.parse( '<div><ul><li>foo</li><li>bar</li></ul></div><div>bom<p>bim</p></div>' );
  95. expect( stringifyModel( modelFragment ) )
  96. .to.equal(
  97. '<div><paragraph>foo</paragraph><paragraph>bar</paragraph></div>' +
  98. '<div><paragraph>bom</paragraph><paragraph>bim</paragraph></div>'
  99. );
  100. } );
  101. it( 'should not autopargraph when disallowed element contains allowed element', () => {
  102. doc.schema.registerItem( 'heading1', '$block' );
  103. buildViewConverter().for( editor.data.viewToModel ).fromElement( 'h1' ).toElement( 'heading1' );
  104. const modelFragment = editor.data.parse( '<div><h1>foo</h1>bar</div>' );
  105. expect( stringifyModel( modelFragment ) )
  106. .to.equal( '<heading1>foo</heading1><paragraph>bar</paragraph>' );
  107. } );
  108. it( 'should not autopargraph when allowed element contains disallowed element', () => {
  109. doc.schema.registerItem( 'heading1', '$block' );
  110. buildViewConverter().for( editor.data.viewToModel ).fromElement( 'h1' ).toElement( 'heading1' );
  111. const modelFragment = editor.data.parse( '<h1><b>foo</b>bar</h1>' );
  112. expect( stringifyModel( modelFragment ) )
  113. .to.equal( '<heading1>foobar</heading1>' );
  114. } );
  115. } );
  116. } );
  117. describe( 'editing pipeline conversion', () => {
  118. it( 'should convert paragraph', () => {
  119. setModelData( doc, '<paragraph>foo</paragraph><paragraph>bar</paragraph>' );
  120. expect( getViewData( editor.editing.view, { withoutSelection: true } ) ).to.equal( '<p>foo</p><p>bar</p>' );
  121. } );
  122. } );
  123. describe( 'autoparagraphing on data load', () => {
  124. it( 'wraps text and place selection at the beginning of that paragraph', () => {
  125. editor.setData( 'foo' );
  126. expect( getModelData( doc ) ).to.equal( '<paragraph>[]foo</paragraph>' );
  127. } );
  128. } );
  129. } );