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