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

Commit

Permalink
Merge dbe2d8f into ecaf056
Browse files Browse the repository at this point in the history
  • Loading branch information
jodator committed Feb 11, 2020
2 parents ecaf056 + dbe2d8f commit 9e12bbf
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 5 deletions.
35 changes: 34 additions & 1 deletion src/view/stylesmap.js
Original file line number Diff line number Diff line change
Expand Up @@ -208,8 +208,12 @@ export default class StylesMap {
* @param {String} name Style name.
*/
remove( name ) {
unset( this._styles, toPath( name ) );
const path = toPath( name );

unset( this._styles, path );
delete this._styles[ name ];

this._cleanEmptyObjectsOnPath( path );
}

/**
Expand Down Expand Up @@ -407,6 +411,35 @@ export default class StylesMap {
return parsed;
}

/**
* Removes empty objects upon removing an entry from internal object.
*
* @param {String} path
* @private
*/
_cleanEmptyObjectsOnPath( path ) {
const pathParts = path.split( '.' );
const isChildPath = pathParts.length > 1;

if ( !isChildPath ) {
return;
}

const parentPath = pathParts.splice( 0, pathParts.length - 1 ).join( '.' );

const parentObject = get( this._styles, parentPath );

if ( !parentObject ) {
return;
}

const isParentEmpty = !Array.from( Object.keys( parentObject ) ).length;

if ( isParentEmpty ) {
this.remove( parentPath );
}
}

/**
* Returns global StylesProcessor instance.
*
Expand Down
34 changes: 34 additions & 0 deletions tests/view/styles/border.js
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,40 @@ describe( 'Border styles normalization', () => {
expect( styles.getAsString( 'border-left' ) ).to.equal( '1px' );
} );

it( 'should properly remove border properties one by one', () => {
styles.setTo( 'border:1px solid blue;' );

expect( styles.toString() ).to.equal(
'border-bottom:1px solid blue;' +
'border-left:1px solid blue;' +
'border-right:1px solid blue;' +
'border-top:1px solid blue;'
);

styles.remove( 'border-color' );

expect( styles.toString() ).to.equal(
'border-bottom:1px solid;' +
'border-left:1px solid;' +
'border-right:1px solid;' +
'border-top:1px solid;'
);

styles.remove( 'border-style' );

expect( styles.toString() ).to.equal(
'border-bottom:1px;' +
'border-left:1px;' +
'border-right:1px;' +
'border-top:1px;'
);

styles.remove( 'border-width' );

expect( styles.isEmpty ).to.be.true;
expect( styles.toString() ).to.equal( '' );
} );

describe( 'normalized values getters', () => {
it( 'should output border-*-color', () => {
styles.setTo( 'border:1px solid #f00;' );
Expand Down
29 changes: 25 additions & 4 deletions tests/view/stylesmap.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import StylesMap, { StylesProcessor } from '../../src/view/stylesmap';
import encodedImage from './_utils/encodedimage.txt';
import { addPaddingRules } from '../../src/view/styles/padding';
import { addMarginRules } from '../../src/view/styles/margin';
import { getBoxSidesValueReducer } from '../../src/view/styles/utils';

describe( 'StylesMap', () => {
Expand All @@ -25,7 +25,7 @@ describe( 'StylesMap', () => {
} ) );
stylesProcessor.setReducer( 'foo', getBoxSidesValueReducer( 'foo' ) );

addPaddingRules( stylesProcessor );
addMarginRules( stylesProcessor );
StylesMap._setProcessor( stylesProcessor );
stylesMap = new StylesMap();
} );
Expand All @@ -49,9 +49,9 @@ describe( 'StylesMap', () => {

describe( 'setTo()', () => {
it( 'should reset styles to a new value', () => {
stylesMap.setTo( 'color:red;margin:1px;' );
stylesMap.setTo( 'color:red;padding:1px;' );

expect( stylesMap.getNormalized() ).to.deep.equal( { color: 'red', margin: '1px' } );
expect( stylesMap.getNormalized() ).to.deep.equal( { color: 'red', padding: '1px' } );

stylesMap.setTo( 'overflow:hidden;' );

Expand Down Expand Up @@ -232,6 +232,27 @@ describe( 'StylesMap', () => {

expect( stylesMap.getAsString( 'margin-top' ) ).to.be.undefined;
} );

it( 'should remove normalized properties one by one', () => {
stylesMap.setTo( 'margin:1px' );

stylesMap.remove( 'margin-top' );
stylesMap.remove( 'margin-right' );
stylesMap.remove( 'margin-bottom' );
stylesMap.remove( 'margin-left' );

expect( stylesMap.toString() ).to.equal( '' );
} );

it( 'should remove path-like property', () => {
stylesMap.setTo( 'text-align:left' );

expect( stylesMap.toString() ).to.equal( 'text-align:left;' );

stylesMap.remove( 'text-align' );

expect( stylesMap.toString() ).to.equal( '' );
} );
} );

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

0 comments on commit 9e12bbf

Please sign in to comment.