Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/utils/lib/uri.js
blob: 24a76d0fff18c99c34c4fc56d007c843a59ec0bd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
// original code: http://code.google.com/p/js-uri/

// Based on the regex in RFC2396 Appendix B.
var URI_RE = /^(?:([^:\/?\#]+):)?(?:\/\/([^\/?\#]*))?([^?\#]*)(?:\?([^\#]*))?(?:\#(.*))?/;

/**
 * Uniform Resource Identifier (URI) - RFC3986
 */
var URI = exports.URI = function(str) {
    if (!str) str = "";
    var result = str.match(URI_RE);
    this.scheme = result[1] || null;
    this.authority = result[2] || null;
    this.path = result[3] || null;
    this.query = result[4] || null;
    this.fragment = result[5] || null;
}

/**
 * Convert the URI to a String.
 */
URI.prototype.toString = function () {
    var str = "";
 
    if (this.scheme)
        str += this.scheme + ":";

    if (this.authority)
        str += "//" + this.authority;

    if (this.path)
        str += this.path;

    if (this.query)
        str += "?" + this.query;

    if (this.fragment)
        str += "#" + this.fragment;
 
    return str;
}

URI.parse = function(uri) {
    return new URI(uri);
}

URI.unescape = function(str, plus) {
    return decodeURI(str).replace(/\+/g, " ");
}

URI.unescapeComponent = function(str, plus) {
    return decodeURIComponent(str).replace(/\+/g, " ");
}