Skip to content

Commit

Permalink
Merge pull request #531 from wp-shortcake/520-persist-attributes-content
Browse files Browse the repository at this point in the history
Always persist unregistered shortcode attributes and inner content
  • Loading branch information
goldenapples committed Oct 29, 2015
2 parents 065d6dd + d9a8bb1 commit 8a23c12
Show file tree
Hide file tree
Showing 4 changed files with 171 additions and 174 deletions.
128 changes: 70 additions & 58 deletions js-tests/build/specs.js
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,23 @@ describe( 'Shortcode View Constructor', function(){
var shortcode = ShortcodeViewConstructor.parseShortcodeString( '[no_inner_content foo="bar"]burrito[/no_inner_content]' );
var _shortcode = $.extend( true, {}, shortcode );
expect( _shortcode.formatShortcode() ).toEqual( '[no_inner_content foo="bar"]burrito[/no_inner_content]' );
ShortcodeViewConstructor.shortcode = {
'type' : 'single',
'tag' : 'no_inner_content',
'attrs' : {
'named' : {
'foo' : 'bar',
},
'numeric' : [],
},
'content' : 'burrito'
};
var ShortcodeViewConstructorWithoutFetch = ShortcodeViewConstructor;
ShortcodeViewConstructorWithoutFetch.delayedFetch = function() {
return new $.Deferred();
};
ShortcodeViewConstructor.initialize();
expect( ShortcodeViewConstructor.shortcodeModel.formatShortcode() ).toEqual( '[no_inner_content foo="bar"]burrito[/no_inner_content]' );
});

