Advertisement
Guest User

Untitled

a guest
Jul 25th, 2016
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.58 KB | None | 0 0
  1. (function(URL, parent){
  2.  
  3. var initialConstructor = parent.URL;
  4.  
  5. var urlPattern = new RegExp(
  6. "^" +
  7. //protocol
  8. "(?:((?:\\w+):)?/?/?)" +
  9. //user:pass
  10. "(?:(?:([^:]*)(?::([^@]*))?)?@)?" +
  11. //host
  12. "(" +
  13. //hostname
  14. "([^:/?#]+)" +
  15. // port
  16. "(?:[:](\\d+))?" +
  17. ")" +
  18. //pathname
  19. "([/][^?]*)?" +
  20. //search
  21. "([?][^#]*)?" +
  22. //hash
  23. "(#[^$]*)?" +
  24. "$", "i"
  25. );
  26.  
  27. function URL(urlString, base){
  28.  
  29. initialConstructor.apply(this, arguments);
  30.  
  31. if(base instanceof URL){
  32. urlString = base.href + '/' + urlString;
  33. }else if(base){
  34. urlString = base + '/' + urlString;
  35. }
  36.  
  37. urlString = urlString.trim();
  38.  
  39. var urlMatch = urlString.match(urlPattern);
  40.  
  41. if(!urlMatch){
  42. throw new TypeError('Failed to construct \'URL\': Invalid URL');
  43. }
  44.  
  45. //replace matched undefined/null with ""
  46. for(var i = 0; i < urlMatch.length; i++){
  47. urlMatch[i] = urlMatch[i] == undefined ? "" : urlMatch[i];
  48. }
  49.  
  50. this.hash = urlMatch[9];
  51. this.host = urlMatch[4];
  52. this.hostname = urlMatch[5];
  53. this.href = urlString;
  54. this.origin = urlMatch[1] + '//' + urlMatch[4];
  55. this.username = urlMatch[2];
  56. this.password = urlMatch[3];
  57. this.pathname = urlMatch[7];
  58. this.port = urlMatch[6];
  59. this.protocol = urlMatch[1];
  60. this.search = urlMatch[8];
  61.  
  62. }
  63.  
  64. parent.URL = URL;
  65.  
  66. URL.prototype = initialConstructor.prototype;
  67.  
  68. URL.createObjectURL = function(){
  69. console.error('URL.createObjectURL() is not supported');
  70. }
  71.  
  72. URL.revokeObjectURL = function(){
  73. console.error('URL.revokeObjectURL() is not supported');
  74. }
  75.  
  76. URL.prototype.toString = function(){
  77. return this.href;
  78. }
  79.  
  80. })
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement