Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Spurious initial slash in empty relative path when calling .path() accessor #82

Closed
wants to merge 2 commits into from

2 participants

@djcsdy
URI("/a/b/c/").
    relativeTo("/a/b/c/").
    toString() === "";
// Good!

URI("/a/b/c/").
    relativeTo("/a/b/c/").
    path() === "/";
// Bad!

URI("").toString() === ""; // Good!

URI("").path() === "/"; // Bad!
  • 8d0c31c adds test cases demonstrating this problem.
  • 8049654 fixes the problem.

Please see the commit logs for rationale behind the changes made.

djcsdy added some commits
@djcsdy djcsdy Add tests demonstrating incorrect empty relative path serialization b…
…y uri.path() and uri.pathname().

Clearly the relative path from"/a/b/c/" to "/a/b/c/" is the empty relative path "", and not "/".

Equivalently, URI("").path() should return the empty string "", but in fact returns "/".

The empty relative path is serialized correctly by .toString(), but incorrectly by .path() and .pathname().
8d0c31c
@djcsdy djcsdy Fix incorrect serialization of empty relative paths.
If the URI includes an authority component, then the path is always absolute. Otherwise, the path may be relative and we should not coerce an empty relative path to the empty absolute path.

We check for the presence of a hostname as a proxy for checking for an authority component, because in our implementation the presence of a hostname determines the presence of an authority component.

There was previously a check to see if the URI was a URN, but that is now redundant since a URN cannot include an authority component.
8049654
@rodneyrehm
Owner

What's the status on this? are there still things missing? I've started merging the open stuff into master

@djcsdy

I believe this is safe to merge.

@rodneyrehm
Owner

I've merged this into master - it will be included in the next release. thank you for your help!

@rodneyrehm rodneyrehm closed this
@djcsdy djcsdy deleted the branch
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Apr 23, 2013
  1. @djcsdy

    Add tests demonstrating incorrect empty relative path serialization b…

    djcsdy authored
    …y uri.path() and uri.pathname().
    
    Clearly the relative path from"/a/b/c/" to "/a/b/c/" is the empty relative path "", and not "/".
    
    Equivalently, URI("").path() should return the empty string "", but in fact returns "/".
    
    The empty relative path is serialized correctly by .toString(), but incorrectly by .path() and .pathname().
  2. @djcsdy

    Fix incorrect serialization of empty relative paths.

    djcsdy authored
    If the URI includes an authority component, then the path is always absolute. Otherwise, the path may be relative and we should not coerce an empty relative path to the empty absolute path.
    
    We check for the presence of a hostname as a proxy for checking for an authority component, because in our implementation the presence of a hostname determines the presence of an authority component.
    
    There was previously a check to see if the URI was a URN, but that is now redundant since a URN cannot include an authority component.
This page is out of date. Refresh to see the latest.
Showing with 9 additions and 1 deletion.
  1. +1 −1  src/URI.js
  2. +8 −0 test/test.js
View
2  src/URI.js
@@ -800,7 +800,7 @@ for (_part in _parts) {
p.pathname = function(v, build) {
if (v === undefined || v === true) {
- var res = this._parts.path || (this._parts.urn ? '' : '/');
+ var res = this._parts.path || (this._parts.hostname ? '/' : '');
return v ? URI.decodePath(res) : res;
} else {
this._parts.path = v ? URI.recodePath(v) : "/";
View
8 test/test.js
@@ -155,6 +155,14 @@ test("path", function() {
u.pathname('/~userhome/@mine;is %2F and/');
equal(u.pathname(), '/~userhome/@mine;is%20%2F%20and/', "path encoding");
equal(u.pathname(true), '/~userhome/@mine;is %2F and/', "path decoded");
+
+ u = new URI('/a/b/c/').relativeTo('/a/b/c/');
+ equal(u.pathname(), '', "empty relative path");
+ equal(u.toString(), '', "empty relative path to string");
+
+ u.pathname('/');
+ equal(u.pathname(), '/', "empty absolute path");
+ equal(u.toString(), '/', "empty absolute path to string");
});
test("query", function() {
var u = new URI("http://example.org/foo.html");
Something went wrong with that request. Please try again.