2010-01-05 8 views
1

Je veux supprimer une liste d'attributs d'événements dom de html? comment faire ça? comme:comment supprimer l'attribut d'événement de html using Hpricot?

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 

merci.

Répondre

2

Utilisez "remove_attr", par exemple:

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

Donc, pour votre document faire quelque chose comme:

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 
+1

'DOM_EVENT_TO_BE_REMOVE =% p [onclick ondblclick onerror onfocus onkeydown]' pourrait être un peu plus propre . – kejadlen

+0

merci beaucoup .. – www

+0

doit-il être doc.search ("* [@ onclick]")? Je pense que la syntaxe css a besoin d'un type d'élément avant de trouver les attributs. Le caractère générique permet de rechercher tous les éléments. – Lukas