|
@@ -143,9 +143,24 @@ class ViewConverterBuilder {
|
|
|
*/
|
|
*/
|
|
|
fromAttribute( key, value = /.*/ ) {
|
|
fromAttribute( key, value = /.*/ ) {
|
|
|
let pattern = {};
|
|
let pattern = {};
|
|
|
- pattern[ key ] = value;
|
|
|
|
|
|
|
|
|
|
- return this.from( pattern );
|
|
|
|
|
|
|
+ if ( key === 'style' || key === 'class' ) {
|
|
|
|
|
+ pattern[ key ] = value;
|
|
|
|
|
+ } else {
|
|
|
|
|
+ pattern.attribute = {};
|
|
|
|
|
+ pattern.attribute[ key ] = value;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ const matcher = new Matcher( pattern );
|
|
|
|
|
+
|
|
|
|
|
+ this._from.push( {
|
|
|
|
|
+ matcher: matcher,
|
|
|
|
|
+ consume: false,
|
|
|
|
|
+ priority: null,
|
|
|
|
|
+ attributeKey: key
|
|
|
|
|
+ } );
|
|
|
|
|
+
|
|
|
|
|
+ return this;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -191,9 +206,9 @@ class ViewConverterBuilder {
|
|
|
* .toAttribute( 'bold', 'true' } );
|
|
* .toAttribute( 'bold', 'true' } );
|
|
|
*
|
|
*
|
|
|
* buildViewConverter().for( dispatcher )
|
|
* buildViewConverter().for( dispatcher )
|
|
|
- * .fromElement( 'img' ).consuming( { name: true, attributes: [ 'src', 'title' ] } )
|
|
|
|
|
|
|
+ * .fromElement( 'img' ).consuming( { name: true, attribute: [ 'src', 'title' ] } )
|
|
|
* .toElement( ( viewElement ) => new ModelElement( 'image', { src: viewElement.getAttribute( 'src' ),
|
|
* .toElement( ( viewElement ) => new ModelElement( 'image', { src: viewElement.getAttribute( 'src' ),
|
|
|
- * title: viewElement.getAttribute( 'title' ) } );
|
|
|
|
|
|
|
+ * title: viewElement.getAttribute( 'title' ) } );
|
|
|
*
|
|
*
|
|
|
* **Note:** All and only values from passed object has to be consumable on converted view element. This means that
|
|
* **Note:** All and only values from passed object has to be consumable on converted view element. This means that
|
|
|
* using `consuming` method, you can either make looser conversion conditions (like in first example) or tighter
|
|
* using `consuming` method, you can either make looser conversion conditions (like in first example) or tighter
|
|
@@ -271,6 +286,11 @@ class ViewConverterBuilder {
|
|
|
// Create model element basing on creator function or element name.
|
|
// Create model element basing on creator function or element name.
|
|
|
const modelElement = element instanceof Function ? element( data.input ) : new ModelElement( element );
|
|
const modelElement = element instanceof Function ? element( data.input ) : new ModelElement( element );
|
|
|
|
|
|
|
|
|
|
+ // Do not convert if element building function returned falsy value.
|
|
|
|
|
+ if ( !modelElement ) {
|
|
|
|
|
+ continue;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
// Check whether generated structure is okay with `Schema`.
|
|
// Check whether generated structure is okay with `Schema`.
|
|
|
const keys = Array.from( modelElement.getAttributeKeys() );
|
|
const keys = Array.from( modelElement.getAttributeKeys() );
|
|
|
|
|
|
|
@@ -314,13 +334,15 @@ class ViewConverterBuilder {
|
|
|
* representing attribute key and attribute value or a function that returns an object with `key` and `value` properties.
|
|
* representing attribute key and attribute value or a function that returns an object with `key` and `value` properties.
|
|
|
* If you provide creator function, it will be passed converted view element as first and only parameter.
|
|
* If you provide creator function, it will be passed converted view element as first and only parameter.
|
|
|
*
|
|
*
|
|
|
- * buildViewConverter().for( dispatcher ).fromAttribute( 'style', { 'font-weight': 'bold' } ).toAttribute( 'bold', 'true' );
|
|
|
|
|
|
|
+ * buildViewConverter().for( dispatcher ).fromAttribute( 'alt' ).toAttribute( 'alt' );
|
|
|
|
|
+ * buildViewConverter().for( dispatcher ).fromAttribute( 'style', { 'font-weight': 'bold' } ).toAttribute( 'bold', true );
|
|
|
* buildViewConverter().for( dispatcher )
|
|
* buildViewConverter().for( dispatcher )
|
|
|
* .fromAttribute( 'class' )
|
|
* .fromAttribute( 'class' )
|
|
|
- * .toAttribute( ( viewElement ) => ( { key: 'class', value: viewElement.getAttribute( 'class' ) } ) );
|
|
|
|
|
|
|
+ * .toAttribute( ( viewElement ) => ( { key: 'class', value: 'class-' + viewElement.getAttribute( 'class' ) } ) );
|
|
|
*
|
|
*
|
|
|
* @param {String|Function} keyOrCreator Attribute key or a creator function.
|
|
* @param {String|Function} keyOrCreator Attribute key or a creator function.
|
|
|
- * @param {String} [value] Attribute value. Required if `keyOrCreator` is a `string`. Ignored otherwise.
|
|
|
|
|
|
|
+ * @param {String} [value] Attribute value. Ignored if `keyOrCreator` is not a `string`. If `keyOrCreator` is `string`,
|
|
|
|
|
+ * if `value` is not set, attribute value from converted element will be used.
|
|
|
*/
|
|
*/
|
|
|
toAttribute( keyOrCreator, value ) {
|
|
toAttribute( keyOrCreator, value ) {
|
|
|
function eventCallbackGen( from ) {
|
|
function eventCallbackGen( from ) {
|
|
@@ -348,7 +370,20 @@ class ViewConverterBuilder {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// Use attribute creator function, if provided.
|
|
// Use attribute creator function, if provided.
|
|
|
- let attribute = keyOrCreator instanceof Function ? keyOrCreator( data.input ) : { key: keyOrCreator, value: value };
|
|
|
|
|
|
|
+ let attribute;
|
|
|
|
|
+
|
|
|
|
|
+ if ( keyOrCreator instanceof Function ) {
|
|
|
|
|
+ attribute = keyOrCreator( data.input );
|
|
|
|
|
+
|
|
|
|
|
+ if ( !attribute ) {
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+ } else {
|
|
|
|
|
+ attribute = {
|
|
|
|
|
+ key: keyOrCreator,
|
|
|
|
|
+ value: value ? value : data.input.getAttribute( from.attributeKey )
|
|
|
|
|
+ };
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
// Set attribute on current `output`. `Schema` is checked inside this helper function.
|
|
// Set attribute on current `output`. `Schema` is checked inside this helper function.
|
|
|
setAttributeOn( data.output, attribute, data, conversionApi );
|
|
setAttributeOn( data.output, attribute, data, conversionApi );
|