Class: Dommy::HTMLTableElement

Inherits:
HTMLElement show all
Defined in:
lib/dommy/html_elements.rb

Overview

‘<table>` — top-level table element. `rows` returns rows from all sections (thead → tbody → tfoot); `tBodies` is a list of tbody elements. `insertRow(-1)` appends to the last tbody (or creates one); `deleteRow` works against the merged `rows` list.

Constant Summary

Constants inherited from Element

Element::ATTRIBUTE_NODE, Element::CDATA_SECTION_NODE, Element::COMMENT_NODE, Element::DOCUMENT_FRAGMENT_NODE, Element::DOCUMENT_NODE, Element::DOCUMENT_POSITION_CONTAINED_BY, Element::DOCUMENT_POSITION_CONTAINS, Element::DOCUMENT_POSITION_DISCONNECTED, Element::DOCUMENT_POSITION_FOLLOWING, Element::DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC, Element::DOCUMENT_POSITION_PRECEDING, Element::DOCUMENT_TYPE_NODE, Element::ELEMENT_NODE, Element::HTML_NAMESPACE, Element::PROCESSING_INSTRUCTION_NODE, Element::REFLECTED_TOKEN_LIST_HOSTS, Element::SHADOW_HOST_TAGS, Element::SVG_NAMESPACE, Element::TEXT_NODE

Constants included from Node

Node::ATTRIBUTE_NODE, Node::CDATA_SECTION_NODE, Node::COMMENT_NODE, Node::DOCUMENT_FRAGMENT_NODE, Node::DOCUMENT_NODE, Node::DOCUMENT_POSITION_CONTAINED_BY, Node::DOCUMENT_POSITION_CONTAINS, Node::DOCUMENT_POSITION_DISCONNECTED, Node::DOCUMENT_POSITION_FOLLOWING, Node::DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC, Node::DOCUMENT_POSITION_PRECEDING, Node::DOCUMENT_TYPE_NODE, Node::ELEMENT_NODE, Node::HTML_NAMESPACE, Node::PROCESSING_INSTRUCTION_NODE, Node::TEXT_NODE

Instance Attribute Summary

Attributes inherited from Element

#document

Instance Method Summary collapse

Methods inherited from HTMLElement

#case_sensitive_attribute_names?

Methods included from Internal::ReflectedAttributes

included

Methods inherited from Element

#[], #[]=, #__dommy_backend_node__, #__internal_set_namespace__, #__internal_shadow_root__, #__test_scroll_log__, #access_key_label, #after, #anchor_href, #animate, #aria_content_attr, #aria_element_attr, #aria_element_get, #aria_element_set, #aria_elements_attr, #aria_elements_get, #aria_elements_set, #aria_find_in_root, #aria_get, #aria_set, #at_xpath, #attach_shadow, #attributes, #base_uri, #before, #blur, #child_element_count, #child_nodes, #children, #class_list, #class_name, #class_name=, #clear_aria_element_ref_for, #click, #clone_node, #closest, #contains?, #dataset, #element_prefix, #equal_node?, #first_child, #first_element_child, #focus, #get_animations, #get_attribute, #get_attribute_names, #get_attribute_node, #get_attribute_node_ns, #get_attribute_ns, #get_bounding_client_rect, #get_client_rects, #get_elements_by_class_name, #get_elements_by_tag_name, #get_elements_by_tag_name_ns, #get_html, #get_inner_html, #has_attribute?, #has_attribute_ns?, #has_attributes?, #has_child_nodes?, #hide_popover, #id, #id=, #initialize, #inner_html, #inner_html=, #insert_adjacent_element, #insert_adjacent_html, #insert_adjacent_text, #insert_before, #insertion_parent!, #is_connected?, #last_child, #last_element_child, #live_child_nodes, #local_name, #matches?, #namespace_uri, #next_element_sibling, #next_sibling, #normalize, #on, #outer_html, #outer_html=, #owner_document, #parent_element, #parent_node, #path, #previous_element_sibling, #previous_sibling, #query_selector, #query_selector_all, #reflected_attr_name, #reflected_token_list, #remove, #remove_attribute, #remove_attribute_node, #remove_attribute_ns, #remove_child, #replace_child, #replace_with_nodes, #request_fullscreen, #role, #role=, #root_node, #same_node?, #scoped_query, #set_attribute, #set_attribute_node, #set_attribute_ns, #shadow_root, #show_popover, #slot, #slot=, #style, #tag_name, #text_content, #text_content=, #to_s, #toggle_attribute, #toggle_popover, #with_selector_errors, #xpath

Methods included from Bridge::Methods

included

Methods included from Internal::ParentNode

#append, #append_child, #prepend, #replace_children

Methods included from Node

#compare_document_position, #get_root_node, #is_default_namespace, #is_equal_node, #is_same_node, #lookup_namespace_uri, #lookup_prefix

Methods included from EventTarget

#__internal_deliver_event__, #__internal_event_parent__, #add_event_listener, capture_flag, #deliver_at, #dispatch_event, js_truthy?, #remove_event_listener

Constructor Details

This class inherits a constructor from Dommy::Element

Instance Method Details

#__js_call__(method, args) ⇒ Object



2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
# File 'lib/dommy/html_elements.rb', line 2321

def __js_call__(method, args)
  case method
  when "insertRow"
    insert_row(args[0] || -1)
  when "deleteRow"
    delete_row(args[0])
  when "createCaption"
    create_caption
  when "deleteCaption"
    delete_caption
  when "createTHead"
    create_t_head
  when "deleteTHead"
    delete_t_head
  when "createTFoot"
    create_t_foot
  when "deleteTFoot"
    delete_t_foot
  when "createTBody"
    create_t_body
  else
    super
  end
end

#__js_get__(key) ⇒ Object



2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
# File 'lib/dommy/html_elements.rb', line 2291

def __js_get__(key)
  case key
  when "caption"
    caption
  when "tHead"
    t_head
  when "tFoot"
    t_foot
  when "tBodies"
    t_bodies
  when "rows"
    rows
  else
    super
  end
end

#__js_set__(key, value) ⇒ Object



2308
2309
2310
2311
2312
2313
2314
2315
# File 'lib/dommy/html_elements.rb', line 2308

def __js_set__(key, value)
  case key
  when "caption"
    self.caption = value
  else
    super
  end
end

#captionObject

Own js_call methods, on top of Element’s.



2147
2148
2149
# File 'lib/dommy/html_elements.rb', line 2147

def caption
  @__node__.element_children.find { |n| n.name == "caption" }&.then { |n| @document.wrap_node(n) }
end

#caption=(new_caption) ⇒ Object



2151
2152
2153
2154
2155
2156
2157
# File 'lib/dommy/html_elements.rb', line 2151

def caption=(new_caption)
  delete_caption
  return unless new_caption.respond_to?(:__dommy_backend_node__)

  first = @__node__.children.first
  first ? first.add_previous_sibling(new_caption.__dommy_backend_node__) : @__node__.add_child(new_caption.__dommy_backend_node__)
end

#create_captionObject



2196
2197
2198
2199
2200
2201
2202
2203
2204
# File 'lib/dommy/html_elements.rb', line 2196

def create_caption
  existing = caption
  return existing if existing

  cap = @document.create_element("caption")
  first = @__node__.children.first
  first ? first.add_previous_sibling(cap.__dommy_backend_node__) : @__node__.add_child(cap.__dommy_backend_node__)
  cap
end

#create_t_bodyObject



2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
# File 'lib/dommy/html_elements.rb', line 2247

def create_t_body
  tb = @document.create_element("tbody")
  last_tbody = t_bodies.last
  if last_tbody
    last_tbody.__dommy_backend_node__.add_next_sibling(tb.__dommy_backend_node__)
  else
    @__node__.add_child(tb.__dommy_backend_node__)
  end

  tb
end

#create_t_footObject



2233
2234
2235
2236
2237
2238
2239
2240
# File 'lib/dommy/html_elements.rb', line 2233

def create_t_foot
  existing = t_foot
  return existing if existing

  foot = @document.create_element("tfoot")
  @__node__.add_child(foot.__dommy_backend_node__)
  foot
end

#create_t_headObject



2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
# File 'lib/dommy/html_elements.rb', line 2212

def create_t_head
  existing = t_head
  return existing if existing

  head = @document.create_element("thead")
  cap = caption
  if cap
    cap.__dommy_backend_node__.add_next_sibling(head.__dommy_backend_node__)
  else
    first = @__node__.children.first
    first ? first.add_previous_sibling(head.__dommy_backend_node__) : @__node__.add_child(head.__dommy_backend_node__)
  end

  head
end

#delete_captionObject



2206
2207
2208
2209
2210
# File 'lib/dommy/html_elements.rb', line 2206

def delete_caption
  cap = caption
  cap&.remove
  nil
end

#delete_row(index) ⇒ Object



2286
2287
2288
2289
# File 'lib/dommy/html_elements.rb', line 2286

def delete_row(index)
  rows[index.to_i]&.remove
  nil
end

#delete_t_footObject



2242
2243
2244
2245
# File 'lib/dommy/html_elements.rb', line 2242

def delete_t_foot
  t_foot&.remove
  nil
end

#delete_t_headObject



2228
2229
2230
2231
# File 'lib/dommy/html_elements.rb', line 2228

def delete_t_head
  t_head&.remove
  nil
end

#insert_row(index = -1)) ⇒ Object

‘table.insertRow(index)` — inserts a `<tr>` at the merged index. Per spec, if no `<tbody>` exists and the table is empty, the row is inserted directly; otherwise it goes into the last `<tbody>`.



