Requires

Provides

URI.js

Provides methods useful in managing the window location and uris.

License:
MIT-style license
Authors:
Sebastian Markbåge, Aaron Newton
  1. 28
  2. 29
  3. 30
  4. 31
  5. 32
var URI = new Class({ Implements: Options, options: {

base: false

  1. 34
  2. 35
  3. 36
  4. 37
  5. 38
  6. 39
  7. 40
  8. 41
  9. 42
  10. 43
  11. 44
  12. 45
  13. 46
  14. 47
  15. 48
  16. 49
  17. 50
  18. 51
  19. 52
  20. 53
  21. 54
  22. 55
  23. 56
  24. 57
  25. 58
  26. 59
  27. 60
  28. 61
  29. 62
  30. 63
  31. 64
  32. 65
  33. 66
  34. 67
  35. 68
  36. 69
  37. 70
  38. 71
  39. 72
  40. 73
  41. 74
  42. 75
  43. 76
  44. 77
  45. 78
  46. 79
  47. 80
  48. 81
  49. 82
  50. 83
  51. 84
  52. 85
  53. 86
  54. 87
  55. 88
  56. 89
  57. 90
  58. 91
  59. 92
  60. 93
  61. 94
  62. 95
  63. 96
  64. 97
  65. 98
  66. 99
  67. 100
  68. 101
  69. 102
  70. 103
  71. 104
  72. 105
  73. 106
  74. 107
  75. 108
  76. 109
  77. 110
  78. 111
  79. 112
  80. 113
  81. 114
  82. 115
  83. 116
  84. 117
  85. 118
  86. 119
  87. 120
  88. 121
  89. 122
  90. 123
  91. 124
  92. 125
  93. 126
  94. 127
  95. 128
  96. 129
  97. 130
  98. 131
  99. 132
  100. 133
  101. 134
  102. 135
  103. 136
  104. 137
  105. 138
  106. 139
  107. 140
  108. 141
  109. 142
  110. 143
  111. 144
  112. 145
  113. 146
  114. 147
  115. 148
  116. 149
  117. 150
  118. 151
  119. 152
  120. 153
  121. 154
  122. 155
  123. 156
  124. 157
  125. 158
  126. 159
  127. 160
  128. 161
  129. 162
}, regex: /^(?:(\w+):)?(?:\/\/(?:(?:([^:@\/]*):?([^:@\/]*))?@)?([^:\/?#]*)(?::(\d*))?)?(\.\.?$|(?:[^?#\/]*\/)*)([^?#]*)(?:\?([^#]*))?(?:#(.*))?/, parts: ['scheme', 'user', 'password', 'host', 'port', 'directory', 'file', 'query', 'fragment'], schemes: {http: 80, https: 443, ftp: 21, rtsp: 554, mms: 1755, file: 0}, initialize: function(uri, options){ this.setOptions(options); var base = this.options.base || URI.base; if (!uri) uri = base; if (uri && uri.parsed) this.parsed = Object.clone(uri.parsed); else this.set('value', uri.href || uri.toString(), base ? new URI(base) : false); }, parse: function(value, base){ var bits = value.match(this.regex); if (!bits) return false; bits.shift(); return this.merge(bits.associate(this.parts), base); }, merge: function(bits, base){ if ((!bits || !bits.scheme) && (!base || !base.scheme)) return false; if (base){ this.parts.every(function(part){ if (bits[part]) return false; bits[part] = base[part] || ''; return true; }); } bits.port = bits.port || this.schemes[bits.scheme.toLowerCase()]; bits.directory = bits.directory ? this.parseDirectory(bits.directory, base ? base.directory : '') : '/'; return bits; }, parseDirectory: function(directory, baseDirectory){ directory = (directory.substr(0, 1) == '/' ? '' : (baseDirectory || '/')) + directory; if (!directory.test(URI.regs.directoryDot)) return directory; var result = []; directory.replace(URI.regs.endSlash, '').split('/').each(function(dir){ if (dir == '..' && result.length > 0) result.pop(); else if (dir != '.') result.push(dir); }); return result.join('/') + '/'; }, combine: function(bits){ return bits.value || bits.scheme + '://' + (bits.user ? bits.user + (bits.password ? ':' + bits.password : '') + '@' : '') + (bits.host || '') + (bits.port && bits.port != this.schemes[bits.scheme] ? ':' + bits.port : '') + (bits.directory || '/') + (bits.file || '') + (bits.query ? '?' + bits.query : '') + (bits.fragment ? '#' + bits.fragment : ''); }, set: function(part, value, base){ if (part == 'value'){ var scheme = value.match(URI.regs.scheme); if (scheme) scheme = scheme[1]; if (scheme && this.schemes[scheme.toLowerCase()] == null) this.parsed = { scheme: scheme, value: value }; else this.parsed = this.parse(value, (base || this).parsed) || (scheme ? { scheme: scheme, value: value } : { value: value }); } else if (part == 'data'){ this.setData(value); } else { this.parsed[part] = value; } return this; }, get: function(part, base){ switch(part){ case 'value': return this.combine(this.parsed, base ? base.parsed : false); case 'data' : return this.getData(); } return this.parsed[part] || ''; }, go: function(){ document.location.href = this.toString(); }, toURI: function(){ return this; }, getData: function(key, part){ var qs = this.get(part || 'query'); if (!(qs || qs === 0)) return key ? null : {}; var obj = qs.parseQueryString(); return key ? obj[key] : obj; }, setData: function(values, merge, part){ if (typeof values == 'string'){ data = this.getData(); data[arguments[0]] = arguments[1]; values = data; } else if (merge){ values = Object.merge(this.getData(), values); } return this.set(part || 'query', Hash.toQueryString(values)); }, clearData: function(part){ return this.set(part || 'query', ''); } }); URI.prototype.toString = URI.prototype.valueOf = function(){ return this.get('value'); }; URI.regs = { endSlash: /\/$/, scheme: /^(\w+):/, directoryDot: /\.\/|\.$/ }; URI.base = new URI(Array.from(document.getElements('base[href]', true)).getLast(), {base: document.location}); String.implement({ toURI: function(options){ return new URI(this, options); } });