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.
9 10 11 |
# File 'opal/browser/effects.rb', line 9 def active_element DOM(`#@native.activeElement`) end |
#body ⇒ Element? (readonly)
Returns 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 |
#head ⇒ Element? (readonly)
Returns 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?.
191 192 193 |
# File 'opal/browser/dom/document.rb', line 191 def hidden? `#@native.hidden` end |
#location ⇒ Location (readonly)
Returns 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 |
#referrer ⇒ String
Returns 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 |
#root ⇒ Element?
Returns the root element of the document.
171 172 173 |
# File 'opal/browser/dom/document.rb', line 171 def root DOM(`#@native.documentElement`) end |
#title ⇒ String
Returns the document title.
181 182 183 |
# File 'opal/browser/dom/document.rb', line 181 def title `#@native.title` end |
#visibility ⇒ String (readonly)
Returns 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 |
Instance Method Details
#[](what) ⇒ Element? Also known as: at
Get the first element matching the given ID, CSS selector or XPath.
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.
107 108 109 |
# File 'opal/browser/dom/document.rb', line 107 def create_comment(content) DOM(`#@native.createComment(#{content})`) end |
#create_document_fragment ⇒ DocumentFragment
Create a new document fragment.
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.
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, **, &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([:classes]).join(" ")}` end if [:id] `#{elem}.id = #{[: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.
98 99 100 |
# File 'opal/browser/dom/document.rb', line 98 def create_text(content) DOM(`#@native.createTextNode(#{content})`) end |
#document ⇒ Object
111 112 113 |
# File 'opal/browser/dom/document.rb', line 111 def document self end |
#inspect ⇒ Object
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.
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.
159 160 161 |
# File 'opal/browser/dom/document.rb', line 159 def ready? `#@native.readyState === "complete" || #@native.readyState === "interactive"` end |