Преглед на файлове

Merge pull request #12 from ckeditor/t/3

Introduced simple autoparagraphing.
Piotrek Koszuliński преди 9 години
родител
ревизия
572754142e

+ 2 - 0
packages/ckeditor5-paragraph/package.json

@@ -9,6 +9,8 @@
   },
   "devDependencies": {
     "@ckeditor/ckeditor5-dev-lint": "^1.0.1",
+    "ckeditor5-clipboard": "ckeditor/ckeditor5-clipboard",
+    "ckeditor5-heading": "ckeditor/ckeditor5-heading",
     "gulp": "^3.9.0",
     "guppy-pre-commit": "^0.4.0"
   },

+ 185 - 1
packages/ckeditor5-paragraph/src/paragraph.js

@@ -8,9 +8,19 @@
  */
 
 import Plugin from '../core/plugin.js';
+
+import ModelElement from '../engine/model/element.js';
+import ModelPosition from '../engine/model/position.js';
+import ModelRange from '../engine/model/range.js';
+import ViewElement from '../engine/view/element.js';
+import ViewRange from '../engine/view/range.js';
+
+import modelWriter from '../engine/model/writer.js';
 import buildModelConverter from '../engine/conversion/buildmodelconverter.js';
 import buildViewConverter from '../engine/conversion/buildviewconverter.js';
 
