提交 bdf91d67 编写于 作者: J Jamis Buck

Make auto_link parse a greater subset of valid url formats.


git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@4896 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
上级 e4a4287c
*SVN*
* Make auto_link parse a greater subset of valid url formats. [Jamis Buck]
* Integration tests: headers beginning with X aren't excluded from the HTTP_ prefix, so X-Requested-With becomes HTTP_X_REQUESTED_WITH as expected. [Mike Clark]
* Tighten rescue clauses. #5985 [james@grayproductions.net]
......
......@@ -313,23 +313,26 @@ def set_cycle(name, cycle_object)
@_cycles[name] = cycle_object
end
AUTO_LINK_RE = /
( # leading text
<\w+.*?>| # leading HTML tag, or
[^=!:'"\/]| # leading punctuation, or
^ # beginning of line
AUTO_LINK_RE = %r{
( # leading text
<\w+.*?>| # leading HTML tag, or
[^=!:'"/]| # leading punctuation, or
^ # beginning of line
)
(
(?:http[s]?:\/\/)| # protocol spec, or
(?:www\.) # www.*
(?:https?://)| # protocol spec, or
(?:www\.) # www.*
)
(
([\w]+:?[=?&\/.-]?)* # url segment
\w+[\/]? # url tail
(?:\#\w*)? # trailing anchor
[-\w]+ # subdomain or domain
(?:\.[-\w]+)* # remaining subdomains or domain
(?::\d+)? # port
(?:/(?:[~\w%.;-]+)?)* # path
(?:\?[\w%&=.;-]+)? # query string
(?:\#\w*)? # trailing anchor
)
([[:punct:]]|\s|<|$) # trailing text
/x unless const_defined?(:AUTO_LINK_RE)
([[:punct:]]|\s|<|$) # trailing text
}x unless const_defined?(:AUTO_LINK_RE)
# Turns all urls into clickable links. If a block is given, each url
# is yielded and the result is used as the link text. Example:
......@@ -339,7 +342,7 @@ def set_cycle(name, cycle_object)
def auto_link_urls(text, href_options = {})
extra_options = tag_options(href_options.stringify_keys) || ""
text.gsub(AUTO_LINK_RE) do
all, a, b, c, d = $&, $1, $2, $3, $5
all, a, b, c, d = $&, $1, $2, $3, $4
if a =~ /<a\s/i # don't replace URL's that are already linked
all
else
......
......@@ -114,6 +114,24 @@ def test_pluralization
assert_equal("2 counts", pluralize(2, "count"))
end
def test_auto_link_parsing
urls = %w(http://www.rubyonrails.com
http://www.rubyonrails.com:80
http://www.rubyonrails.com/~minam
https://www.rubyonrails.com/~minam
http://www.rubyonrails.com/~minam/url%20with%20spaces
http://www.rubyonrails.com/foo.cgi?something=here
http://www.rubyonrails.com/foo.cgi?something=here&and=here
http://www.rubyonrails.com/contact;new
http://www.rubyonrails.com/contact;new%20with%20spaces
http://www.rubyonrails.com/contact;new?with=query&string=params
http://www.rubyonrails.com/~minam/contact;new?with=query&string=params)
urls.each do |url|
assert_equal %(<a href="#{url}">#{url}</a>), auto_link(url)
end
end
def test_auto_linking
email_raw = 'david@loudthinking.com'
email_result = %{<a href="mailto:#{email_raw}">#{email_raw}</a>}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册