urlparse - Python-like URL parser and manipulator


/ Published in: JavaScript
Save to your folder(s)

Split and manipulate URLs easily with this module. It has similar interface to Python's urlparse library. This is patched and modified version from Denis's orignal implementation.


Copy this code and paste it in your HTML
  1. /**
  2. * @projectDescription Poly9's polyvalent URLParser class
  3. *
  4. * Modified to URL reconstruction by Mikko Ohtamaa.
  5. *
  6. * @authorDenis Laprise <[email protected]>, Mikko Ohtamaa <[email protected]>
  7. *
  8. * @version 0.2
  9. * @namespace Poly9
  10. *
  11. * How to query URL:
  12. *
  13. * <pre>
  14. * var p = new Poly9.URLParser('http://user:[email protected]/pathname?arguments=1#fragment');
  15. * p.getHost() == 'poly9.com';
  16. * p.getProtocol() == 'http';
  17. * p.getPathname() == '/pathname';
  18. * p.getQuerystring() == 'arguments=1';
  19. * p.getFragment() == 'fragment';
  20. * p.getUsername() == 'user';
  21. * p.getPassword() == 'password';
  22. * </pre>
  23. *
  24. * How to manipulate URL:
  25. *
  26. * <pre>
  27. * var p = new Poly9.URLParser("http://localhost:8080/path);
  28. *
  29. * p.setQuerystring("foo=bar");
  30. * p.setFragment("anchor");
  31. * p.setPort(7070);
  32. *
  33. * var url = p.getURL() // http://localhost:7070/path?foo=bar#anchor
  34. * </pre>
  35. *
  36. * See the unit test file for more examples.
  37. * URLParser is freely distributable under the terms of an MIT-style license.
  38. *
  39. * The orignal code: https://code.poly9.com/trac/browser/urlparser/urlparser.js
  40. *
  41. * http://poly9.com
  42. *
  43. * http://www.twinapex.com
  44. */
  45.  
  46. if (typeof Poly9 == 'undefined')
  47. var Poly9 = {};
  48.  
  49. /**
  50.  * Creates an URLParser instance
  51.  *
  52.  * @classDescription Creates an URLParser instance
  53.  * @return {Object} return an URLParser object
  54.  * @param {String} url The url to parse
  55.  * @constructor
  56.  * @exception {String} Throws an exception if the specified url is invalid
  57.  */
  58. Poly9.URLParser = function(url) {
  59. this._fields = {'Username' : 4, 'Password' : 5, 'Port' : 7, 'Protocol' : 2, 'Host' : 6, 'Pathname' : 8, 'URL' : 0, 'Querystring' : 9, 'Fragment' : 10};
  60. this._values = {};
  61. this._regex = null;
  62. this.version = 0.2;
  63.  
  64. // MO: Fixed fragments are allowed to contain dash
  65. this._regex = /^((\w+):\/\/)?((\w+):?(\w+)?@)?([^\/\?:]+):?(\d+)?(\/?[^\?#]+)?\??([^#]+)?#?([^#]*)/;
  66. for (var f in this._fields) {
  67. this['get' + f] = this._makeGetter(f);
  68. this['set' + f] = this._makeSetter(f);
  69. }
  70.  
  71. // Patch in getURL, since otherwise field generators override this
  72. this.getURL = this._getURL;
  73.  
  74. if (typeof url != 'undefined')
  75. this._parse(url);
  76. }
  77.  
  78. /**
  79.  * @method
  80.  * @param {String} url The url to parse
  81.  * @exception {String} Throws an exception if the specified url is invalid
  82.  */
  83. Poly9.URLParser.prototype.setURL = function(url) {
  84. this._parse(url);
  85. }
  86.  
  87. /**
  88.  * Rebuild the URL to string form
  89.  *
  90.  * @method
  91.  * @return {String} Reconstructed full URL
  92.  */
  93. Poly9.URLParser.prototype._getURL = function() {
  94. var s;
  95.  
  96. s = this.getProtocol();
  97.  
  98. s += "://"
  99.  
  100. if(this.getUsername() != '') {
  101. s += this.getUsername();
  102. }
  103.  
  104. if(this.getPassword() != '') {
  105. s += ":";
  106. s += this.getPassword();
  107. }
  108.  
  109. if(this.getUsername() != '') {
  110. s += "@";
  111. }
  112.  
  113. s += this.getHost();
  114.  
  115. if(this.getPort() != '') {
  116. s += ":" + this.getPort();
  117. }
  118.  
  119. s += this.getPathname();
  120.  
  121. if(this.getQuerystring() != '') {
  122. s += "?" + this.getQuerystring();
  123. }
  124.  
  125. if(this.getFragment() != '') {
  126. s += "#" + this.getFragment();
  127. }
  128.  
  129. return s;
  130. }
  131.  
  132. Poly9.URLParser.prototype._initValues = function() {
  133. for(var f in this._fields)
  134. this._values[f] = '';
  135. }
  136.  
  137. Poly9.URLParser.prototype._parse = function(url) {
  138. this._initValues();
  139. var r = this._regex.exec(url);
  140. if (!r) throw "DPURLParser::_parse -> Invalid URL"
  141. for(var f in this._fields) if (typeof r[this._fields[f]] != 'undefined')
  142. this._values[f] = r[this._fields[f]];
  143. }
  144.  
  145. Poly9.URLParser.prototype._makeGetter = function(field) {
  146. return function() {
  147. return this._values[field];
  148. }
  149. }
  150.  
  151. Poly9.URLParser.prototype._makeSetter = function(field) {
  152. return function(value) {
  153. this._values[field] = value;
  154. }
  155. }

URL: http://blog.redinnovation.com/2008/11/29/python-like-urlparser-module-for-javascript/

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.