From d3ab2a0f3d5cb017e8ecf3c44d068ed8553573b4 Mon Sep 17 00:00:00 2001 From: Alexander Ljungberg Date: Tue, 29 Oct 2013 15:03:37 +0000 Subject: [PATCH] New: "//site.com/x" style URL support in CPURL and CFURL. With this feature, CFURL and CPURL can now correctly transform a relative scheme URL starting with a double-slash. For example, + CPURL URLWithString:@"//a.se/a" relativeToURL:@"ftp://b.se/c" would represent an absolute URL of "ftp://a.se/a". --- Objective-J/CFURL.js | 13 ++++++++++--- Tests/Foundation/CPURLTest.j | 10 ++++++++++ Tests/Objective-J/CFURLTest.j | 9 ++++++++- 3 files changed, 28 insertions(+), 4 deletions(-) diff --git a/Objective-J/CFURL.js b/Objective-J/CFURL.js index 757af5617..4f266a6d6 100644 --- a/Objective-J/CFURL.js +++ b/Objective-J/CFURL.js @@ -277,9 +277,17 @@ function resolveURL(aURL) absoluteBaseURL = baseURL.absoluteURL(), baseParts = PARTS(absoluteBaseURL); - if (parts.scheme || parts.authority) + if (!parts.scheme && parts.authorityRoot) + { + // Handle "//domain.com/" style links which need to take their scheme from the base URL, + // but nothing else. + resolvedParts = CFURLPartsCreateCopy(parts); + resolvedParts.scheme = baseURL.scheme(); + } + else if (parts.scheme || parts.authority) + { resolvedParts = parts; - + } else { resolvedParts = { }; @@ -302,7 +310,6 @@ function resolveURL(aURL) resolvedParts.path = parts.path; resolvedParts.pathComponents = pathComponents; } - else { var basePathComponents = baseParts.pathComponents, diff --git a/Tests/Foundation/CPURLTest.j b/Tests/Foundation/CPURLTest.j index db606fab0..4a52efd2d 100644 --- a/Tests/Foundation/CPURLTest.j +++ b/Tests/Foundation/CPURLTest.j @@ -103,6 +103,16 @@ var exampleProtocol = "http", [self assert:[url lastPathComponent] equals:examplePathRelative]; } +- (void)testUrlWithDoubleSlashRelativeToHttpUrl +{ + [self assert:"http://example2.com/b/a.html" equals:[[CPURL URLWithString:@"//example2.com/b/a.html" relativeToURL:[CPURL URLWithString:@"http://www.example.com/test/"]] absoluteString]]; +} + +- (void)testUrlWithDoubleSlashRelativeToHttpsUrl +{ + [self assert:"https://example2.com/b/a.html" equals:[[CPURL URLWithString:@"//example2.com/b/a.html" relativeToURL:[CPURL URLWithString:@"https://www.example.com/test/"]] absoluteString]]; +} + - (void)testDeleteComponent { var url = [CPURL URLWithString:exampleFullPath]; diff --git a/Tests/Objective-J/CFURLTest.j b/Tests/Objective-J/CFURLTest.j index 3b7329546..23cd5ec72 100644 --- a/Tests/Objective-J/CFURLTest.j +++ b/Tests/Objective-J/CFURLTest.j @@ -5,7 +5,7 @@ - (void)testRelativeURLs { - var URLStrings = + var URLStrings = { "g:h" : "g:h", "g" : "http://a/b/c/g", @@ -89,4 +89,11 @@ [self assert:new CFURL(URLString).absoluteString() equals:URLStrings[URLString]]; } +- (void)testDoubleSlash +{ + [self assert:"//a" equals:new CFURL("//a").absoluteString()]; + [self assert:"ftp://a" equals:new CFURL("//a", new CFURL("ftp://example.com/b")).absoluteString()]; + [self assert:"ftp://example.com/a" equals:new CFURL("/a", new CFURL("ftp://example.com/b")).absoluteString()]; +} + @end