Created
March 14, 2009 08:40
-
-
Save nurse/79019 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ALLOW_TAGS = %w/font span p b br/ | |
def validate_xml_fragment | |
str = self | |
tags = str.scan(/\G(?:(<[^<>]+>)|[^<>&]+|[^&<>]*(?:&#?\w+;[^&<>]*)+)/) | |
return ERB::Util.html_escape(str) unless $'.empty? | |
stack = [] | |
tags.each do |tag| | |
case tag.first | |
when /<(\w+)(?: +\w+=(?:"[^"]*"|'[^']*'|\w+))* *(\/)?>/ | |
return ERB::Util.html_escape(str) unless ALLOW_TAGS.include?($1) | |
stack.push($1) unless $2 | |
when /<\/(\w+)>/ | |
return ERB::Util.html_escape(str) unless stack.last == $1 | |
stack.pop | |
when nil | |
else | |
return ERB::Util.html_escape(str) | |
end | |
end | |
return ERB::Util.html_escape(str) unless stack.empty? | |
nil while str.gsub!(/^\s+|\s+$|\A<br *\/>|<br *\/>\z/, '') | |
str | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment