From 4e9abdd7f1b4e05f8d1b50ddaa080b3ff63b92d9 Mon Sep 17 00:00:00 2001 From: "Hongli Lai (Phusion)" Date: Thu, 13 Nov 2008 21:49:23 +0100 Subject: Tag helper should output an attribute with the value 'false' instead of omitting the attribute, if the associated option is false but not nil. --- actionpack/lib/action_view/helpers/tag_helper.rb | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) (limited to 'actionpack/lib/action_view') diff --git a/actionpack/lib/action_view/helpers/tag_helper.rb b/actionpack/lib/action_view/helpers/tag_helper.rb index de08672d2d..d37ca766af 100644 --- a/actionpack/lib/action_view/helpers/tag_helper.rb +++ b/actionpack/lib/action_view/helpers/tag_helper.rb @@ -133,10 +133,12 @@ module ActionView unless options.blank? attrs = [] if escape - options.each do |key, value| - next unless value - value = BOOLEAN_ATTRIBUTES.include?(key) ? key : escape_once(value) - attrs << %(#{key}="#{value}") + options.each_pair do |key, value| + if BOOLEAN_ATTRIBUTES.include?(key) + attrs << %(#{key}="#{key}") if value + else + attrs << %(#{key}="#{escape_once(value)}") if !value.nil? + end end else attrs = options.map { |key, value| %(#{key}="#{value}") } -- cgit v1.2.3 From c6c5cd554110f6e62290de3e3008076b2f69e7cb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mislav=20Marohni=C4=87?= Date: Wed, 12 Nov 2008 13:15:57 +0100 Subject: refactor autolink helper. change tests to expect HTML-escaped URLs Signed-off-by: Michael Koziarski --- actionpack/lib/action_view/helpers/text_helper.rb | 46 ++++++++++------------- 1 file changed, 20 insertions(+), 26 deletions(-) (limited to 'actionpack/lib/action_view') diff --git a/actionpack/lib/action_view/helpers/text_helper.rb b/actionpack/lib/action_view/helpers/text_helper.rb index 36f7575652..07f98158f7 100644 --- a/actionpack/lib/action_view/helpers/text_helper.rb +++ b/actionpack/lib/action_view/helpers/text_helper.rb @@ -545,38 +545,32 @@ module ActionView end AUTO_LINK_RE = %r{ - ( # leading text - <\w+.*?>| # leading HTML tag, or - [^=!:'"/]| # leading punctuation, or - ^ # beginning of line - ) - ( - (?:https?://)| # protocol spec, or - (?:www\.) # www.* - ) - ( - [-\w]+ # subdomain or domain - (?:\.[-\w]+)* # remaining subdomains or domain - (?::\d+)? # port - (?:/(?:[~\w\+@%=\(\)-]|(?:[,.;:'][^\s$]))*)* # path - (?:\?[\w\+@%&=.;:-]+)? # query string - (?:\#[\w\-]*)? # trailing anchor - ) - ([[:punct:]]|<|$|) # trailing text - }x unless const_defined?(:AUTO_LINK_RE) + ( https?:// | www\. ) + [^\s<]+ + }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. def auto_link_urls(text, html_options = {}) - extra_options = tag_options(html_options.stringify_keys) || "" + link_attributes = html_options.stringify_keys text.gsub(AUTO_LINK_RE) do - all, a, b, c, d = $&, $1, $2, $3, $4 - if a =~ /]*href="$/ + if href =~ /[^\w\/-]$/ + punctuation = href[-1, 1] + href = href[0, href.length - 1] + else + punctuation = '' + end + + link_text = block_given?? yield(href) : href + href = 'http://' + href unless href.index('http') == 0 + + content_tag(:a, h(link_text), link_attributes.merge('href' => href)) + punctuation else - text = b + c - text = yield(text) if block_given? - %(#{a}#{text}#{d}) + # do not change string; URL is alreay linked + href end end end -- cgit v1.2.3 From 4f984c9d0e66601a81cb5ae6e3b50582e6dc0c2d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mislav=20Marohni=C4=87?= Date: Thu, 13 Nov 2008 22:39:16 +0100 Subject: auto_link helper: add intelligent ending closing bracket handling. add new tests and reorder new ones for readability Signed-off-by: Michael Koziarski [#1353 state:committed] --- actionpack/lib/action_view/helpers/text_helper.rb | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) (limited to 'actionpack/lib/action_view') diff --git a/actionpack/lib/action_view/helpers/text_helper.rb b/actionpack/lib/action_view/helpers/text_helper.rb index 07f98158f7..9bd3d63423 100644 --- a/actionpack/lib/action_view/helpers/text_helper.rb +++ b/actionpack/lib/action_view/helpers/text_helper.rb @@ -549,28 +549,32 @@ module ActionView [^\s<]+ }x unless const_defined?(:AUTO_LINK_RE) + BRACKETS = { ']' => '[', ')' => '(', '}' => '{' } + # Turns all urls into clickable links. If a block is given, each url # is yielded and the result is used as the link text. def auto_link_urls(text, html_options = {}) link_attributes = html_options.stringify_keys text.gsub(AUTO_LINK_RE) do href = $& + punctuation = '' # detect already linked URLs - unless $` =~ /]*href="$/ - if href =~ /[^\w\/-]$/ - punctuation = href[-1, 1] - href = href[0, href.length - 1] - else - punctuation = '' + if $` =~ /]*href="$/ + # do not change string; URL is alreay linked + href + else + # don't include trailing punctuation character as part of the URL + if href.sub!(/[^\w\/-]$/, '') and punctuation = $& and opening = BRACKETS[punctuation] + if href.scan(opening).size > href.scan(punctuation).size + href << punctuation + punctuation = '' + end end link_text = block_given?? yield(href) : href href = 'http://' + href unless href.index('http') == 0 content_tag(:a, h(link_text), link_attributes.merge('href' => href)) + punctuation - else - # do not change string; URL is alreay linked - href end end end -- cgit v1.2.3