|
2 | 2 |
|
3 | 3 | var required = require('requires-port')
|
4 | 4 | , qs = require('querystringify')
|
| 5 | + , slashes = /^[A-Za-z][A-Za-z0-9+-.]*:\/\// |
5 | 6 | , protocolre = /^([a-z][a-z0-9.+-]*:)?(\/\/)?([\S\s]*)/i
|
6 |
| - , slashes = /^[A-Za-z][A-Za-z0-9+-.]*:\/\//; |
| 7 | + , whitespace = '[\\x09\\x0A\\x0B\\x0C\\x0D\\x20\\xA0\\u1680\\u180E\\u2000\\u2001\\u2002\\u2003\\u2004\\u2005\\u2006\\u2007\\u2008\\u2009\\u200A\\u202F\\u205F\\u3000\\u2028\\u2029\\uFEFF]' |
| 8 | + , left = new RegExp('^'+ whitespace +'+'); |
| 9 | + |
| 10 | +/** |
| 11 | + * Trim a given string. |
| 12 | + * |
| 13 | + * @param {String} str String to trim. |
| 14 | + * @public |
| 15 | + */ |
| 16 | +function trimLeft(str) { |
| 17 | + return (str || '').replace(left, ''); |
| 18 | +} |
7 | 19 |
|
8 | 20 | /**
|
9 | 21 | * These are the parse rules for the URL parser, it informs the parser
|
@@ -102,6 +114,7 @@ function lolcation(loc) {
|
102 | 114 | * @private
|
103 | 115 | */
|
104 | 116 | function extractProtocol(address) {
|
| 117 | + address = trimLeft(address); |
105 | 118 | var match = protocolre.exec(address);
|
106 | 119 |
|
107 | 120 | return {
|
@@ -162,6 +175,8 @@ function resolve(relative, base) {
|
162 | 175 | * @private
|
163 | 176 | */
|
164 | 177 | function Url(address, location, parser) {
|
| 178 | + address = trimLeft(address); |
| 179 | + |
165 | 180 | if (!(this instanceof Url)) {
|
166 | 181 | return new Url(address, location, parser);
|
167 | 182 | }
|
@@ -429,6 +444,7 @@ Url.prototype = { set: set, toString: toString };
|
429 | 444 | //
|
430 | 445 | Url.extractProtocol = extractProtocol;
|
431 | 446 | Url.location = lolcation;
|
| 447 | +Url.trimLeft = trimLeft; |
432 | 448 | Url.qs = qs;
|
433 | 449 |
|
434 | 450 | module.exports = Url;
|
0 commit comments