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

Unnamed/numeric attribute fix (revised) #198

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
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' ) ) ) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Lots of double negatives here... We could check $.isNumeric( attr.get( 'attr' ) ) instead?

attrs.push( '"' + attr.get( 'value' ) + '"' );
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we need to wrap in quotes?


// 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