Skip to content

Commit

Permalink
fixes #1844
Browse files Browse the repository at this point in the history
  • Loading branch information
aristath committed Mar 26, 2018
1 parent 04c9837 commit 791c457
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 53 deletions.
61 changes: 35 additions & 26 deletions controls/js/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -1261,6 +1261,39 @@ kirki = jQuery.extend( kirki, {
}
return false;
}
},

validate: {
cssValue: function( value ) {

var validUnits = [ 'rem', 'em', 'ex', '%', 'px', 'cm', 'mm', 'in', 'pt', 'pc', 'ch', 'vh', 'vw', 'vmin', 'vmax' ],
numericValue,
unit;

// Whitelist values.
if ( 0 === value || '0' === value || 'auto' === value || 'inherit' === value || 'initial' === value ) {
return true;
}

// Skip checking if calc().
if ( 0 <= value.indexOf( 'calc(' ) && 0 <= value.indexOf( ')' ) ) {
return true;
}

// Get the numeric value.
numericValue = parseFloat( value );

// Get the unit
unit = value.replace( numericValue, '' );

// Allow unitless.
if ( ! value ) {
return;
}

// Check the validity of the numeric value and units.
return ( ! isNaN( numericValue ) && -1 < jQuery.inArray( unit, validUnits ) );
}
}
}
} );
Expand Down Expand Up @@ -1461,30 +1494,6 @@ kirki = jQuery.extend( kirki, {
this.container.on( 'change keyup paste click', 'input', function() {
control.setting.set( jQuery( this ).val() );
} );
},

kirkiValidateCSSValue: function( value ) {

var validUnits = [ 'rem', 'em', 'ex', '%', 'px', 'cm', 'mm', 'in', 'pt', 'pc', 'ch', 'vh', 'vw', 'vmin', 'vmax' ],
numericValue,
unit;

if (
'string' !== typeof value ||
( '0' === value || ( 0 <= value.indexOf( 'calc(' ) && 0 <= value.indexOf( ')' ) ) ) ||
( 'auto' === value || 'inherit' === value || 'initial' === value )
) {
return true;
}

// Get the numeric value.
numericValue = parseFloat( value );

// Get the unit
unit = value.replace( numericValue, '' );

// Check the validity of the numeric value and units.
return ( isNaN( numericValue ) || -1 === jQuery.inArray( unit, validUnits ) );
}
} );
}() );
Expand Down Expand Up @@ -1690,7 +1699,7 @@ wp.customize.controlConstructor['kirki-dimension'] = wp.customize.kirkiDynamicCo
setting.bind( function( value ) {
var code = 'long_title';

if ( false === control.kirkiValidateCSSValue( value ) ) {
if ( false === kirki.util.validate.cssValue( value ) ) {
setting.notifications.add( code, new wp.customize.Notification(
code,
{
Expand Down Expand Up @@ -1777,7 +1786,7 @@ wp.customize.controlConstructor['kirki-dimensions'] = wp.customize.kirkiDynamicC
setting.notifications.remove( code );

_.each( value, function( val, direction ) {
if ( false === control.kirkiValidateCSSValue( val ) ) {
if ( false === kirki.util.validate.cssValue( val ) ) {
subs[ direction ] = val;
} else {
delete subs[ direction ];
Expand Down
2 changes: 1 addition & 1 deletion controls/js/script.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion controls/js/src/dimension.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ wp.customize.controlConstructor['kirki-dimension'] = wp.customize.kirkiDynamicCo
setting.bind( function( value ) {
var code = 'long_title';

if ( false === control.kirkiValidateCSSValue( value ) ) {
if ( false === kirki.util.validate.cssValue( value ) ) {
setting.notifications.add( code, new wp.customize.Notification(
code,
{
Expand Down
2 changes: 1 addition & 1 deletion controls/js/src/dimensions.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ wp.customize.controlConstructor['kirki-dimensions'] = wp.customize.kirkiDynamicC
setting.notifications.remove( code );

_.each( value, function( val, direction ) {
if ( false === control.kirkiValidateCSSValue( val ) ) {
if ( false === kirki.util.validate.cssValue( val ) ) {
subs[ direction ] = val;
} else {
delete subs[ direction ];
Expand Down
24 changes: 0 additions & 24 deletions controls/js/src/dynamic-control.js
Original file line number Diff line number Diff line change
Expand Up @@ -195,30 +195,6 @@
this.container.on( 'change keyup paste click', 'input', function() {
control.setting.set( jQuery( this ).val() );
} );
},

kirkiValidateCSSValue: function( value ) {

var validUnits = [ 'rem', 'em', 'ex', '%', 'px', 'cm', 'mm', 'in', 'pt', 'pc', 'ch', 'vh', 'vw', 'vmin', 'vmax' ],
numericValue,
unit;

if (
'string' !== typeof value ||
( '0' === value || ( 0 <= value.indexOf( 'calc(' ) && 0 <= value.indexOf( ')' ) ) ) ||
( 'auto' === value || 'inherit' === value || 'initial' === value )
) {
return true;
}

// Get the numeric value.
numericValue = parseFloat( value );

// Get the unit
unit = value.replace( numericValue, '' );

// Check the validity of the numeric value and units.
return ( isNaN( numericValue ) || -1 === jQuery.inArray( unit, validUnits ) );
}
} );
}() );
Expand Down
33 changes: 33 additions & 0 deletions controls/js/src/kirki.util.js
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,39 @@ kirki = jQuery.extend( kirki, {
}
return false;
}
},

validate: {
cssValue: function( value ) {

var validUnits = [ 'rem', 'em', 'ex', '%', 'px', 'cm', 'mm', 'in', 'pt', 'pc', 'ch', 'vh', 'vw', 'vmin', 'vmax' ],
numericValue,
unit;

// Whitelist values.
if ( 0 === value || '0' === value || 'auto' === value || 'inherit' === value || 'initial' === value ) {
return true;
}

// Skip checking if calc().
if ( 0 <= value.indexOf( 'calc(' ) && 0 <= value.indexOf( ')' ) ) {
return true;
}

// Get the numeric value.
numericValue = parseFloat( value );

// Get the unit
unit = value.replace( numericValue, '' );

// Allow unitless.
if ( ! value ) {
return;
}

// Check the validity of the numeric value and units.
return ( ! isNaN( numericValue ) && -1 < jQuery.inArray( unit, validUnits ) );
}
}
}
} );

0 comments on commit 791c457

Please sign in to comment.