8
0
Просмотр исходного кода

Merge pull request #1282 from ckeditor/t/1262

Internal: Added node name as a `SchemaContextDefinition` member. Closes #1262.
Piotr Jasiun 7 лет назад
Родитель
Сommit
f48788ce9c

+ 4 - 4
packages/ckeditor5-engine/src/controller/datacontroller.js

@@ -209,11 +209,11 @@ export default class DataController {
 	 *
 	 * @see #set
 	 * @param {String} data Data to parse.
-	 * @param {module:engine/model/schema~SchemaContextDefinition} [context=['$root']] Base context in which the view will
+	 * @param {module:engine/model/schema~SchemaContextDefinition} [context='$root'] Base context in which the view will
 	 * be converted to the model. See: {@link module:engine/conversion/viewconversiondispatcher~ViewConversionDispatcher#convert}.
 	 * @returns {module:engine/model/documentfragment~DocumentFragment} Parsed data.
 	 */
-	parse( data, context = [ '$root' ] ) {
+	parse( data, context = '$root' ) {
 		// data -> view
 		const viewDocumentFragment = this.processor.toView( data );
 
@@ -231,11 +231,11 @@ export default class DataController {
 	 *
 	 * @param {module:engine/view/element~Element|module:engine/view/documentfragment~DocumentFragment} viewElementOrFragment
 	 * Element or document fragment whose content will be converted.
-	 * @param {module:engine/model/schema~SchemaContextDefinition} [context=['$root']] Base context in which the view will
+	 * @param {module:engine/model/schema~SchemaContextDefinition} [context='$root'] Base context in which the view will
 	 * be converted to the model. See: {@link module:engine/conversion/viewconversiondispatcher~ViewConversionDispatcher#convert}.
 	 * @returns {module:engine/model/documentfragment~DocumentFragment} Output document fragment.
 	 */
-	toModel( viewElementOrFragment, context = [ '$root' ] ) {
+	toModel( viewElementOrFragment, context = '$root' ) {
 		return this.viewToModel.convert( viewElementOrFragment, context );
 	}
 

+ 1 - 1
packages/ckeditor5-engine/src/conversion/viewconversiondispatcher.js

@@ -82,7 +82,7 @@ import mix from '@ckeditor/ckeditor5-utils/src/mix';
  *		// Fire conversion.
  *		// Always take care where the converted model structure will be appended to. If this `viewDocumentFragment`
  *		// is going to be appended directly to a '$root' element, use that in `context`.
- *		viewDispatcher.convert( viewDocumentFragment, { context: [ '$root' ] } );
+ *		viewDispatcher.convert( viewDocumentFragment, '$root' );
  *
  * Before each conversion process, `ViewConversionDispatcher` fires {@link ~ViewConversionDispatcher#event:viewCleanup}
  * event which can be used to prepare tree view for conversion.

+ 3 - 3
packages/ckeditor5-engine/src/dev-utils/model.js

@@ -241,8 +241,8 @@ export function stringify( node, selectionOrPositionOrRange = null ) {
  * @param {Object} [options={}] Additional configuration.
  * @param {Array<Object>} [options.selectionAttributes] A list of attributes which will be passed to the selection.
  * @param {Boolean} [options.lastRangeBackward=false] If set to `true`, the last range will be added as backward.
- * @param {module:engine/model/schema~SchemaContextDefinition} [options.context=[ '$root' ]] The conversion context.
- * If not provided, the default `[ '$root' ]` will be used.
+ * @param {module:engine/model/schema~SchemaContextDefinition} [options.context='$root'] The conversion context.
+ * If not provided, the default `'$root'` will be used.
  * @returns {module:engine/model/element~Element|module:engine/model/text~Text|
  * module:engine/model/documentfragment~DocumentFragment|Object} Returns the parsed model node or
  * an object with two fields: `model` and `selection`, when selection ranges were included in the data to parse.
@@ -280,7 +280,7 @@ export function parse( data, schema, options = {} ) {
 	viewToModel.isDebug = true;
 
 	// Convert view to model.
-	let model = viewToModel.convert( viewDocumentFragment.root, options.context || [ '$root' ] );
+	let model = viewToModel.convert( viewDocumentFragment.root, options.context || '$root' );
 
 	mapper.bindElements( model, viewDocumentFragment.root );
 

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