From 9255af210cccc1ff2bfb83098f5f4d2ce29fd7aa Mon Sep 17 00:00:00 2001 From: Francisco Ryan Tolmasky I Date: Tue, 30 Mar 2010 16:28:42 -0700 Subject: [PATCH] Fix for loading Cappuccino apps locally on Windows. Reviewed by me. --- Objective-J/CFURL.js | 32 +++++++++++++++++++++++++------- Objective-J/StaticResource.js | 8 +++++++- Tests/Objective-J/CFURLTest.j | 29 +++++++++++++++++++++++++++++ 3 files changed, 61 insertions(+), 8 deletions(-) diff --git a/Objective-J/CFURL.js b/Objective-J/CFURL.js index 4c5596100..b9b5052b5 100644 --- a/Objective-J/CFURL.js +++ b/Objective-J/CFURL.js @@ -314,7 +314,7 @@ function resolveURL(aURL) resolvedPathComponents.splice(basePathComponents.length - 1, 1); // If this doesn't start with a "..", then we're simply appending to already standardized paths. - if (pathComponents.length && pathComponents[0] === "..") + if (pathComponents.length && (pathComponents[0] === ".." || pathComponents[0] === ".")) standardizePathComponents(resolvedPathComponents, YES); resolvedParts.pathComponents = resolvedPathComponents; @@ -352,19 +352,26 @@ function standardizePathComponents(/*Array*/ pathComponents, /*BOOL*/ inPlace) var index = 0, resultIndex = 0, count = pathComponents.length, - result = inPlace ? pathComponents : []; + result = inPlace ? pathComponents : [], + startsWithPeriod = NO; for (; index < count; ++index) { var component = pathComponents[index]; - if (component === "" || component === ".") + if (component === "") continue; + if (component === ".") + { + startsWithPeriod = resultIndex === 0; + + continue; + } + if (component !== ".." || resultIndex === 0 || result[resultIndex - 1] === "..") { - //if (resultIndex !== index) - result[resultIndex] = component; + result[resultIndex] = component; resultIndex++; @@ -375,6 +382,9 @@ function standardizePathComponents(/*Array*/ pathComponents, /*BOOL*/ inPlace) --resultIndex; } + if (startsWithPeriod && resultIndex === 0) + result[resultIndex++] = "."; + result.length = resultIndex; return result; @@ -505,10 +515,11 @@ CFURL.prototype.hasDirectoryPath = function() var lastPathComponent = this.lastPathComponent(); hasDirectoryPath = lastPathComponent === "." || lastPathComponent === ".."; + this._hasDirectoryPath = hasDirectoryPath; } - return this._hasDirectoryPath; + return hasDirectoryPath; } DISPLAY_NAME(CFURL.prototype.hasDirectoryPath); @@ -646,7 +657,14 @@ CFURL.prototype.asDirectoryPathURL = function() if (this.hasDirectoryPath()) return this; - return new CFURL(this.lastPathComponent() + "/", this); + var lastPathComponent = this.lastPathComponent(); + + // We do this because on Windows the path may start with C: and be + // misinterpreted as a scheme. + if (lastPathComponent !== "/") + lastPathComponent = "./" + lastPathComponent; + + return new CFURL(lastPathComponent + "/", this); } DISPLAY_NAME(CFURL.prototype.asDirectoryPathURL); diff --git a/Objective-J/StaticResource.js b/Objective-J/StaticResource.js index 681dc8d08..6e3b26244 100644 --- a/Objective-J/StaticResource.js +++ b/Objective-J/StaticResource.js @@ -143,8 +143,14 @@ StaticResource.resourceAtURL = function(/*CFURL|String*/ aURL, /*BOOL*/ resolveA resource = resource._children[name]; else if (resolveAsDirectoriesIfNecessary) - resource = new StaticResource(new CFURL(name, resource.URL()), resource, YES, YES); + { + // We do this because on Windows the path may start with C: and be + // misinterpreted as a scheme. + if (name !== "/") + name = "./" + name; + resource = new StaticResource(new CFURL(name, resource.URL()), resource, YES, YES); + } else throw new Error("Static Resource at " + aURL + " is not resolved (\"" + name + "\")"); } diff --git a/Tests/Objective-J/CFURLTest.j b/Tests/Objective-J/CFURLTest.j index 83000340a..3b7329546 100644 --- a/Tests/Objective-J/CFURLTest.j +++ b/Tests/Objective-J/CFURLTest.j @@ -60,4 +60,33 @@ } } +- (void)testPeriods +{ + var URLStrings = + { + "." : "./", + "./" : "./", + ".//" : "./", + "/." : "/", + "/./" : "/", + "/.//" : "/", + "./a/" : "a/", + "./a" : "a", + ".." : "../", + "../" : "../", + "..//" : "../", + "/.." : "/", + "/../" : "/", + "/..//" : "/", + "../a/" : "../a/", + "../a" : "../a" + }; + + var URLString; + + for (URLString in URLStrings) + if (URLStrings.hasOwnProperty(URLString)) + [self assert:new CFURL(URLString).absoluteString() equals:URLStrings[URLString]]; +} + @end