it( 'Persists custom attribute when parsing a shortcode without the attribute defined in UI', function() {
Expand All @@ -198,13 +215,34 @@ describe( 'Shortcode View Constructor', function(){
var shortcode = ShortcodeViewConstructor.parseShortcodeString( '[no_custom_attribute foo="bar" bar="banana"]' );
var _shortcode = $.extend( true, {}, shortcode );
expect( _shortcode.formatShortcode() ).toEqual( '[no_custom_attribute foo="bar" bar="banana"]' );
ShortcodeViewConstructor.shortcode = {
'type' : 'single',
'tag' : 'no_custom_attribute',
'attrs' : {
'named' : {
'foo' : 'bar',
'bar' : 'banana',
},
'numeric' : [],
},
};
var ShortcodeViewConstructorWithoutFetch = ShortcodeViewConstructor;
ShortcodeViewConstructorWithoutFetch.delayedFetch = function() {
return new $.Deferred();
};
ShortcodeViewConstructor.initialize();
expect( ShortcodeViewConstructor.shortcodeModel.formatShortcode() ).toEqual( '[no_custom_attribute foo="bar" bar="banana"]' );
});

it( 'Reverses the effect of core adding wpautop to shortcode inner content', function(){
var shortcode = {
tag: 'pullquote',
content: 'This quote has</p>\n<p>Multiple line breaks two</p>\n<p>Test one',
type: 'closed',
attrs: {
named: {},
numeric: [],
}
};
var data = {
label: 'Pullquote',
Expand All @@ -225,6 +263,7 @@ describe( 'Shortcode View Constructor', function(){
},
},
type: 'single',
content: null,
};
var data = {
label: 'Pullquote',
Expand Down Expand Up @@ -821,50 +860,58 @@ var shortcodeViewConstructor = {
* values.
*
* @this {Shortcode}
* @param {Object} options Options
* @param {Object} options Options formatted as wp.shortcode.
*/
getShortcodeModel: function( options ) {
var shortcodeModel,
self = this;
var shortcodeModel;

shortcodeModel = sui.shortcodes.findWhere( { shortcode_tag: options.tag } );

if ( ! shortcodeModel ) {
return;
}

shortcodeModel = shortcodeModel.clone();
currentShortcode = shortcodeModel.clone();

shortcodeModel.get('attrs').each( function( attr ) {
var attributes_backup = {};
var attributes = options.attrs;
for ( var key in attributes.named ) {

// Verify value exists for attribute.
if ( ! ( attr.get('attr') in options.attrs.named ) ) {
return;
if ( ! attributes.named.hasOwnProperty( key ) ) {
continue;
}

var value = options.attrs.named[ attr.get('attr') ];
value = attributes.named[ key ];
attr = currentShortcode.get( 'attrs' ).findWhere({ attr: key });

// Reverse the effects of wpautop: https://core.trac.wordpress.org/ticket/34329
value = self.unAutoP( value );
value = this.unAutoP( value );

// Maybe decode value.
if ( attr.get('encode') ) {
if ( attr && attr.get('encode') ) {
value = decodeURIComponent( value );
}

attr.set( 'value', value );
} );
if ( attr ) {
attr.set( 'value', value );
} else {
attributes_backup[ key ] = value;
}
}

currentShortcode.set( 'attributes_backup', attributes_backup );

if ( 'content' in options ) {
var innerContent = shortcodeModel.get('inner_content');
if ( innerContent ) {
// Reverse the effects of wpautop: https://core.trac.wordpress.org/ticket/34329
options.content = self.unAutoP( options.content );
innerContent.set('value', options.content );
if ( options.content ) {
var inner_content = currentShortcode.get( 'inner_content' );
// Reverse the effects of wpautop: https://core.trac.wordpress.org/ticket/34329
options.content = this.unAutoP( options.content );
if ( inner_content ) {
inner_content.set( 'value', options.content );
} else {
currentShortcode.set( 'inner_content_backup', options.content );
}
}

return shortcodeModel;
return currentShortcode;
},

/**
Expand Down Expand Up @@ -971,43 +1018,8 @@ var shortcodeViewConstructor = {
return;
}

currentShortcode = defaultShortcode.clone();

var attributes_backup = {};
var attributes = wp.shortcode.attrs( matches[3] );

for ( var key in attributes.named ) {

if ( ! attributes.named.hasOwnProperty( key ) ) {
continue;
}

value = attributes.named[ key ];
attr = currentShortcode.get( 'attrs' ).findWhere({ attr: key });

if ( attr && attr.get('encode') ) {
value = decodeURIComponent( value );
}

if ( attr ) {
attr.set( 'value', value );
} else {
attributes_backup[ key ] = value;
}
}

currentShortcode.set( 'attributes_backup', attributes_backup );

if ( matches[5] ) {
var inner_content = currentShortcode.get( 'inner_content' );
if ( inner_content ) {
inner_content.set( 'value', this.unAutoP( matches[5] ) );
} else {
currentShortcode.set( 'inner_content_backup', this.unAutoP( matches[5] ) );
}
}

return currentShortcode;
var shortcode = wp.shortcode.fromMatch( matches );
return this.getShortcodeModel( shortcode );
},

/**
Expand Down
39 changes: 39 additions & 0 deletions js-tests/src/shortcodeViewConstructorSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,23 @@ describe( 'Shortcode View Constructor', function(){
var shortcode = ShortcodeViewConstructor.parseShortcodeString( '[no_inner_content foo="bar"]burrito[/no_inner_content]' );
var _shortcode = $.extend( true, {}, shortcode );
expect( _shortcode.formatShortcode() ).toEqual( '[no_inner_content foo="bar"]burrito[/no_inner_content]' );
ShortcodeViewConstructor.shortcode = {
'type' : 'single',
'tag' : 'no_inner_content',
'attrs' : {
'named' : {
'foo' : 'bar',
},
'numeric' : [],
},
'content' : 'burrito'
};
var ShortcodeViewConstructorWithoutFetch = ShortcodeViewConstructor;
ShortcodeViewConstructorWithoutFetch.delayedFetch = function() {
return new $.Deferred();
};
ShortcodeViewConstructor.initialize();
expect( ShortcodeViewConstructor.shortcodeModel.formatShortcode() ).toEqual( '[no_inner_content foo="bar"]burrito[/no_inner_content]' );
});

it( 'Persists custom attribute when parsing a shortcode without the attribute defined in UI', function() {
Expand All @@ -41,13 +58,34 @@ describe( 'Shortcode View Constructor', function(){
var shortcode = ShortcodeViewConstructor.parseShortcodeString( '[no_custom_attribute foo="bar" bar="banana"]' );
var _shortcode = $.extend( true, {}, shortcode );
expect( _shortcode.formatShortcode() ).toEqual( '[no_custom_attribute foo="bar" bar="banana"]' );
ShortcodeViewConstructor.shortcode = {
'type' : 'single',
'tag' : 'no_custom_attribute',
'attrs' : {
'named' : {
'foo' : 'bar',
'bar' : 'banana',
},
'numeric' : [],
},
};
var ShortcodeViewConstructorWithoutFetch = ShortcodeViewConstructor;
ShortcodeViewConstructorWithoutFetch.delayedFetch = function() {
return new $.Deferred();
};
ShortcodeViewConstructor.initialize();
expect( ShortcodeViewConstructor.shortcodeModel.formatShortcode() ).toEqual( '[no_custom_attribute foo="bar" bar="banana"]' );
});

it( 'Reverses the effect of core adding wpautop to shortcode inner content', function(){
var shortcode = {
tag: 'pullquote',
content: 'This quote has</p>\n<p>Multiple line breaks two</p>\n<p>Test one',
type: 'closed',
attrs: {
named: {},
numeric: [],
}
};
var data = {
label: 'Pullquote',
Expand All @@ -68,6 +106,7 @@ describe( 'Shortcode View Constructor', function(){
},
},
type: 'single',
content: null,
};
var data = {
label: 'Pullquote',
Expand Down
89 changes: 31 additions & 58 deletions js/build/shortcode-ui.js
Original file line number Diff line number Diff line change
Expand Up @@ -454,50 +454,58 @@ var shortcodeViewConstructor = {
* values.
*
* @this {Shortcode}
* @param {Object} options Options
* @param {Object} options Options formatted as wp.shortcode.
*/
getShortcodeModel: function( options ) {
var shortcodeModel,
self = this;
var shortcodeModel;

shortcodeModel = sui.shortcodes.findWhere( { shortcode_tag: options.tag } );

if ( ! shortcodeModel ) {
return;
}

shortcodeModel = shortcodeModel.clone();
currentShortcode = shortcodeModel.clone();

shortcodeModel.get('attrs').each( function( attr ) {
var attributes_backup = {};
var attributes = options.attrs;
for ( var key in attributes.named ) {

// Verify value exists for attribute.
if ( ! ( attr.get('attr') in options.attrs.named ) ) {
return;
if ( ! attributes.named.hasOwnProperty( key ) ) {
continue;
}

var value = options.attrs.named[ attr.get('attr') ];
value = attributes.named[ key ];
attr = currentShortcode.get( 'attrs' ).findWhere({ attr: key });

// Reverse the effects of wpautop: https://core.trac.wordpress.org/ticket/34329
value = self.unAutoP( value );
value = this.unAutoP( value );

// Maybe decode value.
if ( attr.get('encode') ) {
if ( attr && attr.get('encode') ) {
value = decodeURIComponent( value );
}

attr.set( 'value', value );
} );
if ( attr ) {
attr.set( 'value', value );
} else {
attributes_backup[ key ] = value;
}
}

currentShortcode.set( 'attributes_backup', attributes_backup );

if ( 'content' in options ) {
var innerContent = shortcodeModel.get('inner_content');
if ( innerContent ) {
// Reverse the effects of wpautop: https://core.trac.wordpress.org/ticket/34329
options.content = self.unAutoP( options.content );
innerContent.set('value', options.content );
if ( options.content ) {
var inner_content = currentShortcode.get( 'inner_content' );
// Reverse the effects of wpautop: https://core.trac.wordpress.org/ticket/34329
options.content = this.unAutoP( options.content );
if ( inner_content ) {
inner_content.set( 'value', options.content );
} else {
currentShortcode.set( 'inner_content_backup', options.content );
}
}

return shortcodeModel;
return currentShortcode;
},

/**
Expand Down Expand Up @@ -604,43 +612,8 @@ var shortcodeViewConstructor = {
return;
}

currentShortcode = defaultShortcode.clone();

var attributes_backup = {};
var attributes = wp.shortcode.attrs( matches[3] );

for ( var key in attributes.named ) {

if ( ! attributes.named.hasOwnProperty( key ) ) {
continue;
}

value = attributes.named[ key ];
attr = currentShortcode.get( 'attrs' ).findWhere({ attr: key });

if ( attr && attr.get('encode') ) {
value = decodeURIComponent( value );
}

if ( attr ) {
attr.set( 'value', value );
} else {
attributes_backup[ key ] = value;
}
}

currentShortcode.set( 'attributes_backup', attributes_backup );

if ( matches[5] ) {
var inner_content = currentShortcode.get( 'inner_content' );
if ( inner_content ) {
inner_content.set( 'value', this.unAutoP( matches[5] ) );
} else {
currentShortcode.set( 'inner_content_backup', this.unAutoP( matches[5] ) );
}
}

return currentShortcode;
var shortcode = wp.shortcode.fromMatch( matches );
return this.getShortcodeModel( shortcode );
},

/**
Expand Down
Loading

0 comments on commit 8a23c12

Please sign in to comment.