Sha256: f7bd564f71790dfe489be86db765ec2990613516c922f101e23c2a6fcabfd16b

Contents?: true

Size: 1.4 KB

Versions: 5

Compression:

Stored size: 1.4 KB

Contents

module CommentSearchers
  if RUBY_PLATFORM == 'opal'
    PHANTOM = `!!window._phantom`
  else
    PHANTOM = false
  end

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

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

  # PhantomJS does not support xpath in document.evaluate
  def find_by_comment_without_xml(text, in_node)
    match_text = " #{text} "
    %x{
      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


  # Returns an unattached div with the nodes from the passed
  # in html.
  def build_from_html(html)
    temp_div = nil
    %x{
      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]);
        }
      }
    }
    return temp_div
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
volt-0.8.1 lib/volt/page/targets/helpers/comment_searchers.rb
volt-0.8.0 lib/volt/page/targets/helpers/comment_searchers.rb
volt-0.7.23 lib/volt/page/targets/helpers/comment_searchers.rb
volt-0.7.22 lib/volt/page/targets/helpers/comment_searchers.rb
volt-0.7.21 lib/volt/page/targets/helpers/comment_searchers.rb