views:

141

answers:

1

I want to remove a list of dom events attribute from html? how to do this? like:

before = "<div onclick="abc" >abc</div>"
after = clean_it(before) // after => "<div>abc</div>"       


DOM_EVENT_TO_BE_REMOVE = "onclick|ondblclick|onerror|onfocus|onkeydown"  // i want to remove these events

// i want to do it like this
def clean_it(html)
  doc = Hpricot(html)    
  doc.search(DOM_EVENT_TO_BE_REMOVE).remove_attribute(DOM_EVENT_TO_BE_REMOVE) 
  doc.to_s
end

thanks.

+2  A: 

Use "remove_attr", E.g:

doc.search("[@onclick]").remove_attr("onclick")

So for your document do something like:

DOM_EVENT_TO_BE_REMOVE = ["onclick", "ondblclick", "onerror", "onfocus", "onkeydown"]
DOM_EVENT_TO_BE_REMOVE.each do |de|
    doc.search("[@#{de}]").remove_attr(de)
end
i5m
`DOM_EVENT_TO_BE_REMOVE = %w[ onclick ondblclick onerror onfocus onkeydown ]` might be a little cleaner.
kejadlen
thank you very much..
www
does it need to be doc.search("*[@onclick]")? I think the css syntax needs an element type before finding the attributes. Wildcard lets it search all elements.
Lukas
I did a limited test without the wildcard before posting the answer and it didn't seem to be required. Also the wiki article doesn't mention the need for it: http://wiki.github.com/hpricot/hpricot/hpricot-challenge (See "Checking for a few attributes"). But your comment is worth bearing in mind.
i5m