Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

make it possible to get/set fragment prefix (#!, #?, #, etc) #55

Closed
wants to merge 1 commit into from

2 participants

@yaniv-aknin

The patch is pretty straightforward.
I apologize for not adding tests; perhaps I can recommend adding a "how to contribute" bit in the docs that will mention exactly how to run them.

@rodneyrehm
Owner

In this case I'd add a static property URI.default_fragment_prefix = '!' and import that on URI instantiation.

@yaniv-aknin

Pardon my weak JS, but won't that set the default for all future URI() instances? I thought it more elegant to make it configurable on a per URI basis, no?

@rodneyrehm
Owner

Well, yes. All I said was move the defaultPrefix variable onto URI so it is actually configurable. That allows you to configure once and use everywhere, while still being able to use some different prefix for specific URI instances...

Regarding the testing, just open the /test directory in your browser. the testing framework is QUnit. fiddle with it!

@yaniv-aknin

Right. I believe I made it as you suggested and added a couple of tests. I hope you'll choose to merge this; and thanks for URI.js!

@rodneyrehm
Owner

I added some comments to your latest commit. :)

@rodneyrehm
Owner

Have you given up on this?

@yaniv-aknin

Not at all, but then life happened, you know.
I'll try to get to it this week, there's really not that much to do.

@rodneyrehm rodneyrehm referenced this pull request from a commit
@rodneyrehm rodneyrehm adding `.fragmentPrefix()` to configure prefix of fragmentURI and fra…
…gmentQuery extensions - Issue #55
ec94d48
@rodneyrehm
Owner

This has been fixed and released in v1.8.2.
(Thank you for your effort!)

@rodneyrehm rodneyrehm closed this
@yaniv-aknin

Thanks for wrapping this up and sorry I dropped the ball on ya... :)

@rodneyrehm
Owner

oh please… you were the reason this came up and I actually looked for good solution. both fragment abuse extensions were initially only meant to inspire custom implementations, not be used literally. I think that has changed now, thanks to you! :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
Showing with 30 additions and 11 deletions.
  1. +23 −10 src/URI.fragmentQuery.js
  2. +7 −1 test/test_fragmentQuery.js
View
33 src/URI.fragmentQuery.js
@@ -38,16 +38,29 @@
var p = URI.prototype;
// old fragment handler we need to wrap
var f = p.fragment;
-// NOTE: google want's #! (hashbang), others might want #? others might want plain #
-// choose the prefix you want to use here
-var prefix = '?';
+// query-encoded fragments aren't standardized, and different codebases use
+// different conventions (Google uses #!, Facebook a bare #, others #?).
+// You can set the prefix for all URI objects with this variable, or modify
+// it on an instance-by-instance basis using uri.fragmentPrefix("!")
+URI.defaultPrefix = '?';
+p.fragmentPrefix = function(newPrefix) {
+ if (newPrefix === undefined) {
+ if (this.prefix === undefined) {
+ return URI.defaultPrefix;
+ } else {
+ return this.prefix;
+ }
+ }
+ this.prefix = newPrefix;
+ return this;
+}
// add fragment(true) and fragment({key: value}) signatures
p.fragment = function(v, build) {
if (v === true) {
- return URI.parseQuery((this._parts.fragment || "").substring(prefix.length));
+ return URI.parseQuery((this._parts.fragment || "").substring(this.fragmentPrefix().length));
} else if (v !== undefined && typeof v !== "string") {
- this._parts.fragment = prefix + URI.buildQuery(v);
+ this._parts.fragment = this.fragmentPrefix() + URI.buildQuery(v);
this.build(!build);
return this;
} else {
@@ -55,9 +68,9 @@ p.fragment = function(v, build) {
}
};
p.addFragment = function(name, value, build) {
- var data = URI.parseQuery((this._parts.fragment || "").substring(prefix.length));
+ var data = URI.parseQuery((this._parts.fragment || "").substring(this.fragmentPrefix().length));
URI.addQuery(data, name, value);
- this._parts.fragment = prefix + URI.buildQuery(data);
+ this._parts.fragment = this.fragmentPrefix() + URI.buildQuery(data);
if (typeof name !== "string") {
build = value;
}
@@ -66,9 +79,9 @@ p.addFragment = function(name, value, build) {
return this;
};
p.removeFragment = function(name, value, build) {
- var data = URI.parseQuery((this._parts.fragment || "").substring(prefix.length));
+ var data = URI.parseQuery((this._parts.fragment || "").substring(this.fragmentPrefix().length));
URI.removeQuery(data, name, value);
- this._parts.fragment = prefix + URI.buildQuery(data);
+ this._parts.fragment = this.fragmentPrefix() + URI.buildQuery(data);
if (typeof name !== "string") {
build = value;
}
@@ -81,4 +94,4 @@ p.removeHash = p.removeFragment;
// extending existing object rather than defining something new
return {};
-}));
+}));
View
8 test/test_fragmentQuery.js
@@ -27,6 +27,12 @@ test("storing query-data in fragment", function() {
u.removeFragment("name");
deepEqual(u.fragment(true), {}, "removing value name");
equal(u.toString(), "http://example.org/#?", "removing value name serialized");
+
+ u = URI("http://example.org/#foo=bar");
+ deepEqual(u.fragmentPrefix("").fragment(true), {foo: "bar"}, "empty fragment prefix");
+
+ u = URI("http://example.org/#!foo=bar");
+ deepEqual(u.fragmentPrefix("!").fragment(true), {foo: "bar"}, "alternative fragment prefix");
});
-})();
+})();
Something went wrong with that request. Please try again.