Class: SleipnirAPI::Searcher

Inherits:
Object
  • Object
show all
Includes:
OptionUtil, Util
Defined in:
lib/sleipnir_api/searcher.rb

Overview

:nodoc:

Constant Summary collapse

HILIGHT_TAG =
"span"
HILIGHT_CLASSNAME =
"ruby-sleipnir-api-hilight"
HILIGHT_STYLE =
"color: black; background-color: #ffff66; font-weight: bold"

Instance Method Summary collapse

Methods included from OptionUtil

parse_option_arguments

Methods included from Util

#api, #ensure_version, #join_keyword

Constructor Details

#initialize(tab) ⇒ Searcher

Returns a new instance of Searcher.



13
14
15
16
# File 'lib/sleipnir_api/searcher.rb', line 13

def initialize(tab)
  @tab = tab
  @range = nil
end

Instance Method Details

#add_hilight_tag(matched, html = nil, tag = HILIGHT_TAG, classname = HILIGHT_CLASSNAME, style = HILIGHT_STYLE) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/sleipnir_api/searcher.rb', line 64

def add_hilight_tag(matched, html=nil, tag=HILIGHT_TAG, classname=HILIGHT_CLASSNAME, style=HILIGHT_STYLE)
  if html.nil?
    "<#{tag} class='#{classname}' style='#{style}'>#{matched}</#{tag}>"
  else
    matched = matched.gsub(/\A(<[^<>]+>)+/, "").gsub(/(<[^<>]+>)+\z/, "")
    html.gsub(/#{Regexp.quote(matched)}/) do
      matched.split(/(<[^<>]+>)/).map{|part|
        if part[0] != ?< and part =~ /\S/
          "<#{tag} class='#{classname}' style='#{style}'>#{part}</#{tag}>"
        else
          part
        end
      }.join
    end
  end
end

#each_find_text(keyword, range = nil) ⇒ Object



18
19
20
21
22
23
24
# File 'lib/sleipnir_api/searcher.rb', line 18

def each_find_text(keyword, range = nil)
  range = @tab.document.body.createTextRange
  while range.FindText(keyword)
    yield range
    range.Collapse(false)
  end
end

#find_text(keyword, *options) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/sleipnir_api/searcher.rb', line 26

def find_text(keyword, *options)
  opts, _ = parse_option_arguments(options)

  if not @range
    @range = @tab.document.body.createTextRange
  else
    @range.Collapse(false)
  end

  if @range.FindText(keyword)
    @range.Select if opts[:select]
    @range.ScrollIntoView if opts[:scroll]
    true
  else
    @tab.document.Selection.empty if opts[:select]
    @range = nil
    false
  end
end

#hilight(*keywords) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/sleipnir_api/searcher.rb', line 46

def hilight(*keywords)
  remove_hilight
  range = @tab.document.body.createTextRange
  matches = []
  keywords.each do |kwd|
    each_find_text(kwd, range) do |range|
      matched_html = range.HTMLText
      matches << matched_html.gsub(/\A(<[^<>]+>)+/, "").gsub(/(<[^<>]+>)+\z/, "")
    end
    range.Collapse(true)
  end

  all=@tab.document.body.innerHTML
  @tab.document.body.innerHTML = matches.uniq.inject(all) do |all,m|
    add_hilight_tag(m, all)
  end
end

#remove_hilightObject



81
82
83
84
85
# File 'lib/sleipnir_api/searcher.rb', line 81

def remove_hilight
  @tab.document.body.getElementsByTagName(HILIGHT_TAG).each do |span|
    span.OuterHTML = span.InnerHTML if span.ClassName == HILIGHT_CLASSNAME
  end
end