Module: JsCodeWrapper

Defined in:
lib/js-code-wrapper.rb

Class Method Summary collapse

Class Method Details

.wrap_js_code(text, options = { :escape_js => false }) ⇒ Object



5
6
7
8
9
10
11
12
13
# File 'lib/js-code-wrapper.rb', line 5

def wrap_js_code(text, options = { :escape_js => false })
  doc = ::Nokogiri::HTML("<text>#{text}</text>")
  if options[:escape_js]
    doc.xpath("//script[not(ancestor::code)] | //*[@*[starts-with(name(), 'on')] and not(ancestor::code)]").each {|elem| elem.replace(::CGI.escapeHTML(elem.to_html)) }
  else
    doc.xpath("//script[not(ancestor::code)] | //*[@*[starts-with(name(), 'on')] and not(ancestor::code)]").wrap('<code>')
  end
  doc.xpath("//body/*").to_s.scan(/<text>(.*)<\/text>/im)[0][0]
end