Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Ticket #8864: fix_strip_links_token_ff.patch

File fix_strip_links_token_ff.patch, 2.1 kB (added by lifofifo, 1 year ago)

Use tokenizer and handle <href="link"> tags

  • actionpack/test/template/text_helper_test.rb

    old new  
    4848   
    4949  def test_strip_links 
    5050    assert_equal "on my mind\nall day long", strip_links("<a href='almost'>on my mind</a>\n<A href='almost'>all day long</A>") 
     51    assert_equal "0wn3d", strip_links("<a href='http://www.rubyonrails.com/'><a href='http://www.rubyonrails.com/' onlclick='steal()'>0wn3d</a></a>") 
     52    assert_equal "Magic", strip_links("<a href='http://www.rubyonrails.com/'>Mag<a href='http://www.ruby-lang.org/'>ic") 
     53    assert_equal "FrrFox", strip_links("<href onlclick='steal()'>FrrFox</a></href>") 
     54    assert_equal "My mind\nall <b>day</b> long", strip_links("<a href='almost'>My mind</a>\n<A href='almost'>all <b>day</b> long</A>") 
    5155  end 
    52  
     56   
    5357  def test_highlighter 
    5458    assert_equal( 
    5559      "This is a <strong class=\"highlight\">beautiful</strong> morning", 
  • actionpack/lib/action_view/helpers/text_helper.rb

    old new  
    322322      # 
    323323      #   strip_links('Blog: <a href="http://www.myblog.com/" class="nav" target=\"_blank\">Visit</a>.') 
    324324      #   # => Blog: Visit 
    325       def strip_links(text) 
    326         text.gsub(/<a\b.*?>(.*?)<\/a>/mi, '\1') 
     325      def strip_links(html) 
     326        # Stupid firefox treats '<href="http://whatever.com" onClick="alert()">something' as link! 
     327         
     328        if html.index("<a") || html.index("<href")  
     329          value = "" 
     330          tokenizer = HTML::Tokenizer.new(html) 
     331          while token = tokenizer.next 
     332            node = HTML::Node.parse(nil, 0, 0, token, false) 
     333            value << node.to_s unless node.is_a?(HTML::Tag) && ["a", "href"].include?(node.name) 
     334          end 
     335          html = value 
     336        end 
     337        html 
    327338      end 
    328339 
    329340      VERBOTEN_TAGS = %w(form script plaintext) unless defined?(VERBOTEN_TAGS)