]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - include/javascript/tiny_mce/classes/util/URI.js
Release 6.2.3
[Github/sugarcrm.git] / include / javascript / tiny_mce / classes / util / URI.js
1 /**
2  * URI.js
3  *
4  * Copyright 2009, Moxiecode Systems AB
5  * Released under LGPL License.
6  *
7  * License: http://tinymce.moxiecode.com/license
8  * Contributing: http://tinymce.moxiecode.com/contributing
9  */
10
11 (function() {
12         var each = tinymce.each;
13
14         /**
15          * This class handles parsing, modification and serialization of URI/URL strings.
16          * @class tinymce.util.URI
17          */
18         tinymce.create('tinymce.util.URI', {
19                 /**
20                  * Constucts a new URI instance.
21                  *
22                  * @constructor
23                  * @method URI
24                  * @param {String} u URI string to parse.
25                  * @param {Object} s Optional settings object.
26                  */
27                 URI : function(u, s) {
28                         var t = this, o, a, b, base_url;
29
30                         // Trim whitespace
31                         u = tinymce.trim(u);
32
33                         // Default settings
34                         s = t.settings = s || {};
35
36                         // Strange app protocol or local anchor
37                         if (/^(mailto|tel|news|javascript|about|data):/i.test(u) || /^\s*#/.test(u)) {
38                                 t.source = u;
39                                 return;
40                         }
41
42                         // Absolute path with no host, fake host and protocol
43                         if (u.indexOf('/') === 0 && u.indexOf('//') !== 0)
44                                 u = (s.base_uri ? s.base_uri.protocol || 'http' : 'http') + '://mce_host' + u;
45
46                         // Relative path http:// or protocol relative //path
47                         if (!/^[\w-]*:?\/\//.test(u)) {
48                                 base_url = s.base_uri ? s.base_uri.path : new tinymce.util.URI(location.href).directory;
49                                 u = ((s.base_uri && s.base_uri.protocol) || 'http') + '://mce_host' + t.toAbsPath(base_url, u);
50                         }
51
52                         // Parse URL (Credits goes to Steave, http://blog.stevenlevithan.com/archives/parseuri)
53                         u = u.replace(/@@/g, '(mce_at)'); // Zope 3 workaround, they use @@something
54                         u = /^(?:(?![^:@]+:[^:@\/]*@)([^:\/?#.]+):)?(?:\/\/)?((?:(([^:@]*):?([^:@]*))?@)?([^:\/?#]*)(?::(\d*))?)(((\/(?:[^?#](?![^?#\/]*\.[^?#\/.]+(?:[?#]|$)))*\/?)?([^?#\/]*))(?:\?([^#]*))?(?:#(.*))?)/.exec(u);
55                         each(["source","protocol","authority","userInfo","user","password","host","port","relative","path","directory","file","query","anchor"], function(v, i) {
56                                 var s = u[i];
57
58                                 // Zope 3 workaround, they use @@something
59                                 if (s)
60                                         s = s.replace(/\(mce_at\)/g, '@@');
61
62                                 t[v] = s;
63                         });
64
65                         if (b = s.base_uri) {
66                                 if (!t.protocol)
67                                         t.protocol = b.protocol;
68
69                                 if (!t.userInfo)
70                                         t.userInfo = b.userInfo;
71
72                                 if (!t.port && t.host == 'mce_host')
73                                         t.port = b.port;
74
75                                 if (!t.host || t.host == 'mce_host')
76                                         t.host = b.host;
77
78                                 t.source = '';
79                         }
80
81                         //t.path = t.path || '/';
82                 },
83
84                 /**
85                  * Sets the internal path part of the URI.
86                  *
87                  * @method setPath
88                  * @param {string} p Path string to set.
89                  */
90                 setPath : function(p) {
91                         var t = this;
92
93                         p = /^(.*?)\/?(\w+)?$/.exec(p);
94
95                         // Update path parts
96                         t.path = p[0];
97                         t.directory = p[1];
98                         t.file = p[2];
99
100                         // Rebuild source
101                         t.source = '';
102                         t.getURI();
103                 },
104
105                 /**
106                  * Converts the specified URI into a relative URI based on the current URI instance location.
107                  *
108                  * @method toRelative
109                  * @param {String} u URI to convert into a relative path/URI.
110                  * @return {String} Relative URI from the point specified in the current URI instance.
111                  * @example
112                  * // Converts an absolute URL to an relative URL url will be somedir/somefile.htm
113                  * var url = new tinymce.util.URI('http://www.site.com/dir/').toRelative('http://www.site.com/dir/somedir/somefile.htm');
114                  */
115                 toRelative : function(u) {
116                         var t = this, o;
117
118                         if (u === "./")
119                                 return u;
120
121                         u = new tinymce.util.URI(u, {base_uri : t});
122
123                         // Not on same domain/port or protocol
124                         if ((u.host != 'mce_host' && t.host != u.host && u.host) || t.port != u.port || t.protocol != u.protocol)
125                                 return u.getURI();
126
127                         o = t.toRelPath(t.path, u.path);
128
129                         // Add query
130                         if (u.query)
131                                 o += '?' + u.query;
132
133                         // Add anchor
134                         if (u.anchor)
135                                 o += '#' + u.anchor;
136
137                         return o;
138                 },
139         
140                 /**
141                  * Converts the specified URI into a absolute URI based on the current URI instance location.
142                  *
143                  * @method toAbsolute
144                  * @param {String} u URI to convert into a relative path/URI.
145                  * @param {Boolean} nh No host and protocol prefix.
146                  * @return {String} Absolute URI from the point specified in the current URI instance.
147                  * @example
148                  * // Converts an relative URL to an absolute URL url will be http://www.site.com/dir/somedir/somefile.htm
149                  * var url = new tinymce.util.URI('http://www.site.com/dir/').toAbsolute('somedir/somefile.htm');
150                  */
151                 toAbsolute : function(u, nh) {
152                         var u = new tinymce.util.URI(u, {base_uri : this});
153
154                         return u.getURI(this.host == u.host && this.protocol == u.protocol ? nh : 0);
155                 },
156
157                 /**
158                  * Converts a absolute path into a relative path.
159                  *
160                  * @method toRelPath
161                  * @param {String} base Base point to convert the path from.
162                  * @param {String} path Absolute path to convert into a relative path.
163                  */
164                 toRelPath : function(base, path) {
165                         var items, bp = 0, out = '', i, l;
166
167                         // Split the paths
168                         base = base.substring(0, base.lastIndexOf('/'));
169                         base = base.split('/');
170                         items = path.split('/');
171
172                         if (base.length >= items.length) {
173                                 for (i = 0, l = base.length; i < l; i++) {
174                                         if (i >= items.length || base[i] != items[i]) {
175                                                 bp = i + 1;
176                                                 break;
177                                         }
178                                 }
179                         }
180
181                         if (base.length < items.length) {
182                                 for (i = 0, l = items.length; i < l; i++) {
183                                         if (i >= base.length || base[i] != items[i]) {
184                                                 bp = i + 1;
185                                                 break;
186                                         }
187                                 }
188                         }
189
190                         if (bp == 1)
191                                 return path;
192
193                         for (i = 0, l = base.length - (bp - 1); i < l; i++)
194                                 out += "../";
195
196                         for (i = bp - 1, l = items.length; i < l; i++) {
197                                 if (i != bp - 1)
198                                         out += "/" + items[i];
199                                 else
200                                         out += items[i];
201                         }
202
203                         return out;
204                 },
205
206                 /**
207                  * Converts a relative path into a absolute path.
208                  *
209                  * @method toAbsPath
210                  * @param {String} base Base point to convert the path from.
211                  * @param {String} path Relative path to convert into an absolute path.
212                  */
213                 toAbsPath : function(base, path) {
214                         var i, nb = 0, o = [], tr, outPath;
215
216                         // Split paths
217                         tr = /\/$/.test(path) ? '/' : '';
218                         base = base.split('/');
219                         path = path.split('/');
220
221                         // Remove empty chunks
222                         each(base, function(k) {
223                                 if (k)
224                                         o.push(k);
225                         });
226
227                         base = o;
228
229                         // Merge relURLParts chunks
230                         for (i = path.length - 1, o = []; i >= 0; i--) {
231                                 // Ignore empty or .
232                                 if (path[i].length == 0 || path[i] == ".")
233                                         continue;
234
235                                 // Is parent
236                                 if (path[i] == '..') {
237                                         nb++;
238                                         continue;
239                                 }
240
241                                 // Move up
242                                 if (nb > 0) {
243                                         nb--;
244                                         continue;
245                                 }
246
247                                 o.push(path[i]);
248                         }
249
250                         i = base.length - nb;
251
252                         // If /a/b/c or /
253                         if (i <= 0)
254                                 outPath = o.reverse().join('/');
255                         else
256                                 outPath = base.slice(0, i).join('/') + '/' + o.reverse().join('/');
257
258                         // Add front / if it's needed
259                         if (outPath.indexOf('/') !== 0)
260                                 outPath = '/' + outPath;
261
262                         // Add traling / if it's needed
263                         if (tr && outPath.lastIndexOf('/') !== outPath.length - 1)
264                                 outPath += tr;
265
266                         return outPath;
267                 },
268
269                 /**
270                  * Returns the full URI of the internal structure.
271                  *
272                  * @method getURI
273                  * @param {Boolean} nh Optional no host and protocol part. Defaults to false.
274                  */
275                 getURI : function(nh) {
276                         var s, t = this;
277
278                         // Rebuild source
279                         if (!t.source || nh) {
280                                 s = '';
281
282                                 if (!nh) {
283                                         if (t.protocol)
284                                                 s += t.protocol + '://';
285
286                                         if (t.userInfo)
287                                                 s += t.userInfo + '@';
288
289                                         if (t.host)
290                                                 s += t.host;
291
292                                         if (t.port)
293                                                 s += ':' + t.port;
294                                 }
295
296                                 if (t.path)
297                                         s += t.path;
298
299                                 if (t.query)
300                                         s += '?' + t.query;
301
302                                 if (t.anchor)
303                                         s += '#' + t.anchor;
304
305                                 t.source = s;
306                         }
307
308                         return t.source;
309                 }
310         });
311 })();