From 5524d2b0fb9b87ef0d1beec092337f836a1043a8 Mon Sep 17 00:00:00 2001 From: Elliott Sprehn Date: Wed, 27 Oct 2010 19:06:40 -0700 Subject: Check if file exists (not a 404) and that document is accessible and not using file:// URLs in Application --- src/scenario/Application.js | 66 ++++++++++++++++++++++++++++++++++++++------- 1 file changed, 56 insertions(+), 10 deletions(-) (limited to 'src/scenario/Application.js') diff --git a/src/scenario/Application.js b/src/scenario/Application.js index e2d34551..eacf3c7b 100644 --- a/src/scenario/Application.js +++ b/src/scenario/Application.js @@ -37,28 +37,71 @@ angular.scenario.Application.prototype.getWindow_ = function() { return contentWindow; }; +/** + * Checks that a URL would return a 2xx success status code. Callback is called + * with no arguments on success, or with an error on failure. + * + * Warning: This requires the server to be able to respond to HEAD requests + * and not modify the state of your application. + * + * @param {string} url Url to check + * @param {Function} callback function(error) that is called with result. + */ +angular.scenario.Application.prototype.checkUrlStatus_ = function(url, callback) { + var self = this; + _jQuery.ajax({ + url: url, + type: 'HEAD', + complete: function(request) { + if (request.status < 200 || request.status >= 300) { + if (!request.status) { + callback.call(self, 'Sandbox Error: Cannot access ' + url); + } else { + callback.call(self, request.status + ' ' + request.statusText); + } + } else { + callback.call(self); + } + } + }); +}; + /** * Changes the location of the frame. * * @param {string} url The URL. If it begins with a # then only the * hash of the page is changed. - * @param {Function} onloadFn function($window, $document) + * @param {Function} loadFn function($window, $document) Called when frame loads. + * @param {Function} errorFn function(error) Called if any error when loading. */ -angular.scenario.Application.prototype.navigateTo = function(url, onloadFn) { +angular.scenario.Application.prototype.navigateTo = function(url, loadFn, errorFn) { var self = this; var frame = this.getFrame_(); - if (url.charAt(0) === '#') { + //TODO(esprehn): Refactor to use rethrow() + errorFn = errorFn || function(e) { throw e; }; + if (/^file:\/\//.test(url)) { + errorFn('Sandbox Error: Cannot load file:// URL.'); + } else if (url.charAt(0) === '#') { url = frame.attr('src').split('#')[0] + url; frame.attr('src', url); - this.executeAction(onloadFn); + this.executeAction(loadFn); } else { frame.css('display', 'none').attr('src', 'about:blank'); - this.context.find('#test-frames').append('