Skip to content

Commit

Permalink
Numeric attribute fix (revised)
Browse files Browse the repository at this point in the history
Since the code has changed a lot since wp-shortcake#180, I’m redoing the changes
with the new file structure
  • Loading branch information
bfintal committed Feb 25, 2015
1 parent 85919fa commit 2b76387
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 24 deletions.
57 changes: 45 additions & 12 deletions js/build/shortcode-ui.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,15 @@ var Shortcode = Backbone.Model.extend({
if ( attr.get( 'attr' ) === 'content' ) {
content = attr.get( 'value' );
} else {
attrs.push( attr.get( 'attr' ) + '="' + attr.get( 'value' ) + '"' );

// Numeric attribute names
if ( ! isNaN( attr.get( 'attr' ) ) ) {
attrs.push( '"' + attr.get( 'value' ) + '"' );

// String attribute names
} else {
attrs.push( attr.get( 'attr' ) + '="' + attr.get( 'value' ) + '"' );
}
}

} );
Expand Down Expand Up @@ -356,6 +364,13 @@ var shortcodeViewConstructor = {
options.shortcode.attrs.named[ attr.get( 'attr') ]
);
}

if ( attr.get( 'attr') in options.shortcode.attrs.numeric ) {
attr.set(
'value',
options.shortcode.attrs.numeric[ attr.get( 'attr') ]
);
}

if ( attr.get( 'attr' ) === 'content' && ( 'content' in options.shortcode ) ) {
attr.set( 'value', options.shortcode.content );
Expand Down Expand Up @@ -430,19 +445,37 @@ var shortcodeViewConstructor = {

if (matches[2]) {

attributeMatches = matches[2].match(/(\S+?=".*?")/g) || [];
// Get all the attributes
attributeMatches = matches[2].match(/([^\s]+)/g) || [];

// convert attribute strings to object.
for (var i = 0; i < attributeMatches.length; i++) {
// Keep track of all the unnamed attributes
var unnamedIndex = 0;

var bitsRegEx = /(\S+?)="(.*?)"/g;
var bits = bitsRegEx.exec(attributeMatches[i]);

attr = currentShortcode.get('attrs').findWhere({
attr : bits[1]
});
if (attr) {
attr.set('value', bits[2]);
// convert attribute strings to object.
for (var i = 0; i < attributeMatches.length; i++) {

// Handler for named attributes
if (attributeMatches[i].match(/\S+?="(.*?)"/) !== null ) {

var bitsRegEx = /(\S+?)="(.*?)"/g;
var bits = bitsRegEx.exec(attributeMatches[i]);

attr = currentShortcode.get('attrs').findWhere({
attr : bits[1]
});
if (attr) {
attr.set('value', bits[2]);
}

// Handler for numeric/unnamed attributes
} else {

attr = currentShortcode.get('attrs').findWhere({
attr : (unnamedIndex++).toString()
});
if (attr) {
attr.set('value', attributeMatches[i].replace(/^"(.*)"$/, '$1'));
}
}

}
Expand Down
10 changes: 9 additions & 1 deletion js/models/shortcode.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,15 @@ var Shortcode = Backbone.Model.extend({
if ( attr.get( 'attr' ) === 'content' ) {
content = attr.get( 'value' );
} else {
attrs.push( attr.get( 'attr' ) + '="' + attr.get( 'value' ) + '"' );

// Numeric attribute names
if ( ! isNaN( attr.get( 'attr' ) ) ) {
attrs.push( '"' + attr.get( 'value' ) + '"' );

// String attribute names
} else {
attrs.push( attr.get( 'attr' ) + '="' + attr.get( 'value' ) + '"' );
}
}

} );
Expand Down
47 changes: 36 additions & 11 deletions js/utils/shortcode-view-constructor.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@ var shortcodeViewConstructor = {
options.shortcode.attrs.named[ attr.get( 'attr') ]
);
}

if ( attr.get( 'attr') in options.shortcode.attrs.numeric ) {
attr.set(
'value',
options.shortcode.attrs.numeric[ attr.get( 'attr') ]
);
}

if ( attr.get( 'attr' ) === 'content' && ( 'content' in options.shortcode ) ) {
attr.set( 'value', options.shortcode.content );
Expand Down Expand Up @@ -100,19 +107,37 @@ var shortcodeViewConstructor = {

if (matches[2]) {

attributeMatches = matches[2].match(/(\S+?=".*?")/g) || [];
// Get all the attributes
attributeMatches = matches[2].match(/([^\s]+)/g) || [];

// convert attribute strings to object.
for (var i = 0; i < attributeMatches.length; i++) {

var bitsRegEx = /(\S+?)="(.*?)"/g;
var bits = bitsRegEx.exec(attributeMatches[i]);
// Keep track of all the unnamed attributes
var unnamedIndex = 0;

attr = currentShortcode.get('attrs').findWhere({
attr : bits[1]
});
if (attr) {
attr.set('value', bits[2]);
// convert attribute strings to object.
for (var i = 0; i < attributeMatches.length; i++) {

// Handler for named attributes
if (attributeMatches[i].match(/\S+?="(.*?)"/) !== null ) {

var bitsRegEx = /(\S+?)="(.*?)"/g;
var bits = bitsRegEx.exec(attributeMatches[i]);

attr = currentShortcode.get('attrs').findWhere({
attr : bits[1]
});
if (attr) {
attr.set('value', bits[2]);
}

// Handler for numeric/unnamed attributes
} else {

attr = currentShortcode.get('attrs').findWhere({
attr : (unnamedIndex++).toString()
});
if (attr) {
attr.set('value', attributeMatches[i].replace(/^"(.*)"$/, '$1'));
}
}

}
Expand Down

0 comments on commit 2b76387

Please sign in to comment.