PageRenderTime 30ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/ajax/libs//1.0.0/URI.js

https://gitlab.com/Mirros/cdnjs
JavaScript | 899 lines | 729 code | 112 blank | 58 comment | 222 complexity | 70efed234c50fa3f983db43d7bf26869 MD5 | raw file
  1. /*
  2. * URL.js - Mutating URLs
  3. *
  4. * Version: 1.0.0
  5. *
  6. * Author: Rodney Rehm
  7. * Web: http://medialize.github.com/URI.js/
  8. *
  9. * Licensed under
  10. * MIT License http://www.opensource.org/licenses/mit-license
  11. * GPL v3 http://opensource.org/licenses/GPL-3.0
  12. *
  13. */
  14. (function(undefined) {
  15. if (!RegExp.escape) {
  16. // still can't believe this isn't a regular function(!)
  17. RegExp.escape = function(string) {
  18. return string.replace( /(\^|\$|\\|\||\/|\*|\+|\?|\{|\}|\(|\)|\[|\]|\.)/g, "\\" + "$1" );
  19. };
  20. }
  21. function isArray(obj) {
  22. return String(Object.prototype.toString.call(obj)) === "[object Array]";
  23. }
  24. function filterArrayValues(data, value) {
  25. var lookup = {},
  26. i, length;
  27. if (isArray(value)) {
  28. for (i = 0, length = value.length; i < length; i++) {
  29. lookup[value[i]] = true;
  30. }
  31. } else {
  32. lookup[value] = true;
  33. }
  34. for (i = 0, length = data.length; i < length; i++) {
  35. if (lookup[data[i]] !== undefined) {
  36. data.splice(i, 1);
  37. length--;
  38. i--;
  39. }
  40. }
  41. return data;
  42. }
  43. // constructor
  44. var URI = function(url) {
  45. // Allow instantiation without the 'new' keyword
  46. if (!(this instanceof URI)) {
  47. return new URI(url);
  48. }
  49. if (url === undefined) {
  50. url = location.href + "";
  51. }
  52. this.href(url);
  53. return this;
  54. },
  55. p = URI.prototype;
  56. // convenience
  57. URI.encode = encodeURIComponent;
  58. URI.decode = decodeURIComponent;
  59. p.encode = URI.encode;
  60. p.decode = URI.decode;
  61. // static properties
  62. URI.idn_expression = /[^a-z0-9\.-]/i;
  63. URI.punycode_expression = /(^xn--)/i;
  64. // well, 333.444.555.666 matches, but it sure ain't no IPv4 - do we care?
  65. URI.ip4_expression = /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/;
  66. // credits to Rich Brown
  67. // source: http://forums.intermapper.com/viewtopic.php?p=1096#1096
  68. // specification: http://www.ietf.org/rfc/rfc4291.txt
  69. URI.ip6_expression = /^\s*((([0-9A-Fa-f]{1,4}:){7}([0-9A-Fa-f]{1,4}|:))|(([0-9A-Fa-f]{1,4}:){6}(:[0-9A-Fa-f]{1,4}|((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){5}(((:[0-9A-Fa-f]{1,4}){1,2})|:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){4}(((:[0-9A-Fa-f]{1,4}){1,3})|((:[0-9A-Fa-f]{1,4})?:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){3}(((:[0-9A-Fa-f]{1,4}){1,4})|((:[0-9A-Fa-f]{1,4}){0,2}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){2}(((:[0-9A-Fa-f]{1,4}){1,5})|((:[0-9A-Fa-f]{1,4}){0,3}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){1}(((:[0-9A-Fa-f]{1,4}){1,6})|((:[0-9A-Fa-f]{1,4}){0,4}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(:(((:[0-9A-Fa-f]{1,4}){1,7})|((:[0-9A-Fa-f]{1,4}){0,5}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:)))(%.+)?\s*$/ ;
  70. // http://www.iana.org/assignments/uri-schemes.html
  71. // http://en.wikipedia.org/wiki/List_of_TCP_and_UDP_port_numbers#Well-known_ports
  72. URI.defaultPorts = {
  73. http: "80",
  74. https: "443",
  75. ftp: "21"
  76. };
  77. URI.parse = function(string) {
  78. var pos, t, parts = {};
  79. // [protocol"://"[username[":"password]"@"]hostname[":"port]]["/"path["?"querystring]["#"fragment]]
  80. // TODO: check http://blog.stevenlevithan.com/archives/parseuri
  81. // extract fragment
  82. pos = string.indexOf('#');
  83. if (pos > -1) {
  84. // escaping?
  85. parts.fragment = string.substr(pos + 1) || null;
  86. string = string.substr(0, pos);
  87. }
  88. // extract query
  89. pos = string.indexOf('?');
  90. if (pos > -1) {
  91. // escaping?
  92. parts.query = string.substr(pos + 1) || null;
  93. string = string.substr(0, pos);
  94. }
  95. // extract protocol
  96. pos = string.indexOf('://');
  97. if (pos > -1) {
  98. parts.protocol = string.substr(0, pos);
  99. string = string.substr(pos + 3);
  100. // extract "user:pass@host:port"
  101. string = URI.parseAuthority(string, parts);
  102. }
  103. // what's left must be the path
  104. parts.path = string;
  105. // and we're done
  106. return parts;
  107. };
  108. URI.parseHost = function(string, parts) {
  109. // extract host:port
  110. var pos = string.indexOf('/');
  111. if (pos === -1) {
  112. pos = string.length;
  113. }
  114. if (string[0] === "[") {
  115. // IPv6 host - http://tools.ietf.org/html/draft-ietf-6man-text-addr-representation-04#section-6
  116. // I claim most client software breaks on IPv6 anyways. To simplify things, URI only accepts
  117. // IPv6+port in the format [2001:db8::1]:80 (for the time being)
  118. var bracketPos = string.indexOf(']');
  119. parts.hostname = string.substring(1, bracketPos) || null;
  120. parts.port = string.substring(bracketPos+2, pos) || null;
  121. } else if (string.indexOf(':') !== string.lastIndexOf(':')) {
  122. // IPv6 host contains multiple colons - but no port
  123. parts.hostname = string.substr(0, pos) || null;
  124. parts.port = null;
  125. } else {
  126. t = string.substr(0, pos).split(':');
  127. parts.hostname = t[0] || null;
  128. parts.port = t[1] || null;
  129. }
  130. return string.substr(pos) || '/';
  131. };
  132. URI.parseAuthority = function(string, parts) {
  133. // extract username:password
  134. var pos = string.indexOf('@');
  135. if (pos > -1) {
  136. t = string.substr(0, pos).split(':');
  137. parts.username = t[0] ? URI.decode(t[0]) : null;
  138. parts.password = t[1] ? URI.decode(t[1]) : null;
  139. string = string.substr(pos + 1);
  140. } else {
  141. parts.username = null;
  142. parts.password = null;
  143. }
  144. return URI.parseHost(string, parts);
  145. };
  146. URI.parseQuery = function(string) {
  147. // throw out the funky business - "?"[name"="value"&"]+
  148. string = string.replace(/&+/g, '&').replace(/^\?*&*|&+$/g, '');
  149. var items = {},
  150. splits = string.split('&'),
  151. length = splits.length;
  152. for (var i = 0; i < length; i++) {
  153. var v = splits[i].split('='),
  154. name = URI.decode(v.shift()),
  155. value = URI.decode(v.join('='));
  156. if (items[name]) {
  157. if (typeof items[name] === "string") {
  158. items[name] = [items[name]];
  159. }
  160. items[name].push(value);
  161. } else {
  162. items[name] = value;
  163. }
  164. }
  165. return items;
  166. };
  167. URI.build = function(parts) {
  168. var t = '';
  169. if (typeof parts.protocol === "string" && parts.protocol.length) {
  170. t += parts.protocol + "://";
  171. }
  172. t += (URI.buildAuthority(parts) || '');
  173. if (typeof parts.path === "string") {
  174. if (parts.path[0] !== '/' && typeof parts.hostname === "string") {
  175. t += '/';
  176. }
  177. t += parts.path;
  178. }
  179. if (typeof parts.query == "string") {
  180. t += '?' + parts.query;
  181. }
  182. if (typeof parts.fragment === "string") {
  183. t += '#' + parts.fragment;
  184. }
  185. return t;
  186. };
  187. URI.buildHost = function(parts) {
  188. var t = '';
  189. if (!parts.hostname) {
  190. return '';
  191. } else if (URI.ip6_expression.test(parts.hostname)) {
  192. if (parts.port) {
  193. t += "[" + parts.hostname + "]:" + parts.port;
  194. } else {
  195. // don't know if we should always wrap IPv6 in []
  196. // the RFC explicitly says SHOULD, not MUST.
  197. t += parts.hostname;
  198. }
  199. } else {
  200. t += parts.hostname;
  201. if (parts.port) {
  202. t += ':' + parts.port;
  203. }
  204. }
  205. return t;
  206. };
  207. URI.buildAuthority = function(parts) {
  208. var t = '';
  209. if (parts.username) {
  210. t += URI.encode(parts.username);
  211. if (parts.password) {
  212. t += ':' + URI.encode(parts.password);
  213. }
  214. t += "@";
  215. }
  216. t += URI.buildHost(parts);
  217. return t;
  218. };
  219. URI.buildQuery = function(data) {
  220. var t = "";
  221. for (var key in data) {
  222. if (Object.hasOwnProperty.call(data, key)) {
  223. var name = URI.encode(key + "");
  224. if (isArray(data[key])) {
  225. var unique = {};
  226. for (var i = 0, length = data[key].length; i < length; i++) {
  227. if (data[key][i] !== undefined && unique[data[key][i] + ""] === undefined) {
  228. t += "&" + name + "=" + URI.encode(data[key][i] + "");
  229. unique[data[key][i] + ""] = true;
  230. }
  231. }
  232. } else if (data[key] !== undefined) {
  233. t += "&" + name + "=" + URI.encode(data[key] + "");
  234. }
  235. }
  236. }
  237. return t.substr(1);
  238. };
  239. URI.addQuery = function(data, name, value) {
  240. if (typeof name === "object") {
  241. for (var key in name) {
  242. if (Object.prototype.hasOwnProperty.call(name, key)) {
  243. URI.addQuery(data, key, name[key]);
  244. }
  245. }
  246. } else if (typeof name === "string") {
  247. if (data[name] === undefined) {
  248. data[name] = value;
  249. return;
  250. } else if (typeof data[name] === "string") {
  251. data[name] = [data[name]];
  252. }
  253. if (!isArray(value)) {
  254. value = [value];
  255. }
  256. data[name] = data[name].concat(value);
  257. } else {
  258. throw new TypeError("URI.addQuery() accepts an object, string as the name parameter");
  259. }
  260. };
  261. URI.removeQuery = function(data, name, value) {
  262. if (isArray(name)) {
  263. for (var i = 0, length = name.length; i < length; i++) {
  264. data[name[i]] = undefined;
  265. }
  266. } else if (typeof name === "object") {
  267. for (var key in name) {
  268. if (Object.prototype.hasOwnProperty.call(name, key)) {
  269. URI.removeQuery(data, key, name[key]);
  270. }
  271. }
  272. } else if (typeof name === "string") {
  273. if (value !== undefined) {
  274. if (data[name] === value) {
  275. data[name] = undefined;
  276. } else if (isArray(data[name])) {
  277. data[name] = filterArrayValues(data[name], value);
  278. }
  279. } else {
  280. data[name] = undefined;
  281. }
  282. } else {
  283. throw new TypeError("URI.addQuery() accepts an object, string as the first parameter");
  284. }
  285. };
  286. URI.commonPath = function(one, two) {
  287. var length = Math.min(one.length, two.length),
  288. pos;
  289. // find first non-matching character
  290. for (pos = 0; pos < length; pos++) {
  291. if (one[pos] !== two[pos]) {
  292. pos--;
  293. break;
  294. }
  295. }
  296. if (pos < 1) {
  297. return one[0] === two[0] && one[0] === '/' ? '/' : '';
  298. }
  299. // revert to last /
  300. if (one[pos] !== '/') {
  301. pos = one.substr(0, pos).lastIndexOf('/');
  302. }
  303. return one.substr(0, pos + 1);
  304. };
  305. p.build = function() {
  306. this._string = URI.build(this._parts);
  307. return this;
  308. };
  309. p.toString = function() {
  310. // return this.build()._string;
  311. return this._string;
  312. };
  313. p.valueOf = function() {
  314. return this.toString();
  315. };
  316. // generate simple accessors
  317. var _parts = {protocol: 'protocol', username: 'username', password: 'password', hostname: 'hostname', port: 'port'},
  318. _part;
  319. for (_part in _parts) {
  320. p[_part] = (function(_part){
  321. return function(v, build) {
  322. if (v === undefined) {
  323. return this._parts[_part] || "";
  324. } else {
  325. this._parts[_part] = v;
  326. build !== false && this.build();
  327. return this;
  328. }
  329. };
  330. })(_parts[_part]);
  331. }
  332. // generate accessors with optionally prefixed input
  333. _parts = {query: '?', fragment: '#'};
  334. for (_part in _parts) {
  335. p[_part] = (function(_part, _key){
  336. return function(v, build) {
  337. if (v === undefined) {
  338. return this._parts[_part] || "";
  339. } else {
  340. if (v !== null) {
  341. v = v + "";
  342. if (v[0] === _key) {
  343. v = v.substr(1);
  344. }
  345. }
  346. this._parts[_part] = v;
  347. build !== false && this.build();
  348. return this;
  349. }
  350. };
  351. })(_part, _parts[_part]);
  352. }
  353. // generate accessors with prefixed output
  354. _parts = {search: ['?', 'query'], hash: ['#', 'fragment']};
  355. for (_part in _parts) {
  356. p[_part] = (function(_part, _key){
  357. return function(v, build) {
  358. var t = this[_part](v, build);
  359. return typeof t === "string" && t.length ? (_key + t) : t;
  360. };
  361. })(_parts[_part][1], _parts[_part][0]);
  362. }
  363. p.pathname = function(v, build) {
  364. if (v === undefined) {
  365. return this._parts.path || "/";
  366. } else {
  367. this._parts.path = v || "/";
  368. build !== false && this.build();
  369. return this;
  370. }
  371. };
  372. p.path = p.pathname;
  373. p.href = function(href, build) {
  374. if (href === undefined) {
  375. return this.toString();
  376. } else {
  377. this._string = "";
  378. this._parts = {
  379. protocol: null,
  380. username: null,
  381. password: null,
  382. hostname: null,
  383. port: null,
  384. path: null,
  385. query: null,
  386. fragment: null
  387. };
  388. var _URI = href instanceof URI,
  389. _object = typeof href === "object" && (href.hostname || href.path),
  390. key;
  391. if (typeof href === "string") {
  392. this._parts = URI.parse(href);
  393. } else if (_URI || _object) {
  394. var src = _URI ? href._parts : href;
  395. for (key in src) {
  396. if (Object.hasOwnProperty.call(this._parts, key)) {
  397. this._parts[key] = src[key];
  398. }
  399. }
  400. } else {
  401. throw new TypeError("invalid input");
  402. }
  403. build !== false && this.build();
  404. return this;
  405. }
  406. };
  407. // identification accessors
  408. p.is = function(what) {
  409. var ip = false,
  410. ip4 = false,
  411. ip6 = false,
  412. name = false,
  413. idn = false,
  414. punycode = false,
  415. relative = true;
  416. if (this._parts.hostname) {
  417. relative = false;
  418. ip4 = URI.ip4_expression.test(this._parts.hostname);
  419. ip6 = URI.ip6_expression.test(this._parts.hostname);
  420. ip = ip4 || ip6;
  421. name = !ip;
  422. idn = name && URI.idn_expression.test(this._parts.hostname);
  423. punycode = name && URI.punycode_expression.test(this._parts.hostname);
  424. }
  425. switch (what.toLowerCase()) {
  426. case 'relative':
  427. return relative;
  428. // hostname identification
  429. case 'domain':
  430. case 'name':
  431. return name;
  432. case 'ip':
  433. return ip;
  434. case 'ip4':
  435. case 'ipv4':
  436. case 'inet4':
  437. return ip4;
  438. case 'ip6':
  439. case 'ipv6':
  440. case 'inet6':
  441. return ip6;
  442. case 'idn':
  443. return idn;
  444. case 'punycode':
  445. return punycode;
  446. }
  447. return null;
  448. };
  449. // combination accessors
  450. p.host = function(v, build) {
  451. if (v === undefined) {
  452. return this._parts.hostname ? URI.buildHost(this._parts) : "";
  453. } else {
  454. URI.parseHost(v, this._parts);
  455. build !== false && this.build();
  456. return this;
  457. }
  458. };
  459. p.authority = function(v, build) {
  460. if (v === undefined) {
  461. return this._parts.hostname ? URI.buildAuthority(this._parts) : "";
  462. } else {
  463. URI.parseAuthority(v, this._parts);
  464. build !== false && this.build();
  465. return this;
  466. }
  467. };
  468. // fraction accessors
  469. p.domain = function(v, build) {
  470. // convenience, return "example.org" from "www.example.org"
  471. if (v === undefined) {
  472. if (!this._parts.hostname || this.is('IP')) {
  473. return "";
  474. }
  475. // "localhost" is a domain, too
  476. return this._parts.hostname.match(/\.?([^\.]+.[^\.]+)$/)[1] || this._parts.hostname;
  477. } else {
  478. if (!v) {
  479. throw new TypeError("cannot set domain empty");
  480. } else if (!this._parts.hostname || this.is('IP')) {
  481. this._parts.hostname = v;
  482. } else {
  483. var replace = new RegExp(RegExp.escape(this.domain()) + "$");
  484. this._parts.hostname = this._parts.hostname.replace(replace, v);
  485. }
  486. build !== false && this.build();
  487. return this;
  488. }
  489. };
  490. p.tld = function(v, build) {
  491. // return "org" from "www.example.org"
  492. if (v === undefined) {
  493. if (!this._parts.hostname || this.is('IP')) {
  494. return "";
  495. }
  496. var pos = this._parts.hostname.lastIndexOf('.');
  497. return this._parts.hostname.substr(pos + 1);
  498. } else {
  499. if (!v) {
  500. throw new TypeError("cannot set TLD empty");
  501. } else if (!this._parts.hostname || this.is('IP')) {
  502. throw new ReferenceError("cannot set TLD on non-domain host");
  503. } else {
  504. var replace = new RegExp(RegExp.escape(this.tld()) + "$");
  505. this._parts.hostname = this._parts.hostname.replace(replace, v);
  506. }
  507. build !== false && this.build();
  508. return this;
  509. }
  510. };
  511. p.directory = function(v, build) {
  512. if (v === undefined) {
  513. if (!this._parts.path || this._parts.path === '/') {
  514. return '/';
  515. }
  516. var end = this._parts.path.length - this.filename().length - 1;
  517. return this._parts.path.substring(0, end);
  518. } else {
  519. var e = this._parts.path.length - this.filename().length,
  520. directory = this._parts.path.substring(0, e),
  521. replace = new RegExp('^' + RegExp.escape(directory));
  522. // fully qualifier directories begin with a slash
  523. if (!this.is('relative')) {
  524. if (!v) {
  525. v = '/';
  526. }
  527. if (v[0] !== '/') {
  528. v = "/" + v;
  529. }
  530. }
  531. // directories always end with a slash
  532. if (v && v[v.length - 1] !== '/') {
  533. v += '/';
  534. }
  535. this._parts.path = this._parts.path.replace(replace, v);
  536. build !== false && this.build();
  537. return this;
  538. }
  539. };
  540. p.filename = function(v, build) {
  541. if (v === undefined) {
  542. if (!this._parts.path || this._parts.path === '/') {
  543. return "";
  544. }
  545. var pos = this._parts.path.lastIndexOf('/');
  546. return this._parts.path.substr(pos+1);
  547. } else {
  548. if (v[0] === '/') {
  549. v = v.substr(1);
  550. }
  551. var replace = new RegExp(RegExp.escape(this.filename()) + "$");
  552. this._parts.path = this._parts.path.replace(replace, v);
  553. build !== false && this.build();
  554. return this;
  555. }
  556. };
  557. p.suffix = function(v, build) {
  558. if (v === undefined) {
  559. if (!this._parts.path || this._parts.path === '/') {
  560. return "";
  561. }
  562. var filename = this.filename(),
  563. pos = filename.lastIndexOf('.'),
  564. s;
  565. if (pos === -1) {
  566. return "";
  567. }
  568. // suffix may only contain alnum characters (yup, I made this up.)
  569. s = filename.substr(pos+1);
  570. return (/^[a-z0-9]+$/i).test(s) ? s : "";
  571. } else {
  572. if (v[0] === '.') {
  573. v = v.substr(1);
  574. }
  575. var suffix = this.suffix(),
  576. replace;
  577. if (!suffix) {
  578. if (!v) {
  579. return this;
  580. }
  581. this._parts.path += '.' + v;
  582. } else if (!v) {
  583. replace = new RegExp(RegExp.escape("." + suffix) + "$");
  584. } else {
  585. replace = new RegExp(RegExp.escape(suffix) + "$");
  586. }
  587. if (replace) {
  588. this._parts.path = this._parts.path.replace(replace, v);
  589. }
  590. build !== false && this.build();
  591. return this;
  592. }
  593. };
  594. // mutating query string
  595. var q = p.query;
  596. p.query = function(v, build) {
  597. if (v === true) {
  598. return URI.parseQuery(this._parts.query);
  599. } else if (v !== undefined && typeof v !== "string") {
  600. this._parts.query = URI.buildQuery(v);
  601. build !== false && this.build();
  602. return this;
  603. } else {
  604. return q.call(this, v, build);
  605. }
  606. };
  607. p.addQuery = function(name, value, build) {
  608. var data = URI.parseQuery(this._parts.query);
  609. URI.addQuery(data, name, value);
  610. this._parts.query = URI.buildQuery(data);
  611. if (typeof name !== "string") {
  612. build = value;
  613. }
  614. build !== false && this.build();
  615. return this;
  616. };
  617. p.removeQuery = function(name, value, build) {
  618. var data = URI.parseQuery(this._parts.query);
  619. URI.removeQuery(data, name, value);
  620. this._parts.query = URI.buildQuery(data);
  621. if (typeof name !== "string") {
  622. build = value;
  623. }
  624. build !== false && this.build();
  625. return this;
  626. };
  627. p.addSearch = p.addQuery;
  628. p.removeSearch = p.removeQuery;
  629. // sanitizing URLs
  630. p.normalize = function() {
  631. return this
  632. .normalizeHostname(false)
  633. .normalizePort(false)
  634. .normalizePath(false)
  635. .normalizeQuery(false)
  636. .normalizeFragment(false)
  637. .build();
  638. };
  639. p.normalizeHostname = function(build) {
  640. if (this.is('IDN') && window.punycode) {
  641. this._parts.hostname = punycode.toASCII(this._parts.hostname);
  642. } else if (this.is('IPv6') && window.IPv6) {
  643. this._parts.hostname = IPv6.best(this._parts.hostname);
  644. }
  645. build !== false && this.build();
  646. return this;
  647. };
  648. p.normalizePort = function(build) {
  649. // remove port of it's the protocol's default
  650. if (typeof this._parts.protocol === "string" && this._parts.port === URI.defaultPorts[this._parts.protocol]) {
  651. this._parts.port = null;
  652. }
  653. build !== false && this.build();
  654. return this;
  655. };
  656. p.normalizePath = function(build) {
  657. if (!this._parts.path || this._parts.path === '/') {
  658. return this;
  659. }
  660. var _was_relative,
  661. _was_relative_prefix,
  662. _path = this._parts.path,
  663. _parent, _pos;
  664. // handle relative paths
  665. if (_path[0] !== '/') {
  666. if (_path[0] === '.') {
  667. _was_relative_prefix = _path.substr(0, _path.indexOf('/'));
  668. }
  669. _was_relative = true;
  670. _path = '/' + _path;
  671. }
  672. // resolve simples
  673. _path = _path.replace(/(\/(\.\/)+)|\/{2,}/g, '/');
  674. // resolve parents
  675. while (true) {
  676. _parent = _path.indexOf('/../');
  677. if (_parent === -1) {
  678. // no more ../ to resolve
  679. break;
  680. } else if (_parent === 0) {
  681. // top level cannot be relative...
  682. _path = _path.substr(3);
  683. break;
  684. }
  685. _pos = _path.substr(0, _parent).lastIndexOf('/');
  686. if (_pos === -1) {
  687. _pos = _parent;
  688. }
  689. _path = _path.substr(0, _pos) + _path.substr(_parent + 3);
  690. }
  691. // revert to relative
  692. if (_was_relative && this.is('relative')) {
  693. if (_was_relative_prefix){
  694. _path = _was_relative_prefix + _path;
  695. } else {
  696. _path = _path.substr(1);
  697. }
  698. }
  699. this._parts.path = _path;
  700. build !== false && this.build();
  701. return this;
  702. };
  703. p.normalizePathname = p.normalizePath;
  704. p.normalizeQuery = function(build) {
  705. if (typeof this._parts.query === "string") {
  706. if (!this._parts.query.length) {
  707. this._parts.query = null;
  708. } else {
  709. this.query(URI.parseQuery(this._parts.query));
  710. }
  711. }
  712. build !== false && this.build();
  713. return this;
  714. };
  715. p.normalizeFragment = function(build) {
  716. if (!this._parts.fragment) {
  717. this._parts.fragment = null;
  718. }
  719. build !== false && this.build();
  720. return this;
  721. };
  722. p.normalizeSearch = p.normalizeQuery;
  723. p.normalizeHash = p.normalizeFragment;
  724. // resolving relative and absolute URLs
  725. p.absoluteTo = function(base) {
  726. if (!this.is('relative')) {
  727. throw new Error('Cannot resolve non-relative URL');
  728. }
  729. if (!(base instanceof URI)) {
  730. base = new URI(base);
  731. }
  732. var resolved = new URI(this),
  733. properties = ['protocol', 'username', 'password', 'hostname', 'port'];
  734. for (var i = 0, p; p = properties[i]; i++) {
  735. resolved._parts[p] = base._parts[p];
  736. }
  737. if (resolved.path()[0] !== '/') {
  738. resolved._parts.path = base.directory() + '/' + resolved._parts.path;
  739. resolved.normalizePath();
  740. }
  741. resolved.build();
  742. return resolved;
  743. };
  744. p.relativeTo = function(base) {
  745. if (!(base instanceof URI)) {
  746. base = new URI(base);
  747. }
  748. if (this.path()[0] !== '/' || base.path()[0] !== '/') {
  749. throw new Error('Cannot calculate common path from non-relative URLs');
  750. }
  751. var relative = new URI(this),
  752. properties = ['protocol', 'username', 'password', 'hostname', 'port'],
  753. common = URI.commonPath(relative.path(), base.path()),
  754. _base = base.directory();
  755. for (var i = 0, p; p = properties[i]; i++) {
  756. relative._parts[p] = null;
  757. }
  758. if (!common || common === '/') {
  759. return relative;
  760. }
  761. if (_base + '/' === common) {
  762. relative._parts.path = './' + relative.filename();
  763. } else {
  764. var parents = '../',
  765. _common = new RegExp('^' + RegExp.escape(common)),
  766. _parents = _base.replace(_common, '/').match(/\//g).length -1;
  767. while (_parents--) {
  768. parents += '../';
  769. }
  770. relative._parts.path = relative._parts.path.replace(_common, parents);
  771. }
  772. relative.build();
  773. return relative;
  774. };
  775. window.URI = URI;
  776. })();