summaryrefslogtreecommitdiff
path: root/TLD/tld_template.js
blob: 71906d75fef08370f8157a22e8c18cf96e6d9a21 (plain)
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
var tld = {
  normalize(d) { return d; },

  isIp(d) { return this._ipRx.test(d); },

  getDomain(domain) {
    if (domain === "localhost" || this.isIp(domain)) return domain;

    domain = this.normalize(domain);
    var pos = domain.search(this._tldEx);
    if(pos === -1 ) {
      pos = domain.search(this._tldRx);
      if (pos === -1) {
        // TLD not in the public suffix list, fall back to the "one-dot rule"
        pos = domain.lastIndexOf(".");
        if (pos === -1) {
          return "";
        }
      }
      pos = domain.lastIndexOf(".", pos - 1) + 1;
    } else if(domain[pos] == ".") {
      ++pos;
    }
    return pos <= 0 ? domain : domain.substring(pos);
  },

  getPublicSuffix(domain) {
    if (this.isIp(domain)) return "";

    domain = this.normalize(domain);
    var pos = domain.search(this._tldEx);
    if(pos < 0) {
      pos = domain.search(this._tldRx);
      if(pos >= 0 && domain[pos] == ".") pos++;
    } else {
      pos = domain.indexOf(".", pos + 1) + 1;
    }
    return pos < 0 ? "" : domain.substring(pos);
  },

  _ipRx: /^(?:0\.|[1-9]\d{0,2}\.){3}(?:0|[1-9]\d{0,2})$|:.*:/i,

  _tldRx: /(?:\.|^)%tld_rx%$/
  ,
  _tldEx: /(?:\.|^)%tld_ex%$/
}