+import isArray from '../utils/lib/lodash/isArray.js';
+
 /**
  * The paragraph feature for the editor.
  * Introduces the `<paragraph>` element in the model which renders as a `<p>` element in the DOM and data.
@@ -23,11 +33,12 @@ export default class Paragraph extends Plugin {
 	 */
 	init() {
 		const editor = this.editor;
+		const doc = editor.document;
 		const data = editor.data;
 		const editing = editor.editing;
 
 		// Schema.
-		editor.document.schema.registerItem( 'paragraph', '$block' );
+		doc.schema.registerItem( 'paragraph', '$block' );
 
 		// Build converter from model to view for data and editing pipelines.
 		buildModelConverter().for( data.modelToView, editing.modelToView )
@@ -38,5 +49,178 @@ export default class Paragraph extends Plugin {
 		buildViewConverter().for( data.viewToModel )
 			.fromElement( 'p' )
 			.toElement( 'paragraph' );
+
+		// Autoparagraph text.
+		data.viewToModel.on( 'text', ( evt, data, consumable, conversionApi ) => {
+			autoparagraphText( doc, evt, data, consumable, conversionApi );
+		}, { priority: 'lowest' } );
+
+		// Post-fix potential subsequent paragraphs created by autoparagraphText().
+		data.viewToModel.on( 'element', mergeSubsequentParagraphs, { priority: 'lowest' } );
+		data.viewToModel.on( 'documentFragment', mergeSubsequentParagraphs, { priority: 'lowest' } );
+
+		// Convert paragraph-like elements to paragraphs if they weren't consumed.
+		// It's a 'low' priority in order to hook in before the default 'element' converter
+		// which would then convert children before handling this element.
+		data.viewToModel.on( 'element', ( evt, data, consumable, conversionApi ) => {
+			autoparagraphParagraphLikeElements( doc, evt, data, consumable, conversionApi );
+		}, { priority: 'low' } );
+	}
+}
+
+/**
+ * List of element names which should be treated by the autoparagraphing algorithms as
+ * paragraph-like. This means that e.g. the following content:
+ *
+ *		<h1>Foo</h1>
+ *		<table>
+ *			<tr>
+ *				<td>X</td>
+ *				<td>
+ *					<ul>
+ *						<li>Y</li>
+ *						<li>Z</li>
+ *					</ul>
+ *				</td>
+ *			</tr>
+ *		</table>
+ *
+ * Contains five paragraph-like elements – `<h1>` and two `<td>` and two `<li>`.
+ * Hence, if none of the features is going to convert  those elements the above content will be automatically handled
+ * by the paragraph feature and converted to:
+ *
+ *		<p>Foo</p>
+ *		<p>X</p>
+ *		<p>Y</p>
+ *		<p>Z</p>
+ *
+ * Note: The `<td>` containing two `<li>` elements was ignored – the inner-most paragraph-like elements
+ * have priority upon conversion.
+ *
+ * @member {Set.<String>} module:paragraph/paragraph~Paragraph.paragraphLikeElements
+ */
+Paragraph.paragraphLikeElements = new Set( [
+	'blockquote',
+	'dd',
+	'div',
+	'dt',
+	'h1',
+	'h2',
+	'h3',
+	'h4',
+	'h5',
+	'h6',
+	'li',
+	'p',
+	'td'
+] );
+
+const paragraphsToMerge = new WeakSet();
+
+function autoparagraphText( doc, evt, data, consumable, conversionApi ) {
+	// If text wasn't consumed by the default converter...
+	if ( !consumable.test( data.input ) ) {
+		return;
+	}
+
+	// And paragraph is allowed in this context...
+	if ( !doc.schema.check( { name: 'paragraph', inside: data.context } ) ) {
+		return;
 	}
+
+	// Let's do autoparagraphing.
+
+	const paragraph = new ModelElement( 'paragraph' );
+
+	paragraphsToMerge.add( paragraph );
+
+	data.context.push( paragraph );
+
+	const text = conversionApi.convertItem( data.input, consumable, data );
+
+	if ( text ) {
+		data.output = paragraph;
+		paragraph.appendChildren( text );
+	}
+
+	data.context.pop();
+}
+
+function autoparagraphParagraphLikeElements( doc, evt, data, consumable, conversionApi ) {
+	// If this is a paragraph-like element...
+	if ( !Paragraph.paragraphLikeElements.has( data.input.name ) ) {
+		return;
+	}
+
+	// Which wasn't consumed by its own converter...
+	if ( !consumable.test( data.input, { name: true } ) ) {
+		return;
+	}
+
+	// And there are no other paragraph-like elements inside this tree...
+	if ( hasParagraphLikeContent( data.input ) ) {
+		return;
+	}
+
+	// And paragraph is allowed in this context...
+	if ( !doc.schema.check( { name: 'paragraph', inside: data.context } ) ) {
+		return;
+	}
+
+	// Let's convert this element to a paragraph and then all its children.
+
+	consumable.consume( data.input, { name: true } );
+
+	const paragraph = new ModelElement( 'paragraph' );
+
+	data.context.push( paragraph );
+
+	const convertedChildren = conversionApi.convertChildren( data.input, consumable, data );
+
+	paragraph.appendChildren( modelWriter.normalizeNodes( convertedChildren ) );
+
+	// Remove the created paragraph from the stack for other converters.
+	// See https://github.com/ckeditor/ckeditor5-engine/issues/736
+	data.context.pop();
+
+	data.output = paragraph;
+}
+
+// Merges subsequent paragraphs if they should be merged (see shouldMerge).
+function mergeSubsequentParagraphs( evt, data ) {
+	if ( !data.output ) {
+		return;
+	}
+
+	let node;
+
+	if ( isArray( data.output ) ) {
+		node = data.output[ 0 ];
+	} else {
+		node = data.output.getChild( 0 );
+	}
+
+	while ( node && node.nextSibling ) {
+		const nextSibling = node.nextSibling;
+
+		if ( paragraphsToMerge.has( node ) && paragraphsToMerge.has( nextSibling ) ) {
+			modelWriter.insert( ModelPosition.createAt( node, 'end' ), Array.from( nextSibling.getChildren() ) );
+			modelWriter.remove( ModelRange.createOn( nextSibling ) );
+		} else {
+			node = node.nextSibling;
+		}
+	}
+}
+
+// Checks whether an element has paragraph-like descendant.
+function hasParagraphLikeContent( element ) {
+	const range = ViewRange.createIn( element );
+
+	for ( const value of range ) {
+		if ( value.item instanceof ViewElement && Paragraph.paragraphLikeElements.has( value.item.name ) ) {
+			return true;
+		}
+	}
+
+	return false;
 }

+ 64 - 0
packages/ckeditor5-paragraph/tests/manual/clipboard-integration.html

