浏览代码

Added integration tests for pasting and generic conversion for paragraph-like elements.

Piotrek Koszuliński 9 年之前
父节点
当前提交
0ea74b0f82

+ 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"
   },

+ 88 - 11
packages/ckeditor5-paragraph/src/paragraph.js

@@ -10,7 +10,11 @@
 import Plugin from '../core/plugin.js';
 
 import ModelElement from '../engine/model/element.js';
+import ModelPosition from '../engine/model/position.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';
 
@@ -53,6 +57,13 @@ export default class Paragraph extends Plugin {
 		// 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' } );
 	}
 }
 
@@ -62,25 +73,35 @@ export default class Paragraph extends Plugin {
  *
  *		<h1>Foo</h1>
  *		<table>
- *			<tr><td>X</td><td>Y</td></tr>
- *			<tr><td>X</td><td>Y</td></tr>
+ *			<tr>
+ *				<td>X</td>
+ *				<td>
+ *					<ul>
+ *						<li>Y</li>
+ *						<li>Z</li>
+ *					</ul>
+ *				</td>
+ *			</tr>
  *		</table>
  *
- * Contains three paragraph-like elements – `<h1>` and two `<tr>`. Hence, if none of the features is going to convert
- * those elements the above content will automatically be handled by the paragraph feature and converted to:
+ * 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>XY</p>
- *		<p>XY</p>
+ *		<p>X</p>
+ *		<p>Y</p>
+ *		<p>Z</p>
  *
- * Note that subsequent cells were merged, but `<tr>` elements were maintaned.
+ * 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( [ 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'tr', 'li' ] );
+Paragraph.paragraphLikeElements = new Set( [ 'p', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'td', 'li', 'div' ] );
 
 // A map of paragraphs to merge (model elements) to their block contexts in the view.
-// The context is used in order to check whether two paragraps should really be merged.
+// The context is used in order to check whether two paragraphs should really be merged.
 // E.g.
 // <h1>foo</h1><h1>bar</h1> => <p>foo</p><p>bar</p> (contexts: two different h1 elements – no merge)
 // <div>foo<img>bar</div> => <p>foo</p><p><img></p><p>bar</p> (contexts: 3x the same div element – merge)
@@ -103,13 +124,56 @@ function autoparagraphText( doc, evt, data, consumable, conversionApi ) {
 
 	paragraphsToMerge.set( paragraph, getBlockContext( data.input ) );
 
-	const newContext = data.context.concat( paragraph );
-	const text = conversionApi.convertItem( data.input, consumable, { context: newContext } );
+	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 );
+
+	modelWriter.insert( ModelPosition.createAt( paragraph ), 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).
@@ -154,3 +218,16 @@ function getBlockContext( node ) {
 
 	return blockLikeAncestor ? blockLikeAncestor : node.root;
 }
+
+// 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;
+}

+ 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>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>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>'
+					);
+				} );
+		} );
+	} );
+} );

+ 122 - 29
packages/ckeditor5-paragraph/tests/paragraph.js

@@ -63,7 +63,7 @@ describe( 'Paragraph feature', () => {
 			expect( editor.getData() ).to.equal( '<p>foo</p><p>baz</p>' );
 		} );
 
-		describe( 'generic block converter (autoparagraphing)', () => {
+		describe( 'generic text converter (text autoparagraphing)', () => {
 			it( 'should autoparagraph text', () => {
 				editor.setData( 'foo' );
 
@@ -71,6 +71,13 @@ describe( 'Paragraph feature', () => {
 				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 autoparagraph text next to allowed element', () => {
 				doc.schema.registerItem( 'heading1', '$block' );
 				buildViewConverter().for( editor.data.viewToModel ).fromElement( 'h1' ).toElement( 'heading1' );
@@ -81,56 +88,37 @@ describe( 'Paragraph feature', () => {
 					.to.equal( '<heading1>foo</heading1><paragraph>bar</paragraph><paragraph>bom</paragraph>' );
 			} );
 
-			it( 'should autoparagraph 3 inline things into one paragraph', () => {
+			it( 'should autoparagraph 3 inline 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 autoparagraph 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 autoparagraph ul > li', () => {
-				const modelFragment = editor.data.parse( '<ul><li>foo</li><li>bar</li></ul>' );
-
-				expect( stringifyModel( modelFragment ) )
-					.to.equal( '<paragraph>foo</paragraph><paragraph>bar</paragraph>' );
-			} );
-
-			it( 'should autoparagraph tr', () => {
-				const modelFragment = editor.data.parse( '<table><tr><td>a</td><td>b</td></tr><tr><td>c</td><td>d</td></tr></table>' );
+			it( 'should not autoparagraph 3 inline inline nodes (in clipboardHolder)', () => {
+				const modelFragment = editor.data.parse( 'foo<b>bar</b>bom', '$clipboardHolder' );
 
 				expect( stringifyModel( modelFragment ) )
-					.to.equal( '<paragraph>ab</paragraph><paragraph>cd</paragraph>' );
+					.to.equal( 'foobarbom' );
 			} );
 
-			it( 'should autoparagraph inside some container', () => {
+			it( 'should autoparagraph text inside converted container', () => {
 				doc.schema.registerItem( 'div' );
-				doc.schema.allow( { name: '$inline', attributes: [ 'bold' ] } );
 				doc.schema.allow( { name: 'div', inside: '$root' } );
 				doc.schema.allow( { name: 'paragraph', inside: 'div' } );
 
-				buildViewConverter().for( editor.data.viewToModel )
-					.fromElement( 'b' )
-					.toAttribute( 'bold', true );
-
 				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>' );
+				const modelFragment = editor.data.parse( '<div>foo</div><div>bom<p>bim</p></div>' );
 
 				expect( stringifyModel( modelFragment ) )
 					.to.equal(
-						'<div><paragraph>foo</paragraph><paragraph>bar</paragraph></div>' +
+						'<div><paragraph>foo</paragraph></div>' +
 						'<div><paragraph>bom</paragraph><paragraph>bim</paragraph></div>'
 					);
 			} );
 
-			it( 'should not autopargraph when disallowed element contains allowed element', () => {
+			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' );
 
@@ -140,7 +128,7 @@ describe( 'Paragraph feature', () => {
 					.to.equal( '<heading1>foo</heading1><paragraph>bar</paragraph>' );
 			} );
 
-			it( 'should not autopargraph when allowed element contains disallowed element', () => {
+			it( 'should not autoparagraph text in disallowed element', () => {
 				doc.schema.registerItem( 'heading1', '$block' );
 				buildViewConverter().for( editor.data.viewToModel ).fromElement( 'h1' ).toElement( 'heading1' );
 
@@ -150,6 +138,111 @@ describe( 'Paragraph feature', () => {
 					.to.equal( '<heading1>foobar</heading1>' );
 			} );
 		} );
+
+		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 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>'
+					);
+			} );
+		} );
 	} );
 
 	describe( 'editing pipeline conversion', () => {