Class: Browser::DOM::Document

Inherits:
Element show all
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 inherited from Element

#attribute_nodes, #attributes, #class_name, #class_names, #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, #at_css, #at_xpath, #blur, create, #css, #data, #focus, #focused?, #hide, #inner_dom, #inner_dom=, new, #remove_attribute, #remove_class, #search, #show, #style, #toggle, #xpath

Methods included from Event::Target

#off, #on, #on!, #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?, new, #parse, #path, #prepend_to, #remove, #remove_child, #replace, #text?, #traverse

Instance Attribute Details

#active_elementElement (readonly)

Returns the element with focus.

Returns:

  • (Element)

    the element with focus



6
7
8
# File 'opal/browser/effects.rb', line 6

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

#bodyElement? (readonly)

Returns the body element of the document.

Returns:

  • (Element?)

    the body element of the document



25
26
27
# File 'opal/browser/dom/document.rb', line 25

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

#headElement? (readonly)

Returns the head element of the document.

Returns:

  • (Element?)

    the head element of the document



58
59
60
# File 'opal/browser/dom/document.rb', line 58

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

#locationLocation (readonly)

Returns the location for the document.

Returns:

  • (Location)

    the location for the document



82
83
84
# File 'opal/browser/location.rb', line 82

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

#rootElement?

Returns the root element of the document.

Returns:

  • (Element?)

    the root element of the document



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

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

#style_sheetsArray<CSS::StyleSheet> (readonly)

Returns the style sheets for the document.

Returns:



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

def style_sheets
  Native::Array.new(`#@native.styleSheets`) {|e|
    CSS::StyleSheet.new(e)
  }
end

#titleString

Returns the document title.

Returns:

  • (String)

    the document title



124
125
126
# File 'opal/browser/dom/document.rb', line 124

def title
  `#@native.title`
end

#windowWindow (readonly)

Returns the window for the document.

Returns:

  • (Window)

    the window for the document

Raises:

  • (NotImplementedError)


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

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



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

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

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

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

#create_element(name, options = {}) ⇒ Element

Create a new element for the document.

Parameters:

  • name (String)

    the node name

  • options (Hash) (defaults to: {})

    optional :namespace name

Returns:



35
36
37
38
39
40
41
# File 'opal/browser/dom/document.rb', line 35

def create_element(name, options = {})
  if ns = options[:namespace]
    DOM(`#@native.createElementNS(#{ns}, #{name})`)
  else
    DOM(`#@native.createElement(name)`)
  end
end

#create_text(content) ⇒ Text

Create a new text node for the document.

Parameters:

  • content (String)

    the text content

Returns:



48
49
50
# File 'opal/browser/dom/document.rb', line 48

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

#documentObject



52
53
54
# File 'opal/browser/dom/document.rb', line 52

def document
  self
end

#inspectObject



62
63
64
# File 'opal/browser/dom/document.rb', line 62

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

#ready(&block) ⇒ Object

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

Raises:

  • (NotImplementedError)


94
95
96
97
98
99
100
101
102
103
104
# File 'opal/browser/dom/document.rb', line 94

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)


100
101
102
# File 'opal/browser/dom/document.rb', line 100

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