UpcastHelpers
Upcast conversion helper functions.
Learn more about upcast helpers.
Methods
constructor( dispatchers )inheritedmodule:engine/conversion/upcasthelpers~UpcastHelpers#constructoradd( conversionHelper ) → thisinheritedmodule:engine/conversion/upcasthelpers~UpcastHelpers#addRegisters a conversion helper.
Note: See full usage example in the
conversion.for()method description.Parameters
conversionHelper : ( dispatcher: UpcastDispatcher ) => voidThe function to be called on event.
Returns
this
attributeToAttribute( config = { [config.converterPriority], config.model, config.view } ) → thismodule:engine/conversion/upcasthelpers~UpcastHelpers#attributeToAttributeView attribute to model attribute conversion helper.
This conversion results in setting an attribute on a model node. For example, view
<img src="foo.jpg"></img>becomes<imageBlock source="foo.jpg"></imageBlock>in the model.This helper is meant to convert view attributes from view elements which got converted to the model, so the view attribute is set only on the corresponding model node:
<div class="dark"><div>foo</div></div> --> <div dark="true"><div>foo</div></div>Copy codeAbove,
class="dark"attribute is added only to the<div>elements that has it. This is in contrast toelementToAttributewhich sets attributes for all the children in the model:<strong>Foo</strong> --> <strong><p>Foo</p></strong> --> <paragraph><$text bold="true">Foo</$text></paragraph>Copy codeAbove is a sample of HTML code, that goes through autoparagraphing (first step) and then is converted (second step). Even though
<strong>is over<p>element,bold="true"was added to the text.Keep in mind that the attribute will be set only if it is allowed by schema configuration.
editor.conversion.for( 'upcast' ).attributeToAttribute( { view: 'src', model: 'source' } ); editor.conversion.for( 'upcast' ).attributeToAttribute( { view: { key: 'src' }, model: 'source' } ); editor.conversion.for( 'upcast' ).attributeToAttribute( { view: { key: 'src' }, model: 'source', converterPriority: 'normal' } ); editor.conversion.for( 'upcast' ).attributeToAttribute( { view: { key: 'data-style', value: /[\s\S]+/ }, model: 'styled' } ); editor.conversion.for( 'upcast' ).attributeToAttribute( { view: { name: 'img', key: 'class', value: 'styled-dark' }, model: { key: 'styled', value: 'dark' } } ); editor.conversion.for( 'upcast' ).attributeToAttribute( { view: { key: 'class', value: /styled-[\S]+/ }, model: { key: 'styled' value: ( viewElement, conversionApi ) => { const regexp = /styled-([\S]+)/; const match = viewElement.getAttribute( 'class' ).match( regexp ); return match[ 1 ]; } } } );Copy codeConverting styles works a bit differently as it requires
view.stylesto be an object and by default a model attribute will be set totrueby such a converter. You can set the model attribute to any value by providing thevaluecallback that returns the desired value.// Default conversion of font-weight style will result in setting bold attribute to true. editor.conversion.for( 'upcast' ).attributeToAttribute( { view: { styles: { 'font-weight': 'bold' } }, model: 'bold' } ); // This converter will pass any style value to the `lineHeight` model attribute. editor.conversion.for( 'upcast' ).attributeToAttribute( { view: { styles: { 'line-height': /[\s\S]+/ } }, model: { key: 'lineHeight', value: ( viewElement, conversionApi ) => viewElement.getStyle( 'line-height' ) } } );Copy codeSee
conversion.for()to learn how to add a converter to the conversion process.Parameters
config : objectConversion configuration.
Properties[ config.converterPriority ] : PriorityStringConverter priority. Defaults to
low.config.model : string | objectModel attribute key or an object with
keyandvalueproperties, describing the model attribute.valueproperty may be set as a function that takes a view element and upcast conversion API and returns the value. IfStringis given, the model attribute value will be same as view attribute value.config.view : string | objectSpecifies which view attribute will be converted. If a
Stringis passed, attributes with given key will be converted. If anObjectis passed, it must have a requiredkeyproperty, specifying view attribute key, and may have an optionalvalueproperty, specifying view attribute value and optionalnameproperty specifying a view element name from/on which the attribute should be converted.valuecan be given as aString, aRegExpor a function callback, that takes view attribute value as the only parameter and returnsBoolean.
Returns
this
dataToMarker( config = { [config.converterPriority], [config.model], config.view } ) → thismodule:engine/conversion/upcasthelpers~UpcastHelpers#dataToMarkerView-to-model marker conversion helper.
Converts view data created by
#markerToData()back to a model marker.This converter looks for specific view elements and view attributes that mark marker boundaries. See
#markerToData()to learn what view data is expected by this converter.The
config.viewproperty is equal to the marker group name to convert.By default, this converter creates markers with the
group:namename convention (to match the defaultmarkerToDataconversion).The conversion configuration can take a function that will generate a marker name. If such function is set as the
config.modelparameter, it is passed thenamepart from the view element or attribute and it is expected to return a string with the marker name.Basic usage:
// Using the default conversion. // In this case, all markers from the `comment` group will be converted. // The conversion will look for `<comment-start>` and `<comment-end>` tags and // `data-comment-start-before`, `data-comment-start-after`, // `data-comment-end-before` and `data-comment-end-after` attributes. editor.conversion.for( 'upcast' ).dataToMarker( { view: 'comment' } );Copy codeAn example of a model that may be generated by this conversion:
// View: <p>Foo<comment-start name="commentId:uid"></comment-start>bar</p> <figure data-comment-end-after="commentId:uid" class="image"><img src="abc.jpg" /></figure> // Model: <paragraph>Foo[bar</paragraph> <imageBlock src="abc.jpg"></imageBlock>]Copy codeWhere
[]are boundaries of a marker that will receive thecomment:commentId:uidname.Other examples of usage:
// Using a custom function which is the same as the default conversion: editor.conversion.for( 'upcast' ).dataToMarker( { view: 'comment', model: ( name, conversionApi ) => 'comment:' + name, } ); // Using the converter priority: editor.conversion.for( 'upcast' ).dataToMarker( { view: 'comment', model: ( name, conversionApi ) => 'comment:' + name, converterPriority: 'high' } );Copy codeSee
conversion.for()to learn how to add a converter to the conversion process.Parameters
config : objectConversion configuration.
Properties[ config.converterPriority ] : PriorityStringConverter priority.
[ config.model ] : UpcastMarkerFromAttributeCreatorFunctionA function that takes the
namepart from the view element or attribute and upcast conversion API and returns the marker name.config.view : stringThe marker group name to convert.
Returns
this
elementToAttribute( config = { [config.converterPriority], config.model, config.view } ) → thismodule:engine/conversion/upcasthelpers~UpcastHelpers#elementToAttributeView element to model attribute conversion helper.
This conversion results in setting an attribute on a model node. For example, view
<strong>Foo</strong>becomesFoomodel text node withboldattribute set totrue.This helper is meant to set a model attribute on all the elements that are inside the converted element:
<strong>Foo</strong> --> <strong><p>Foo</p></strong> --> <paragraph><$text bold="true">Foo</$text></paragraph>Copy codeAbove is a sample of HTML code, that goes through autoparagraphing (first step) and then is converted (second step). Even though
<strong>is over<p>element,bold="true"was added to the text. SeeattributeToAttributefor comparison.Keep in mind that the attribute will be set only if it is allowed by schema configuration.
editor.conversion.for( 'upcast' ).elementToAttribute( { view: 'strong', model: 'bold' } ); editor.conversion.for( 'upcast' ).elementToAttribute( { view: 'strong', model: 'bold', converterPriority: 'high' } ); editor.conversion.for( 'upcast' ).elementToAttribute( { view: { name: 'span', classes: 'bold' }, model: 'bold' } ); editor.conversion.for( 'upcast' ).elementToAttribute( { view: { name: 'span', classes: [ 'styled', 'styled-dark' ] }, model: { key: 'styled', value: 'dark' } } ); editor.conversion.for( 'upcast' ).elementToAttribute( { view: { name: 'span', styles: { 'font-size': /[\s\S]+/ } }, model: { key: 'fontSize', value: ( viewElement, conversionApi ) => { const fontSize = viewElement.getStyle( 'font-size' ); const value = fontSize.substr( 0, fontSize.length - 2 ); if ( value <= 10 ) { return 'small'; } else if ( value > 12 ) { return 'big'; } return null; } } } );Copy codeSee
conversion.for()to learn how to add a converter to the conversion process.Parameters
config : objectConversion configuration.
Properties[ config.converterPriority ] : PriorityStringConverter priority. Defaults to
low.config.model : string | objectModel attribute key or an object with
keyandvalueproperties, describing the model attribute.valueproperty may be set as a function that takes a view element and upcast conversion API and returns the value. IfStringis given, the model attribute value will be set totrue.config.view : MatcherPatternPattern matching all view elements which should be converted.
Returns
this
elementToElement( config = { [config.converterPriority], config.model, config.view } ) → thismodule:engine/conversion/upcasthelpers~UpcastHelpers#elementToElementView element to model element conversion helper.
This conversion results in creating a model element. For example, view
<p>Foo</p>becomes<paragraph>Foo</paragraph>in the model.Keep in mind that the element will be inserted only if it is allowed by schema configuration.
editor.conversion.for( 'upcast' ).elementToElement( { view: 'p', model: 'paragraph' } ); editor.conversion.for( 'upcast' ).elementToElement( { view: 'p', model: 'paragraph', converterPriority: 'high' } ); editor.conversion.for( 'upcast' ).elementToElement( { view: { name: 'p', classes: 'fancy' }, model: 'fancyParagraph' } ); editor.conversion.for( 'upcast' ).elementToElement( { view: { name: 'p', classes: 'heading' }, model: ( viewElement, conversionApi ) => { const modelWriter = conversionApi.writer; return modelWriter.createElement( 'heading', { level: viewElement.getAttribute( 'data-level' ) } ); } } );Copy codeSee
conversion.for()to learn how to add a converter to the conversion process.Parameters
config : objectConversion configuration.
Properties[ config.converterPriority ] : PriorityStringConverter priority.
config.model : string | UpcastElementCreatorFunctionName of the model element, a model element instance or a function that takes a view element and upcast conversion API and returns a model element. The model element will be inserted in the model.
config.view : MatcherPatternPattern matching all view elements which should be converted. If not set, the converter will fire for every view element.
Returns
this
elementToMarker( config = { [config.converterPriority], config.model, config.view } ) → thismodule:engine/conversion/upcasthelpers~UpcastHelpers#elementToMarkerView element to model marker conversion helper.
This conversion results in creating a model marker. For example, if the marker was stored in a view as an element:
<p>Fo<span data-marker="comment" data-comment-id="7"></span>o</p><p>B<span data-marker="comment" data-comment-id="7"></span>ar</p>, after the conversion is done, the marker will be available in model document markers.Note: When this helper is used in the data upcast in combination with
#markerToData()in the data downcast, then invalid HTML code (e.g. a span between table cells) may be produced by the latter converter.In most of the cases, the
dataToMarkershould be used instead.editor.conversion.for( 'upcast' ).elementToMarker( { view: 'marker-search', model: 'search' } ); editor.conversion.for( 'upcast' ).elementToMarker( { view: 'marker-search', model: 'search', converterPriority: 'high' } ); editor.conversion.for( 'upcast' ).elementToMarker( { view: 'marker-search', model: ( viewElement, conversionApi ) => 'comment:' + viewElement.getAttribute( 'data-comment-id' ) } ); editor.conversion.for( 'upcast' ).elementToMarker( { view: { name: 'span', attributes: { 'data-marker': 'search' } }, model: 'search' } );Copy codeSee
conversion.for()to learn how to add a converter to the conversion process.Parameters
config : objectConversion configuration.
Properties[ config.converterPriority ] : PriorityStringConverter priority.
config.model : string | UpcastMarkerFromElementCreatorFunctionName of the model marker, or a function that takes a view element and returns a model marker name.
config.view : MatcherPatternPattern matching all view elements which should be converted.
Returns
this