Skip to content
This repository was archived by the owner on Jun 6, 2019. It is now read-only.

Commit cf5d82e

Browse files
committed
Merge pull request #9 from jokeyrhyme/origin-property
fixes #8 by implementing the `origin` property
2 parents 35e0e6a + e2978a6 commit cf5d82e

File tree

3 files changed

+78
-53
lines changed

3 files changed

+78
-53
lines changed

url.js

+24
Original file line numberDiff line numberDiff line change
@@ -557,6 +557,30 @@
557557
if ('#' == hash[0])
558558
hash = hash.slice(1);
559559
parse.call(this, hash, 'fragment');
560+
},
561+
562+
get origin() {
563+
var host;
564+
if (this._isInvalid || !this._scheme) {
565+
return '';
566+
}
567+
// javascript: Gecko returns String(""), WebKit/Blink String("null")
568+
// Gecko throws error for "data://"
569+
// data: Gecko returns "", Blink returns "data://", WebKit returns "null"
570+
// Gecko returns String("") for file: mailto:
571+
// WebKit/Blink returns String("SCHEME://") for file: mailto:
572+
switch (this._scheme) {
573+
case 'data':
574+
case 'file':
575+
case 'javascript':
576+
case 'mailto':
577+
return 'null';
578+
}
579+
host = this.host;
580+
if (!host) {
581+
return '';
582+
}
583+
return this._scheme + '://' + host;
560584
}
561585
};
562586

