# Conversion

class

A utility class that helps add converters to upcast and downcast dispatchers.

We recommend reading the [editor conversion](../framework/deep-dive/conversion/intro.md) guide first to understand the core concepts of the conversion mechanisms.

An instance of the conversion manager is available in the [`editor.conversion`](module_core_editor_editor-Editor.md#member-conversion) property and by default has the following groups of dispatchers (i.e. directions of conversion):

* `downcast` (editing and data downcasts)
* `editingDowncast`
* `dataDowncast`
* `upcast`

#### One-way converters

To add a converter to a specific group, use the [`for()`](#function-for) method:

```typescript
// Add a converter to editing downcast and data downcast.
editor.conversion.for( 'downcast' ).elementToElement( config ) );

// Add a converter to the data pipepline only:
editor.conversion.for( 'dataDowncast' ).elementToElement( dataConversionConfig ) );

// And a slightly different one for the editing pipeline:
editor.conversion.for( 'editingDowncast' ).elementToElement( editingConversionConfig ) );
```

See [`for()`](#function-for) method documentation to learn more about available conversion helpers and how to use your custom ones.

#### Two-way converters

Besides using one-way converters via the `for()` method, you can also use other methods available in this class to add two-way converters (upcast and downcast):

* [`elementToElement()`](#function-elementToElement) – Model element to view element and vice versa.
* [`attributeToElement()`](#function-attributeToElement) – Model attribute to view element and vice versa.
* [`attributeToAttribute()`](#function-attributeToAttribute) – Model attribute to view attribute and vice versa.

[See source](https://github.com/ckeditor/ckeditor5/blob/master/packages/ckeditor5-engine/src/conversion/conversion.ts#L75)

<a id="properties">

## Properties

<a id="member-_downcast">

### `_downcast: Array<DowncastDispatcher>` _(private)_

[See source](https://github.com/ckeditor/ckeditor5/blob/master/packages/ckeditor5-engine/src/conversion/conversion.ts#L81)

<a id="member-_helpers">

### `_helpers: Map<string, DowncastHelpers | UpcastHelpers>` _(private)_

Maps dispatchers group name to ConversionHelpers instances.

[See source](https://github.com/ckeditor/ckeditor5/blob/master/packages/ckeditor5-engine/src/conversion/conversion.ts#L79)

<a id="member-_upcast">

### `_upcast: Array<UpcastDispatcher>` _(private)_

[See source](https://github.com/ckeditor/ckeditor5/blob/master/packages/ckeditor5-engine/src/conversion/conversion.ts#L82)

<a id="methods">

## Methods

<a id="function-constructor">

### `constructor( downcastDispatchers, upcastDispatchers )`

Creates a new conversion instance.

[See source](https://github.com/ckeditor/ckeditor5/blob/master/packages/ckeditor5-engine/src/conversion/conversion.ts#L87)

#### Parameters

* `downcastDispatchers: ArrayOrItem<DowncastDispatcher>`
* `upcastDispatchers: ArrayOrItem<UpcastDispatcher>`

<a id="function-addAlias">

### `addAlias( alias, dispatcher ) → void`

[See source](https://github.com/ckeditor/ckeditor5/blob/master/packages/ckeditor5-engine/src/conversion/conversion.ts#L107)

#### Parameters

* `alias: string`
* `dispatcher: DowncastDispatcher | UpcastDispatcher`

#### Returns

* `void`

<a id="function-addAlias">

### `addAlias( alias, dispatcher ) → void`

[See source](https://github.com/ckeditor/ckeditor5/blob/master/packages/ckeditor5-engine/src/conversion/conversion.ts#L103)

#### Parameters

* `` alias: `${ string }Upcast` ``
* `dispatcher: UpcastDispatcher`

#### Returns

* `void`

<a id="function-addAlias">

### `addAlias( alias, dispatcher ) → void`

[See source](https://github.com/ckeditor/ckeditor5/blob/master/packages/ckeditor5-engine/src/conversion/conversion.ts#L99)

#### Parameters

* `` alias: `${ string }Downcast` ``
* `dispatcher: DowncastDispatcher`

#### Returns

* `void`

<a id="function-attributeToAttribute">

### `attributeToAttribute( definition = { definition.converterPriority?, definition.model, definition.upcastAlso?, definition.view, definition.converterPriority?, definition.model, definition.upcastAlso?, definition.view } ) → void`

Sets up converters between the model and the view that convert a model attribute to a view attribute (and vice versa). For example, `<imageBlock src='foo.jpg'></imageBlock>` is converted to `<img src='foo.jpg'></img>` (the same attribute key and value). This type of converters is intended to be used with [model element](module_engine_model_element-ModelElement.md) nodes. To convert the text attributes, the [`attributeToElement converter`](#function-attributeToElement)should be set up.

```typescript
// A simple conversion from the `source` model attribute to the `src` view attribute (and vice versa).
editor.conversion.attributeToAttribute( { model: 'source', view: 'src' } );

// Attribute values are strictly specified.
editor.conversion.attributeToAttribute( {
	model: {
		name: 'imageInline',
		key: 'aside',
		values: [ 'aside' ]
	},
	view: {
		aside: {
			name: 'img',
			key: 'class',
			value: [ 'aside', 'half-size' ]
		}
	}
} );

// Set the style attribute.
editor.conversion.attributeToAttribute( {
	model: {
		name: 'imageInline',
		key: 'aside',
		values: [ 'aside' ]
	},
	view: {
		aside: {
			name: 'img',
			key: 'style',
			value: {
				float: 'right',
				width: '50%',
				margin: '5px'
			}
		}
	}
} );

// Conversion from and to a model attribute key whose value is an enum (`align=right|center`).
// Use `upcastAlso` to define other view elements that should also be converted to the `align=right` attribute.
editor.conversion.attributeToAttribute( {
	model: {
		key: 'align',
		values: [ 'right', 'center' ]
	},
	view: {
		right: {
			key: 'class',
			value: 'align-right'
		},
		center: {
			key: 'class',
			value: 'align-center'
		}
	},
	upcastAlso: {
		right: {
			styles: {
				'text-align': 'right'
			}
		},
		center: {
			styles: {
				'text-align': 'center'
			}
		}
	}
} );
```

The `definition.model` parameter specifies which model attribute should be converted from and to. It can be a `{ key, [ values ], [ name ] }` object or a `String`, which will be treated like `{ key: definition.model }`. The `key` property is the model attribute key to convert from and to. The `values` are the possible model attribute values. If the `values` parameter is not set, the model attribute value will be the same as the view attribute value. If `name` is set, the conversion will be set up only for model elements with the given name.

The `definition.view` parameter specifies which view attribute should be converted from and to. It can be a `{ key, value, [ name ] }` object or a `String`, which will be treated like `{ key: definition.view }`. The `key` property is the view attribute key to convert from and to. The `value` is the view attribute value to convert from and to. If `definition.value` is not set, the view attribute value will be the same as the model attribute value. If `key` is `'class'`, `value` can be a `String` or an array of `String`s. If `key` is `'style'`, `value` is an object with key-value pairs. In other cases, `value` is a `String`. If `name` is set, the conversion will be set up only for model elements with the given name. If `definition.model.values` is set, `definition.view` is an object that assigns values from `definition.model.values` to `{ key, value, [ name ] }` objects.

`definition.upcastAlso` specifies which other matching view elements should also be upcast to the given model configuration. If `definition.model.values` is set, `definition.upcastAlso` should be an object assigning values from `definition.model.values` to [`MatcherPattern`](module_engine_view_matcher-MatcherPattern.md)s or arrays of [`MatcherPattern`](module_engine_view_matcher-MatcherPattern.md)s.

**Note:** `definition.model` and `definition.view` form should be mirrored, so the same types of parameters should be given in both parameters.

[See source](https://github.com/ckeditor/ckeditor5/blob/master/packages/ckeditor5-engine/src/conversion/conversion.ts#L628)

#### Type parameters

* `TValues: extends string`

#### Parameters

* `definition: object`

  The converter definition.

  Properties

  * `definition.converterPriority?: PriorityString`

  * `definition.model: string | object`

    The model attribute to convert from and to.

  * `definition.upcastAlso?: ArrayOrItem<( string | DowncastAttributeCreatorFunction | (DowncastAttributeDescriptor & { name?: string | undefined; }) )>`

    Any view element matching `definition.upcastAlso` will also be converted to the given model attribute. `definition.upcastAlso` is used only if `config.model.values` is specified.

  * `definition.view: string | ( DowncastAttributeDescriptor & { name?: string | undefined; } )`

    The view attribute to convert from and to.

  * `definition.converterPriority?: PriorityString`

  * `definition.model: object`

    The model attribute to convert from and to.

  * `definition.upcastAlso?: Record<TValues, DowncastAttributeCreatorFunction | ( DowncastAttributeDescriptor & { name?: string | undefined; } )>`

    Any view element matching `definition.upcastAlso` will also be converted to the given model attribute. `definition.upcastAlso` is used only if `config.model.values` is specified.

  * `definition.view: Record<TValues, DowncastAttributeDescriptor & object>`

    The view attribute to convert from and to.

#### Returns

* `void`

<a id="function-attributeToElement">

### `attributeToElement( definition = { definition.converterPriority?, definition.model, definition.upcastAlso?, definition.view, definition.converterPriority?, definition.model, definition.upcastAlso?, definition.view } ) → void`

Sets up converters between the model and the view that convert a model attribute to a view element (and vice versa). For example, a model text node with `"Foo"` as data and the `bold` attribute will be turned to `<strong>Foo</strong>` in the view.

```typescript
// A simple conversion from the `bold=true` attribute to the `<strong>` view element (and vice versa).
editor.conversion.attributeToElement( { model: 'bold', view: 'strong' } );

// Override other converters by specifying a converter definition with a higher priority.
editor.conversion.attributeToElement( { model: 'bold', view: 'b', converterPriority: 'high' } );

// View specified as an object instead of a string.
editor.conversion.attributeToElement( {
	model: 'bold',
	view: {
		name: 'span',
		classes: 'bold'
	}
} );

// Use `config.model.name` to define the conversion only from a given node type, `$text` in this case.
// The same attribute on different elements may then be handled by a different converter.
editor.conversion.attributeToElement( {
	model: {
		key: 'textDecoration',
		values: [ 'underline', 'lineThrough' ],
		name: '$text'
	},
	view: {
		underline: {
			name: 'span',
			styles: {
				'text-decoration': 'underline'
			}
		},
		lineThrough: {
			name: 'span',
			styles: {
				'text-decoration': 'line-through'
			}
		}
	}
} );

// Use `upcastAlso` to define other view elements that should also be converted to the `bold` attribute.
editor.conversion.attributeToElement( {
	model: 'bold',
	view: 'strong',
	upcastAlso: [
		'b',
		{
			name: 'span',
			classes: 'bold'
		},
		{
			name: 'span',
			styles: {
				'font-weight': 'bold'
			}
		},
		viewElement => {
			const fontWeight = viewElement.getStyle( 'font-weight' );

			if ( viewElement.is( 'element', 'span' ) && fontWeight && /\d+/.test() && Number( fontWeight ) > 500 ) {
				// Returned value can be an object with the matched properties.
				// These properties will be "consumed" during the conversion.
				// See `engine.view.Matcher~MatcherPattern` and `engine.view.Matcher#match` for more details.

				return {
					name: true,
					styles: [ 'font-weight' ]
				};
			}
		}
	]
} );

// Conversion from and to a model attribute key whose value is an enum (`fontSize=big|small`).
// `upcastAlso` set as callback enables a conversion of a wide range of different view elements.
editor.conversion.attributeToElement( {
	model: {
		key: 'fontSize',
		values: [ 'big', 'small' ]
	},
	view: {
		big: {
			name: 'span',
			styles: {
				'font-size': '1.2em'
			}
		},
		small: {
			name: 'span',
			styles: {
				'font-size': '0.8em'
			}
		}
	},
	upcastAlso: {
		big: viewElement => {
			const fontSize = viewElement.getStyle( 'font-size' );

			if ( !fontSize ) {
				return null;
			}

			const match = fontSize.match( /(\d+)\s*px/ );

			if ( !match ) {
				return null;
			}

			const size = Number( match[ 1 ] );

			if ( viewElement.is( 'element', 'span' ) && size > 10 ) {
				// Returned value can be an object with the matched properties.
				// These properties will be "consumed" during the conversion.
				// See `engine.view.Matcher~MatcherPattern` and `engine.view.Matcher#match` for more details.

				return { name: true, styles: [ 'font-size' ] };
			}

			return null;
		},
		small: viewElement => {
			const fontSize = viewElement.getStyle( 'font-size' );

			if ( !fontSize ) {
				return null;
			}

			const match = fontSize.match( /(\d+)\s*px/ );

			if ( !match ) {
				return null;
			}

			const size = Number( match[ 1 ] );

			if ( viewElement.is( 'element', 'span' ) && size < 10 ) {
				// Returned value can be an object with the matched properties.
				// These properties will be "consumed" during the conversion.
				// See `engine.view.Matcher~MatcherPattern` and `engine.view.Matcher#match` for more details.

				return { name: true, styles: [ 'font-size' ] };
			}

			return null;
		}
	}
} );
```

The `definition.model` parameter specifies which model attribute should be converted from or to. It can be a `{ key, value }` object describing the attribute key and value to convert or a `String` specifying just the attribute key (in such a case `value` is set to `true`).

[See source](https://github.com/ckeditor/ckeditor5/blob/master/packages/ckeditor5-engine/src/conversion/conversion.ts#L483)

#### Type parameters

* `TValues: extends string`

#### Parameters

* `definition: object`

  The converter definition.

  Properties

  * `definition.converterPriority?: PriorityString`
  * `definition.model: string | object`
  * `definition.upcastAlso?: ArrayOrItem<MatcherPattern>`
  * `definition.view: ViewElementDefinition`
  * `definition.converterPriority?: PriorityString`
  * `definition.model: object`
  * `definition.upcastAlso?: Record<TValues, ArrayOrItem<MatcherPattern>>`
  * `definition.view: Record<TValues, ViewElementDefinition>`

#### Returns

* `void`

<a id="function-elementToElement">

### `elementToElement( definition = { definition.converterPriority?, definition.model, definition.upcastAlso?, definition.view } ) → void`

Sets up converters between the model and the view that convert a model element to a view element (and vice versa). For example, the model `<paragraph>Foo</paragraph>` is turned into `<p>Foo</p>` in the view.

```typescript
// A simple conversion from the `paragraph` model element to the `<p>` view element (and vice versa).
editor.conversion.elementToElement( { model: 'paragraph', view: 'p' } );

// Override other converters by specifying a converter definition with a higher priority.
editor.conversion.elementToElement( { model: 'paragraph', view: 'div', converterPriority: 'high' } );

// View specified as an object instead of a string.
editor.conversion.elementToElement( {
	model: 'fancyParagraph',
	view: {
		name: 'p',
		classes: 'fancy'
	}
} );

// Use `upcastAlso` to define other view elements that should also be converted to a `paragraph` element.
editor.conversion.elementToElement( {
	model: 'paragraph',
	view: 'p',
	upcastAlso: [
		'div',
		{
			// Any element with the `display: block` style.
			styles: {
				display: 'block'
			}
		}
	]
} );

// `upcastAlso` set as callback enables a conversion of a wide range of different view elements.
editor.conversion.elementToElement( {
	model: 'heading',
	view: 'h2',
	// Convert "heading-like" paragraphs to headings.
	upcastAlso: viewElement => {
		const fontSize = viewElement.getStyle( 'font-size' );

		if ( !fontSize ) {
			return null;
		}

		const match = fontSize.match( /(\d+)\s*px/ );

		if ( !match ) {
			return null;
		}

		const size = Number( match[ 1 ] );

		if ( size > 26 ) {
			// Returned value can be an object with the matched properties.
			// These properties will be "consumed" during the conversion.
			// See `engine.view.Matcher~MatcherPattern` and `engine.view.Matcher#match` for more details.

			return { name: true, styles: [ 'font-size' ] };
		}

		return null;
	}
} );
```

`definition.model` is a `String` with a model element name to convert from or to.

[See source](https://github.com/ckeditor/ckeditor5/blob/master/packages/ckeditor5-engine/src/conversion/conversion.ts#L304)

#### Parameters

* `definition: object`

  The converter definition.

  Properties

  * `definition.converterPriority?: PriorityString`
  * `definition.model: string`
  * `definition.upcastAlso?: ArrayOrItem<string | RegExp | MatcherFunctionPattern | MatcherObjectPattern | ViewElementObjectDefinition>`
  * `definition.view: ViewElementDefinition`

#### Returns

* `void`

<a id="function-for">

### `for( groupName ) → UpcastHelpers`

[See source](https://github.com/ckeditor/ckeditor5/blob/master/packages/ckeditor5-engine/src/conversion/conversion.ts#L150)

#### Parameters

* `groupName: 'upcast'`

#### Returns

* `UpcastHelpers`

<a id="function-for">

### `for( groupName ) → ConversionType<T>`

[See source](https://github.com/ckeditor/ckeditor5/blob/master/packages/ckeditor5-engine/src/conversion/conversion.ts#L151)

#### Type parameters

* `T: extends string`

#### Parameters

* `groupName: T`

#### Returns

* `ConversionType<T>`

<a id="function-for">

### `for( groupName ) → DowncastHelpers`

[See source](https://github.com/ckeditor/ckeditor5/blob/master/packages/ckeditor5-engine/src/conversion/conversion.ts#L149)

#### Parameters

* `groupName: 'downcast' | 'dataDowncast' | 'editingDowncast'`

#### Returns

* `DowncastHelpers`

<a id="function-_createConversionHelpers">

### `_createConversionHelpers( options = { options.dispatchers, options.isDowncast, options.name } ) → void` _(private)_

Creates and caches conversion helpers for given dispatchers group.

[See source](https://github.com/ckeditor/ckeditor5/blob/master/packages/ckeditor5-engine/src/conversion/conversion.ts#L668)

#### Parameters

* `options: object`

  Group name.

  Properties

  * `options.dispatchers: Array<DowncastDispatcher | UpcastDispatcher>`

    Dispatchers to register.

  * `options.isDowncast: boolean`

    Whether downcast group.

  * `options.name: string`

    Group name.

#### Returns

* `void`

---

Full index of the CKEditor 5 API reference: [llms.txt](llms.txt)
