module CGI::Util

def escapeElement(string, *elements)

# "
<A HREF="url"></A>"
print CGI.escapeElement('
', ["A", "IMG"])

# "
<A HREF="url"></A>"
print CGI.escapeElement('
', "A", "IMG")

instance, the double-quotes surrounding attribute values).
The attribute list of the open tag will also be escaped (for
This matches both the start and the end tag of that element.
is specified by the name of the element, without angle brackets.
Takes an element or elements or array of elements. Each element

Escape only the tags of certain HTML elements in +string+.
def escapeElement(string, *elements)
  elements = elements[0] if elements[0].kind_of?(Array)
  unless elements.empty?
    string.gsub(/<\/?(?:#{elements.join("|")})\b[^<>]*+>?/im) do
      CGI.escapeHTML($&)
    end
  else
    string
  end
end