public_suffix
A domain parser based on the Public Suffix List, for identifying the root and suffix/TLD of URLs.
A domain parser for dart based on the Public Suffix List
English project snapshot. Visit GitHub for the latest content.
A domain parser based on the Public Suffix List
public_suffix is a dart library for identifying the public suffixes (or TLDs), root domains and registrable parts of URLs.
github.io as suffix while ICANN only recognises the io part.public_suffix.dart.DefaultSuffixRules from a suffix rule list.PublicSuffix to parse URLs.PublicSuffix objects.public_suffix.dart.SuffixRules instance from a suffix rule list.PublicSuffix to parse URLs, passing in the SuffixRules instance from step 2.PublicSuffix objects.import 'package:public_suffix/public_suffix.dart';
main() {
// Load a list of suffix rules.
var suffixListString = 'load the list into this string';
DefaultSuffixRules.initFromString(suffixListString);
// Parse a URL.
var parsedUrl = PublicSuffix(urlString: 'https://www.komposten.github.io');
// Obtain information using the many getters, for example:
print(parsedUrl.suffix); // github.io
print(parsedUrl.root); // komposten
print(parsedUrl.domain); // komposten.github.io
print(parsedUrl.icannDomain); // github.io
// public_suffix also supports punycoded URLs:
parsedUrl = PublicSuffix(urlString: 'https://www.xn--6qq79v.cn');
print(parsedUrl.domain); // xn--6qq79v.cn
print(parsedUrl.punyDecoded.domain); // 你好.cn
}
public_suffix requires a list of suffix rules to work. There is no list bundled by default as these lists are updated frequently.
To use public_suffix you need to first load a list of rules, and then either initialise DefaultSuffixRules (for static access) or create
your own instance of SuffixRules.
The easiest way to use public_suffix is to get a list of rules and pass that list to DefaultSuffixRules. The PublicSuffix class will use this
list by default.
There are two ways of initialising it:
DefaultSuffixRules.initFromString()://Example using Flutter
import 'package:flutter/services.dart';
import 'package:public_suffix/public_suffix.dart';
main() {
var suffixList = rootBundle.loadString('assets/public_suffix_list.dat');
DefaultSuffixRules.initFromString(suffixList);
}
dart:io or the dart:html-based helper and load the list from a URI://Example using dart:io; for dart:html use browser_helper.dart instead.
import 'package:public_suffix/io_helper.dart';
main() async {
var uri = Uri.parse('https://publicsuffix.org/list/public_suffix_list.dat');
await SuffixRulesHelper.initDefaultListFromUri(listUri);
}
An alternative way to use public_suffix is to manually create an instance of SuffixRules and then pass that in when creating PublicSuffix objects.
Just like before, there are two ways of doing this:
SuffixRules constructors://Example using Flutter
import 'package:flutter/services.dart';
import 'package:public_suffix/public_suffix.dart';
main() {
var suffixList = rootBundle.loadString('assets/public_suffix_list.dat');
var suffixRules = SuffixRules.fromString(suffixList);
}
dart:io or the dart:html-based helper and load the list from a URI://Example using dart:io; for dart:html use browser_helper.dart instead.
import 'package:public_suffix/io_helper.dart';
main() async {
var uri = Uri.parse('https://publicsuffix.org/list/public_suffix_list.dat');
var suffixRules = await SuffixRulesHelper.createListFromUri(listUri);
}
Note: Don't overload publicsuffix.org's servers by repeatedly retrieving the suffix list from them. Cache a copy somewhere instead, and update that copy when the master copy is updated.
Several utility functions can be found in the DomainUtils class (imported from public_suffix.dart). These currently include:
isSubdomainOf: Checks if a given URL is a subdomain of another domain.isSubdomain: Checks if a given URL has a subdomain part.isKnownSuffix: Checks if a given suffix (e.g. co.uk) is listed in the suffix rule list.hasValidDomain: Checks if a given URL contains a registrable domain part.Most of these create PublicSuffix objects internally. If you are already working with PublicSuffix objects, there are similar methods you can call directly on those to avoid creating new ones.
public_suffix uses gitmoji to indicate commit types (bug fix, new feature, re-factor, etc.)
Commit messages should generally follow this format:
:emoji: Write a short summary on line one
Put additional information two lines down.
Use imperative forms of verbs. For example "Add", "Fix" and "Move" instead of "Added", "Fixed" and "Moved".
public_suffix is licensed under the MIT license. See LICENSE for the full license text.
Start with your use case, compare repository activity, and check the original project before adopting it.
“A repository is the most basic element of GitHub.”
GitHub Docs — About repositories ↗