Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>As the main issue was HTML tags enclosed in HTML entities angle brackets -- which is totally mangled by Nokogiri -- we solved it by just removing the aforementioned HTML tags, escaping the non-HTML-tag angle brackets and then putting the HTML tags back. It sounds a little hackish but it's working perfectly. Our first goal was escaping email addresses enclosed in angle brackets, but this approach (supposedly) works for any kind text.</p> <pre><code># Does not run on ruby 1.9 require 'test/unit' require 'test/unit/ui/console/testrunner' require 'nokogiri' require 'active_support/secure_random' def filter_html(content) # Used to mark highlighted words. random_hex = SecureRandom.hex(6) # Remove highlighting. highlighted_terms = [] without_highlighting = content.to_s.gsub(/&lt;b class="highlight"&gt;(.*?)&lt;\/b&gt;/) do |match| highlighted_terms &lt;&lt; $1 "highlight-#{random_hex}:#{$1}" end # Escape non-HTML angle brackets. escaped_content = without_highlighting.to_s.gsub(/&lt;(?:\s*\/)?([^!\-\-].*?)&gt;/) do |excerpt| capture = $1 tag = capture.split(/[^a-zA-Z1-6]/).reject(&amp;:empty?).first !!Nokogiri::HTML::ElementDescription[tag] ? excerpt : "&amp;lt;#{capture}&amp;gt;" end # Add highlighting back. highlighted_terms.uniq.each do |term| escaped_content.gsub!(/highlight-#{random_hex}:(#{term})/) do |match| "&lt;b class=\"highlight\"&gt;#{$1}&lt;/b&gt;" end end escaped_content end class HTMLTest &lt; Test::Unit::TestCase def setup @raw_html = &lt;&lt;-EOS &lt;html&gt; &lt;foo@bar.baz&gt; &lt;p&gt;&lt;foo@&lt;b class="highlight"&gt;bar&lt;/b&gt;.baz&gt;&lt;/p&gt; &lt;p&gt; &lt;foo@&lt;b class="highlight"&gt;bar&lt;/b&gt;.baz&gt; &lt;/p&gt; &lt; don't erase this &gt; &lt;/html&gt; EOS @filtered_html = &lt;&lt;-EOS &lt;html&gt; &amp;lt;foo@bar.baz&amp;gt; &lt;p&gt;&amp;lt;foo@&lt;b class="highlight"&gt;bar&lt;/b&gt;.baz&amp;gt;&lt;/p&gt; &lt;p&gt; &amp;lt;foo@&lt;b class="highlight"&gt;bar&lt;/b&gt;.baz&amp;gt; &lt;/p&gt; &amp;lt; don't erase this &amp;gt; &lt;/html&gt; EOS end def test_filter_html assert_equal(@filtered_html, filter_html(@raw_html)) end end # It passes! Test::Unit::UI::Console::TestRunner.run(HTMLTest) </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload