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
15 changes: 15 additions & 0 deletions lib/public_suffix/rule.rb
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,21 @@ def rule
value == "" ? STAR : STAR + DOT + value
end

# Checks if this rule matches +name+.
#
# Unlike {Base#match?}, an exact match against +value+ is not enough,
# because the wildcard label requires an additional leading label
# to be present in +name+. For example, "*.com" doesn't match "com",
# only names with at least one label before it (e.g. "example.com").
#
# @param name [String] the domain name to check
# @return [Boolean]
def match?(name)
return super if value == ""

name.chomp(value).end_with?(DOT)
end

# Decomposes the domain name according to rule properties.
#
# @param domain [#to_s] The domain name to decompose
Expand Down
17 changes: 15 additions & 2 deletions test/unit/rule_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,11 @@ def test_match_standard

def test_match_wildcard_and_exception
[
# FIXME
# [PublicSuffix::Rule.factory("*.com"), "com", false],
[PublicSuffix::Rule.factory("*.com"), "com", false],
[PublicSuffix::Rule.factory("*.com"), "example.com", true],
[PublicSuffix::Rule.factory("*.com"), "foo.example.com", true],
[PublicSuffix::Rule.factory("*.co.uk"), "co.uk", false],
[PublicSuffix::Rule.factory("*.co.uk"), "example.co.uk", true],
[PublicSuffix::Rule.factory("!example.com"), "com", false],
[PublicSuffix::Rule.factory("!example.com"), "example.com", true],
[PublicSuffix::Rule.factory("!example.com"), "foo.example.com", true],
Expand Down Expand Up @@ -243,6 +244,18 @@ def test_parts
assert_equal %w[co uk], @klass.build("*.co.uk").parts
end

def test_match
# the wildcard label requires an extra leading label in the input,
# so the rule's own value alone must not match
[
["uk", false],
["google.uk", true],
["foo.google.uk", true],
].each do |input, expected|
assert_equal expected, @klass.build("*.uk").match?(input)
end
end

def test_decompose
assert_equal [nil, nil], @klass.build("*.do").decompose("nic.do")
assert_equal %w[google co.uk], @klass.build("*.uk").decompose("google.co.uk")
Expand Down