Class: EPUB::Searcher::XHTML::Restricted

Inherits:
EPUB::Searcher::XHTML show all
Defined in:
lib/epub/searcher/xhtml.rb

Constant Summary

Constants inherited from EPUB::Searcher::XHTML

ALGORITHMS

Instance Method Summary collapse

Methods inherited from EPUB::Searcher::XHTML

#initialize, search_text

Constructor Details

This class inherits a constructor from EPUB::Searcher::XHTML

Instance Method Details

#search_text(word, element = nil) ⇒ Array<Result>

Parameters:

  • element (REXML::Element, Nokogiri::XML::Element) (defaults to: nil)

Returns:



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/epub/searcher/xhtml.rb', line 28

def search_text(word, element=nil)
  results = []

  elem_index = 0
  (element || @element).children.each do |child|
    if child.element?
      child_step = Result::Step.new(:element, elem_index, {:name => child.name, :id => child.attribute_with_prefix('id')})
      if child.name == 'img'
        if child.attribute_with_prefix('alt').index(word)
          results << Result.new([child_step], nil, nil)
        end
      else
        search_text(word, child).each do |sub_result|
          results << Result.new([child_step] + sub_result.parent_steps, sub_result.start_steps, sub_result.end_steps)
        end
      end
      elem_index += 1
    elsif child.text?
      text_index = elem_index
      char_index = 0
      text_step = Result::Step.new(:text, text_index)
      while char_index = child.text.index(word, char_index)
        results << Result.new([text_step], [Result::Step.new(:character, char_index)], [Result::Step.new(:character, char_index + word.length)])
        char_index += 1
      end
    end
  end

  results
end