class HtmlTestDomHook

Public Instance Methods

compile(request) click to toggle source
# File lib/test_dom_hook.rb, line 8
def compile(request)
  request
end
run!(request) click to toggle source
# File lib/test_dom_hook.rb, line 12
def run!(request)
  options = request.options
  expected = expected_html request
  actual = compile_content request

  if is_dom_ok expected, actual, options
    [render_html(actual), :passed]
  else
    [render_fail_html(actual, expected), :failed]
  end
end

Private Instance Methods

build_iframe(content) click to toggle source
# File lib/test_dom_hook.rb, line 88
  def build_iframe(content)
    dom = hexp(content).to_dom
    <<html
<div class="mu-browser"#{page_title dom}#{page_favicon dom} data-srcdoc="#{transform_content(content)}">
</div>
html
  end
build_result(name, content) click to toggle source
# File lib/test_dom_hook.rb, line 70
  def build_result(name, content)
    <<html
<br>
<strong>#{t name}</strong>
#{build_iframe content}
html
  end
comparable_hexp(content, options) click to toggle source
# File lib/test_dom_hook.rb, line 36
def comparable_hexp(content, options)
  content = squeeze_inner_whitespaces content unless options['keep_inner_whitespaces']
  exp = hexp content
  exp = exp.replace('script') { [] } if options['output_ignore_scripts']
  exp = exp.replace('style') { [] } if options['output_ignore_styles']
  exp = remove_outer_whitespaces exp unless options['keep_outer_whitespaces']
  exp
end
compile_content(request) click to toggle source
# File lib/test_dom_hook.rb, line 104
def compile_content(request)
  request.extra.presence || request.content
end
contents_match?(expected, actual, options) click to toggle source
# File lib/test_dom_hook.rb, line 30
def contents_match?(expected, actual, options)
  comparable_hexp(expected, options) == comparable_hexp(actual, options)
rescue
  expected == actual
end
expected_html(request) click to toggle source
# File lib/test_dom_hook.rb, line 108
def expected_html(request)
  request.test.is_a?(Hash) ? request.test['output'] : request.test
end
hexp(content) click to toggle source
# File lib/test_dom_hook.rb, line 58
def hexp(content)
  Hexp.parse("<html>#{content}</html>")
end
is_dom_ok(expected, actual, options) click to toggle source
# File lib/test_dom_hook.rb, line 26
def is_dom_ok(expected, actual, options)
  expected.blank? || contents_match?(expected, actual, options)
end
page_favicon(dom) click to toggle source
# File lib/test_dom_hook.rb, line 83
def page_favicon(dom)
  dom.xpath("//link[@rel='icon' and @href]").first
    .try { |tag| " data-favicon=\"#{tag['href']}\"" }
end
page_title(dom) click to toggle source
# File lib/test_dom_hook.rb, line 78
def page_title(dom)
  title = dom.xpath('//title').first&.text
  title.present? ? " data-title=\"#{title}\"" : ''
end
remove_outer_whitespaces(hexp) click to toggle source
# File lib/test_dom_hook.rb, line 45
def remove_outer_whitespaces(hexp)
  hexp.flat_map_children do |node|
    next remove_outer_whitespaces(node) unless node.text?
    [node.strip.presence].compact
  end
end
render_fail_html(actual, expected) click to toggle source
# File lib/test_dom_hook.rb, line 66
def render_fail_html(actual, expected)
  "#{build_result :actual, actual}#{build_result :expected, expected}"
end
render_html(actual) click to toggle source
# File lib/test_dom_hook.rb, line 62
def render_html(actual)
  build_iframe actual
end
replace_anchors(content) click to toggle source
# File lib/test_dom_hook.rb, line 100
def replace_anchors(content)
  content.gsub(/href="#(\w*)"/, 'href="about:srcdoc#\1"')
end
squeeze_inner_whitespaces(content) click to toggle source
# File lib/test_dom_hook.rb, line 52
def squeeze_inner_whitespaces(content)
  %W(\r \n \t)
      .reduce(content.strip) { |c, it| c.gsub(it, ' ') }
      .squeeze(' ')
end
transform_content(content) click to toggle source
# File lib/test_dom_hook.rb, line 96
def transform_content(content)
  replace_anchors(content).escape_html
end