@@ -0,0 +1,64 @@
+<head>
+	<link rel="stylesheet" href="/theme/ckeditor.css">
+	<style>
+		#copybin dd,
+		#copybin div,
+		#copybin dt,
+		#copybin h1,
+		#copybin h2,
+		#copybin h3,
+		#copybin h4,
+		#copybin h5,
+		#copybin h6,
+		#copybin li,
+		#copybin p,
+		#copybin td
+		{
+			border: solid 1px blue;
+			padding: 3px;
+		}
+	</style>
+</head>
+
+<div id="editor">
+	<p>Paste here</p>
+</div>
+
+<h2>Copy from here</h2>
+
+<div id=copybin>
+	<h1>Heading 1</h1>
+	<h2><a href="x">Heading</a> 2</h2>
+	<h3><em>Heading</em> 3</h3>
+	<h4>Heading 4</h4>
+	<h5><em>Heading</em> 5</h5>
+	<h6>Heading 6</h6>
+
+	<ul>
+		<li>List item 1</li>
+		<li>List item 2</li>
+		<li>List item 3</li>
+		<li>
+			List item 4
+			<ol>
+				<li>List item 4.1</li>
+				<li>
+					<h1>Bar</h1>
+					<div>Foo</div>
+					<ul>
+						<li>Bom!</li>
+					</ul>
+				</li>
+			</ol>
+		</li>
+	</ul>
+
+	<div>
+		<div>Foo</div>
+		<table>
+			<caption>Foo!</caption>
+			<tr><td><strong>1.1</strong></td><td>1.2</td></tr>
+			<tr><td>2.1</td><td>2.2</td></tr>
+		</table>
+	</div>
+</div>

+ 36 - 0
packages/ckeditor5-paragraph/tests/manual/clipboard-integration.js

@@ -0,0 +1,36 @@
+/**
+ * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
+ * For licensing, see LICENSE.md.
+ */
+
+/* globals console, window, document */
+
+import ClassicEditor from 'ckeditor5/editor-classic/classic.js';
+import Typing from 'ckeditor5/typing/typing.js';
+import Paragraph from 'ckeditor5/paragraph/paragraph.js';
+import Undo from 'ckeditor5/undo/undo.js';
+import Enter from 'ckeditor5/enter/enter.js';
+import Clipboard from 'ckeditor5/clipboard/clipboard.js';
+import Link from 'ckeditor5/link/link.js';
+import Bold from 'ckeditor5/basic-styles/bold.js';
+import Italic from 'ckeditor5/basic-styles/italic.js';
+
+ClassicEditor.create( document.querySelector( '#editor' ), {
+	plugins: [
+		Typing,
+		Paragraph,
+		Undo,
+		Enter,
+		Clipboard,
+		Link,
+		Bold,
+		Italic
+	],
+	toolbar: [ 'bold', 'italic', 'link', 'undo', 'redo' ]
+} )
+.then( editor => {
+	window.editor = editor;
+} )
+.catch( err => {
+	console.error( err.stack );
+} );

+ 7 - 0
packages/ckeditor5-paragraph/tests/manual/clipboard-integration.md

