PageRenderTime 48ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/ajax/libs//1.1.0/URI.js

https://gitlab.com/Mirros/cdnjs
JavaScript | 947 lines | 724 code | 128 blank | 95 comment | 231 complexity | c4a0b9176428ed69b0e4626a1785beea MD5 | raw file
  1. /*
  2. * URL.js - Mutating URLs
  3. *
  4. * Version: 1.1.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. function escapeRegEx(string) {
  16. // https://github.com/medialize/URI.js/commit/85ac21783c11f8ccab06106dba9735a31a86924d#commitcomment-821963
  17. return string.replace(/([.*+?^=!:${}()|[\]\/\\])/g, '\\$1');
  18. }
  19. function isArray(obj) {
  20. return String(Object.prototype.toString.call(obj)) === "[object Array]";
  21. }
  22. function filterArrayValues(data, value) {
  23. var lookup = {},
  24. i, length;
  25. if (isArray(value)) {
  26. for (i = 0, length = value.length; i < length; i++) {
  27. lookup[value[i]] = true;
  28. }
  29. } else {
  30. lookup[value] = true;
  31. }
  32. for (i = 0, length = data.length; i < length; i++) {
  33. if (lookup[data[i]] !== undefined) {
  34. data.splice(i, 1);
  35. length--;
  36. i--;
  37. }
  38. }
  39. return data;
  40. }
  41. // constructor
  42. var URI = function(url) {
  43. // TODO: constructor(url, base) see http://dvcs.w3.org/hg/url/raw-file/tip/Overview.html#constructor
  44. // Allow instantiation without the 'new' keyword
  45. if (!(this instanceof URI)) {
  46. return new URI(url);
  47. }
  48. if (url === undefined) {
  49. url = location.href + "";
  50. }
  51. this.href(url);
  52. return this;
  53. },
  54. p = URI.prototype;
  55. // convenience
  56. URI.encode = encodeURIComponent;
  57. URI.decode = decodeURIComponent;
  58. p.encode = URI.encode;
  59. p.decode = URI.decode;
  60. // static properties
  61. URI.idn_expression = /[^a-z0-9\.-]/i;
  62. URI.punycode_expression = /(xn--)/i;
  63. // well, 333.444.555.666 matches, but it sure ain't no IPv4 - do we care?
  64. URI.ip4_expression = /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/;
  65. // credits to Rich Brown
  66. // source: http://forums.intermapper.com/viewtopic.php?p=1096#1096
  67. // specification: http://www.ietf.org/rfc/rfc4291.txt
  68. 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*$/ ;
  69. // gruber revised expression - http://rodneyrehm.de/t/url-regex.html
  70. URI.find_uri_expression = /\b((?:[a-z][\w-]+:(?:\/{1,3}|[a-z0-9%])|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}\/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'".,<>?«»“”‘’]))/ig;
  71. // http://www.iana.org/assignments/uri-schemes.html
  72. // http://en.wikipedia.org/wiki/List_of_TCP_and_UDP_port_numbers#Well-known_ports
  73. URI.defaultPorts = {
  74. http: "80",
  75. https: "443",
  76. ftp: "21"
  77. };
  78. URI.parse = function(string) {
  79. var pos, t, parts = {};
  80. // [protocol"://"[username[":"password]"@"]hostname[":"port]"/"?][path]["?"querystring]["#"fragment]
  81. // extract fragment
  82. pos = string.indexOf('#');
  83. if (pos > -1) {
  84. // escaping?
  85. parts.fragment = string.substring(pos + 1) || null;
  86. string = string.substring(0, pos);
  87. }
  88. // extract query
  89. pos = string.indexOf('?');
  90. if (pos > -1) {
  91. // escaping?
  92. parts.query = string.substring(pos + 1) || null;
  93. string = string.substring(0, pos);
  94. }
  95. // extract protocol
  96. pos = string.indexOf('://');
  97. if (pos > -1) {
  98. parts.protocol = string.substring(0, pos);
  99. string = string.substring(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.substring(0, pos) || null;
  124. parts.port = null;
  125. } else {
  126. t = string.substring(0, pos).split(':');
  127. parts.hostname = t[0] || null;
  128. parts.port = t[1] || null;
  129. }
  130. return string.substring(pos) || '/';
  131. };
  132. URI.parseAuthority = function(string, parts) {
  133. // extract username:password
  134. var pos = string.indexOf('@');
  135. if (pos > -1) {
  136. t = string.substring(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.substring(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. // no "=" is null according to http://dvcs.w3.org/hg/url/raw-file/tip/Overview.html#collect-url-parameters
  156. value = v.length ? URI.decode(v.join('=')) : null;
  157. if (items[name]) {
  158. if (typeof items[name] === "string") {
  159. items[name] = [items[name]];
  160. }
  161. items[name].push(value);
  162. } else {
  163. items[name] = value;
  164. }
  165. }
  166. return items;
  167. };
  168. URI.build = function(parts) {
  169. var t = '';
  170. if (typeof parts.protocol === "string" && parts.protocol.length) {
  171. t += parts.protocol + "://";
  172. }
  173. t += (URI.buildAuthority(parts) || '');
  174. if (typeof parts.path === "string") {
  175. if (parts.path[0] !== '/' && typeof parts.hostname === "string") {
  176. t += '/';
  177. }
  178. t += parts.path;
  179. }
  180. if (typeof parts.query == "string") {
  181. t += '?' + parts.query;
  182. }
  183. if (typeof parts.fragment === "string") {
  184. t += '#' + parts.fragment;
  185. }
  186. return t;
  187. };
  188. URI.buildHost = function(parts) {
  189. var t = '';
  190. if (!parts.hostname) {
  191. return '';
  192. } else if (URI.ip6_expression.test(parts.hostname)) {
  193. if (parts.port) {
  194. t += "[" + parts.hostname + "]:" + parts.port;
  195. } else {
  196. // don't know if we should always wrap IPv6 in []
  197. // the RFC explicitly says SHOULD, not MUST.
  198. t += parts.hostname;
  199. }
  200. } else {
  201. t += parts.hostname;
  202. if (parts.port) {
  203. t += ':' + parts.port;
  204. }
  205. }
  206. return t;
  207. };
  208. URI.buildAuthority = function(parts) {
  209. var t = '';
  210. if (parts.username) {
  211. t += URI.encode(parts.username);
  212. if (parts.password) {
  213. t += ':' + URI.encode(parts.password);
  214. }
  215. t += "@";
  216. }
  217. t += URI.buildHost(parts);
  218. return t;
  219. };
  220. URI.buildQuery = function(data) {
  221. // according to http://tools.ietf.org/html/rfc3986 or http://labs.apache.org/webarch/uri/rfc/rfc3986.html
  222. // unreserved = ALPHA / DIGIT / "-" / "." / "_" / "~"
  223. // pct-encoded = "%" HEXDIG HEXDIG
  224. // sub-delims = "!" / "$" / "&" / "'" / "(" / ")" / "*" / "+" / "," / ";" / "="
  225. // pchar = unreserved / pct-encoded / sub-delims / ":" / "@"
  226. // query = *( pchar / "/" / "?" )
  227. // being »-._~!$&'()*+,;=:@/?« and alnum
  228. // so the query string may contain »-._~!$&'()*+,;=:@/?« unencoded.
  229. // &= have special meaning to paramter delimitation, thus need to be encoded ["&" : "%26", "=" : "%3D"].
  230. // the RFC explicitly states ?/foo being a valid use case, no mention of parameter syntax!
  231. // I have yet to hunt down the specification of ?property1=value1&property2
  232. // pointer: http://en.wikipedia.org/wiki/Query_string
  233. // Note that Wikipedia is nice, but it is NOT a reference
  234. // For the time being a full encodeURIComponent won't hurt (much?)
  235. // https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/encodeURI
  236. // https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/encodeURIComponent
  237. // TODO: figure out if ?key=value is the only allowed syntax (in HTTP)
  238. // TODO: figure out how to proplery encode query string parameters
  239. var t = "";
  240. for (var key in data) {
  241. if (Object.hasOwnProperty.call(data, key)) {
  242. var name = URI.encode(key + "");
  243. if (isArray(data[key])) {
  244. var unique = {};
  245. for (var i = 0, length = data[key].length; i < length; i++) {
  246. if (data[key][i] !== undefined && unique[data[key][i] + ""] === undefined) {
  247. // don't append "=" for null values, according to http://dvcs.w3.org/hg/url/raw-file/tip/Overview.html#url-parameter-serialization
  248. t += "&" + name + (data[key][i] !== null ? "=" + URI.encode(data[key][i] + "") : "");
  249. unique[data[key][i] + ""] = true;
  250. }
  251. }
  252. } else if (data[key] !== undefined) {
  253. // don't append "=" for null values, according to http://dvcs.w3.org/hg/url/raw-file/tip/Overview.html#url-parameter-serialization
  254. t += "&" + name + (data[key] !== null ? "=" + URI.encode(data[key] + "") : "");
  255. }
  256. }
  257. }
  258. return t.substring(1);
  259. };
  260. URI.addQuery = function(data, name, value) {
  261. if (typeof name === "object") {
  262. for (var key in name) {
  263. if (Object.prototype.hasOwnProperty.call(name, key)) {
  264. URI.addQuery(data, key, name[key]);
  265. }
  266. }
  267. } else if (typeof name === "string") {
  268. if (data[name] === undefined) {
  269. data[name] = value;
  270. return;
  271. } else if (typeof data[name] === "string") {
  272. data[name] = [data[name]];
  273. }
  274. if (!isArray(value)) {
  275. value = [value];
  276. }
  277. data[name] = data[name].concat(value);
  278. } else {
  279. throw new TypeError("URI.addQuery() accepts an object, string as the name parameter");
  280. }
  281. };
  282. URI.removeQuery = function(data, name, value) {
  283. if (isArray(name)) {
  284. for (var i = 0, length = name.length; i < length; i++) {
  285. data[name[i]] = undefined;
  286. }
  287. } else if (typeof name === "object") {
  288. for (var key in name) {
  289. if (Object.prototype.hasOwnProperty.call(name, key)) {
  290. URI.removeQuery(data, key, name[key]);
  291. }
  292. }
  293. } else if (typeof name === "string") {
  294. if (value !== undefined) {
  295. if (data[name] === value) {
  296. data[name] = undefined;
  297. } else if (isArray(data[name])) {
  298. data[name] = filterArrayValues(data[name], value);
  299. }
  300. } else {
  301. data[name] = undefined;
  302. }
  303. } else {
  304. throw new TypeError("URI.addQuery() accepts an object, string as the first parameter");
  305. }
  306. };
  307. URI.commonPath = function(one, two) {
  308. var length = Math.min(one.length, two.length),
  309. pos;
  310. // find first non-matching character
  311. for (pos = 0; pos < length; pos++) {
  312. if (one[pos] !== two[pos]) {
  313. pos--;
  314. break;
  315. }
  316. }
  317. if (pos < 1) {
  318. return one[0] === two[0] && one[0] === '/' ? '/' : '';
  319. }
  320. // revert to last /
  321. if (one[pos] !== '/') {
  322. pos = one.substring(0, pos).lastIndexOf('/');
  323. }
  324. return one.substring(0, pos + 1);
  325. };
  326. URI.withinString = function(string, callback) {
  327. // expression used is "gruber revised" (@gruber v2) determined to be the best solution in
  328. // a regex sprint we did a couple of ages ago at
  329. // * http://mathiasbynens.be/demo/url-regex
  330. // * http://rodneyrehm.de/t/url-regex.html
  331. return string.replace(URI.find_uri_expression, callback);
  332. };
  333. p.build = function() {
  334. this._string = URI.build(this._parts);
  335. return this;
  336. };
  337. p.toString = function() {
  338. // return this.build()._string;
  339. return this._string;
  340. };
  341. p.valueOf = function() {
  342. return this.toString();
  343. };
  344. // generate simple accessors
  345. var _parts = {protocol: 'protocol', username: 'username', password: 'password', hostname: 'hostname', port: 'port'},
  346. _part;
  347. for (_part in _parts) {
  348. p[_part] = (function(_part){
  349. return function(v, build) {
  350. if (v === undefined) {
  351. return this._parts[_part] || "";
  352. } else {
  353. this._parts[_part] = v;
  354. build !== false && this.build();
  355. return this;
  356. }
  357. };
  358. })(_parts[_part]);
  359. }
  360. // generate accessors with optionally prefixed input
  361. _parts = {query: '?', fragment: '#'};
  362. for (_part in _parts) {
  363. p[_part] = (function(_part, _key){
  364. return function(v, build) {
  365. if (v === undefined) {
  366. return this._parts[_part] || "";
  367. } else {
  368. if (v !== null) {
  369. v = v + "";
  370. if (v[0] === _key) {
  371. v = v.substring(1);
  372. }
  373. }
  374. this._parts[_part] = v;
  375. build !== false && this.build();
  376. return this;
  377. }
  378. };
  379. })(_part, _parts[_part]);
  380. }
  381. // generate accessors with prefixed output
  382. _parts = {search: ['?', 'query'], hash: ['#', 'fragment']};
  383. for (_part in _parts) {
  384. p[_part] = (function(_part, _key){
  385. return function(v, build) {
  386. var t = this[_part](v, build);
  387. return typeof t === "string" && t.length ? (_key + t) : t;
  388. };
  389. })(_parts[_part][1], _parts[_part][0]);
  390. }
  391. p.pathname = function(v, build) {
  392. if (v === undefined) {
  393. return this._parts.path || "/";
  394. } else {
  395. // TODO: properly escape path segments
  396. this._parts.path = v || "/";
  397. build !== false && this.build();
  398. return this;
  399. }
  400. };
  401. p.path = p.pathname;
  402. p.href = function(href, build) {
  403. if (href === undefined) {
  404. return this.toString();
  405. } else {
  406. this._string = "";
  407. this._parts = {
  408. protocol: null,
  409. username: null,
  410. password: null,
  411. hostname: null,
  412. port: null,
  413. path: null,
  414. query: null,
  415. fragment: null
  416. };
  417. var _URI = href instanceof URI,
  418. _object = typeof href === "object" && (href.hostname || href.path),
  419. key;
  420. if (typeof href === "string") {
  421. this._parts = URI.parse(href);
  422. } else if (_URI || _object) {
  423. var src = _URI ? href._parts : href;
  424. for (key in src) {
  425. if (Object.hasOwnProperty.call(this._parts, key)) {
  426. this._parts[key] = src[key];
  427. }
  428. }
  429. } else {
  430. throw new TypeError("invalid input");
  431. }
  432. build !== false && this.build();
  433. return this;
  434. }
  435. };
  436. // identification accessors
  437. p.is = function(what) {
  438. var ip = false,
  439. ip4 = false,
  440. ip6 = false,
  441. name = false,
  442. idn = false,
  443. punycode = false,
  444. relative = true;
  445. if (this._parts.hostname) {
  446. relative = false;
  447. ip4 = URI.ip4_expression.test(this._parts.hostname);
  448. ip6 = URI.ip6_expression.test(this._parts.hostname);
  449. ip = ip4 || ip6;
  450. name = !ip;
  451. idn = name && URI.idn_expression.test(this._parts.hostname);
  452. punycode = name && URI.punycode_expression.test(this._parts.hostname);
  453. }
  454. switch (what.toLowerCase()) {
  455. case 'relative':
  456. return relative;
  457. // hostname identification
  458. case 'domain':
  459. case 'name':
  460. return name;
  461. case 'ip':
  462. return ip;
  463. case 'ip4':
  464. case 'ipv4':
  465. case 'inet4':
  466. return ip4;
  467. case 'ip6':
  468. case 'ipv6':
  469. case 'inet6':
  470. return ip6;
  471. case 'idn':
  472. return idn;
  473. case 'punycode':
  474. return punycode;
  475. }
  476. return null;
  477. };
  478. // combination accessors
  479. p.host = function(v, build) {
  480. if (v === undefined) {
  481. return this._parts.hostname ? URI.buildHost(this._parts) : "";
  482. } else {
  483. URI.parseHost(v, this._parts);
  484. build !== false && this.build();
  485. return this;
  486. }
  487. };
  488. p.authority = function(v, build) {
  489. if (v === undefined) {
  490. return this._parts.hostname ? URI.buildAuthority(this._parts) : "";
  491. } else {
  492. URI.parseAuthority(v, this._parts);
  493. build !== false && this.build();
  494. return this;
  495. }
  496. };
  497. // fraction accessors
  498. p.domain = function(v, build) {
  499. // convenience, return "example.org" from "www.example.org"
  500. if (v === undefined) {
  501. if (!this._parts.hostname || this.is('IP')) {
  502. return "";
  503. }
  504. // "localhost" is a domain, too
  505. return this._parts.hostname.match(/\.?([^\.]+.[^\.]+)$/)[1] || this._parts.hostname;
  506. } else {
  507. if (!v) {
  508. throw new TypeError("cannot set domain empty");
  509. } else if (!this._parts.hostname || this.is('IP')) {
  510. this._parts.hostname = v;
  511. } else {
  512. var replace = new RegExp(escapeRegEx(this.domain()) + "$");
  513. this._parts.hostname = this._parts.hostname.replace(replace, v);
  514. }
  515. build !== false && this.build();
  516. return this;
  517. }
  518. };
  519. p.tld = function(v, build) {
  520. // return "org" from "www.example.org"
  521. if (v === undefined) {
  522. if (!this._parts.hostname || this.is('IP')) {
  523. return "";
  524. }
  525. var pos = this._parts.hostname.lastIndexOf('.');
  526. return this._parts.hostname.substring(pos + 1);
  527. } else {
  528. if (!v) {
  529. throw new TypeError("cannot set TLD empty");
  530. } else if (!this._parts.hostname || this.is('IP')) {
  531. throw new ReferenceError("cannot set TLD on non-domain host");
  532. } else {
  533. var replace = new RegExp(escapeRegEx(this.tld()) + "$");
  534. this._parts.hostname = this._parts.hostname.replace(replace, v);
  535. }
  536. build !== false && this.build();
  537. return this;
  538. }
  539. };
  540. p.directory = function(v, build) {
  541. if (v === undefined) {
  542. if (!this._parts.path || this._parts.path === '/') {
  543. return '/';
  544. }
  545. var end = this._parts.path.length - this.filename().length - 1;
  546. return this._parts.path.substring(0, end);
  547. } else {
  548. var e = this._parts.path.length - this.filename().length,
  549. directory = this._parts.path.substring(0, e),
  550. replace = new RegExp('^' + escapeRegEx(directory));
  551. // fully qualifier directories begin with a slash
  552. if (!this.is('relative')) {
  553. if (!v) {
  554. v = '/';
  555. }
  556. if (v[0] !== '/') {
  557. v = "/" + v;
  558. }
  559. }
  560. // directories always end with a slash
  561. if (v && v[v.length - 1] !== '/') {
  562. v += '/';
  563. }
  564. this._parts.path = this._parts.path.replace(replace, v);
  565. build !== false && this.build();
  566. return this;
  567. }
  568. };
  569. p.filename = function(v, build) {
  570. if (v === undefined) {
  571. if (!this._parts.path || this._parts.path === '/') {
  572. return "";
  573. }
  574. var pos = this._parts.path.lastIndexOf('/');
  575. return this._parts.path.substring(pos+1);
  576. } else {
  577. if (v[0] === '/') {
  578. v = v.substring(1);
  579. }
  580. var replace = new RegExp(escapeRegEx(this.filename()) + "$");
  581. this._parts.path = this._parts.path.replace(replace, v);
  582. build !== false && this.build();
  583. return this;
  584. }
  585. };
  586. p.suffix = function(v, build) {
  587. if (v === undefined) {
  588. if (!this._parts.path || this._parts.path === '/') {
  589. return "";
  590. }
  591. var filename = this.filename(),
  592. pos = filename.lastIndexOf('.'),
  593. s;
  594. if (pos === -1) {
  595. return "";
  596. }
  597. // suffix may only contain alnum characters (yup, I made this up.)
  598. s = filename.substring(pos+1);
  599. return (/^[a-z0-9]+$/i).test(s) ? s : "";
  600. } else {
  601. if (v[0] === '.') {
  602. v = v.substring(1);
  603. }
  604. var suffix = this.suffix(),
  605. replace;
  606. if (!suffix) {
  607. if (!v) {
  608. return this;
  609. }
  610. this._parts.path += '.' + v;
  611. } else if (!v) {
  612. replace = new RegExp(escapeRegEx("." + suffix) + "$");
  613. } else {
  614. replace = new RegExp(escapeRegEx(suffix) + "$");
  615. }
  616. if (replace) {
  617. this._parts.path = this._parts.path.replace(replace, v);
  618. }
  619. build !== false && this.build();
  620. return this;
  621. }
  622. };
  623. // mutating query string
  624. var q = p.query;
  625. p.query = function(v, build) {
  626. if (v === true) {
  627. return URI.parseQuery(this._parts.query);
  628. } else if (v !== undefined && typeof v !== "string") {
  629. this._parts.query = URI.buildQuery(v);
  630. build !== false && this.build();
  631. return this;
  632. } else {
  633. return q.call(this, v, build);
  634. }
  635. };
  636. p.addQuery = function(name, value, build) {
  637. var data = URI.parseQuery(this._parts.query);
  638. URI.addQuery(data, name, value);
  639. this._parts.query = URI.buildQuery(data);
  640. if (typeof name !== "string") {
  641. build = value;
  642. }
  643. build !== false && this.build();
  644. return this;
  645. };
  646. p.removeQuery = function(name, value, build) {
  647. var data = URI.parseQuery(this._parts.query);
  648. URI.removeQuery(data, name, value);
  649. this._parts.query = URI.buildQuery(data);
  650. if (typeof name !== "string") {
  651. build = value;
  652. }
  653. build !== false && this.build();
  654. return this;
  655. };
  656. p.addSearch = p.addQuery;
  657. p.removeSearch = p.removeQuery;
  658. // sanitizing URLs
  659. p.normalize = function() {
  660. return this
  661. .normalizeProtocol(false)
  662. .normalizeHostname(false)
  663. .normalizePort(false)
  664. .normalizePath(false)
  665. .normalizeQuery(false)
  666. .normalizeFragment(false)
  667. .build();
  668. };
  669. p.normalizeProtocol = function(build) {
  670. if (typeof this._parts.protocol === "string") {
  671. this._parts.protocol = this._parts.protocol.toLowerCase();
  672. build !== false && this.build();
  673. }
  674. return this;
  675. };
  676. p.normalizeHostname = function(build) {
  677. if (this._parts.hostname) {
  678. if (this.is('IDN') && window.punycode) {
  679. this._parts.hostname = punycode.toASCII(this._parts.hostname);
  680. } else if (this.is('IPv6') && window.IPv6) {
  681. this._parts.hostname = IPv6.best(this._parts.hostname);
  682. }
  683. this._parts.hostname = this._parts.hostname.toLowerCase();
  684. build !== false && this.build();
  685. }
  686. return this;
  687. };
  688. p.normalizePort = function(build) {
  689. // remove port of it's the protocol's default
  690. if (typeof this._parts.protocol === "string" && this._parts.port === URI.defaultPorts[this._parts.protocol]) {
  691. this._parts.port = null;
  692. build !== false && this.build();
  693. }
  694. return this;
  695. };
  696. p.normalizePath = function(build) {
  697. if (!this._parts.path || this._parts.path === '/') {
  698. return this;
  699. }
  700. var _was_relative,
  701. _was_relative_prefix,
  702. _path = this._parts.path,
  703. _parent, _pos;
  704. // handle relative paths
  705. if (_path[0] !== '/') {
  706. if (_path[0] === '.') {
  707. _was_relative_prefix = _path.substring(0, _path.indexOf('/'));
  708. }
  709. _was_relative = true;
  710. _path = '/' + _path;
  711. }
  712. // resolve simples
  713. _path = _path.replace(/(\/(\.\/)+)|\/{2,}/g, '/');
  714. // resolve parents
  715. while (true) {
  716. _parent = _path.indexOf('/../');
  717. if (_parent === -1) {
  718. // no more ../ to resolve
  719. break;
  720. } else if (_parent === 0) {
  721. // top level cannot be relative...
  722. _path = _path.substring(3);
  723. break;
  724. }
  725. _pos = _path.substring(0, _parent).lastIndexOf('/');
  726. if (_pos === -1) {
  727. _pos = _parent;
  728. }
  729. _path = _path.substring(0, _pos) + _path.substring(_parent + 3);
  730. }
  731. // revert to relative
  732. if (_was_relative && this.is('relative')) {
  733. if (_was_relative_prefix){
  734. _path = _was_relative_prefix + _path;
  735. } else {
  736. _path = _path.substring(1);
  737. }
  738. }
  739. // TODO: normalize pathname elements: /%7Esmith/home.html -> /~smith/home.html
  740. // see http://labs.apache.org/webarch/uri/rfc/rfc3986.html#path
  741. this._parts.path = _path;
  742. build !== false && this.build();
  743. return this;
  744. };
  745. p.normalizePathname = p.normalizePath;
  746. p.normalizeQuery = function(build) {
  747. if (typeof this._parts.query === "string") {
  748. if (!this._parts.query.length) {
  749. this._parts.query = null;
  750. } else {
  751. this.query(URI.parseQuery(this._parts.query));
  752. }
  753. build !== false && this.build();
  754. }
  755. return this;
  756. };
  757. p.normalizeFragment = function(build) {
  758. if (!this._parts.fragment) {
  759. this._parts.fragment = null;
  760. build !== false && this.build();
  761. }
  762. return this;
  763. };
  764. p.normalizeSearch = p.normalizeQuery;
  765. p.normalizeHash = p.normalizeFragment;
  766. // resolving relative and absolute URLs
  767. p.absoluteTo = function(base) {
  768. if (!this.is('relative')) {
  769. throw new Error('Cannot resolve non-relative URL');
  770. }
  771. if (!(base instanceof URI)) {
  772. base = new URI(base);
  773. }
  774. var resolved = new URI(this),
  775. properties = ['protocol', 'username', 'password', 'hostname', 'port'];
  776. for (var i = 0, p; p = properties[i]; i++) {
  777. resolved._parts[p] = base._parts[p];
  778. }
  779. if (resolved.path()[0] !== '/') {
  780. resolved._parts.path = base.directory() + '/' + resolved._parts.path;
  781. resolved.normalizePath();
  782. }
  783. resolved.build();
  784. return resolved;
  785. };
  786. p.relativeTo = function(base) {
  787. if (!(base instanceof URI)) {
  788. base = new URI(base);
  789. }
  790. if (this.path()[0] !== '/' || base.path()[0] !== '/') {
  791. throw new Error('Cannot calculate common path from non-relative URLs');
  792. }
  793. var relative = new URI(this),
  794. properties = ['protocol', 'username', 'password', 'hostname', 'port'],
  795. common = URI.commonPath(relative.path(), base.path()),
  796. _base = base.directory();
  797. for (var i = 0, p; p = properties[i]; i++) {
  798. relative._parts[p] = null;
  799. }
  800. if (!common || common === '/') {
  801. return relative;
  802. }
  803. if (_base + '/' === common) {
  804. relative._parts.path = './' + relative.filename();
  805. } else {
  806. var parents = '../',
  807. _common = new RegExp('^' + escapeRegEx(common)),
  808. _parents = _base.replace(_common, '/').match(/\//g).length -1;
  809. while (_parents--) {
  810. parents += '../';
  811. }
  812. relative._parts.path = relative._parts.path.replace(_common, parents);
  813. }
  814. relative.build();
  815. return relative;
  816. };
  817. window.URI = URI;
  818. })();