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

Constructor Details

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

Instance Method Details

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

Parameters:

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

Returns:



26
27
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
# File 'lib/epub/searcher/xhtml.rb', line 26

def search(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 => Parser::Utils.extract_attribute(child, 'id')})
      if child.name == 'img'
        if Parser::Utils.extract_attribute(child, 'alt').index(word)
          results << Result.new([child_step], nil, nil)
        end
      else
        search(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