Class: Browser::DOM::Document
- Includes:
- DocumentOrShadowRoot
- Defined in:
- opal/browser/effects.rb,
opal/browser/location.rb,
opal/browser/dom/document.rb
Constant Summary
Constants inherited from Element
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
-
#active_element ⇒ Element
readonly
The element with focus.
-
#body ⇒ Element?
readonly
The body element of the document.
-
#head ⇒ Element?
readonly
The head element of the document.
-
#hidden? ⇒ Boolean
readonly
Is the page considered hidden?.
-
#location ⇒ Location
readonly
The location for the document.
-
#referrer ⇒ String
The referring document, or empty string if direct access.
-
#root ⇒ Element?
The root element of the document.
-
#title ⇒ String
The document title.
-
#visibility ⇒ String
readonly
The visibility state of the document - prerender, hidden or visible.
-
#window ⇒ Window
readonly
The window for the document.
Attributes included from DocumentOrShadowRoot
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
-
#[](what) ⇒ Element?
(also: #at)
Get the first element matching the given ID, CSS selector or XPath.
-
#create_comment(content) ⇒ Comment
Create a new comment node for the document.
-
#create_document_fragment ⇒ DocumentFragment
Create a new document fragment.
-
#create_element(name, builder = nil, **options, &block) ⇒ Element
Create a new element for the document.
-
#create_text(content) ⇒ Text
Create a new text node for the document.
- #document ⇒ Object
- #inspect ⇒ Object
-
#ready(&block) ⇒ Object
Wait for the document to be ready and call the block.
-
#ready? ⇒ Boolean
Check if the document is ready.
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_element ⇒ Element (readonly)
Returns the element with focus.
8 9 10 |
# File 'opal/browser/effects.rb', line 8 def active_element DOM(`#@native.activeElement`) end |
#body ⇒ Element? (readonly)
Returns the body element of the document.
27 28 29 30 31 |
# File 'opal/browser/dom/document.rb', line 27 def body DOM(`#@native.body`) rescue ArgumentError raise '$document.body is not defined; try to wrap your code in $document.ready{}' end |
#head ⇒ Element? (readonly)
Returns the head element of the document.
116 117 118 |
# File 'opal/browser/dom/document.rb', line 116 def head DOM(`#@native.getElementsByTagName("head")[0]`) end |
#hidden? ⇒ Boolean (readonly)
Returns is the page considered hidden?.
190 191 192 |
# File 'opal/browser/dom/document.rb', line 190 def hidden? `#@native.hidden` end |
#location ⇒ Location (readonly)
Returns 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 |
#referrer ⇒ String
Returns the referring document, or empty string if direct access.
164 165 166 |
# File 'opal/browser/dom/document.rb', line 164 def referrer `#@native.referrer` end |
#root ⇒ Element?
Returns the root element of the document.
170 171 172 |
# File 'opal/browser/dom/document.rb', line 170 def root DOM(`#@native.documentElement`) end |
#title ⇒ String
Returns the document title.
180 181 182 |
# File 'opal/browser/dom/document.rb', line 180 def title `#@native.title` end |
#visibility ⇒ String (readonly)
Returns the visibility state of the document - prerender, hidden or visible.
196 197 198 |
# File 'opal/browser/dom/document.rb', line 196 def visibility `#@native.visibilityState` end |
#window ⇒ Window (readonly)
Returns the window for the document.
211 212 213 |
# File 'opal/browser/dom/document.rb', line 211 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.
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_comment(content) ⇒ Comment
Create a new comment node for the document.
106 107 108 |
# File 'opal/browser/dom/document.rb', line 106 def create_comment(content) DOM(`#@native.createComment(#{content})`) end |
#create_document_fragment ⇒ DocumentFragment
Create a new document fragment.
88 89 90 |
# File 'opal/browser/dom/document.rb', line 88 def create_document_fragment DOM(`#@native.createDocumentFragment()`) end |
#create_element(name, builder = nil, **options, &block) ⇒ Element
Create a new element for the document.
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 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'opal/browser/dom/document.rb', line 44 def create_element(name, builder=nil, **, &block) opts = {} if [:is] ||= (.dig(:attrs, :is)) opts[:is] = [:is] end if ns = [:namespace] elem = `#@native.createElementNS(#{ns}, #{name}, #{opts.to_n})` else elem = `#@native.createElement(name, #{opts.to_n})` end if [:classes] `#{elem}.className = #{Array(options[:classes]).join(" ")}` end if [:id] `#{elem}.id = #{options[:id]}` end if [:attrs] [: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.
97 98 99 |
# File 'opal/browser/dom/document.rb', line 97 def create_text(content) DOM(`#@native.createTextNode(#{content})`) end |
#document ⇒ Object
110 111 112 |
# File 'opal/browser/dom/document.rb', line 110 def document self end |
#inspect ⇒ Object
120 121 122 |
# File 'opal/browser/dom/document.rb', line 120 def inspect "#<DOM::Document>" end |
#ready(&block) ⇒ Object
Wait for the document to be ready and call the block.
152 153 154 155 156 157 158 159 160 161 162 |
# File 'opal/browser/dom/document.rb', line 152 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.
158 159 160 |
# File 'opal/browser/dom/document.rb', line 158 def ready? `#@native.readyState === "complete" || #@native.readyState === "interactive"` end |