-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhostname.dart
38 lines (31 loc) · 1.28 KB
/
hostname.dart
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
import "../document.dart";
import "./hostname/validator.dart";
/// RFC1132 internet hostname (only ASCII segments)
class KdlHostname extends KdlValue<String> {
/// Construct a new `KdlHostname`
KdlHostname(super.value, [super.type]);
/// Convert a `KdlString` into a `KdlHostname`
static KdlHostname? convert(KdlValue value, [String type = 'hostname']) {
if (value is! KdlString) return null;
var validator = HostnameValidator(value.value);
if (!validator.isValid()) throw "invalid hostname ${value.value}";
return KdlHostname(value.value, type);
}
}
/// RFC5890 internationalized internet hostname
/// (only `xn--`-prefixed ASCII "punycode" segments, or non-ASCII segments)
class KdlIdnHostname extends KdlHostname {
/// Unicode value
String unicodeValue;
/// Construct a new `KdlIDNHostname`
KdlIdnHostname(String value, this.unicodeValue, [String? type])
: super(value, type);
/// Convert a `KdlString` into a `KdlIDNHostname`
static KdlIdnHostname? convert(KdlValue value,
[String type = 'idn-hostname']) {
if (value is! KdlString) return null;
var validator = IdnHostnameValidator(value.value);
if (!validator.isValid()) throw "invalid hostname ${value.value}";
return KdlIdnHostname(validator.ascii, validator.unicode, type);
}
}