Skip to content

Instantly share code, notes, and snippets.

@coryschires
Created June 3, 2010 20:27
Show Gist options
  • Save coryschires/424427 to your computer and use it in GitHub Desktop.
Save coryschires/424427 to your computer and use it in GitHub Desktop.
scrub_html helper method
# removes disallowed opening and closing html tags from a passed string
def scrub_html(string)
disallowed_tags = %w[p ul li ol blockquote cite pre code dl dt dd table tr th frameset from input select option address h1 h2 h3 h4 h5 h6]
disallowed_tags.each do |tag|
opening_tag = /<\s*#{tag}[^>]*>{1}/i
closing_tag = /<\/\s*#{tag}\s*>/i
hr_tag = /<\s*hr\s*\/*>/i
string.gsub!(opening_tag, '')
string.gsub!(closing_tag, '')
string.gsub!(hr_tag, '')
end
string
end
# rspec test
it "should remove all disallowed opening and closing html tags" do
text = "< p >I < p>am <blockquote style='bold'>a </ p>test <ADDRESS STYLE='font-weight:bold'>string with <frameset>loads of </select>totally messed<hr>< hr >< HR><hr /><hr > up</p><h1> HTML. Please <cite style='bold'>help </H1>me.</p>"
scrub_html(text).should == "I am a test string with loads of totally messed up HTML. Please help me."
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment