From eae9c49368235acff2fe85389ed26416c615d8f2 Mon Sep 17 00:00:00 2001 From: Brian Moschel Date: Thu, 4 Nov 2010 01:53:59 -0500 Subject: [PATCH 1/3] fixing windows style paths and relative filesystem paths for Envjs.uri --- specs/platform/core.js | 11 +++++++++-- src/platform/core/xhr.js | 11 +++++++++-- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/specs/platform/core.js b/specs/platform/core.js index 4d656a03..b6cee3bb 100644 --- a/specs/platform/core.js +++ b/specs/platform/core.js @@ -42,8 +42,6 @@ test('Envjs.uri', function(){ equals(uri, 'http://envjs.com/specs/env/spec.html', 'uri'); equals(uri.toString(), 'http://envjs.com/specs/env/spec.html', 'uri'); - document = null; - uri = Envjs.uri('http://envjs.com/specs/env/spec.html'); ok(uri, 'Able to create uri'); equals(uri, 'http://envjs.com/specs/env/spec.html', 'uri'); @@ -59,6 +57,15 @@ test('Envjs.uri', function(){ uri = Envjs.uri('file:///foo/bar/'); equals(uri, 'file:///foo/bar/', 'File, absolute, with ending "/"'); + + // handle windows style file paths, firefox will convert this to a file: URL + uri = Envjs.uri('C:\\foo\\bar\\index.html'); + equals(uri, 'file:///C:/foo/bar/index.html', 'File, absolute, converted slashes'); + + // when there is no document and you pass a relative path, it should be converted to a file: URL + document = null; + uri = Envjs.uri('specs/env/spec.html'); + ok(/file\:\/\/\/.*\/specs\/env\/spec.html/.test(uri), 'Relative filesystem paths work'); uri = Envjs.uri('http://foo.com'); equals(uri, 'http://foo.com/', 'http, absolute, without path, without ending "/"'); diff --git a/src/platform/core/xhr.js b/src/platform/core/xhr.js index 562a222b..50060e53 100644 --- a/src/platform/core/xhr.js +++ b/src/platform/core/xhr.js @@ -23,6 +23,7 @@ Envjs.getcwd = function() { * @param {Object} base (semi-optional) The base url used in resolving "path" above */ Envjs.uri = function(path, base) { + path = path.replace(/\\/g, '/'); //console.log('constructing uri from path %s and base %s', path, base); path = path+''; // Semi-common trick is to make an iframe with src='javascript:false' @@ -37,6 +38,12 @@ Envjs.uri = function(path, base) { return urlparse.urlnormalize(path); } + // if path is a Windows style absolute path (C:\foo\bar\index.html) + // make it a file: URL + if (path.match('^[a-zA-Z]+:/')) { + return 'file:///' + urlparse.urlnormalize(path); + } + // interesting special case, a few very large websites use // '//foo/bar/' to mean 'http://foo/bar' if (path.match('^//')) { @@ -58,7 +65,7 @@ Envjs.uri = function(path, base) { // if base is still empty, then we are in QA mode loading local // files. Get current working directory if (!base) { - base = 'file://' + Envjs.getcwd() + '/'; + base = 'file:///' + (""+Envjs.getcwd()).replace(/\\/g, '/') + '/'; } // handles all cases if path is abosulte or relative to base // 3rd arg is "false" --> remove fragments @@ -173,4 +180,4 @@ Envjs.localXHR = function(url, xhr, connection, data){ __extend__(Envjs, urlparse); -}(/*Envjs.XMLHttpRequest.Core*/)); \ No newline at end of file +}(/*Envjs.XMLHttpRequest.Core*/)); From 74d3bcd86bbdbadb9c7d28645cbec22f18d4f349 Mon Sep 17 00:00:00 2001 From: Brian Moschel Date: Thu, 4 Nov 2010 02:00:46 -0500 Subject: [PATCH 2/3] accidentally removed document=null in the tests, restoring that --- specs/platform/core.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/specs/platform/core.js b/specs/platform/core.js index b6cee3bb..232047a2 100644 --- a/specs/platform/core.js +++ b/specs/platform/core.js @@ -41,6 +41,8 @@ test('Envjs.uri', function(){ ok(uri, 'Able to create uri'); equals(uri, 'http://envjs.com/specs/env/spec.html', 'uri'); equals(uri.toString(), 'http://envjs.com/specs/env/spec.html', 'uri'); + + document = null; uri = Envjs.uri('http://envjs.com/specs/env/spec.html'); ok(uri, 'Able to create uri'); From 08a81973a9357627c9bb333285ca472d2c9812be Mon Sep 17 00:00:00 2001 From: Chris Nash Date: Wed, 18 Apr 2012 15:25:31 -0700 Subject: [PATCH 3/3] Incorporated filesystem path fixes for Windows file paths (as in pull request #23, 031b304/b645a70 from moschel. Added Windows batch files to run envjs using either Rhino or the Rhino debugger. Fixed an issue in xhr.js Envjs.localXHR when running under Rhino, url parameter was tested for pattern match to determine Content-Type, but fails under Rhino since the url parameter passed is a Java object, not a string. --- bin/debug.bat | 1 + bin/envjs.bat | 1 + envjs/platform/core.js | 20 +++++++++++++++----- src/platform/core/xhr.js | 10 ++++++---- 4 files changed, 23 insertions(+), 9 deletions(-) create mode 100644 bin/debug.bat create mode 100644 bin/envjs.bat diff --git a/bin/debug.bat b/bin/debug.bat new file mode 100644 index 00000000..d73571ee --- /dev/null +++ b/bin/debug.bat @@ -0,0 +1 @@ +java -cp rhino/js.jar org.mozilla.javascript.tools.debugger.Main envjs/rhino.js %* diff --git a/bin/envjs.bat b/bin/envjs.bat new file mode 100644 index 00000000..07be351a --- /dev/null +++ b/bin/envjs.bat @@ -0,0 +1 @@ +java -Xmx512M -jar rhino/js.jar -opt -1 envjs/rhino.js %* diff --git a/envjs/platform/core.js b/envjs/platform/core.js index 9d53a2cf..d48302ab 100644 --- a/envjs/platform/core.js +++ b/envjs/platform/core.js @@ -2763,6 +2763,7 @@ Envjs.getcwd = function() { * @param {Object} base (semi-optional) The base url used in resolving "path" above */ Envjs.uri = function(path, base) { + path = path.replace(/\\/g, '/'); //console.log('constructing uri from path %s and base %s', path, base); path = path+''; // Semi-common trick is to make an iframe with src='javascript:false' @@ -2777,6 +2778,12 @@ Envjs.uri = function(path, base) { return urlparse.urlnormalize(path); } + // if path is a Windows style absolute path (C:\foo\bar\index.html) + // make it a file: URL + if (path.match('^[a-zA-Z]+:/')) { + return 'file:///' + urlparse.urlnormalize(path); + } + // interesting special case, a few very large websites use // '//foo/bar/' to mean 'http://foo/bar' if (path.match('^//')) { @@ -2798,7 +2805,7 @@ Envjs.uri = function(path, base) { // if base is still empty, then we are in QA mode loading local // files. Get current working directory if (!base) { - base = 'file://' + Envjs.getcwd() + '/'; + base = 'file:///' + (""+Envjs.getcwd()).replace(/\\/g, '/') + '/'; } // handles all cases if path is abosulte or relative to base // 3rd arg is "false" --> remove fragments @@ -2884,13 +2891,15 @@ Envjs.localXHR = function(url, xhr, connection, data){ xhr.statusText = "ok"; xhr.responseText = Envjs.readFromFile(url); try{ - if(url.match(/html$/)){ + //url as passed in here might be an object, so stringify it + var urlstring = url.toString(); + if(urlstring.match(/html$/)){ xhr.responseHeaders["Content-Type"] = 'text/html'; - }else if(url.match(/.xml$/)){ + }else if(urlstring.match(/.xml$/)){ xhr.responseHeaders["Content-Type"] = 'text/xml'; - }else if(url.match(/.js$/)){ + }else if(urlstring.match(/.js$/)){ xhr.responseHeaders["Content-Type"] = 'text/javascript'; - }else if(url.match(/.json$/)){ + }else if(urlstring.match(/.json$/)){ xhr.responseHeaders["Content-Type"] = 'application/json'; }else{ xhr.responseHeaders["Content-Type"] = 'text/plain'; @@ -2914,6 +2923,7 @@ Envjs.localXHR = function(url, xhr, connection, data){ __extend__(Envjs, urlparse); }(/*Envjs.XMLHttpRequest.Core*/)); + (function(){ var log = Envjs.logger('Envjs.Window'); diff --git a/src/platform/core/xhr.js b/src/platform/core/xhr.js index 50060e53..70e28d28 100644 --- a/src/platform/core/xhr.js +++ b/src/platform/core/xhr.js @@ -151,13 +151,15 @@ Envjs.localXHR = function(url, xhr, connection, data){ xhr.statusText = "ok"; xhr.responseText = Envjs.readFromFile(url); try{ - if(url.match(/html$/)){ + //url as passed in here might be an object, so stringify it + var urlstring = url.toString(); + if(urlstring.match(/html$/)){ xhr.responseHeaders["Content-Type"] = 'text/html'; - }else if(url.match(/.xml$/)){ + }else if(urlstring.match(/.xml$/)){ xhr.responseHeaders["Content-Type"] = 'text/xml'; - }else if(url.match(/.js$/)){ + }else if(urlstring.match(/.js$/)){ xhr.responseHeaders["Content-Type"] = 'text/javascript'; - }else if(url.match(/.json$/)){ + }else if(urlstring.match(/.json$/)){ xhr.responseHeaders["Content-Type"] = 'application/json'; }else{ xhr.responseHeaders["Content-Type"] = 'text/plain';