Ver código fonte

Internal: Added node name as a SchemaContextDefinition member.

Oskar Wróbel 7 anos atrás
pai
commit
837750a5cd

+ 13 - 8
packages/ckeditor5-engine/src/model/schema.js

@@ -1114,14 +1114,16 @@ export class SchemaContext {
 	/**
 	 * Creates an instance of the context.
 	 *
-	 * @param {module:engine/model/schema~SchemaContextDefinition|module:engine/model/schema~SchemaContext} context
+	 * @param {module:engine/model/schema~SchemaContextDefinition} context
 	 */
 	constructor( context ) {
 		if ( context instanceof SchemaContext ) {
 			return context;
 		}
 
-		if ( !Array.isArray( context ) ) {
+		if ( typeof context == 'string' ) {
+			context = [ context ];
+		} else if ( !Array.isArray( context ) ) {
 			// `context` is item or position.
 			// Position#getAncestors() doesn't accept any parameters but it works just fine here.
 			context = context.getAncestors( { includeSelf: true } );
@@ -1239,11 +1241,11 @@ export class SchemaContext {
  * * By defining a **node** – in this cases this node and all its ancestors will be used.
  * * By defining a **position** in the document – in this case all its ancestors will be used.
  * * By defining an **array of nodes** – in this case this array defines the entire context.
- * * By defining an **array of node names** (potentially, mixed with real nodes) – in this case
- * nodes definied by strings will be "mocked". Using strings is not recommended as it
- * means that the context will be unrealistic (e.g. attributes of these nodes are not specified).
- * However, at times this may be the only way to define the context (e.g. when checking some
- * hypothetical situation).
+ * * By defining a **name of node** - in this case node will be "mocked". It is not recommended because context
+ * will be unrealistic (e.g. attributes of these nodes are not specified). However, at times this may be the only
+ * way to define the context (e.g. when checking some hypothetical situation).
+ * * By defining an **array of node names** (potentially, mixed with real nodes) – The same as **name of node**
+ * but it is possible to create a path.
  * * By defining a {@link module:engine/model/schema~SchemaContext} instance - in this case the same instance as provided
  * will be return.
  *
@@ -1263,6 +1265,9 @@ export class SchemaContext {
  *		// Check in [ rootElement, paragraphElement ].
  *		schema.checkChild( [ rootElement, paragraphElement ], 'foo' );
  *
+ *		// Check only fakeParagraphElement.
+ *		schema.checkChild( 'paragraph', 'foo' );
+ *
  *		// Check in [ fakeRootElement, fakeBarElement, paragraphElement ].
  *		schema.checkChild( [ '$root', 'bar', paragraphElement ], 'foo' );
  *
@@ -1294,7 +1299,7 @@ export class SchemaContext {
  *		schema.checkChild( [ ...positionInParagraph.getAncestors(), '$text' ], 'bold' );
  *
  * @typedef {module:engine/model/node~Node|module:engine/model/position~Position|module:engine/model/schema~SchemaContext|
- * Array.<String|module:engine/model/node~Node>} module:engine/model/schema~SchemaContextDefinition
+ * String|Array.<String|module:engine/model/node~Node>} module:engine/model/schema~SchemaContextDefinition
  */
 
 /**

+ 6 - 0
packages/ckeditor5-engine/tests/model/schema.js

@@ -2690,6 +2690,12 @@ describe( 'SchemaContext', () => {
 			expect( Array.from( ctx.getItem( 2 ).getAttributeKeys() ).sort() ).to.deep.equal( [ 'align' ] );
 		} );
 
+		it( 'creates context based on a string', () => {
+			const ctx = new SchemaContext( 'paragraph' );
+
+			expect( Array.from( ctx.getNames() ) ).to.deep.equal( [ 'paragraph' ] );
+		} );
+
 		it( 'creates context based on a SchemaContext instance', () => {
 			const previousCtx = new SchemaContext( [ 'a', 'b', 'c' ] );