Class: Mud::HtmlResult

Inherits:
Object
  • Object
show all
Defined in:
lib/mud/html_result.rb

Instance Method Summary collapse

Constructor Details

#initialize(html, js) ⇒ HtmlResult

Returns a new instance of HtmlResult.



4
5
6
7
# File 'lib/mud/html_result.rb', line 4

def initialize(html, js)
  @html = html
  @js = js
end

Instance Method Details

#to_sObject



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/mud/html_result.rb', line 9

def to_s
  doc = html_doc
  script = doc.search('//script').find { |script_tag| /.*\/dev$/.match script_tag.attributes['src'] }

  if script
    script.remove_attribute(:src)
  else
    script = Hpricot::Elem.new('script', :type => 'text/javascript')
    head = doc.at('/html/head')

    unless head
      head = Hpricot::Elem.new('head')
      doc.root.children.unshift(head)
    end

    head.children = (head.children || []).unshift(script)
  end

  script.inner_html = @js.to_s

  doc.to_html
end