@@ -0,0 +1,7 @@
+## Paragraph – clipboard integration
+
+Check how the paragraph feature handles converting paragraph-like blocks which don't have their own converters.
+
+Such blocks can be copied from the second part of this test. Each of them is marked with blue border.
+
+You can also paste the content of [this article](https://medium.com/content-uneditable/contenteditable-the-good-the-bad-and-the-ugly-261a38555e9c#.k6kcioz51).

+ 142 - 0
packages/ckeditor5-paragraph/tests/paragraph-intergration.js

@@ -0,0 +1,142 @@
+/**
+ * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
+ * For licensing, see LICENSE.md.
+ */
+
+import Paragraph from 'ckeditor5/paragraph/paragraph.js';
+import Clipboard from 'ckeditor5/clipboard/clipboard.js';
+import HeadingEngine from 'ckeditor5/heading/headingengine.js';
+import VirtualTestEditor from 'tests/core/_utils/virtualtesteditor.js';
+import {
+	getData as getModelData,
+	setData as setModelData
+} from 'ckeditor5/engine/dev-utils/model.js';
+import { parse as parseView } from 'ckeditor5/engine/dev-utils/view.js';
+
+describe( 'Paragraph feature – integration', () => {
+	describe( 'with clipboard', () => {
+		it( 'pastes h1+h2+p as p+p+p when heading feature is not present', () => {
+			return VirtualTestEditor.create( {
+					plugins: [ Paragraph, Clipboard ]
+				} )
+				.then( newEditor => {
+					const editor = newEditor;
+					const doc = editor.document;
+
+					setModelData( doc, '<paragraph>[]</paragraph>' );
+
+					editor.editing.view.fire( 'clipboardInput', {
+						content: parseView( '<h1>foo</h1><h2>bar</h2><p>bom</p>' )
+					} );
+
+					expect( getModelData( doc ) ).to.equal( '<paragraph>foo</paragraph><paragraph>bar</paragraph><paragraph>bom[]</paragraph>' );
+				} );
+		} );
+
+		// Explainer: the heading feature is configured to handle h2-h4 elements, so h1 has no handler.
+		it( 'pastes h1+h2+p as p+h2+p when heading feature is present', () => {
+			return VirtualTestEditor.create( {
+					plugins: [ Paragraph, Clipboard, HeadingEngine ]
+				} )
+				.then( newEditor => {
+					const editor = newEditor;
+					const doc = editor.document;
+
+					setModelData( doc, '<paragraph>[]</paragraph>' );
+
+					editor.editing.view.fire( 'clipboardInput', {
+						content: parseView( '<h1>foo</h1><h2>bar</h2><p>bom</p>' )
+					} );
+
+					expect( getModelData( doc ) ).to.equal( '<paragraph>foo</paragraph><heading1>bar</heading1><paragraph>bom[]</paragraph>' );
+				} );
+		} );
+
+		it( 'pastes ul>li+li as p+p when list feature is not present', () => {
+			return VirtualTestEditor.create( {
+					plugins: [ Paragraph, Clipboard ]
+				} )
+				.then( newEditor => {
+					const editor = newEditor;
+					const doc = editor.document;
+
+					setModelData( doc, '<paragraph>[]</paragraph>' );
+
+					editor.editing.view.fire( 'clipboardInput', {
+						content: parseView( '<ul><li>foo</li><li>bar</li></ul>' )
+					} );
+
+					expect( getModelData( doc ) ).to.equal( '<paragraph>foo</paragraph><paragraph>bar[]</paragraph>' );
+				} );
+		} );
+
+		// Check whether the paragraph feature doesn't breaking pasting such content by trying to
+		// handle the li element.
+		it( 'pastes ul>li>h2+h3+p as h2+h3+p when heading feature is present', () => {
+			return VirtualTestEditor.create( {
+					plugins: [ Paragraph, Clipboard, HeadingEngine ]
+				} )
+				.then( newEditor => {
+					const editor = newEditor;
+					const doc = editor.document;
+
+					setModelData( doc, '<paragraph>[]</paragraph>' );
+
+					editor.editing.view.fire( 'clipboardInput', {
+						content: parseView( '<ul><li>x</li><li><h2>foo</h2><h3>bar</h3><p>bom</p></li><li>x</li></ul>' )
+					} );
+
+					expect( getModelData( doc ) ).to.equal(
+						'<paragraph>x</paragraph>' +
+						'<heading1>foo</heading1><heading2>bar</heading2><paragraph>bom</paragraph>' +
+						'<paragraph>x[]</paragraph>'
+					);
+				} );
+		} );
+
+		// See 'should convert ul>li>ul>li+li (in clipboard holder)' in clipboard.js.
+		it( 'pastes ul>li>ul>li+li', () => {
+			return VirtualTestEditor.create( {
+					plugins: [ Paragraph, Clipboard ]
+				} )
+				.then( newEditor => {
+					const editor = newEditor;
+					const doc = editor.document;
+
+					setModelData( doc, '<paragraph>[]</paragraph>' );
+
+					editor.editing.view.fire( 'clipboardInput', {
+						content: parseView( '<ul><li>a<ul><li>b</li><li>c</li></ul></li></ul>' )
+					} );
+
+					expect( getModelData( doc ) ).to.equal(
+						'<paragraph>a</paragraph>' +
+						'<paragraph>b</paragraph>' +
+						'<paragraph>c[]</paragraph>'
+					);
+				} );
+		} );
+
+		// See 'should convert ul>li>p,text (in clipboard holder)' in clipboard.js.
+		it( 'pastes ul>li>p,text', () => {
+			return VirtualTestEditor.create( {
+					plugins: [ Paragraph, Clipboard ]
+				} )
+				.then( newEditor => {
+					const editor = newEditor;
+					const doc = editor.document;
+
+					setModelData( doc, '<paragraph>[]</paragraph>' );
+
+					editor.editing.view.fire( 'clipboardInput', {
+						content: parseView( '<ul><li><p>a</p>b</li></ul>' )
+					} );
+
+					expect( getModelData( doc ) ).to.equal(
+						'<paragraph>a</paragraph>' +
+						'<paragraph>b[]</paragraph>'
+					);
+				} );
+		} );
+	} );
+} );

+ 278 - 3
packages/ckeditor5-paragraph/tests/paragraph.js

@@ -5,9 +5,18 @@
 
 import Paragraph from 'ckeditor5/paragraph/paragraph.js';
 import VirtualTestEditor from 'tests/core/_utils/virtualtesteditor.js';
-import { getData as getModelData } from 'ckeditor5/engine/dev-utils/model.js';
+import {
+	getData as getModelData,
+	setData as setModelData,
+	stringify as stringifyModel
+} from 'ckeditor5/engine/dev-utils/model.js';
 import { getData as getViewData } from 'ckeditor5/engine/dev-utils/view.js';
 
+import buildViewConverter from 'ckeditor5/engine/conversion/buildviewconverter.js';
+
+import ModelDocumentFragment from 'ckeditor5/engine/model/documentfragment.js';
+import ModelText from 'ckeditor5/engine/model/text.js';
+
 describe( 'Paragraph feature', () => {
 	let editor, doc;
 
@@ -31,6 +40,10 @@ describe( 'Paragraph feature', () => {
 		expect( doc.schema.check( { name: '$inline', inside: 'paragraph' } ) ).to.be.true;
 	} );
 
+	it( 'should have a static paragraphLikeElements property', () => {
+		expect( Paragraph ).to.have.property( 'paragraphLikeElements' );
+	} );
+
 	describe( 'data pipeline conversions', () => {
 		it( 'should convert paragraph', () => {
 			editor.setData( '<p>foobar</p>' );
@@ -52,14 +65,276 @@ describe( 'Paragraph feature', () => {
 			expect( getModelData( doc, { withoutSelection: true } ) ).to.equal( '<paragraph>foo</paragraph><paragraph>baz</paragraph>' );
 			expect( editor.getData() ).to.equal( '<p>foo</p><p>baz</p>' );
 		} );
+
+		describe( 'generic text converter (text autoparagraphing)', () => {
+			it( 'should autoparagraph text', () => {
+				editor.setData( 'foo' );
+
+				expect( getModelData( doc, { withoutSelection: true } ) ).to.equal( '<paragraph>foo</paragraph>' );
+				expect( editor.getData() ).to.equal( '<p>foo</p>' );
+			} );
+
+			it( 'should not autoparagraph text (in clipboard holder)', () => {
+				const modelFragment = editor.data.parse( 'foo', '$clipboardHolder' );
+
+				expect( stringifyModel( modelFragment ) )
+					.to.equal( 'foo' );
+			} );
+
+			it( 'should not autoparagraph text (in a context which does not allow paragraphs', () => {
+				doc.schema.registerItem( 'specialRoot' );
+
+				const modelFragment = editor.data.parse( 'foo', 'specialRoot' );
+
+				expect( stringifyModel( modelFragment ) )
+					.to.equal( '' );
+			} );
+
+			it( 'should autoparagraph text next to allowed element', () => {
+				doc.schema.registerItem( 'heading1', '$block' );
+				buildViewConverter().for( editor.data.viewToModel ).fromElement( 'h1' ).toElement( 'heading1' );
+
+				const modelFragment = editor.data.parse( '<h1>foo</h1>bar<p>bom</p>' );
+
+				expect( stringifyModel( modelFragment ) )
+					.to.equal( '<heading1>foo</heading1><paragraph>bar</paragraph><paragraph>bom</paragraph>' );
+			} );
+
+			it( 'should autoparagraph 3 inline nodes into one paragraph', () => {
+				const modelFragment = editor.data.parse( 'foo<b>bar</b>bom' );
+
+				expect( stringifyModel( modelFragment ) )
+					.to.equal( '<paragraph>foobarbom</paragraph>' );
+			} );
+
+			it( 'should not autoparagraph 3 inline nodes (in clipboardHolder)', () => {
+				const modelFragment = editor.data.parse( 'foo<b>bar</b>bom', '$clipboardHolder' );
+
+				expect( stringifyModel( modelFragment ) )
+					.to.equal( 'foobarbom' );
+			} );
+
+			it( 'should autoparagraph text inside converted container', () => {
+				doc.schema.registerItem( 'div' );
+				doc.schema.allow( { name: 'div', inside: '$root' } );
+				doc.schema.allow( { name: 'paragraph', inside: 'div' } );
+
+				buildViewConverter().for( editor.data.viewToModel ).fromElement( 'div' ).toElement( 'div' );
+
+				const modelFragment = editor.data.parse( '<div>foo</div><div>bom<p>bim</p></div>' );
+
+				expect( stringifyModel( modelFragment ) )
+					.to.equal(
+						'<div><paragraph>foo</paragraph></div>' +
+						'<div><paragraph>bom</paragraph><paragraph>bim</paragraph></div>'
+					);
+			} );
+
+			it( 'should autoparagraph text inside disallowed element next to allowed element', () => {
+				doc.schema.registerItem( 'heading1', '$block' );
+				buildViewConverter().for( editor.data.viewToModel ).fromElement( 'h1' ).toElement( 'heading1' );
+
+				const modelFragment = editor.data.parse( '<div><h1>foo</h1>bar</div>' );
+
+				expect( stringifyModel( modelFragment ) )
+					.to.equal( '<heading1>foo</heading1><paragraph>bar</paragraph>' );
+			} );
+
+			it( 'should not autoparagraph text in disallowed element', () => {
+				doc.schema.registerItem( 'heading1', '$block' );
+				buildViewConverter().for( editor.data.viewToModel ).fromElement( 'h1' ).toElement( 'heading1' );
+
+				const modelFragment = editor.data.parse( '<h1><b>foo</b>bar</h1>' );
+
+				expect( stringifyModel( modelFragment ) )
+					.to.equal( '<heading1>foobar</heading1>' );
+			} );
+
+			it( 'should not fail when text is not allowed in paragraph', () => {
+				doc.schema.disallow( { name: '$text', inside: [ '$root', 'paragraph' ] } );
+
+				const modelFragment = editor.data.parse( 'foo' );
+
+				expect( stringifyModel( modelFragment ) ).to.equal( '' );
+			} );
+
+			it( 'creates normalized model', () => {
+				const modelFragment = editor.data.parse( 'foo<b>bar</b>bom' );
+
+				expect( modelFragment ).to.be.instanceof( ModelDocumentFragment );
+				expect( modelFragment.getChild( 0 ).childCount ).to.equal( 1 );
+				expect( modelFragment.getChild( 0 ).getChild( 0 ) ).to.be.instanceOf( ModelText );
+			} );
+
+			it( 'does not break converting inline elements', () => {
+				doc.schema.allow( { name: '$inline', attributes: [ 'bold' ] } );
+				buildViewConverter().for( editor.data.viewToModel )
+					.fromElement( 'b' )
+					.toAttribute( 'bold', true );
+
+				const modelFragment = editor.data.parse( 'foo<b>bar</b>bom' );
+
+				// The result of this test is wrong due to https://github.com/ckeditor/ckeditor5-paragraph/issues/10.
+				// It's meant to catch the odd situation in mergeSubsequentParagraphs when data.output may be an array
+				// for a while
+				expect( stringifyModel( modelFragment ) ).to.equal( '<paragraph>foobarbom</paragraph>' );
+			} );
+
+			// This test was taken from the list package.
+			it( 'does not break when some converter returns nothing', () => {
+				editor.data.viewToModel.on( 'element:li', ( evt, data, consumable ) => {
+					consumable.consume( data.input, { name: true } );
+				}, { priority: 'highest' } );
+
+				const modelFragment = editor.data.parse( '<ul><li></li></ul>' );
+
+				expect( stringifyModel( modelFragment ) ).to.equal( '' );
+			} );
+		} );
+
+		describe( 'generic block converter (paragraph-like element handling)', () => {
+			it( 'should convert h1+h2', () => {
+				const modelFragment = editor.data.parse( '<h1>foo</h1><h2>bar</h2>' );
+
+				expect( stringifyModel( modelFragment ) )
+					.to.equal( '<paragraph>foo</paragraph><paragraph>bar</paragraph>' );
+			} );
+
+			it( 'should convert h1+h2 (in clipboard holder)', () => {
+				const modelFragment = editor.data.parse( '<h1>foo</h1><h2>bar</h2>', '$clipboardHolder' );
+
+				expect( stringifyModel( modelFragment ) )
+					.to.equal( '<paragraph>foo</paragraph><paragraph>bar</paragraph>' );
+			} );
+
+			it( 'should not convert h1+h2 (in a context which does not allow paragraphs)', () => {
+				doc.schema.registerItem( 'div' );
+				doc.schema.registerItem( 'specialRoot' );
+				doc.schema.allow( { name: 'div', inside: 'specialRoot' } );
+				doc.schema.allow( { name: '$text', inside: 'div' } );
+
+				buildViewConverter().for( editor.data.viewToModel ).fromElement( 'div' ).toElement( 'div' );
+
+				const modelFragment = editor.data.parse( '<h1>foo</h1><h2>bar</h2><div>bom</div>', 'specialRoot' );
+
+				expect( stringifyModel( modelFragment ) )
+					.to.equal( '<div>bom</div>' );
+			} );
+
+			it( 'should convert ul,ol>li', () => {
+				const modelFragment = editor.data.parse( '<ul><li>a</li><li>b</li></ul><ol><li>c</li></ol>' );
+
+				expect( stringifyModel( modelFragment ) )
+					.to.equal( '<paragraph>a</paragraph><paragraph>b</paragraph><paragraph>c</paragraph>' );
+			} );
+
+			it( 'should convert ul,ol>li (in clipboard holder)', () => {
+				const modelFragment = editor.data.parse( '<ul><li>a</li><li>b</li></ul><ol><li>c</li></ol>', '$clipboardHolder' );
+
+				expect( stringifyModel( modelFragment ) )
+					.to.equal( '<paragraph>a</paragraph><paragraph>b</paragraph><paragraph>c</paragraph>' );
+			} );
+
+			it( 'should convert ul>li>ul>li+li', () => {
+				const modelFragment = editor.data.parse( '<ul><li>a<ul><li>b</li><li>c</li></ul></li></ul>' );
+
+				expect( stringifyModel( modelFragment ) )
+					.to.equal( '<paragraph>a</paragraph><paragraph>b</paragraph><paragraph>c</paragraph>' );
+			} );
+
+			// "b" is not autoparagraphed because clipboard holder allows text nodes.
+			// There's a similar integrational test what's going to happen when pasting in paragraph-integration.js.
+			it( 'should convert ul>li>ul>li+li (in clipboard holder)', () => {
+				const modelFragment = editor.data.parse( '<ul><li>a<ul><li>b</li><li>c</li></ul></li></ul>', '$clipboardHolder' );
+
+				expect( stringifyModel( modelFragment ) )
+					.to.equal( 'a<paragraph>b</paragraph><paragraph>c</paragraph>' );
+			} );
+
+			it( 'should convert ul>li>p,text', () => {
+				const modelFragment = editor.data.parse( '<ul><li><p>a</p>b</li></ul>' );
+
+				expect( stringifyModel( modelFragment ) )
+					.to.equal( '<paragraph>a</paragraph><paragraph>b</paragraph>' );
+			} );
+
+			// "b" is not autoparagraphed because clipboard holder allows text nodes.
+			// There's a similar integrational test what's going to happen when pasting in paragraph-integration.js.
+			it( 'should convert ul>li>p,text (in clipboard holder)', () => {
+				const modelFragment = editor.data.parse( '<ul><li><p>a</p>b</li></ul>', '$clipboardHolder' );
+
+				expect( stringifyModel( modelFragment ) )
+					.to.equal( '<paragraph>a</paragraph>b' );
+			} );
+
+			it( 'should convert td', () => {
+				const modelFragment = editor.data.parse( '<table><tr><td>a</td><td>b</td></tr><tr><td>c</td><td>d</td></tr></table>' );
+
+				expect( stringifyModel( modelFragment ) )
+					.to.equal( '<paragraph>a</paragraph><paragraph>b</paragraph><paragraph>c</paragraph><paragraph>d</paragraph>' );
+			} );
+
+			it( 'should convert td (in clipboardHolder)', () => {
+				const modelFragment = editor.data.parse(
+					'<table><tr><td>a</td><td>b</td></tr><tr><td>c</td><td>d</td></tr></table>',
+					'$clipboardHolder'
+				);
+
+				expect( stringifyModel( modelFragment ) )
+					.to.equal( '<paragraph>a</paragraph><paragraph>b</paragraph><paragraph>c</paragraph><paragraph>d</paragraph>' );
+			} );
+
+			it( 'should convert li inside converted container', () => {
+				doc.schema.registerItem( 'div' );
+				doc.schema.allow( { name: 'div', inside: '$root' } );
+				doc.schema.allow( { name: 'paragraph', inside: 'div' } );
+
+				buildViewConverter().for( editor.data.viewToModel ).fromElement( 'div' ).toElement( 'div' );
+
+				const modelFragment = editor.data.parse( '<div><ul><li>foo</li><li>bar</li></ul></div><div>bom<p>bim</p></div>' );
+
+				expect( stringifyModel( modelFragment ) )
+					.to.equal(
+						'<div><paragraph>foo</paragraph><paragraph>bar</paragraph></div>' +
+						'<div><paragraph>bom</paragraph><paragraph>bim</paragraph></div>'
+					);
+			} );
+
+			it( 'should convert li inside disallowed container', () => {
+				const modelFragment = editor.data.parse( '<div><ul><li>foo</li><li>bar</li></ul></div><div>bom<p>bim</p></div>' );
+
+				expect( stringifyModel( modelFragment ) )
+					.to.equal(
+						'<paragraph>foo</paragraph><paragraph>bar</paragraph>' +
+						'<paragraph>bom</paragraph><paragraph>bim</paragraph>'
+					);
+			} );
+
+			it( 'creates normalized model', () => {
+				const modelFragment = editor.data.parse( '<h1><span>foo</span><span>bar</span>' );
+
+				expect( stringifyModel( modelFragment ) ).to.equal( '<paragraph>foobar</paragraph>' );
+
+				expect( modelFragment ).to.be.instanceof( ModelDocumentFragment );
+				expect( modelFragment.getChild( 0 ).childCount ).to.equal( 1 );
+				expect( modelFragment.getChild( 0 ).getChild( 0 ) ).to.be.instanceOf( ModelText );
+			} );
+		} );
 	} );
 
 	describe( 'editing pipeline conversion', () => {
 		it( 'should convert paragraph', () => {
-			// Workaround for setting model data: https://github.com/ckeditor/ckeditor5-engine/issues/455
-			editor.setData( '<p>foo</p><p>bar</p>' );
+			setModelData( doc, '<paragraph>foo</paragraph><paragraph>bar</paragraph>' );
 
 			expect( getViewData( editor.editing.view, { withoutSelection: true } ) ).to.equal( '<p>foo</p><p>bar</p>' );
 		} );
 	} );
+
+	describe( 'autoparagraphing on data load', () => {
+		it( 'wraps text and place selection at the beginning of that paragraph', () => {
+			editor.setData( 'foo' );
+
+			expect( getModelData( doc ) ).to.equal( '<paragraph>[]foo</paragraph>' );
+		} );
+	} );
 } );