Browse Source

Changed: Minor refactoring.

Szymon Cofalik 8 năm trước cách đây
mục cha
commit
26f070e916

+ 8 - 6
packages/ckeditor5-engine/src/controller/deletecontent.js

@@ -41,9 +41,11 @@ export default function deleteContent( selection, batch, options = {} ) {
 		return;
 	}
 
+	const schema = batch.document.schema;
+
 	// 1. Replace the entire content with paragraph.
 	// See: https://github.com/ckeditor/ckeditor5-engine/issues/1012#issuecomment-315017594.
-	if ( !options.doNotResetEntireContent && shouldEntireContentBeReplacedWithParagraph( batch.document.schema, selection ) ) {
+	if ( !options.doNotResetEntireContent && shouldEntireContentBeReplacedWithParagraph( schema, selection ) ) {
 		replaceEntireContentWithParagraph( batch, selection );
 
 		return;
@@ -74,14 +76,14 @@ export default function deleteContent( selection, batch, options = {} ) {
 		//
 		// e.g. bold is disallowed for <H1>
 		// <h1>Fo{o</h1><p>b}a<b>r</b><p> -> <h1>Fo{}a<b>r</b><h1> -> <h1>Fo{}ar<h1>.
-		batch.document.schema.removeDisallowedAttributes( startPos.parent.getChildren(), startPos, batch );
+		schema.removeDisallowedAttributes( startPos.parent.getChildren(), startPos, batch );
 	}
 
 	selection.setCollapsedAt( startPos );
 
 	// 4. Autoparagraphing.
 	// Check if a text is allowed in the new container. If not, try to create a new paragraph (if it's allowed here).
-	if ( shouldAutoparagraph( batch.document, startPos ) ) {
+	if ( shouldAutoparagraph( schema, startPos ) ) {
 		insertParagraph( batch, startPos, selection );
 	}
 
@@ -158,9 +160,9 @@ function mergeBranches( batch, startPos, endPos ) {
 	mergeBranches( batch, startPos, endPos );
 }
 
-function shouldAutoparagraph( doc, position ) {
-	const isTextAllowed = doc.schema.check( { name: '$text', inside: position } );
-	const isParagraphAllowed = doc.schema.check( { name: 'paragraph', inside: position } );
+function shouldAutoparagraph( schema, position ) {
+	const isTextAllowed = schema.check( { name: '$text', inside: position } );
+	const isParagraphAllowed = schema.check( { name: 'paragraph', inside: position } );
 
 	return !isTextAllowed && isParagraphAllowed;
 }

+ 5 - 7
packages/ckeditor5-engine/src/model/schema.js

@@ -418,8 +418,8 @@ export default class Schema {
 	/**
 	 * Removes disallowed by {@link module:engine/model/schema~Schema schema} attributes from given nodes.
 	 * When {@link module:engine/model/batch~Batch batch} parameter is provided then attributes will be removed
-	 * by creating {@link module:engine/model/delta/attributedelta~AttributeDelta attributeDeltas} otherwise
-	 * attributes will be removed directly from provided nodes using {@link module:engine/model/node~Node node} API.
+	 * using that batch, by creating {@link module:engine/model/delta/attributedelta~AttributeDelta attribute deltas}.
+	 * Otherwise, attributes will be removed directly from provided nodes using {@link module:engine/model/node~Node node} API.
 	 *
 	 * @param {Iterable.<module:engine/model/node~Node>} nodes Nodes that will be filtered.
 	 * @param {module:engine/model/schema~SchemaPath} inside Path inside which schema will be checked.
@@ -429,13 +429,12 @@ export default class Schema {
 		for ( const node of nodes ) {
 			const name = node.is( 'text' ) ? '$text' : node.name;
 			const attributes = Array.from( node.getAttributeKeys() );
-			const normalizedQueryPath = Schema._normalizeQueryPath( inside );
-			const queryPath = normalizedQueryPath.join( ' ' );
+			const queryPath = Schema._normalizeQueryPath( inside );
 
 			// When node with attributes is not allowed in current position.
 			if ( !this.check( { name, attributes, inside: queryPath } ) ) {
 				// Let's remove attributes one by one.
-				// This should be improved to check all combination of attributes.
+				// TODO: this should be improved to check all combination of attributes.
 				for ( const attribute of node.getAttributeKeys() ) {
 					if ( !this.check( { name, attributes: attribute, inside: queryPath } ) ) {
 						if ( batch ) {
@@ -448,8 +447,7 @@ export default class Schema {
 			}
 
 			if ( node.is( 'element' ) ) {
-				const newQueryPath = normalizedQueryPath.concat( [ node.name ] );
-				this.removeDisallowedAttributes( node.getChildren(), newQueryPath, batch );
+				this.removeDisallowedAttributes( node.getChildren(), queryPath.concat( node.name ), batch );
 			}
 		}
 	}