module Volt::CommentSearchers

Constants

NO_XPATH

Public Instance Methods

build_from_html(html) click to toggle source

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

# 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`) click to toggle source
# File lib/volt/page/targets/helpers/comment_searchers.rb, line 9
def find_by_comment(text, in_node = `document`)
  if NO_XPATH
    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();
    `
    node
  end
end
find_by_comment_without_xml(text, in_node) click to toggle source

PhantomJS does not support xpath in document.evaluate

# 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