-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathparseuri.js
More file actions
135 lines (109 loc) · 4.14 KB
/
parseuri.js
File metadata and controls
135 lines (109 loc) · 4.14 KB
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
// parseUri 1.2.2
// (c) Steven Levithan <stevenlevithan.com>
// MIT License
//
// Upgraded by Anatoly Lapshin
(function() {
Array.prototype.contains = function(obj) {
var i = this.length;
while (i--) {
if (this[i] === obj) {
return true;
}
}
return false;
};
var URI = window.URI = {};
var extensionPack = {
toAbsolute: function(pattern) {
if (pattern.isRelative()) return;
this.host = pattern.host;
this.port = pattern.port;
this.protocol = pattern.protocol;
this.user = pattern.user;
this.password = pattern.password;
return this.toString();
},
isAbsolute: function() {
return !this.isRelative();
},
isRelative: function() {
return !this.host;
},
toString: function(except) {
var except = except || [];
var result = "";
var original = "";
if (this.protocol) {
if (!except.contains('protocol')) result += this.protocol + "://";
original += this.protocol + "://";
if (this.user) {
if (!except.contains('userInfo')) result += this.user;
original += this.user;
if (this.password) {
if (!except.contains('userInfo')) result += ":" + this.password;
original += ":" + this.password;
}
if (!except.contains('userInfo')) result += "@";
original += "@";
}
if (this.host) {
if (!except.contains('host')) result += this.host;
original += this.host;
}
if (this.port) {
if (!except.contains('port')) result += ":" + this.port;
original += ":" + this.port;
}
}
if (this.path) {
if (!except.contains('path')) result += this.path;
original += this.path;
}
if (this.queryKey) {
var queryString = "";
var first = true;
for (var key in this.queryKey) {
if (!this.queryKey.hasOwnProperty(key)) continue;
if (!first) queryString += "&";
queryString += key + "=" + this.queryKey[key];
first = false;
}
if (queryString.length > 0) {
if (!except.contains('queryKey')) result += "?" + queryString;
original += "?" + queryString;
}
}
if (this.anchor) {
if (!except.contains('anchor')) result += "#" + this.anchor;
original += "#" + this.anchor;
}
jQuery.extend(this, URI.parse(original));
return result;
}
};
URI.parse = function (str) {
var o = URI.options,
m = o.parser[o.strictMode ? "strict" : "loose"].exec(str),
uri = {},
i = 14;
while (i--) uri[o.key[i]] = m[i] || "";
uri[o.q.name] = {};
uri[o.key[12]].replace(o.q.parser, function ($0, $1, $2) {
if ($1) uri[o.q.name][$1] = $2;
});
return jQuery.extend(uri, extensionPack);
};
URI.options = {
strictMode: false,
key: ["source","protocol","authority","userInfo","user","password","host","port","relative","path","directory","file","query","anchor"],
q: {
name: "queryKey",
parser: /(?:^|&)([^&=]*)=?([^&]*)/g
},
parser: {
strict: /^(?:([^:\/?#]+):)?(?:\/\/((?:(([^:@]*)(?::([^:@]*))?)?@)?([^:\/?#]*)(?::(\d*))?))?((((?:[^?#\/]*\/)*)([^?#]*))(?:\?([^#]*))?(?:#(.*))?)/,
loose: /^(?:(?![^:@]+:[^:@\/]*@)([^:\/?#.]+):)?(?:\/\/)?((?:(([^:@]*)(?::([^:@]*))?)?@)?([^:\/?#]*)(?::(\d*))?)(((\/(?:[^?#](?![^?#\/]*\.[^?#\/.]+(?:[?#]|$)))*\/?)?([^?#\/]*))(?:\?([^#]*))?(?:#(.*))?)/
}
};
})();