diff options
author | Pierre Frisch | 2011-12-22 07:25:50 -0800 |
---|---|---|
committer | Valerio Virgillito | 2012-01-27 11:18:17 -0800 |
commit | b89a7ee8b956c96a1dcee995ea840feddc5d4b27 (patch) | |
tree | 0f3136ab0ecdbbbed6a83576581af0a53124d6f1 /node_modules/montage/core/url.js | |
parent | 2401f05d1f4b94d45e4568b81fc73e67b969d980 (diff) | |
download | ninja-b89a7ee8b956c96a1dcee995ea840feddc5d4b27.tar.gz |
First commit of Ninja to ninja-internal
Signed-off-by: Valerio Virgillito <rmwh84@motorola.com>
Diffstat (limited to 'node_modules/montage/core/url.js')
-rw-r--r-- | node_modules/montage/core/url.js | 317 |
1 files changed, 317 insertions, 0 deletions
diff --git a/node_modules/montage/core/url.js b/node_modules/montage/core/url.js new file mode 100644 index 00000000..3f529e35 --- /dev/null +++ b/node_modules/montage/core/url.js | |||
@@ -0,0 +1,317 @@ | |||
1 | /* <copyright> | ||
2 | This file contains proprietary software owned by Motorola Mobility, Inc.<br/> | ||
3 | No rights, expressed or implied, whatsoever to this software are provided by Motorola Mobility, Inc. hereunder.<br/> | ||
4 | (c) Copyright 2011 Motorola Mobility, Inc. All Rights Reserved. | ||
5 | </copyright> */ | ||
6 | |||
7 | // This module is used during the boot-strapping, so it can be required as | ||
8 | // a normal CommonJS module, but alternately bootstraps Montage if there | ||
9 | // is a bootstrap global variable. | ||
10 | (function (definition) { | ||
11 | if (typeof bootstrap !== "undefined") { | ||
12 | bootstrap("core/url", definition); | ||
13 | } else { | ||
14 | bootstrap(require, exports); | ||
15 | } | ||
16 | })(function (require, exports) { | ||
17 | |||
18 | // from Narwhal's URL module: | ||
19 | // https://raw.github.com/280north/narwhal/master/packages/narwhal-lib/lib/uri.js | ||
20 | // from Chiron's HTTP module: | ||
21 | // http://code.google.com/p/chironjs/source/browse/trunk/src/http.js | ||
22 | // in turn, based on Steve Levithan's work | ||
23 | // http://blog.stevenlevithan.com/archives/parseuri | ||
24 | |||
25 | var urlKeys = [ | ||
26 | "url", | ||
27 | "scheme", | ||
28 | "authorityRoot", | ||
29 | "authority", | ||
30 | "userInfo", | ||
31 | "user", | ||
32 | "password", | ||
33 | "domain", | ||
34 | "port", | ||
35 | "path", | ||
36 | "root", | ||
37 | "directory", | ||
38 | "file", | ||
39 | "search", | ||
40 | "query", | ||
41 | "hash" | ||
42 | ]; | ||
43 | |||
44 | var urlExpression = new RegExp( /* url */ | ||
45 | "^" + | ||
46 | "(?:" + | ||
47 | "([^:/?#]+):" + /* scheme */ | ||
48 | ")?" + | ||
49 | "(?:" + | ||
50 | "(//)" + /* authorityRoot */ | ||
51 | "(" + /* authority */ | ||
52 | "(?:" + | ||
53 | "(" + /* userInfo */ | ||
54 | "([^:@]*)" + /* user */ | ||
55 | ":?" + | ||
56 | "([^@]*)" + /* password */ | ||
57 | ")?" + | ||
58 | "@" + | ||
59 | ")?" + | ||
60 | "([^:/?#]*)" + /* domain */ | ||
61 | "(?::(\\d*))?" + /* port */ | ||
62 | ")" + | ||
63 | ")?" + | ||
64 | "(" + /* path */ | ||
65 | "(/?)" + /* root */ | ||
66 | "((?:[^?#/]*/)*)" + | ||
67 | "([^?#]*)" + /* file */ | ||
68 | ")" + | ||
69 | "(\\?([^#]*))?" + /* search, query */ | ||
70 | "(?:#(.*))?" + /* hash */ | ||
71 | "$" | ||
72 | ); | ||
73 | |||
74 | exports.parse = function (url) { | ||
75 | url = String(url); | ||
76 | |||
77 | var items = {}; | ||
78 | var parts = urlExpression.exec(url); | ||
79 | var i; | ||
80 | |||
81 | for (i = 0; i < parts.length; i++) { | ||
82 | items[urlKeys[i]] = parts[i] ? parts[i] : ""; | ||
83 | } | ||
84 | |||
85 | items.root = (items.root || items.authorityRoot) ? '/' : ''; | ||
86 | |||
87 | items.directories = items.directory.split("/"); | ||
88 | if (items.directories[items.directories.length - 1] == "") { | ||
89 | items.directories.pop(); | ||
90 | } | ||
91 | |||
92 | /* normalize */ | ||
93 | var directories = []; | ||
94 | for (i = 0; i < items.directories.length; i++) { | ||
95 | var directory = items.directories[i]; | ||
96 | if (directory == '.') { | ||
97 | } else if (directory == '..') { | ||
98 | if (directories.length && directories[directories.length - 1] != '..') | ||
99 | directories.pop(); | ||
100 | else | ||
101 | directories.push('..'); | ||
102 | } else { | ||
103 | directories.push(directory); | ||
104 | } | ||
105 | } | ||
106 | items.directories = directories; | ||
107 | |||
108 | items.domains = items.domain.split("."); | ||
109 | |||
110 | return items; | ||
111 | }; | ||
112 | |||
113 | exports.format = function (object) { | ||
114 | if (typeof(object) == 'undefined') | ||
115 | throw new Error("UrlError: URL undefined for urls#format"); | ||
116 | if (object instanceof String || typeof(object) == 'string') | ||
117 | return object; | ||
118 | var domain = | ||
119 | object.domains ? | ||
120 | object.domains.join(".") : | ||
121 | object.domain; | ||
122 | var userInfo = ( | ||
123 | object.user || | ||
124 | object.password | ||
125 | ) ? | ||
126 | ( | ||
127 | (object.user || "") + | ||
128 | (object.password ? ":" + object.password : "") | ||
129 | ) : | ||
130 | object.userInfo; | ||
131 | var authority = ( | ||
132 | userInfo || | ||
133 | domain || | ||
134 | object.port | ||
135 | ) ? ( | ||
136 | (userInfo ? userInfo + "@" : "") + | ||
137 | (domain || "") + | ||
138 | (object.port ? ":" + object.port : "") | ||
139 | ) : | ||
140 | object.authority; | ||
141 | var directory = | ||
142 | object.directories ? | ||
143 | object.directories.join("/") : | ||
144 | object.directory; | ||
145 | var path = | ||
146 | directory || object.file ? | ||
147 | ( | ||
148 | (directory ? directory + "/" : "") + | ||
149 | (object.file || "") | ||
150 | ) : | ||
151 | object.path; | ||
152 | var search = | ||
153 | object.query ? "?" + object.query : | ||
154 | (object.search || ""); | ||
155 | return ( | ||
156 | (object.scheme ? object.scheme + ":" : "") + | ||
157 | (authority ? "//" + authority : "") + | ||
158 | (object.root || (authority && path) ? "/" : "") + | ||
159 | (path ? path : "") + | ||
160 | (search) + | ||
161 | (object.hash ? "#" + object.hash : "") | ||
162 | ) || object.url || ""; | ||
163 | }; | ||
164 | |||
165 | /* | ||
166 | returns an object representing a URL resolved from | ||
167 | a relative location and a source location. | ||
168 | */ | ||
169 | exports.resolveObject = function (source, relative) { | ||
170 | if (!source) | ||
171 | return relative; | ||
172 | |||
173 | source = exports.parse(source); | ||
174 | relative = exports.parse(relative); | ||
175 | |||
176 | if (relative.url == "") | ||
177 | return source; | ||
178 | |||
179 | delete source.url; | ||
180 | delete source.authority; | ||
181 | delete source.domain; | ||
182 | delete source.userInfo; | ||
183 | delete source.path; | ||
184 | delete source.directory; | ||
185 | delete source.search; | ||
186 | delete source.query; | ||
187 | delete source.hash; | ||
188 | |||
189 | if (relative.authorityRoot) { | ||
190 | if (!relative.scheme) { | ||
191 | relative.scheme = source.scheme; | ||
192 | } | ||
193 | source = relative; | ||
194 | } else if ( | ||
195 | relative.scheme && relative.scheme != source.scheme || | ||
196 | relative.authority && relative.authority != source.authority | ||
197 | ) { | ||
198 | source = relative; | ||
199 | } else if (relative.root) { | ||
200 | source.directories = relative.directories; | ||
201 | } else { | ||
202 | |||
203 | var directories = source.directories | ||
204 | .concat(relative.directories); | ||
205 | source.directories = []; | ||
206 | for (var i = 0; i < directories.length; i++) { | ||
207 | var directory = directories[i]; | ||
208 | if (directory == "") { | ||
209 | } else if (directory == ".") { | ||
210 | } else if (directory == "..") { | ||
211 | if (source.directories.length) { | ||
212 | source.directories.pop(); | ||
213 | } else { | ||
214 | source.directories.push('..'); | ||
215 | } | ||
216 | } else { | ||
217 | source.directories.push(directory); | ||
218 | } | ||
219 | } | ||
220 | |||
221 | if (relative.file == ".") { | ||
222 | relative.file = ""; | ||
223 | } else if (relative.file == "..") { | ||
224 | source.directories.pop(); | ||
225 | relative.file = ""; | ||
226 | } | ||
227 | } | ||
228 | |||
229 | |||
230 | if (relative.root) | ||
231 | source.root = relative.root; | ||
232 | if (relative.protcol) | ||
233 | source.scheme = relative.scheme; | ||
234 | if (relative.path || !relative.hash) | ||
235 | source.file = relative.file; | ||
236 | if (relative.query) | ||
237 | source.query = relative.query; | ||
238 | if (relative.hash) | ||