Class: Browser::DOM::Document

Inherits:
Element show all
Includes:
DocumentOrShadowRoot
Defined in:
opal/browser/effects.rb,
opal/browser/location.rb,
opal/browser/dom/document.rb

Constant Summary

Constants inherited from Element

Element::Img

Constants inherited from Node

Node::ATTRIBUTE_NODE, Node::CDATA_SECTION_NODE, Node::COMMENT_NODE, Node::DOCUMENT_FRAGMENT_NODE, Node::DOCUMENT_NODE, Node::DOCUMENT_TYPE_NODE, Node::ELEMENT_NODE, Node::ENTITY_NODE, Node::ENTITY_REFERENCE_NOCE, Node::NOTATION_NODE, Node::PROCESSING_INSTRUCTION_NODE, Node::TEXT_NODE

Instance Attribute Summary collapse

Attributes included from DocumentOrShadowRoot

#style_sheets

Attributes inherited from Element

#attribute_nodes, #attributes, #class_name, #class_names, #editable, #height, #id, #offset, #position, #scroll, #size, #style!, #width

Attributes inherited from Node

#child, #children, #element_children, #first_element_child, #inner_html, #last_element_child, #name, #namespace, #next, #next_element, #node_type, #parent, #previous, #previous_element, #value

Instance Method Summary collapse

Methods inherited from Element

#/, #=~, #[]=, #add_class, #animate, #animation_queue, #at_css, #at_xpath, #blur, #click, create, #css, #data, def_selector, #edit, #editable?, #fade_in, #fade_out, #fade_toggle, #focus, #focused?, #hide, #inner_dom, #inner_dom=, native_matches?, new, #remove_attribute, #remove_class, #search, selector, #shadow, #shadow?, #show, #slide_down, #slide_toggle, #slide_up, #style, subclasses, tag_name, #toggle, #toggle_class, #visible?, #xpath

Methods included from Event::Target

#off, #on, #on!, #one, #trigger, #trigger!

Methods inherited from Node

#<<, #==, #>>, #add_child, #add_next_sibling, #add_previous_sibling, #ancestors, #append_to, #blank?, #cdata?, #clear, #comment?, #content, #content=, #document?, #elem?, #fragment?, #initialize_copy, new, #parse, #path, #prepend_to, #remove, #remove_child, #replace, #text?, #traverse

Methods included from NativeCachedWrapper

#restricted?, #set_native_reference

Instance Attribute Details

#active_elementElement (readonly)

Returns the element with focus.

Returns:

  • (Element)

    the element with focus


9
10
11
# File 'opal/browser/effects.rb', line 9

def active_element
  DOM(`#@native.activeElement`)
end

#bodyElement? (readonly)

Returns the body element of the document.

Returns:

  • (Element?)

    the body element of the document


27
28
29
# File 'opal/browser/dom/document.rb', line 27

def body
  DOM(`#@native.body`)
end

#headElement? (readonly)

Returns the head element of the document.

Returns:

  • (Element?)

    the head element of the document


94
95
96
# File 'opal/browser/dom/document.rb', line 94

def head
  DOM(`#@native.getElementsByTagName("head")[0]`)
end

#hidden?Boolean (readonly)

Returns is the page considered hidden?.

Returns:

  • (Boolean)

    is the page considered hidden?


168
169
170
# File 'opal/browser/dom/document.rb', line 168

def hidden?
  `#@native.hidden`
end

#locationLocation (readonly)

Returns the location for the document.

Returns:

  • (Location)

    the location for the document


88
89
90
# File 'opal/browser/location.rb', line 88

def location
  Location.new(`#@native.location`) if `#@native.location`
end

#referrerString

Returns the referring document, or empty string if direct access.

Returns:

  • (String)

    the referring document, or empty string if direct access


142
143
144
# File 'opal/browser/dom/document.rb', line 142

def referrer
  `#@native.referrer`
end

#rootElement?

Returns the root element of the document.

Returns:

  • (Element?)

    the root element of the document


