Revision fb690bc22dd76408313b02fc08b55cbdf3350c22 authored by jgraham on 03 January 2014, 16:04:38 UTC, committed by jgraham on 03 January 2014, 16:04:38 UTC
Add tool for generating test list in JSON format
2 parent s 94d07b3 + a1ef7c8
Raw File
urltestparser.js
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" },
      results = []
  function Test() {
    this.input = ""
    this.base = ""
    this.scheme = ""
    this.username = ""
    this.password = null
    this.host = ""
    this.port = ""
    this.path = ""
    this.query = ""
    this.fragment = ""
    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
}
back to top