From 03d9f40ab402990a13a55c47b6563644b3fa49a2 Mon Sep 17 00:00:00 2001 From: davisshaver Date: Wed, 5 Aug 2015 15:00:13 -0400 Subject: [PATCH 1/3] Pass false shortcake value for checkboxes --- js-tests/build/specs.js | 4 ++-- js/build/field-color.js | 4 ++-- js/build/shortcode-ui.js | 4 ++-- js/src/models/shortcode.js | 4 ++-- 4 files changed, 8 insertions(+), 8 deletions(-) diff --git a/js-tests/build/specs.js b/js-tests/build/specs.js index 11a40a67..47452ed0 100644 --- a/js-tests/build/specs.js +++ b/js-tests/build/specs.js @@ -449,8 +449,8 @@ 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; } diff --git a/js/build/field-color.js b/js/build/field-color.js index 80a55e95..941454f2 100644 --- a/js/build/field-color.js +++ b/js/build/field-color.js @@ -165,8 +165,8 @@ 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; } diff --git a/js/build/shortcode-ui.js b/js/build/shortcode-ui.js index 7b209b06..8a12998f 100644 --- a/js/build/shortcode-ui.js +++ b/js/build/shortcode-ui.js @@ -198,8 +198,8 @@ 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; } diff --git a/js/src/models/shortcode.js b/js/src/models/shortcode.js index 9a2f4a2b..3d351f70 100644 --- a/js/src/models/shortcode.js +++ b/js/src/models/shortcode.js @@ -66,8 +66,8 @@ 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; } From 6543181b9794d7d00ab87ad107bd9ce2a3af8f2c Mon Sep 17 00:00:00 2001 From: davisshaver Date: Fri, 7 Aug 2015 11:07:43 -0400 Subject: [PATCH 2/3] Demonstrate falsey checkbox value is passed --- js-tests/build/specs.js | 17 +++++++++++++---- js-tests/src/shortcodeModelSpec.js | 17 +++++++++++++---- 2 files changed, 26 insertions(+), 8 deletions(-) diff --git a/js-tests/build/specs.js b/js-tests/build/specs.js index 47452ed0..c3210549 100644 --- a/js-tests/build/specs.js +++ b/js-tests/build/specs.js @@ -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: { @@ -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() { @@ -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"]' ); }); }); diff --git a/js-tests/src/shortcodeModelSpec.js b/js-tests/src/shortcodeModelSpec.js index 7228d659..8cdf2a92 100644 --- a/js-tests/src/shortcodeModelSpec.js +++ b/js-tests/src/shortcodeModelSpec.js @@ -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: { @@ -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() { @@ -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"]' ); }); }); From a5766f0be851a203c6bfaba4faba762ed0a58a86 Mon Sep 17 00:00:00 2001 From: davisshaver Date: Fri, 7 Aug 2015 11:19:36 -0400 Subject: [PATCH 3/3] Handle '' value as false for the checkbox --- js-tests/build/specs.js | 2 ++ js/build/field-color.js | 2 ++ js/build/shortcode-ui.js | 2 ++ js/src/models/shortcode.js | 2 ++ 4 files changed, 8 insertions(+) diff --git a/js-tests/build/specs.js b/js-tests/build/specs.js index c3210549..e846e8e8 100644 --- a/js-tests/build/specs.js +++ b/js-tests/build/specs.js @@ -461,6 +461,8 @@ Shortcode = Backbone.Model.extend({ // 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' ) + '"' ); diff --git a/js/build/field-color.js b/js/build/field-color.js index 941454f2..7706ed57 100644 --- a/js/build/field-color.js +++ b/js/build/field-color.js @@ -168,6 +168,8 @@ Shortcode = Backbone.Model.extend({ // 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' ) + '"' ); diff --git a/js/build/shortcode-ui.js b/js/build/shortcode-ui.js index 8a12998f..967a17e0 100644 --- a/js/build/shortcode-ui.js +++ b/js/build/shortcode-ui.js @@ -201,6 +201,8 @@ Shortcode = Backbone.Model.extend({ // 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' ) + '"' ); diff --git a/js/src/models/shortcode.js b/js/src/models/shortcode.js index 3d351f70..2936916a 100644 --- a/js/src/models/shortcode.js +++ b/js/src/models/shortcode.js @@ -69,6 +69,8 @@ Shortcode = Backbone.Model.extend({ // 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' ) + '"' );