CFURL/CPURL fixes/enhancements

- Fixed bizarre bug where local variable path was being set as attribute of object in CPURL -pathComponents.
- Added CPURL -URLByDeletingLastPathComponent.
- Linted CFURL.js.
- CFURL constructor should copy when passed a CFURL.
- Added CFURL.createCopyDeletingLastPathComponent.
- Added test for URLByDeletingLastPathComponent.
This commit is contained in:
Aparajita Fishman committed 2012-04-12 10:45:01 -04:00
1 parent 6293380406
commit 5fed81e1dc
3 files changed
+49 -6

No files matched your search

+9 -2
View File
@@ -122,8 +122,8 @@ CPURLCustomIconKey = @"CPURLCustomIconKey";
- (CPArray)pathComponents
{
var path = self.pathComponents();
return [path copy];
var components = self.pathComponents();
return [components copy];
}
// if absolute, returns the same as path
@@ -202,6 +202,13 @@ CPURLCustomIconKey = @"CPURLCustomIconKey";
return self.pathExtension();
}
- (CPURL)URLByDeletingLastPathComponent
{
var result = self.createCopyDeletingLastPathComponent();
result.isa = [self class];
return result;
}
- (CPURL)standardizedURL
{
return self.standardizedURL();
+22 -4
View File
@@ -122,10 +122,9 @@ function CFURLGetParts(/*CFURL*/ aURL)
{
var split = parts.path.split("/"),
pathComponents = parts.pathComponents,
index = 0,
count = split.length;
for (; index < count; ++index)
for (index = 0; index < count; ++index)
{
var component = split[index];
@@ -160,7 +159,7 @@ GLOBAL(CFURL) = function(/*CFURL|String*/ aURL, /*CFURL*/ aBaseURL)
if (aURL instanceof CFURL)
{
if (!aBaseURL)
return aURL;
return new CFURL(aURL.absoluteString());
var existingBaseURL = aURL.baseURL();
@@ -360,7 +359,7 @@ function standardizePathComponents(/*Array*/ pathComponents, /*BOOL*/ inPlace)
var component = pathComponents[index];
if (component === "")
continue;
continue;
if (component === ".")
{
@@ -564,6 +563,25 @@ CFURL.prototype.path = function()
DISPLAY_NAME(CFURL.prototype.path);
CFURL.prototype.createCopyDeletingLastPathComponent = function()
{
var parts = PARTS(this),
components = standardizePathComponents(parts.pathComponents, NO);
if (components.length > 0)
if (components.length > 1 || components[0] !== "/")
components.pop();
// pathFromPathComponents() returns an empty path for ["/"]
var isRoot = components.length === 1 && components[0] === "/";
parts.pathComponents = components;
parts.path = isRoot ? "/" : pathFromPathComponents(components, NO);
return new CFURL(URLStringFromParts(parts));
};
DISPLAY_NAME(CFURL.prototype.createCopyDeletingLastPathComponent);
CFURL.prototype.pathComponents = function()
{
return PARTS(this).pathComponents;
+18
View File
@@ -103,6 +103,24 @@ var exampleProtocol = "http",
[self assert:[url lastPathComponent] equals:examplePathRelative];
}
- (void)testDeleteComponent
{
var url = [CPURL URLWithString:exampleFullPath];
url = [url URLByDeletingLastPathComponent];
[self assert:[url absoluteString] equals:examplePathBase.substring(0, examplePathBase.length - 1)];
url = [url URLByDeletingLastPathComponent];
[self assert:[url absoluteString] equals:@"/"];
url = [url URLByDeletingLastPathComponent];
[self assert:[url absoluteString] equals:@"/"];
url = [CPURL URLWithString:@"foo"];
url = [url URLByDeletingLastPathComponent];
[self assert:[url absoluteString] equals:@""];
}
- (void)testURLToString
{
[self assert:String([CPURL URLWithString:exampleURL]) equals:exampleURL];