Module: Volt::CommentSearchers

Included in:
DomSection, DomTemplate
Defined in:
lib/volt/page/targets/helpers/comment_searchers.rb

Constant Summary collapse

NO_XPATH =
false

Instance Method Summary collapse

Instance Method Details

#build_from_html(html) ⇒ Object

Returns an unattached div with the nodes from the passed in html.



52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/volt/page/targets/helpers/comment_searchers.rb', line 52

def build_from_html(html)
  temp_div = nil
  `
    temp_div = document.createElement('div');
    var doc = jQuery.parseHTML(html);

    if (doc) {
      for (var i=0;i < doc.length;i++) {
        temp_div.appendChild(doc[i]);
      }
    }
  `
  temp_div
end

#find_by_comment(text, in_node = `document`) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/volt/page/targets/helpers/comment_searchers.rb', line 9

def find_by_comment(text, in_node = `document`)
  if NO_XPATH
    return find_by_comment_without_xml(text, in_node)
  else
    node = nil

    `
      node = document.evaluate("//comment()[. = ' " + text + " ']", in_node, null, XPathResult.UNORDERED_NODE_ITERATOR_TYPE, null).iterateNext();
    `
    return node
  end
end

#find_by_comment_without_xml(text, in_node) ⇒ Object

PhantomJS does not support xpath in document.evaluate



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/volt/page/targets/helpers/comment_searchers.rb', line 23

def find_by_comment_without_xml(text, in_node)
  match_text = " #{text} "
  `
    function walk(node) {
      if (node.nodeType === 8 && node.nodeValue === match_text) {
        return node;
      }

      var children = node.childNodes;
      if (children) {
        for (var i=0;i < children.length;i++) {
          var matched = walk(children[i]);
          if (matched) {
            return matched;
          }
        }
      }

      return null;
    }


    return walk(in_node);

  `
end