Skip to content
This repository has been archived by the owner on Jun 26, 2020. It is now read-only.

Commit

Permalink
Simplify code.
Browse files Browse the repository at this point in the history
  • Loading branch information
jodator committed Oct 7, 2019
1 parent 3455103 commit 15de90c
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 28 deletions.
50 changes: 22 additions & 28 deletions src/view/styles.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ export default class Styles {
for ( const key of map.keys() ) {
const value = map.get( key );

this._getNormalizedForm( key, value );
this._toNormalizedForm( key, value );
}
}

Expand Down Expand Up @@ -207,7 +207,7 @@ export default class Styles {
this.insertProperty( key, nameOrObject[ key ] );
}
} else {
this._getNormalizedForm( nameOrObject, value );
this._toNormalizedForm( nameOrObject, value );
}
}

Expand Down Expand Up @@ -335,54 +335,33 @@ export default class Styles {
return parsed;
}

/**
* Appends style definition to the internal styles object.
*
* @param {String} nameOrPath
* @param {String|Object} valueOrObject
* @private
*/
_appendStyleValue( nameOrPath, valueOrObject ) {
if ( isObject( valueOrObject ) ) {
if ( nameOrPath.includes( '.' ) ) {
const got = get( this._styles, nameOrPath );

set( this._styles, nameOrPath, merge( {}, got, valueOrObject ) );
} else {
this._styles[ nameOrPath ] = merge( {}, this._styles[ nameOrPath ], valueOrObject );
}
} else {
set( this._styles, nameOrPath, valueOrObject );
}
}

/**
* Parse style property value to a normalized form.
*
* @param {String} propertyName Name of style property.
* @param {String} value Value of style property.
* @private
*/
_getNormalizedForm( propertyName, value ) {
_toNormalizedForm( propertyName, value ) {
if ( isObject( value ) ) {
this._appendStyleValue( toPath( propertyName ), value );

appendStyleValue( this._styles, toPath( propertyName ), value );
return;
}

// Set directly to an object.
if ( setOnPathStyles.includes( propertyName ) ) {
this._appendStyleValue( toPath( propertyName ), value );
appendStyleValue( this._styles, toPath( propertyName ), value );

return;
}

if ( this.normalizers.has( propertyName ) ) {
const parser = this.normalizers.get( propertyName );

// TODO: merge with appendStyleValue?
this._styles = merge( {}, this._styles, parser( value ) );
} else {
this._appendStyleValue( propertyName, value );
appendStyleValue( this._styles, propertyName, value );
}
}

Expand Down Expand Up @@ -674,3 +653,18 @@ function parseInlineStyles( stylesString ) {
function toPath( name ) {
return name.replace( '-', '.' );
}

// Appends style definition to the styles object.
//
// @param {String} nameOrPath
// @param {String|Object} valueOrObject
// @private
function appendStyleValue( stylesObject, nameOrPath, valueOrObject ) {
let valueToSet = valueOrObject;

if ( isObject( valueOrObject ) ) {
valueToSet = merge( {}, get( stylesObject, nameOrPath ), valueOrObject );
}

set( stylesObject, nameOrPath, valueToSet );
}
8 changes: 8 additions & 0 deletions tests/view/styles.js
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,14 @@ describe( 'Styles', () => {
expect( styles.getInlineProperty( 'color' ) ).to.equal( 'blue' );
expect( styles.getInlineProperty( 'margin-top' ) ).to.equal( '1px' );
} );

it( 'should set object property', () => {
styles.setStyle( 'margin:1px;' );
styles.insertProperty( 'margin', { right: '2px' } );

expect( styles.getInlineProperty( 'margin-left' ) ).to.equal( '1px' );
expect( styles.getInlineProperty( 'margin-right' ) ).to.equal( '2px' );
} );
} );

describe( 'removeProperty()', () => {
Expand Down

0 comments on commit 15de90c

Please sign in to comment.