I started looking at a dashcode project the other day. Within a few minutes, I realised that I needed something like URI.pm, in order to resolve URIs into an absolute form. I had a quick look around and there didn’t seem to be anything code out there that does this.
So, I had a quick look at RFC 3986 and noticed that amongst other things it contains pseudo-code for parsing and resolving URLs. I coded it up first in Ruby, which took the pseudo-code almost word for word, and made some tests for it. Then I translated into JavaScript and figured out how to use Test.Simple.
The end result is js-uri, a small URI object. It does what I need at the moment.
// Parsing. var some_uri = new URI("http://www.example.com/foo/bar"); alert(some_uri.authority); // www.example.com alert(some_uri); // http://www.example.com/foo/bar // Resolving. var blah = new URI("blah"); var blah_full = blah.resolve(some_uri); alert(blah_full); // http://www.example.com/foo/blah
There’s quite a bit it doesn’t do yet.
- %-escaping and unescaping.
- Parsing the URL in more detail. It would be useful to pull out the query parameters and host / port for instance.
- For some reason, the tests only work in Firefox. Not sure why.
But hopefully it’s still useful.
In the process of developing the JavaScript, I spent ages tracking down one particular problem. I was trying to introduce new scope to define a couple of private helper functions:
(function () { function merge(base, rel_path) { ... } function remove_dot_segments(path) { ... } URI.prototype.resolve = function (base) { var target = new URI(); // ... return target; } })();
Looks perfectly legit right? It caused syntax errors in every browser I tried. Eventually, JSLint managed to tell me the problem: a missing semicolon after the URI.prototype.resolve
definition. I hate JavaScript for trying to guess where the semicolon should go. Because it can get it so wrong like this.