PageRenderTime 38ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/web/js/dojo/src/uri/Uri.js

#
JavaScript | 113 lines | 77 code | 20 blank | 16 comment | 55 complexity | a4cf33fbc4f1b7da9359f9e433c729e0 MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-2.0
  1. dojo.provide("dojo.uri.Uri");
  2. dojo.uri = new function() {
  3. this.dojoUri = function (/*dojo.uri.Uri||String*/uri) {
  4. // summary: returns a Uri object resolved relative to the dojo root
  5. return new dojo.uri.Uri(dojo.hostenv.getBaseScriptUri(), uri);
  6. }
  7. this.moduleUri = function(/*String*/module, /*dojo.uri.Uri||String*/uri){
  8. // summary: returns a Uri object relative to a module
  9. // description: Examples: dojo.uri.moduleUri("dojo.widget","templates/template.html"), or dojo.uri.moduleUri("acme","images/small.png")
  10. var loc = dojo.hostenv.getModuleSymbols(module).join('/');
  11. if(!loc){
  12. return null;
  13. }
  14. if(loc.lastIndexOf("/") != loc.length-1){
  15. loc += "/";
  16. }
  17. //If the path is an absolute path (starts with a / or is on another domain/xdomain)
  18. //then don't add the baseScriptUri.
  19. var colonIndex = loc.indexOf(":");
  20. var slashIndex = loc.indexOf("/");
  21. if(loc.charAt(0) != "/" && (colonIndex == -1 || colonIndex > slashIndex)){
  22. loc = dojo.hostenv.getBaseScriptUri() + loc;
  23. }
  24. return new dojo.uri.Uri(loc,uri);
  25. }
  26. this.Uri = function (/*dojo.uri.Uri||String...*/) {
  27. // summary: Constructor to create an object representing a URI.
  28. // description:
  29. // Each argument is evaluated in order relative to the next until
  30. // a canonical uri is produced. To get an absolute Uri relative
  31. // to the current document use
  32. // new dojo.uri.Uri(document.baseURI, uri)
  33. // TODO: support for IPv6, see RFC 2732
  34. // resolve uri components relative to each other
  35. var uri = arguments[0];
  36. for (var i = 1; i < arguments.length; i++) {
  37. if(!arguments[i]) { continue; }
  38. // Safari doesn't support this.constructor so we have to be explicit
  39. var relobj = new dojo.uri.Uri(arguments[i].toString());
  40. var uriobj = new dojo.uri.Uri(uri.toString());
  41. if ((relobj.path=="")&&(relobj.scheme==null)&&(relobj.authority==null)&&(relobj.query==null)) {
  42. if (relobj.fragment != null) { uriobj.fragment = relobj.fragment; }
  43. relobj = uriobj;
  44. } else if (relobj.scheme == null) {
  45. relobj.scheme = uriobj.scheme;
  46. if (relobj.authority == null) {
  47. relobj.authority = uriobj.authority;
  48. if (relobj.path.charAt(0) != "/") {
  49. var path = uriobj.path.substring(0,
  50. uriobj.path.lastIndexOf("/") + 1) + relobj.path;
  51. var segs = path.split("/");
  52. for (var j = 0; j < segs.length; j++) {
  53. if (segs[j] == ".") {
  54. if (j == segs.length - 1) { segs[j] = ""; }
  55. else { segs.splice(j, 1); j--; }
  56. } else if (j > 0 && !(j == 1 && segs[0] == "") &&
  57. segs[j] == ".." && segs[j-1] != "..") {
  58. if (j == segs.length - 1) { segs.splice(j, 1); segs[j - 1] = ""; }
  59. else { segs.splice(j - 1, 2); j -= 2; }
  60. }
  61. }
  62. relobj.path = segs.join("/");
  63. }
  64. }
  65. }
  66. uri = "";
  67. if (relobj.scheme != null) { uri += relobj.scheme + ":"; }
  68. if (relobj.authority != null) { uri += "//" + relobj.authority; }
  69. uri += relobj.path;
  70. if (relobj.query != null) { uri += "?" + relobj.query; }
  71. if (relobj.fragment != null) { uri += "#" + relobj.fragment; }
  72. }
  73. this.uri = uri.toString();
  74. // break the uri into its main components
  75. var regexp = "^(([^:/?#]+):)?(//([^/?#]*))?([^?#]*)(\\?([^#]*))?(#(.*))?$";
  76. var r = this.uri.match(new RegExp(regexp));
  77. this.scheme = r[2] || (r[1] ? "" : null);
  78. this.authority = r[4] || (r[3] ? "" : null);
  79. this.path = r[5]; // can never be undefined
  80. this.query = r[7] || (r[6] ? "" : null);
  81. this.fragment = r[9] || (r[8] ? "" : null);
  82. if (this.authority != null) {
  83. // server based naming authority
  84. regexp = "^((([^:]+:)?([^@]+))@)?([^:]*)(:([0-9]+))?$";
  85. r = this.authority.match(new RegExp(regexp));
  86. this.user = r[3] || null;
  87. this.password = r[4] || null;
  88. this.host = r[5];
  89. this.port = r[7] || null;
  90. }
  91. this.toString = function(){ return this.uri; }
  92. }
  93. };