Permalink
Browse files

Final fixes for static asset serving, passing tests

  • Loading branch information...
1 parent a504623 commit af657e919a12ca82ed6225888ae972ce837eb8b0 @mde mde committed Jan 27, 2014
Showing with 76 additions and 54 deletions.
  1. +7 −2 lib/base_config.js
  2. +49 −33 lib/template/helpers/index.js
  3. +4 −3 lib/template/helpers/utils.js
  4. +16 −16 test/templates/helpers/helpers.js
View
@@ -50,8 +50,13 @@ config = {
, heartbeatWindow: 20000
// Place to look for static content to serve in dev-mode
, staticFilePath: path.normalize(cwd + '/public')
-// Name of the directory to prepend to static file URLs
-, staticSourceDir: ''
+// Name of host to use for serving static assets -- default to
+// empty string, to allow stuff to be served from '/js', '/css', etc.
+, assetHost: ''
+// Prefix for js/css/img directories for serving static assets
+// Relative paths in helpers (scriptLink, styleLink, imageTag)
+// will use this prefix + plus asset type
+, assetBasePath: '/'
// static file cache control
, cacheControl: {
// should contain mime-types as keys and expiration time in seconds
@@ -19,7 +19,31 @@ var utils = require('utilities')
, path = require('path')
, helperUtils = require('./utils')
, flashUtils = require('./flash')
- , Data;
+ , Data
+ // FIXME: pass this config in
+ , geddyConfig = (typeof geddy != 'undefined' && geddy.config) || {}
+ , _getAssetPath;
+
+_getAssetPath = function (assetType, src) {
+ var host
+ , basePath
+ , hasProto = /^http:\/\/|^https:\/\/|^data:/
+ , isAbs;
+
+ // Does not handle exotic protocols like SPDY, or file:// URLs
+ isAbs = hasProto.test(src) || utils.file.isAbsolute(src);
+ if (isAbs) {
+ return src;
+ }
+
+ host = geddyConfig.assetHost || ''
+ basePath = geddyConfig.assetBasePath || '/';
+
+ // Include poss. extra leading slash in path.join to ensure
+ // there's at least one
+ return host + path.join('/', basePath, assetType, src);
+};
+
/**
@name helpers
@@ -73,21 +97,6 @@ exports.urlFor = {
}
/**
- @name helpers#sourcePath
- @public
- @function
- @return {String} A full path to a static file
- @description Returns a url including the source directory specified in config
- @param {String} source The base URL path
-*/
-exports.sourcePath = {
- name: 'sourcePath',
- action: function(source) {
- return path.normalize(path.join('/', geddy.config.staticSourceDir, source));
- }
-}
-
-/**
@name helpers#contentTag
@public
@function
@@ -175,10 +184,11 @@ exports.linkTo = {
exports.scriptLink = {
name: 'scriptLink',
action: function (source, htmlOptions) {
- htmlOptions = htmlOptions || {};
- htmlOptions = utils.mixin({ src: exports.sourcePath.action(source) }, htmlOptions)
+ var opts = htmlOptions || {}
+ , src = _getAssetPath('js', source);
- return exports.contentTag.action('script', '', htmlOptions);
+ utils.mixin(opts, {src: src});
+ return exports.contentTag.action('script', '', opts);
}
};
@@ -194,9 +204,10 @@ exports.scriptLink = {
exports.styleLink = {
name: 'styleLink',
action: function (source, htmlOptions) {
- htmlOptions = htmlOptions || {};
+ var opts = htmlOptions || {}
+ , src = _getAssetPath('css', source);
- return exports.contentTag.action('link', exports.sourcePath.action(source), htmlOptions);
+ return exports.contentTag.action('link', src, opts);
}
};
@@ -216,29 +227,34 @@ exports.styleLink = {
exports.imageTag = {
name: 'imageTag',
action: function (source, htmlOptions) {
- htmlOptions = htmlOptions || {};
+ var opts = htmlOptions || {}
+ , src = _getAssetPath('img', source);
// If size option is included
- if('size' in htmlOptions) {
- var size = htmlOptions.size
+ if('size' in opts) {
+ var size = opts.size
, pat = /([0-9]+x[0-9]*|[0-9]+)/;
if (size.match(pat)) {
- delete htmlOptions.size;
+ delete opts.size;
if (size.match(/[0-9]+x[0-9]*/)) {
- // It's seperate width and height
- htmlOptions.width = htmlOptions.width || size.replace(/x[0-9]*/, '');
- htmlOptions.height = htmlOptions.height || size.replace(/[0-9]+x/, '');
- } else {
+ // It's separate width and height
+ opts.width = opts.width || size.replace(/x[0-9]*/, '');
+ opts.height = opts.height || size.replace(/[0-9]+x/, '');
+ }
+ else {
// Same size width and height
- htmlOptions.width = htmlOptions.width || size;
- htmlOptions.height = htmlOptions.height || size;
+ opts.width = opts.width || size;
+ opts.height = opts.height || size;
}
- } else delete htmlOptions.size;
+ }
+ else {
+ delete opts.size;
+ }
}
- return exports.contentTag.action('img', exports.sourcePath.action(source), htmlOptions);
+ return exports.contentTag.action('img', src, opts);
}
}
@@ -1,4 +1,5 @@
var utils = require('utilities')
+ , geddyConfig = (typeof geddy != 'undefined' && geddy.config) || {}
// Include empty params so that conditionals will work without registerData being called
, Data = {params: {}};
@@ -238,7 +239,7 @@ exports.urls = {
var protocol = options.protocol;
// Set default protocol if none is given
- if (geddy.config.spdy || geddy.config.ssl) {
+ if (geddyConfig.spdy || geddyConfig.ssl) {
protocol = protocol || 'https';
} else {
protocol = protocol || 'http';
@@ -274,8 +275,8 @@ exports.urls = {
var domain = options.domain
, hostname = '';
- if (geddy.config.hostname) {
- domain = domain || geddy.config.hostname;
+ if (geddyConfig.hostname) {
+ domain = domain || geddyConfig.hostname;
}
if (options.subdomain) {
@@ -207,43 +207,43 @@ tests = {
}
, 'test standard for imageLink': function () {
- var string = helpers.imageLink('images/google.png', 'http://google.com');
- assert.equal(string, '<a href="http://google.com"><img alt="images/google.png" src="images/google.png" /></a>');
+ var string = helpers.imageLink('google.png', 'http://google.com');
+ assert.equal(string, '<a href="http://google.com"><img alt="/img/google.png" src="/img/google.png" /></a>');
}
, 'test custom alt text for image in imageLink': function () {
- var string = helpers.imageLink('images/google.png', 'http://google.com', { alt: '' });
- assert.equal(string, '<a href="http://google.com"><img alt="" src="images/google.png" /></a>');
+ var string = helpers.imageLink('google.png', 'http://google.com', { alt: 'zerp zerp' });
+ assert.equal(string, '<a href="http://google.com"><img alt="zerp zerp" src="/img/google.png" /></a>');
}
, 'test custom alt text for image and using custom size option in imageLink': function () {
- var string = helpers.imageLink('images/google.png', 'http://google.com', { alt: '', size: '40x50' });
- assert.equal(string, '<a href="http://google.com"><img alt="" height="50" src="images/google.png" width="40" /></a>');
+ var string = helpers.imageLink('google.png', 'http://google.com', { alt: '', size: '40x50' });
+ assert.equal(string, '<a href="http://google.com"><img alt="" height="50" src="/img/google.png" width="40" /></a>');
}
, 'test custom alt text for image and data object for link in imageLink': function () {
- var string = helpers.imageLink('images/google.png', 'http://google.com', { alt: '' }, { data: {goTo: 'http://google.com'} });
- assert.equal(string, '<a data-go-to="http://google.com" href="http://google.com"><img alt="" src="images/google.png" /></a>');
+ var string = helpers.imageLink('google.png', 'http://google.com', { alt: '' }, { data: {goTo: 'http://google.com'} });
+ assert.equal(string, '<a data-go-to="http://google.com" href="http://google.com"><img alt="" src="/img/google.png" /></a>');
}
, 'test standard for imageTag': function () {
- var string = helpers.imageTag('images/google.png');
- assert.equal(string, '<img alt="images/google.png" src="images/google.png" />');
+ var string = helpers.imageTag('google.png');
+ assert.equal(string, '<img alt="/img/google.png" src="/img/google.png" />');
}
, 'test custom alt text for image in imageTag': function () {
- var string = helpers.imageTag('images/google.png', { alt: '' });
- assert.equal(string, '<img alt="" src="images/google.png" />');
+ var string = helpers.imageTag('google.png', { alt: '' });
+ assert.equal(string, '<img alt="" src="/img/google.png" />');
}
, 'test custom size attribute for image in imageTag': function () {
- var string = helpers.imageTag('images/google.png', { size: '40x50' });
- assert.equal(string, '<img alt="images/google.png" height="50" src="images/google.png" width="40" />');
+ var string = helpers.imageTag('google.png', { size: '40x50' });
+ assert.equal(string, '<img alt="/img/google.png" height="50" src="/img/google.png" width="40" />');
}
, 'test malformed size attribute for image in imageTag': function () {
- var string = helpers.imageTag('images/google.png', { size: 'a string' });
- assert.equal(string, '<img alt="images/google.png" src="images/google.png" />');
+ var string = helpers.imageTag('google.png', { size: 'a string' });
+ assert.equal(string, '<img alt="/img/google.png" src="/img/google.png" />');
}
, 'test standard in linkTo': function () {

0 comments on commit af657e9

Please sign in to comment.