Skip to content

fragmentURI methods for working with single page apps #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 26 additions & 3 deletions src/URI.js
Original file line number Diff line number Diff line change
Expand Up @@ -360,8 +360,9 @@ p.build = function() {
};

p.toString = function() {
// return this.build()._string;
return this._string;
return this.isFragmentURI() ?
(new URI(this.parentURI)).fragment(this._string).toString() :
this._string;
};
p.valueOf = function() {
return this.toString();
Expand Down Expand Up @@ -894,6 +895,28 @@ p.relativeTo = function(base) {
return relative;
};

p.fragmentURI = function() {
if (this.isFragmentURI()) {
throw new TypeError("cannot nest fragmentURIs");
}

var frag = new URI(this.fragment());
frag.parentURI = this;
return frag;
}

p.isFragmentURI = function() {
return !!this.parentURI;
}

p.reassemble = function() {
if (!this.isFragmentURI()) {
throw new TypeError("can only reassemble fragment URIs");
}

return this.parentURI.fragment(this._string);
}

window.URI = URI;

})();
})();
32 changes: 32 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,38 @@ test("removeQuery", function() {
equal(u.query(), 'foo=baz&foo=bam&bar=3', 'removing object');
});

module("fragmentURI");
test("fragmentURI retrieval", function() {
var u = new URI("http://www.example.org/?userid=1#/single/page/app?page=5");
equal(u.fragmentURI().path(), "/single/page/app", "retrieve path from fragmentURI");
equal(u.fragmentURI().query(), "page=5", "retrieve query from fragmentURI");
raises(function() {
u.fragmentURI().fragmentURI();
}, TypeError, "nested calls to fragmentURI raises error");
});
test("isFragmentURI", function() {
var u = new URI("http://www.example.org/?userid=1#/single/page/app?page=5");
ok(u.fragmentURI().isFragmentURI(), "returns true when in fragmentURI");
ok(!u.isFragmentURI(), "returns false when not in fragmentURI");
});
test("fragmentURI mutation", function() {
var u = new URI("http://www.example.org/?userid=1#/single/page/app?page=5");
equal(u.fragmentURI().path("/otherpath")+"", "http://www.example.org/?userid=1#/otherpath?page=5", "string of modified fragmentURI returns reassembled full URL");
equal(u+"", "http://www.example.org/?userid=1#/single/page/app?page=5", "modifying a fragmentURI without calling reassemble, does not modify the original URI");
u.fragmentURI().path("/foo").addSearch("num", 6).reassemble();
equal(u+"", "http://www.example.org/?userid=1#/foo?page=5&num=6", "reassemble modifies the original URL");

u = new URI("http://www.example.org/?userid=1#/single/page/app?page=5");
var frag = u.fragmentURI();
frag.path("/foo");
u.port("8080");
frag.reassemble();
equal(u+"", "http://www.example.org:8080/?userid=1#/foo?page=5", "reassemble references the parent URI, not a copy");

raises(function() {
u.reassemble();
}, TypeError, "calling reassemble when not in a fragmentURI raises error");
});

module("normalizing");
test("normalize", function() {
Expand Down