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, #inner_html, #offset, #outer_html, #position, #scroll, #size, #style!, #width

Attributes inherited from Node

#child, #children, #element_children, #first_element_child, #last_element_child, #name, #namespace, #next, #next_element, #node_type, #outer_html, #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_is?, 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, #attached?, #blank?, #cdata?, #clear, #comment?, #content, #content=, #custom?, #document?, #elem?, #fragment?, #initialize, #initialize_copy, new, #parse, #path, #prepend_to, #remove, #remove_child, #replace, #text?, #traverse

Methods included from NativeCachedWrapper

#restricted?, #set_native_reference

Constructor Details

This class inherits a constructor from Browser::DOM::Node

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



28
29
30
31
32
# File 'opal/browser/dom/document.rb', line 28

def body
  DOM(`#@native.body`)
rescue ArgumentError
  raise '$document.body is not defined; try to wrap your code in $document.ready{}'
end

#headElement? (readonly)

Returns the head element of the document.

Returns:

  • (Element?)

    the head element of the document



117
118
119
# File 'opal/browser/dom/document.rb', line 117

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

#hidden?Boolean (readonly)

Returns is the page considered hidden?.

Returns:

  • (Boolean)

    is the page considered hidden?



191
192
193
# File 'opal/browser/dom/document.rb', line 191

def hidden?
  `#@native.hidden`
end

#locationLocation (readonly)

Returns the location for the document.

Returns:

  • (Location)

    the location for the document



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

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



165
166
167
# File 'opal/browser/dom/document.rb', line 165

def referrer
  `#@native.referrer`
end

#rootElement?

Returns the root element of the document.

Returns:

  • (Element?)

    the root element of the document



171
172
173
# File 'opal/browser/dom/document.rb', line 171

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

#titleString

Returns the document title.

Returns:

  • (String)

    the document title



181
182
183
# File 'opal/browser/dom/document.rb', line 181

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



197
198
199
# File 'opal/browser/dom/document.rb', line 197

def visibility
  `#@native.visibilityState`
end

#windowWindow (readonly)

Returns the window for the document.

Returns:

  • (Window)

    the window for the document

Raises:

  • (NotImplementedError)


212
213
214
# File 'opal/browser/dom/document.rb', line 212

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



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

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

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

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

#create_comment(content) ⇒ Comment

Create a new comment node for the document.

Parameters:

  • content (String)

    the comment content

Returns:



107
108
109
# File 'opal/browser/dom/document.rb', line 107

def create_comment(content)
  DOM(`#@native.createComment(#{content})`)
end

#create_document_fragmentDocumentFragment

Create a new document fragment.

Returns:



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

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

#create_element(name, builder = nil, **options, &block) ⇒ Element

Create a new element for the document.

Parameters:

  • name (String)

    the node name

  • builder (Browser::DOM::Builder) (defaults to: nil)

    optional builder to append element to

  • 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:



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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'opal/browser/dom/document.rb', line 45

def create_element(name, builder=nil, **options, &block)
  opts = {}

  if options[:is] ||= (options.dig(:attrs, :is))
    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 = DOM(elem)

  if block_given?
    dom.inner_dom(builder, &block)
  end

  if builder
    builder << dom
  end

  dom
end

#create_text(content) ⇒ Text

Create a new text node for the document.

Parameters:

  • content (String)

    the text content

Returns:



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

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

#documentObject



111
112
113
# File 'opal/browser/dom/document.rb', line 111

def document
  self
end

#inspectObject



121
122
123
# File 'opal/browser/dom/document.rb', line 121

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

#ready(&block) ⇒ Object

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

Raises:

  • (NotImplementedError)


153
154
155
156
157
158
159
160
161
162
163
# File 'opal/browser/dom/document.rb', line 153

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)


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

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