seba
seba

Reputation: 153

How to match an inline JavaScript event with Nokogiri

Does anyone know a method to identify an inline JavaScript event-handler with Nokogiri or something else?

I'd like to extract the onload="alert('Page is loaded');" from this tag:

<body onload="alert('Page is loaded');">
</body>

I need a generic method which can identify all JavaScript event handlers.


Edit :

I need to prevent users from recording inline JavaScript event-handler within a wysiwyg (tinyMCE) form.

For now, I do that with gsub :

self.description.gsub(/onafterprint/i, '****')       
self.description.gsub(/onbeforeprint/i, '****')       
self.description.gsub(/onbeforeunload/i, '****')
...

This is not really smart... I was wondering if it would be possible with Nokogiri? Would it more usefull and faster ?

I allready know how to find all my doc's attribute with Nokogiri (maybe not the best method)

html_doc.xpath('//@*').each do |e|
    puts e.name
    # I need something like that :
    e.remove if e.javascript_event_handler?
end

My main Question is how to match every existing inline JavaScript event-handler. It don't seems that a generic method exists. Am I wrong ?

Upvotes: 0

Views: 139

Answers (2)

Igor Benikov
Igor Benikov

Reputation: 897

Inline event handlers starts with "on" prefix (exclude animations, i think), so you can write something as:

html_doc.xpath('//@*').each do |e|
  e.remove if e.name.start_with? "on"
end

Upvotes: 0

guitarman
guitarman

Reputation: 3310

May be this can help you:

//body[contains(@onload,'alert')]

Upvotes: 0

Related Questions