Skip to content
Merged
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
6 changes: 5 additions & 1 deletion lib/uri/mailto.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,11 @@ class MailTo < Generic
HEADER_REGEXP = /\A(?<hfield>(?:%\h\h|[!$'-.0-;@-Z_a-z~])*=(?:%\h\h|[!$'-.0-;@-Z_a-z~])*)(?:&\g<hfield>)*\z/
# practical regexp for email address
# https://html.spec.whatwg.org/multipage/input.html#valid-e-mail-address
EMAIL_REGEXP = /\A(?!\.)(?!.*\.{2})[a-zA-Z0-9.!\#$%&'*+\/=?^_`{|}~-]+(?<!\.)@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*\z/
EMAIL_REGEXP = %r[\A#{
atext = %q[(?:[a-zA-Z0-9!\#$%&'*+\/=?^_`{|}~-]+)]
}(?:\.#{atext})*@#{
label = %q[(?:[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)]
}(?:\.#{label})*\z]
# :startdoc:

#
Expand Down
16 changes: 16 additions & 0 deletions test/uri/test_mailto.rb
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,22 @@ def test_check_to
end
end

def test_email_regexp
re = URI::MailTo::EMAIL_REGEXP

rate = 1000
longlabel = '.' + 'invalid'.ljust(63, 'd')
endlabel = ''
pre = ->(n) {'a@invalid' + longlabel*(n*rate) + endlabel}
assert_linear_performance(1..10, pre: pre) do |to|
re =~ to or flunk
end
endlabel = '.' + 'email'.rjust(64, 'd')
assert_linear_performance(1..10, pre: pre) do |to|
re =~ to and flunk
end
end

def test_to_s
u = URI::MailTo.build([nil, 'subject=Ruby'])

Expand Down
Loading