Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions lib/public_suffix/list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ def self.parse(input, private_domains: true)
# @yieldparam [PublicSuffix::List] self The newly created instance.
def initialize
@rules = {}
@max_rule_size = 0
yield(self) if block_given?
end

Expand Down Expand Up @@ -140,6 +141,7 @@ def each(&block)
# @return [self]
def add(rule)
@rules[rule.value] = rule_to_entry(rule)
@max_rule_size = [@max_rule_size, (rule.value.count DOT) + 1].max
self
end
alias << add
Expand All @@ -163,6 +165,7 @@ def empty?
# @return [self]
def clear
@rules.clear
@max_rule_size = 0
self
end

Expand Down Expand Up @@ -204,14 +207,14 @@ def select(name, ignore_private: false)
query = parts[index]
rules = []

loop do
limit = [parts.size, @max_rule_size].min

while index < limit
match = @rules[query]
rules << entry_to_rule(match, query) if !match.nil? && (ignore_private == false || match.private == false)

index += 1
break if index >= parts.size

query = parts[index] + DOT + query
query = parts[index] + DOT + query if index < limit
end

rules
Expand Down
3 changes: 3 additions & 0 deletions test/unit/public_suffix_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ class PublicSuffixTest < Minitest::Test
def test_private_domains_enabled_by_default
domain = PublicSuffix.parse("www.example.blogspot.com")
assert_equal "blogspot.com", domain.tld

domain = PublicSuffix.parse("example.s3.cn-north-1.amazonaws.com.cn")
assert_equal "s3.cn-north-1.amazonaws.com.cn", domain.tld
end

def test_private_domains_disable
Expand Down