Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pass explicit false value for falsey checkboxes #413

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 17 additions & 6 deletions js-tests/build/specs.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,11 @@ describe( "Shortcode Model", function() {
type: 'text',
value: 'test value',
placeholder: 'test placeholder',
}, {
attr: 'checkbox',
label: 'Attribute',
type: 'checkbox',
value: 'true',
}
],
inner_content: {
Expand All @@ -94,8 +99,9 @@ describe( "Shortcode Model", function() {

it( 'Attribute data set correctly..', function() {
expect( shortcode.get( 'attrs' ) instanceof ShortcodeAttributes ).toEqual( true );
expect( shortcode.get( 'attrs' ).length ).toEqual( 1 );
expect( shortcode.get( 'attrs' ).length ).toEqual( 2 );
expect( shortcode.get( 'attrs' ).first().get('type') ).toEqual( data.attrs[0].type );
expect( shortcode.get( 'attrs' ).last().get('type') ).toEqual( data.attrs[1].type );
});

it( 'Inner content set correctly..', function() {
Expand All @@ -114,16 +120,19 @@ describe( "Shortcode Model", function() {
var _shortcode = jQuery.extend( true, {}, shortcode );

// Test with attribute and with content.
expect( _shortcode.formatShortcode() ).toEqual( '[test_shortcode attr="test value"]test content[/test_shortcode]' );
expect( _shortcode.formatShortcode() ).toEqual( '[test_shortcode attr="test value" checkbox="true"]test content[/test_shortcode]' );

// Test without content.
_shortcode.get('inner_content').unset( 'value' );
expect( _shortcode.formatShortcode() ).toEqual( '[test_shortcode attr="test value"]' );
expect( _shortcode.formatShortcode() ).toEqual( '[test_shortcode attr="test value" checkbox="true"]' );

// Test without attributes
_shortcode.get( 'attrs' ).first().unset( 'value' );
expect( _shortcode.formatShortcode() ).toEqual( '[test_shortcode]' );
expect( _shortcode.formatShortcode() ).toEqual( '[test_shortcode checkbox="true"]' );

// Test with falsey checkbox
_shortcode.get( 'attrs' ).last().set( 'value', 'false' );
expect( _shortcode.formatShortcode() ).toEqual( '[test_shortcode checkbox="false"]' );
});

});
Expand Down Expand Up @@ -449,9 +458,11 @@ Shortcode = Backbone.Model.extend({

this.get( 'attrs' ).each( function( attr ) {

// Skip empty attributes.
if ( ! attr.get( 'value' ) || attr.get( 'value' ).length < 1 ) {
// Skip empty attributes
if ( ( ! attr.get( 'value' ) || attr.get( 'value' ).length < 1 ) && ( attr.attributes.type != 'checkbox' ) ) {
return;
} else if ( ( ! attr.get( 'value' ) || attr.get( 'value' ).length < 1 ) && ( attr.attributes.type == 'checkbox' ) ) {
attr.set( 'value', 'false' );
}

attrs.push( attr.get( 'attr' ) + '="' + attr.get( 'value' ) + '"' );
Expand Down
17 changes: 13 additions & 4 deletions js-tests/src/shortcodeModelSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ describe( "Shortcode Model", function() {
type: 'text',
value: 'test value',
placeholder: 'test placeholder',
}, {
attr: 'checkbox',
label: 'Attribute',
type: 'checkbox',
value: 'true',
}
],
inner_content: {
Expand All @@ -38,8 +43,9 @@ describe( "Shortcode Model", function() {

it( 'Attribute data set correctly..', function() {
expect( shortcode.get( 'attrs' ) instanceof ShortcodeAttributes ).toEqual( true );
expect( shortcode.get( 'attrs' ).length ).toEqual( 1 );
expect( shortcode.get( 'attrs' ).length ).toEqual( 2 );
expect( shortcode.get( 'attrs' ).first().get('type') ).toEqual( data.attrs[0].type );
expect( shortcode.get( 'attrs' ).last().get('type') ).toEqual( data.attrs[1].type );
});

it( 'Inner content set correctly..', function() {
Expand All @@ -58,16 +64,19 @@ describe( "Shortcode Model", function() {
var _shortcode = jQuery.extend( true, {}, shortcode );

// Test with attribute and with content.
expect( _shortcode.formatShortcode() ).toEqual( '[test_shortcode attr="test value"]test content[/test_shortcode]' );
expect( _shortcode.formatShortcode() ).toEqual( '[test_shortcode attr="test value" checkbox="true"]test content[/test_shortcode]' );

// Test without content.
_shortcode.get('inner_content').unset( 'value' );
expect( _shortcode.formatShortcode() ).toEqual( '[test_shortcode attr="test value"]' );
expect( _shortcode.formatShortcode() ).toEqual( '[test_shortcode attr="test value" checkbox="true"]' );

// Test without attributes
_shortcode.get( 'attrs' ).first().unset( 'value' );
expect( _shortcode.formatShortcode() ).toEqual( '[test_shortcode]' );
expect( _shortcode.formatShortcode() ).toEqual( '[test_shortcode checkbox="true"]' );

// Test with falsey checkbox
_shortcode.get( 'attrs' ).last().set( 'value', 'false' );
expect( _shortcode.formatShortcode() ).toEqual( '[test_shortcode checkbox="false"]' );
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd expect this to be checkbox=""...

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@danielbachhuber That would evaluate as null, which would trigger the default to override (Which is the behavior we are trying to prevent).

});

});
6 changes: 4 additions & 2 deletions js/build/field-color.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,9 +165,11 @@ Shortcode = Backbone.Model.extend({

this.get( 'attrs' ).each( function( attr ) {

// Skip empty attributes.
if ( ! attr.get( 'value' ) || attr.get( 'value' ).length < 1 ) {
// Skip empty attributes
if ( ( ! attr.get( 'value' ) || attr.get( 'value' ).length < 1 ) && ( attr.attributes.type != 'checkbox' ) ) {
return;
} else if ( ( ! attr.get( 'value' ) || attr.get( 'value' ).length < 1 ) && ( attr.attributes.type == 'checkbox' ) ) {
attr.set( 'value', 'false' );
}

attrs.push( attr.get( 'attr' ) + '="' + attr.get( 'value' ) + '"' );
Expand Down
6 changes: 4 additions & 2 deletions js/build/shortcode-ui.js
Original file line number Diff line number Diff line change
Expand Up @@ -198,9 +198,11 @@ Shortcode = Backbone.Model.extend({

this.get( 'attrs' ).each( function( attr ) {

// Skip empty attributes.
if ( ! attr.get( 'value' ) || attr.get( 'value' ).length < 1 ) {
// Skip empty attributes
if ( ( ! attr.get( 'value' ) || attr.get( 'value' ).length < 1 ) && ( attr.attributes.type != 'checkbox' ) ) {
return;
} else if ( ( ! attr.get( 'value' ) || attr.get( 'value' ).length < 1 ) && ( attr.attributes.type == 'checkbox' ) ) {
attr.set( 'value', 'false' );
}

attrs.push( attr.get( 'attr' ) + '="' + attr.get( 'value' ) + '"' );
Expand Down
6 changes: 4 additions & 2 deletions js/src/models/shortcode.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,11 @@ Shortcode = Backbone.Model.extend({

this.get( 'attrs' ).each( function( attr ) {

// Skip empty attributes.
if ( ! attr.get( 'value' ) || attr.get( 'value' ).length < 1 ) {
// Skip empty attributes
if ( ( ! attr.get( 'value' ) || attr.get( 'value' ).length < 1 ) && ( attr.attributes.type != 'checkbox' ) ) {
return;
} else if ( ( ! attr.get( 'value' ) || attr.get( 'value' ).length < 1 ) && ( attr.attributes.type == 'checkbox' ) ) {
attr.set( 'value', 'false' );
}

attrs.push( attr.get( 'attr' ) + '="' + attr.get( 'value' ) + '"' );
Expand Down