Module: Ferrum::Frame::DOM

Included in:
Ferrum::Frame
Defined in:
lib/ferrum/frame/dom.rb

Instance Method Summary collapse

Instance Method Details

#at_css(selector, within: nil) ⇒ Object



77
78
79
80
81
82
83
84
# File 'lib/ferrum/frame/dom.rb', line 77

def at_css(selector, within: nil)
  node_id = within&.node_id || @page.document_id

  id = @page.command("DOM.querySelector",
               nodeId: node_id,
               selector: selector)["nodeId"]
  build_node(id)
end

#at_xpath(selector, within: nil) ⇒ Object



39
40
41
# File 'lib/ferrum/frame/dom.rb', line 39

def at_xpath(selector, within: nil)
  xpath(selector, within: within).first
end

#bodyObject



35
36
37
# File 'lib/ferrum/frame/dom.rb', line 35

def body
  evaluate("document.documentElement.outerHTML")
end

#css(selector, within: nil) ⇒ Object

FIXME css doesn’t work for a frame w/o execution_id



68
69
70
71
72
73
74
75
# File 'lib/ferrum/frame/dom.rb', line 68

def css(selector, within: nil)
  node_id = within&.node_id || @page.document_id

  ids = @page.command("DOM.querySelectorAll",
                      nodeId: node_id,
                      selector: selector)["nodeIds"]
  ids.map { |id| build_node(id) }.compact
end

#current_titleObject



27
28
29
# File 'lib/ferrum/frame/dom.rb', line 27

def current_title
  evaluate("window.top.document.title")
end

#current_urlObject



23
24
25
# File 'lib/ferrum/frame/dom.rb', line 23

def current_url
  evaluate("window.top.location.href")
end

#doctypeObject



31
32
33
# File 'lib/ferrum/frame/dom.rb', line 31

def doctype
  evaluate("new XMLSerializer().serializeToString(document.doctype)")
end

#xpath(selector, within: nil) ⇒ Object

FIXME: Check within



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/ferrum/frame/dom.rb', line 44

def xpath(selector, within: nil)
  evaluate_async(%(
    try {
      let selector = arguments[0];
      let within = arguments[1] || document;
      let results = [];

      let xpath = document.evaluate(selector, within, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
      for (let i = 0; i < xpath.snapshotLength; i++) {
        results.push(xpath.snapshotItem(i));
      }

      arguments[2](results);
    } catch (error) {
      // DOMException.INVALID_EXPRESSION_ERR is undefined, using pure code
      if (error.code == DOMException.SYNTAX_ERR || error.code == 51) {
        throw "Invalid Selector";
      } else {
        throw error;
      }
    }), @page.timeout, selector, within)
end