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



79
80
81
82
83
84
85
86
87
88
# File 'lib/ferrum/frame/dom.rb', line 79

def at_css(selector, within: nil)
  expr = <<~JS
    function(selector, within) {
      within ||= document
      return within.querySelector(selector);
    }
  JS

  evaluate_func(expr, selector, within)
end

#at_xpath(selector, within: nil) ⇒ Object



57
58
59
60
61
62
63
64
65
66
# File 'lib/ferrum/frame/dom.rb', line 57

def at_xpath(selector, within: nil)
  expr = <<~JS
    function(selector, within) {
      within ||= document
      let xpath = document.evaluate(selector, within, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
      return xpath.snapshotItem(0);
    }
  JS
  evaluate_func(expr, selector, within)
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



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

def css(selector, within: nil)
  expr = <<~JS
    function(selector, within) {
      within ||= document
      return Array.from(within.querySelectorAll(selector));
    }
  JS

  evaluate_func(expr, selector, within)
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("document.doctype && new XMLSerializer().serializeToString(document.doctype)")
end

#xpath(selector, within: nil) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/ferrum/frame/dom.rb', line 39

def xpath(selector, within: nil)
  expr = <<~JS
    function(selector, within) {
      let results = [];
      within ||= document

      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));
      }

      return results;
    }
  JS

  evaluate_func(expr, selector, within)
end