Skip to content

Commit

Permalink
Added a couple tests for HTML tags, and fixed conditionals so they have
Browse files Browse the repository at this point in the history
brackets to please @mde (;
  • Loading branch information
larzconwell committed Jul 23, 2012
1 parent daae6e9 commit e570502
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 36 deletions.
4 changes: 1 addition & 3 deletions lib/template/helpers/action.js
Expand Up @@ -35,9 +35,7 @@ exports.create = function(options) {
if(!utils.array.included(options.action, ['index', 'add', 'show', 'edit'])) return;

// Default action function
action = function() {
return helpersBase.urlFor.action(options);
};
action = helpersBase.urlFor.action(options);

// If action requires Id then change the default action
if(utils.array.included(options.action, idActions)) {
Expand Down
108 changes: 75 additions & 33 deletions lib/template/helpers/utils.js
@@ -1,6 +1,8 @@
var utils = require('../../utils')
, Data;

// Register Object for use with `exports.urls`, needs
// a `data.params` to be of any use
exports.registerData = function(data) { Data = data; };

/*
Expand All @@ -20,7 +22,6 @@ exports.tags = {
'link': {name: 'link', content: 'href'},
'meta': {name: 'meta', content: 'content'},
'img': {name: 'img', content: 'src'},
// Could be `placeholder` but not all browser support it
'input': {name: 'input', content: 'value'},
'area': {name: 'area', content: 'href'},
'base': {name: 'link', content: 'href'},
Expand All @@ -40,16 +41,26 @@ exports.tags = {
* html attributes given
*/
contentTagString: function(tag, content, htmlOptions) {
if(!tag) return;
if(!content) content = '';
if(!tag) {
return;
}
if(!content) {
content = '';
}
htmlOptions = htmlOptions || {};

var selfClosing = tag in this.selfClosingTags ? this.selfClosingTags[tag] : undefined
var selfClosing = (tag in this.selfClosingTags) ? this.selfClosingTags[tag] : undefined
, tagOptions;

if(selfClosing) htmlOptions[selfClosing.content] = htmlOptions[selfClosing.content] || content || false;
if(tag === 'a') htmlOptions.href = htmlOptions.href || content;
if(tag === 'img') htmlOptions.alt = htmlOptions.alt === '' ? htmlOptions.alt : htmlOptions.alt || content;
if(selfClosing) {
htmlOptions[selfClosing.content] = htmlOptions[selfClosing.content] || content || false;
}
if(tag === 'a') {
htmlOptions.href = htmlOptions.href || content;
}
if(tag === 'img') {
htmlOptions.alt = htmlOptions.alt === '' ? htmlOptions.alt : htmlOptions.alt || content;
}
tagOptions = this.tagOptions(htmlOptions);

content = this.preContentStrings[tag] ?
Expand All @@ -69,7 +80,9 @@ exports.tags = {
* Returns a string of parsed HTML options sorted alphabetically
*/
tagOptions: function(options) {
if(!options) return;
if(!options) {
return;
}

var self = this
, attrs = []
Expand All @@ -93,7 +106,9 @@ exports.tags = {
// If the attribute is a boolean attribute
// parse it as a bool attribute
else if(utils.array.included(key, self.boolAttributes)) {
if(value) attrs.push(self.boolAttribute(key));
if(value) {
attrs.push(self.boolAttribute(key));
}
}
// Just a normal attribute, parse it normally
else if(value || value === '') {
Expand All @@ -103,11 +118,13 @@ exports.tags = {

if(attrs.length > 0) {
return ' ' + attrs.sort().join(' ');
} else return '';
} else {
return '';
}
},

/*
* dataAttribute(attribute<String>, value<String/Boolean>)
* dataAttribute(attribute<String>, value<String/Number/Array/Boolean>)
*
* Returns a parsed HTML data attribute
*/
Expand All @@ -127,13 +144,15 @@ exports.tags = {
},

/*
* tagAttribute(attribute<String>)
* tagAttribute(attribute<String>, value<String/Number/Array/Boolean>)
*
* Returns a parsed HTML attribute
* Returns a parsed HTML attribute
*/
tagAttribute: function(attribute, value) {
// Only allow Strings to be an object
if(typeof value === 'object') value = value.join(' ');
// If value is an Array join them with a space
if(typeof value === 'object' && value instanceof Array) {
value = value.join(' ');
}

return attribute + '="' + value + '"';
}
Expand Down Expand Up @@ -172,7 +191,9 @@ exports.urls = {
// Set default protocol if none is given
if(geddy.config.ssl) {
protocol = protocol || 'https';
} else protocol = protocol || 'http';
} else {
protocol = protocol || 'http';
}

// Remove slashes, etc.
protocol = protocol.replace(/:\/*/, '');
Expand All @@ -182,7 +203,9 @@ exports.urls = {
if(utils.array.included(protocol.replace(/:\/*/, ''), this.slashedProtocols)) {
if(protocol.replace(/:\/*/, '') === 'file') {
protocol += '///';
} else protocol += '//';
} else {
protocol += '//';
}
}

return protocol;
Expand All @@ -192,23 +215,29 @@ exports.urls = {
// Returns a String containing the user and password in the format 'user:password@'
if(options.username && options.password) {
return this.escape(options.username) + ':' + this.escape(options.password) + '@';
} else return '';
} else {
return '';
}
},

genHost: function(options) {
options = options || {};
var domain = options.domain
, hostname = '';

if(geddy.config.hostname) domain = domain || geddy.config.hostname;
if(geddy.config.hostname) {
domain = domain || geddy.config.hostname;
}

if(options.subdomain) {
hostname += options.subdomain + '.';
}
hostname += domain;

// Add port if one is given
if(options.port) hostname += ':' + options.port;
if(options.port) {
hostname += ':' + options.port;
}

// Add slash if nothing else is being added to the url
if(options.trailingSlash && !options.controller && !options.id && !options.action) {
Expand All @@ -228,7 +257,9 @@ exports.urls = {

path += '/' + options.controller;

if(!options.action && !options.id) options.action = 'index';
if(!options.action && !options.id) {
options.action = 'index';
}
if(options.action === 'index') {
if(options.trailingSlash) path += '/';
return path;
Expand All @@ -237,15 +268,16 @@ exports.urls = {
if(options.id) {
// If no controller is given attempt to get the current one
if(!options.controller && Data.params.controller) {
var controller = Data.params.controller[0].toLowerCase() +
Data.params.controller.slice(1, Data.params.controller.length);
var controller = utils.string.decapitalize(Data.params.controller);

options.controller = controller;
path += '/' + controller;
}
path += '/' + options.id;

if(!options.action) options.action = 'show';
if(!options.action) {
options.action = 'show';
}
if(options.action === 'show') {
if(options.trailingSlash) path += '/';
return path;
Expand All @@ -256,19 +288,19 @@ exports.urls = {

// If no controller is given attempt to get the current one
if(!options.controller && Data.params.controller) {
var controller = Data.params.controller[0].toLowerCase() +
Data.params.controller.slice(1, Data.params.controller.length);
var controller = utils.string.decapitalize(Data.params.controller);;

options.controller = controller;
path += controller + '/';
}

if(options.action === 'show') {
if(options.trailingSlash) path += '/';
return path;
return options.trailingSlash ? path += '/' : path;
} else {
path += options.action;
if(options.trailingSlash) path += '/';
if(options.trailingSlash) {
path += '/';
}
}
}

Expand All @@ -292,7 +324,9 @@ exports.urls = {
query += i == 0 ? '?' : '&';
query += this.escape(options[i].key) + '=' + this.escape(options[i].value);
}
} else return query;
} else {
return query;
}

return query;
},
Expand All @@ -301,7 +335,9 @@ exports.urls = {
options = options || {};
var fragment = '';

if(options.fragment) fragment += '#' + this.escape(options.fragment);
if(options.fragment) {
fragment += '#' + this.escape(options.fragment);
}

return fragment;
},
Expand All @@ -311,9 +347,15 @@ exports.urls = {

// Set up alias's
options.domain = options.domain || options.host;
if(options.host) delete options.host; // Delete it as it's not used
if(options.host) {
// Delete it as it's not used
delete options.host;
}
options.fragment = options.fragment || options.anchor;
if(options.anchor) delete options.anchor; // Delete it as it's not used
if(options.anchor) {
// Delete it as it's not used
delete options.anchor;
}

// If `host` option exists set `relPath` as false
options = utils.object.merge({ relPath: !options.domain }, options);
Expand Down
10 changes: 10 additions & 0 deletions test/helpers.js
Expand Up @@ -66,6 +66,16 @@ tests = {
assert.equal(string, '<a data-go-to="http://google.com" href="http://google.com">http://google.com</a>');
}

, 'test array for data in contentTag': function() {
var string = helpers.contentTag('a', 'http://google.com', { omg: ['odd input'] });
assert.equal(string, '<a href="http://google.com" omg="odd input">http://google.com</a>');
}

, 'test array with multiple items for data in contentTag': function() {
var string = helpers.contentTag('a', 'http://google.com', { omg: ['odd input', 'this is weird'] });
assert.equal(string, '<a href="http://google.com" omg="odd input this is weird">http://google.com</a>');
}

, 'test normal data attributes in contentTag': function() {
var string = helpers.contentTag('a', 'http://google.com', { dataGoTo: 'http://google.com' });
assert.equal(string, '<a data-go-to="http://google.com" href="http://google.com">http://google.com</a>');
Expand Down

0 comments on commit e570502

Please sign in to comment.