urltestparser.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
function URLTestParser(input) {
22
var relativeSchemes = ["ftp", "file", "gopher", "http", "https", "ws", "wss"],
33
tokenMap = { "\\": "\\", n: "\n", r: "\r", s: " ", t: "\t", f: "\f" }
4-
resultMap = { s: "scheme", u: "username", pass: "password", h: "host", port: "port", p: "path", q: "query", f: "fragment" },
4+
resultMap = { s: "scheme", u: "username", pass: "password", h: "host", port: "port", p: "path", q: "query", f: "fragment", o: "origin" },
55
results = []
66
function Test() {
77
this.input = ""
@@ -14,6 +14,7 @@ function URLTestParser(input) {
1414
this.path = ""
1515
this.query = ""
1616
this.fragment = ""
17+
this.origin = ""
1718
Object.defineProperties(this, {
1819
"href": { get: function() { return !this.scheme ? this.input : this.protocol + (relativeSchemes.indexOf(this.scheme) != -1 ? "//" + (("" != this.username || null != this.password) ? this.username + (null != this.password ? ":" + this.password : "") + "@" : "") + this.host : "") + (this.port ? ":" + this.port : "") + this.path + this.query + this.fragment } },
1920
"protocol": { get: function() { return this.scheme + ":" } },

urltests.txt

+52-52
Original file line numberDiff line numberDiff line change
@@ -156,69 +156,69 @@ http://example.com/\uFEFF/foo s:http h:example.com p:/%EF%BB%BF/foo
156156
http://example.com/\u202E/foo/\u202D/bar s:http h:example.com p:/%E2%80%AE/foo/%E2%80%AD/bar
157157

158158
# Based on http://trac.webkit.org/browser/trunk/LayoutTests/fast/url/script-tests/relative.js
159-
http://www.google.com/foo?bar=baz# about:blank s:http h:www.google.com p:/foo q:?bar=baz f:#
160-
http://www.google.com/foo?bar=baz#\s\u00BB s:http h:www.google.com p:/foo q:?bar=baz f:#\s\u00BB
161-
http://[www.google.com]/ s:http h:[www.google.com] p:/
159+
http://www.google.com/foo?bar=baz# about:blank s:http h:www.google.com p:/foo q:?bar=baz f:# o:http://www.google.com
160+
http://www.google.com/foo?bar=baz#\s\u00BB s:http h:www.google.com p:/foo q:?bar=baz f:#\s\u00BB o:http://www.google.com
161+
http://[www.google.com]/ s:http h:[www.google.com] p:/ o:http://[www.google.com]
162162
http://www.google.com s:http h:www.google.com p:/
163163
http://192.0x00A80001 s:http h:192.0x00a80001 p:/
164164
http://www/foo%2Ehtml s:http h:www p:/foo%2Ehtml
165165
http://www/foo/%2E/html s:http h:www p:/foo/html
166166
http://user:pass@/
167167
http://%25DOMAIN:[email protected]/ s:http u:%25DOMAIN pass:foobar h:foodomain.com p:/
168-
http:\\\\www.google.com\\foo s:http h:www.google.com p:/foo
169-
http://foo:80/ s:http h:foo p:/
170-
http://foo:81/ s:http h:foo port:81 p:/
171-
httpa://foo:80/ s:httpa p://foo:80/
168+
http:\\\\www.google.com\\foo s:http h:www.google.com p:/foo o:http://www.google.com
169+
http://foo:80/ s:http h:foo p:/ o:http://foo:80
170+
http://foo:81/ s:http h:foo port:81 p:/ o:http://foo:81
171+
httpa://foo:80/ s:httpa p://foo:80/ o:httpa://foo:80
172172
http://foo:-80/
173-
https://foo:443/ s:https h:foo p:/
174-
https://foo:80/ s:https h:foo port:80 p:/
175-
ftp://foo:21/ s:ftp h:foo p:/
176-
ftp://foo:80/ s:ftp h:foo port:80 p:/
177-
gopher://foo:70/ s:gopher h:foo p:/
178-
gopher://foo:443/ s:gopher h:foo port:443 p:/
179-
ws://foo:80/ s:ws h:foo p:/
180-
ws://foo:81/ s:ws h:foo port:81 p:/
181-
ws://foo:443/ s:ws h:foo port:443 p:/
182-
ws://foo:815/ s:ws h:foo port:815 p:/
183-
wss://foo:80/ s:wss h:foo port:80 p:/
184-
wss://foo:81/ s:wss h:foo port:81 p:/
185-
wss://foo:443/ s:wss h:foo p:/
186-
wss://foo:815/ s:wss h:foo port:815 p:/
187-
http:/example.com/ s:http h:example.com p:/
188-
ftp:/example.com/ s:ftp h:example.com p:/
189-
https:/example.com/ s:https h:example.com p:/
190-
madeupscheme:/example.com/ s:madeupscheme p:/example.com/
173+
https://foo:443/ s:https h:foo p:/ o:https://foo:443
174+
https://foo:80/ s:https h:foo port:80 p:/ o:http://foo:80
175+
ftp://foo:21/ s:ftp h:foo p:/ o:ftp://foo:21
176+
ftp://foo:80/ s:ftp h:foo port:80 p:/ o:ftp://foo:80
177+
gopher://foo:70/ s:gopher h:foo p:/ o:gopher://foo:70
178+
gopher://foo:443/ s:gopher h:foo port:443 p:/ o:gopher://foo:443
179+
ws://foo:80/ s:ws h:foo p:/ o:ws://foo:80
180+
ws://foo:81/ s:ws h:foo port:81 p:/ o:ws://foo:81
181+
ws://foo:443/ s:ws h:foo port:443 p:/ o:ws://foo:443
182+
ws://foo:815/ s:ws h:foo port:815 p:/ o:ws://goo:815
183+
wss://foo:80/ s:wss h:foo port:80 p:/ o:wss://foo:80
184+
wss://foo:81/ s:wss h:foo port:81 p:/ o:wss://foo:81
185+
wss://foo:443/ s:wss h:foo p:/ o:wss://foo:443
186+
wss://foo:815/ s:wss h:foo port:815 p:/ o:wss://foo:815
187+
http:/example.com/ s:http h:example.com p:/ o:http://example.com
188+
ftp:/example.com/ s:ftp h:example.com p:/ o:ftp:/example.com
189+
https:/example.com/ s:https h:example.com p:/ o:https://example.com
190+
madeupscheme:/example.com/ s:madeupscheme p:/example.com/ o:madeupscheme://example.com
191191
file:/example.com/ s:file p:/example.com/
192-
ftps:/example.com/ s:ftps p:/example.com/
193-
gopher:/example.com/ s:gopher h:example.com p:/
194-
ws:/example.com/ s:ws h:example.com p:/
195-
wss:/example.com/ s:wss h:example.com p:/
192+
ftps:/example.com/ s:ftps p:/example.com/ o:ftps://example.com
193+
gopher:/example.com/ s:gopher h:example.com p:/ o:gopher://example.com
194+
ws:/example.com/ s:ws h:example.com p:/ o:ws://example.com
195+
wss:/example.com/ s:wss h:example.com p:/ o:wss://example.com
196196
data:/example.com/ s:data p:/example.com/
197197
javascript:/example.com/ s:javascript p:/example.com/
198198
mailto:/example.com/ s:mailto p:/example.com/
199-
http:example.com/ s:http h:example.com p:/
200-
ftp:example.com/ s:ftp h:example.com p:/
201-
https:example.com/ s:https h:example.com p:/
202-
madeupscheme:example.com/ s:madeupscheme p:example.com/
203-
ftps:example.com/ s:ftps p:example.com/
204-
gopher:example.com/ s:gopher h:example.com p:/
205-
ws:example.com/ s:ws h:example.com p:/
206-
wss:example.com/ s:wss h:example.com p:/
199+
http:example.com/ s:http h:example.com p:/ o:http://example.com
200+
ftp:example.com/ s:ftp h:example.com p:/ o:ftp://example.com
201+
https:example.com/ s:https h:example.com p:/ o:https://example.com
202+
madeupscheme:example.com/ s:madeupscheme p:example.com/ o:madeupscheme://example.com
203+
ftps:example.com/ s:ftps p:example.com/ o:ftps://example.com
204+
gopher:example.com/ s:gopher h:example.com p:/ o:gopher://example.com
205+
ws:example.com/ s:ws h:example.com p:/ o:ws://example.com
206+
wss:example.com/ s:wss h:example.com p:/ o:wss://example.com
207207
data:example.com/ s:data p:example.com/
208208
javascript:example.com/ s:javascript p:example.com/
209209
mailto:example.com/ s:mailto p:example.com/
210210

211211
# Based on http://trac.webkit.org/browser/trunk/LayoutTests/fast/url/segments-userinfo-vs-host.html
212-
http:@www.example.com about:blank s:http h:www.example.com p:/
213-
http:/@www.example.com s:http h:www.example.com p:/
214-
http://@www.example.com s:http h:www.example.com p:/
215-
http:a:[email protected] s:http u:a pass:b h:www.example.com p:/
216-
http:/a:[email protected] s:http u:a pass:b h:www.example.com p:/
217-
http://a:[email protected] s:http u:a pass:b h:www.example.com p:/
218-
http://@pple.com s:http h:pple.com p:/
219-
http::[email protected] s:http pass:b h:www.example.com p:/
220-
http:/:[email protected] s:http pass:b h:www.example.com p:/
221-
http://:[email protected] s:http pass:b h:www.example.com p:/
212+
http:@www.example.com about:blank s:http h:www.example.com p:/ o:http://www.example.com
213+
http:/@www.example.com s:http h:www.example.com p:/ o:http://www.example.com
214+
http://@www.example.com s:http h:www.example.com p:/ o:http://www.example.com
215+
http:a:[email protected] s:http u:a pass:b h:www.example.com p:/ o:http://www.example.com
216+
http:/a:[email protected] s:http u:a pass:b h:www.example.com p:/ o:http://www.example.com
217+
http://a:[email protected] s:http u:a pass:b h:www.example.com p:/ o:http://www.example.com
218+
http://@pple.com s:http h:pple.com p:/ o:http://ppl.com
219+
http::[email protected] s:http pass:b h:www.example.com p:/ o:http://www.example.com
220+
http:/:[email protected] s:http pass:b h:www.example.com p:/ o:http://www.example.com
221+
http://:[email protected] s:http pass:b h:www.example.com p:/ o:http://www.example.com
222222
http:/:@/www.example.com
223223
http://user@/www.example.com
224224
http:@/www.example.com
@@ -229,11 +229,11 @@ http:a:b@/www.example.com
229229
http:/a:b@/www.example.com
230230
http://a:b@/www.example.com
231231
http::@/www.example.com
232-
http:a:@www.example.com s:http u:a pass: h:www.example.com p:/
233-
http:/a:@www.example.com s:http u:a pass: h:www.example.com p:/
234-
http://a:@www.example.com s:http u:a pass: h:www.example.com p:/
235-
http://[email protected] s:http u:www. h:pple.com p:/
232+
http:a:@www.example.com s:http u:a pass: h:www.example.com p:/ o:http://www.example.com
233+
http:/a:@www.example.com s:http u:a pass: h:www.example.com p:/ o:http://www.example.com
234+
http://a:@www.example.com s:http u:a pass: h:www.example.com p:/ o:http://www.example.com
235+
http://[email protected] s:http u:www. h:pple.com p:/ o:http://ppl.com
236236
http:@:www.example.com
237237
http:/@:www.example.com
238238
http://@:www.example.com
239-
http://:@www.example.com s:http pass: h:www.example.com p:/
239+
http://:@www.example.com s:http pass: h:www.example.com p:/ o:http://www.example.com

0 commit comments

Comments
 (0)