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".
This commit is contained in:
Alexander Ljungberg
2013-10-29 15:03:37 +00:00
parent af26523e36
commit d3ab2a0f3d
3 changed files with 28 additions and 4 deletions
+10 -3
View File
@@ -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,
+10
View File
@@ -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];
+8 -1
View File
@@ -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