148
149
150
# File 'opal/browser/dom/document.rb', line 148

def root
  DOM(`#@native.documentElement`)
end

#titleString

Returns the document title.

Returns:

  • (String)

    the document title


158
159
160
# File 'opal/browser/dom/document.rb', line 158

def title
  `#@native.title`
end

#visibilityString (readonly)

Returns the visibility state of the document - prerender, hidden or visible.

Returns:

  • (String)

    the visibility state of the document - prerender, hidden or visible


174
175
176
# File 'opal/browser/dom/document.rb', line 174

def visibility
  `#@native.visibilityState`
end

#windowWindow (readonly)

Returns the window for the document.

Returns:

  • (Window)

    the window for the document

Raises:

  • (NotImplementedError)

189
190
191
# File 'opal/browser/dom/document.rb', line 189

def window
  Window.new(`#@native.defaultView`)
end

Instance Method Details

#[](what) ⇒ Element? Also known as: at

Get the first element matching the given ID, CSS selector or XPath.

Parameters:

  • what (String)

    ID, CSS selector or XPath

Returns:

  • (Element?)

    the first matching element


11
12
13
14
15
16
17
18
19
20
21
# File 'opal/browser/dom/document.rb', line 11

def [](what)
  %x{
    var result = #@native.getElementById(what);

    if (result) {
      return #{DOM(`result`)};
    }
  }

  css(what).first || xpath(what).first
end

#create_document_fragmentDocumentFragment

Create a new document fragment.

Returns:


75
76
77
# File 'opal/browser/dom/document.rb', line 75

def create_document_fragment
  DOM(`#@native.createDocumentFragment()`)
end

#create_element(name, **options) ⇒ Element

Create a new element for the document.

Parameters:

  • name (String)

    the node name

  • options (String)

    :namespace optional namespace name

  • options (String)

    :is optional WebComponents is parameter

  • options (String)

    :id optional id to set

  • options (Array<String>)

    :classes optional classes to set

  • options (Hash)

    :attrs optional attributes to set

Returns:


41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'opal/browser/dom/document.rb', line 41

def create_element(name, **options)
  opts = {}

  if options[:is] ||= (options.dig(:attrs, :fragment))
    opts[:is] = options[:is]
  end

  if ns = options[:namespace]
    elem = `#@native.createElementNS(#{ns}, #{name}, #{opts.to_n})`
  else
    elem = `#@native.createElement(name, #{opts.to_n})`
  end

  if options[:classes]
    `#{elem}.className = #{Array(options[:classes]).join(" ")}`
  end

  if options[:id]
    `#{elem}.id = #{options[:id]}`
  end

  if options[:attrs]
    options[:attrs].each do |k,v|
      next unless v
      `#{elem}.setAttribute(#{k}, #{v})`
    end
  end

  DOM(elem)
end

#create_text(content) ⇒ Text

Create a new text node for the document.

Parameters:

  • content (String)

    the text content

Returns:


84
85
86
# File 'opal/browser/dom/document.rb', line 84

def create_text(content)
  DOM(`#@native.createTextNode(#{content})`)
end

#documentObject


88
89
90
# File 'opal/browser/dom/document.rb', line 88

def document
  self
end

#inspectObject


98
99
100
# File 'opal/browser/dom/document.rb', line 98

def inspect
  "#<DOM::Document>"
end

#ready(&block) ⇒ Object

Wait for the document to be ready and call the block.

Raises:

  • (NotImplementedError)

130
131
132
133
134
135
136
137
138
139
140
# File 'opal/browser/dom/document.rb', line 130

def ready(&block)
  raise ArgumentError, 'no block given' unless block

  return block.call if ready?

  on 'dom:load' do |e|
    e.off

    block.call
  end
end

#ready?Boolean

Check if the document is ready.

Returns:

  • (Boolean)

136
137
138
# File 'opal/browser/dom/document.rb', line 136

def ready?
  `#@native.readyState === "complete" || #@native.readyState === "interactive"`
end