Kaynağa Gözat

Moved common helper functions to the model/utils.js.

Oskar Wróbel 8 yıl önce
ebeveyn
işleme
3d5d638913

+ 1 - 32
packages/ckeditor5-engine/src/controller/deletecontent.js

@@ -11,6 +11,7 @@ import LivePosition from '../model/liveposition';
 import Position from '../model/position';
 import Range from '../model/range';
 import Element from '../model/element';
+import { removeDisallowedAttributes } from '../model/utils';
 
 /**
  * Deletes content of the selection and merge siblings. The resulting selection is always collapsed.
@@ -217,35 +218,3 @@ function shouldEntireContentBeReplacedWithParagraph( schema, selection ) {
 
 	return schema.check( { name: 'paragraph', inside: limitElement.name } );
 }
-
-// Gets a name under which we should check this node in the schema.
-//
-// @private
-// @param {module:engine/model/node~Node} node The node.
-function getNodeSchemaName( node ) {
-	if ( node.is( 'text' ) ) {
-		return '$text';
-	}
-
-	return node.name;
-}
-
-// Removes disallowed by schema attributes from given text nodes.
-//
-// @private
-// @param {module:engine/model/node~Node|Array<module:engine/model/node~Node>} nodes
-// @param {module:engine/model/schema~SchemaPath} schemaPath
-// @param {module:engine/model/schema~Schema} schema
-function removeDisallowedAttributes( nodes, schemaPath, schema ) {
-	if ( !Array.isArray( nodes ) ) {
-		nodes = [ nodes ];
-	}
-
-	for ( const node of nodes ) {
-		for ( const attribute of node.getAttributeKeys() ) {
-			if ( !schema.check( { name: getNodeSchemaName( node ), attributes: attribute, inside: schemaPath } ) ) {
-				node.removeAttribute( attribute );
-			}
-		}
-	}
-}

+ 1 - 32
packages/ckeditor5-engine/src/controller/insertcontent.js

@@ -12,6 +12,7 @@ import LivePosition from '../model/liveposition';
 import Element from '../model/element';
 import Range from '../model/range';
 import log from '@ckeditor/ckeditor5-utils/src/log';
+import { getNodeSchemaName, removeDisallowedAttributes } from '../model/utils';
 
 /**
  * Inserts content into the editor (specified selection) as one would expect the paste
@@ -438,35 +439,3 @@ class Insertion {
 		return this.schema.objects.has( getNodeSchemaName( node ) );
 	}
 }
-
-// Gets a name under which we should check this node in the schema.
-//
-// @private
-// @param {module:engine/model/node~Node} node The node.
-function getNodeSchemaName( node ) {
-	if ( node.is( 'text' ) ) {
-		return '$text';
-	}
-
-	return node.name;
-}
-
-// Removes disallowed by schema attributes from given text nodes.
-//
-// @private
-// @param {module:engine/model/node~Node|Array<module:engine/model/node~Node>} nodes
-// @param {module:engine/model/schema~SchemaPath} schemaPath
-// @param {module:engine/model/schema~Schema} schema
-function removeDisallowedAttributes( nodes, schemaPath, schema ) {
-	if ( !Array.isArray( nodes ) ) {
-		nodes = [ nodes ];
-	}
-
-	for ( const node of nodes ) {
-		for ( const attribute of node.getAttributeKeys() ) {
-			if ( !schema.check( { name: getNodeSchemaName( node ), attributes: attribute, inside: schemaPath } ) ) {
-				node.removeAttribute( attribute );
-			}
-		}
-	}
-}

+ 43 - 0
packages/ckeditor5-engine/src/model/utils.js

@@ -0,0 +1,43 @@
+/**
+ * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
+ * For licensing, see LICENSE.md.
+ */
+
+/**
+ * @module engine/model/utils
+ */
+
+/**
+ * Gets a name under which we should check this node in the schema.
+ *
+ * @param {module:engine/model/node~Node} node The node.
+ * @returns {String} node name.
+ */
+export function getNodeSchemaName( node ) {
+	if ( node.is( 'text' ) ) {
+		return '$text';
+	}
+
+	return node.name;
+}
+
+/**
+ * Removes disallowed by schema attributes from given nodes.
+ *
+ * @param {module:engine/model/node~Node|Array<module:engine/model/node~Node>} nodes List of nodes or a single node to filter.
+ * @param {module:engine/model/schema~SchemaPath} schemaPath
+ * @param {module:engine/model/schema~Schema} schema
+ */
+export function removeDisallowedAttributes( nodes, schemaPath, schema ) {
+	if ( !Array.isArray( nodes ) ) {
+		nodes = [ nodes ];
+	}
+
+	for ( const node of nodes ) {
+		for ( const attribute of node.getAttributeKeys() ) {
+			if ( !schema.check( { name: getNodeSchemaName( node ), attributes: attribute, inside: schemaPath } ) ) {
+				node.removeAttribute( attribute );
+			}
+		}
+	}
+}

+ 64 - 0
packages/ckeditor5-engine/tests/model/utils.js

@@ -0,0 +1,64 @@
+/**
+ * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
+ * For licensing, see LICENSE.md.
+ */
+
+import { getNodeSchemaName, removeDisallowedAttributes } from '../../src/model/utils';
+import Element from '../../src/model/element';
+import Text from '../../src/model/text';
+import Document from '../../src/model/document';
+import { setData, getData } from '../../src/dev-utils/model';
+
+describe( 'model utils', () => {
+	describe( 'getNodeSchemaName()', () => {
+		it( 'should return schema name for a given Element', () => {
+			const element = new Element( 'paragraph' );
+
+			expect( getNodeSchemaName( element ) ).to.equal( 'paragraph' );
+		} );
+
+		it( 'should return schema name for a Text', () => {
+			const element = new Text();
+
+			expect( getNodeSchemaName( element ) ).to.equal( '$text' );
+		} );
+	} );
+
+	describe( 'removeDisallowedAttributes()', () => {
+		let doc;
+
+		beforeEach( () => {
+			doc = new Document();
+			doc.createRoot();
+
+			const schema = doc.schema;
+
+			schema.registerItem( 'paragraph', '$block' );
+
+			schema.allow( { name: '$text', attributes: 'a', inside: 'paragraph' } );
+			schema.allow( { name: '$text', attributes: 'c', inside: 'paragraph' } );
+		} );
+
+		it( 'should remove disallowed by schema attributes from list of nodes', () => {
+			setData( doc, '<paragraph>f<$text a="1" b="1">o</$text>ob<$text b="1" c="1">a</$text>r</paragraph>' );
+
+			const paragraph = doc.getRoot().getChild( 0 );
+
+			removeDisallowedAttributes( Array.from( paragraph.getChildren() ), [ paragraph ], doc.schema );
+
+			expect( getData( doc, { withoutSelection: true } ) )
+				.to.equal( '<paragraph>f<$text a="1">o</$text>ob<$text c="1">a</$text>r</paragraph>' );
+		} );
+
+		it( 'should remove disallowed by schema attributes from a single node', () => {
+			setData( doc, '<paragraph><$text a="1" b="1">foo</$text></paragraph>' );
+
+			const paragraph = doc.getRoot().getChild( 0 );
+
+			removeDisallowedAttributes( paragraph.getChild( 0 ), [ paragraph ], doc.schema );
+
+			expect( getData( doc, { withoutSelection: true } ) )
+				.to.equal( '<paragraph><$text a="1">foo</$text></paragraph>' );
+		} );
+	} );
+} );