2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
# File 'lib/dommy/html_elements.rb', line 2263

def insert_row(index = -1)
  list = rows.to_a
  raw = index.to_i
  raise DOMException::IndexSizeError, "row index #{raw} out of range" if raw < -1 || raw > list.size

  idx = raw == -1 ? list.size : raw

  tr = @document.create_element("tr")
  if idx == list.size
    target_section = t_bodies.last || create_t_body
    target_section.append_child(tr)
  else
    anchor = list[idx]
    section = anchor.__dommy_backend_node__.parent
    if section
      anchor.__dommy_backend_node__.add_previous_sibling(tr.__dommy_backend_node__)
      @document.notify_child_list_mutation(target_node: section, added_nodes: [tr.__dommy_backend_node__], removed_nodes: [])
    end
  end

  tr
end

#rowsObject



2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
# File 'lib/dommy/html_elements.rb', line 2179

def rows
  el = self
  HTMLCollection.new do
    ordered = []
    head = el.__dommy_backend_node__.element_children.find { |n| n.name == "thead" }
    bodies = el.__dommy_backend_node__.element_children.select { |n| n.name == "tbody" }
    direct = el.__dommy_backend_node__.element_children.select { |n| n.name == "tr" }
    foot = el.__dommy_backend_node__.element_children.find { |n| n.name == "tfoot" }
    [head, *bodies, foot].compact.each do |sec|
      sec.element_children.select { |n| n.name == "tr" }.each { |n| ordered << n }
    end

    direct.each { |n| ordered << n }
    ordered.map { |n| el.document.wrap_node(n) }.compact
  end
end

#t_bodiesObject



2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
# File 'lib/dommy/html_elements.rb', line 2167

def t_bodies
  el = self
  HTMLCollection.new do
    el
      .__dommy_backend_node__
      .element_children
      .select { |n| n.name == "tbody" }
      .map { |n| el.document.wrap_node(n) }
      .compact
  end
end

#t_footObject



2163
2164
2165
# File 'lib/dommy/html_elements.rb', line 2163

def t_foot
  @__node__.element_children.find { |n| n.name == "tfoot" }&.then { |n| @document.wrap_node(n) }
end

#t_headObject



2159
2160
2161
# File 'lib/dommy/html_elements.rb', line 2159

def t_head
  @__node__.element_children.find { |n| n.name == "thead" }&.then { |n| @document.wrap_node(n) }
end