forked from webcomponents/URL
-
Notifications
You must be signed in to change notification settings - Fork 0
/
urltestparser.js
72 lines (72 loc) · 2.57 KB
/
urltestparser.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
function URLTestParser(input) {
var relativeSchemes = ["ftp", "file", "gopher", "http", "https", "ws", "wss"],
tokenMap = { "\\": "\\", n: "\n", r: "\r", s: " ", t: "\t", f: "\f" }
resultMap = { s: "scheme", u: "username", pass: "password", h: "host", port: "port", p: "path", q: "query", f: "fragment", o: "origin" },
results = []
function Test() {
this.input = ""
this.base = ""
this.scheme = ""
this.username = ""
this.password = null
this.host = ""
this.port = ""
this.path = ""
this.query = ""
this.fragment = ""
this.origin = ""
Object.defineProperties(this, {
"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 } },
"protocol": { get: function() { return this.scheme + ":" } },
"search": { get: function() { return "?" == this.query ? "" : this.query } },
"hash": { get: function() { return "#" == this.fragment ? "" : this.fragment } }
})
}
function normalize(input) {
var output = ""
for(var i = 0, l = input.length; i < l; i++) {
var c = input[i]
if(c == "\\") {
var nextC = input[++i]
if(tokenMap.hasOwnProperty(nextC)) {
output += tokenMap[nextC]
} else if(nextC == "u") {
output += String.fromCharCode(parseInt(input[++i] + input[++i] + input[++i] + input[++i], 16))
} else {
throw new Error("Input is invalid.")
}
} else {
output += c
}
}
return output
}
var lines = input.split("\n")
for(var i = 0, l = lines.length; i < l; i++) {
var line = lines[i]
if(line === "" || line.indexOf("#", 0) === 0) {
continue
}
var pieces = line.split(" "),
result = new Test()
result.input = normalize(pieces.shift())
var base = pieces.shift()
if(base === "" || base === undefined) {
result.base = results[results.length - 1].base
} else {
result.base = normalize(base)
}
for(var ii = 0, ll = pieces.length; ii < ll; ii++) {
var piece = pieces[ii]
if(piece.indexOf("#", 0) === 0) {
continue
}
var subpieces = piece.split(":"),
token = subpieces.shift()
value = subpieces.join(":")
result[resultMap[token]] = normalize(value)
}
results.push(result)
}
return results
}