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 collapse

JS_METHOD_NAMES =

Own js_call methods, on top of Element’s.

%w[
  insertRow deleteRow createCaption deleteCaption createTHead deleteTHead
  createTFoot deleteTFoot createTBody
].freeze

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::PROCESSING_INSTRUCTION_NODE, Element::SHADOW_HOST_TAGS, 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::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 inherited from Element

#[], #[]=, #__dommy_backend_node__, #__internal_shadow_root__, #__test_scroll_log__, #after, #anchor_href, #animate, #append, #append_child, #at_xpath, #attach_shadow, #attributes, #base_uri, #before, #blur, #child_element_count, #child_nodes, #children, #class_list, #class_name, #class_name=, #click, #clone_node, #closest, #compare_document_position, #contains?, #dataset, #equal_node?, #first_child, #first_element_child, #focus, #get_animations, #get_attribute, #get_attribute_node, #get_elements_by_class_name, #get_elements_by_tag_name, #get_html, #get_inner_html, #has_attribute?, #has_attributes?, #has_child_nodes?, #id, #id=, #initialize, #inner_html, #inner_html=, #insert_adjacent_element, #insert_adjacent_html, #insert_adjacent_text, #insert_before, #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, #prepend, #previous_element_sibling, #previous_sibling, #query_selector, #query_selector_all, #reflected_attr_name, #remove, #remove_attribute, #remove_attribute_node, #remove_child, #replace_child, #replace_children, #replace_with_nodes, #role, #role=, #root_node, #same_node?, #set_attribute, #set_attribute_node, #shadow_root, #slot, #slot=, #style, #tag_name, #text_content, #text_content=, #to_s, #toggle_attribute, #xpath

Methods included from EventTarget

#__internal_deliver_event__, #add_event_listener, #dispatch_event, #invoke_listener, #remove_event_listener

Constructor Details

This class inherits a constructor from Dommy::Element

Instance Method Details

#__js_call__(method, args) ⇒ Object



3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
# File 'lib/dommy/html_elements.rb', line 3101

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



3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
# File 'lib/dommy/html_elements.rb', line 3075

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_method_names__Object



2927
2928
2929
# File 'lib/dommy/html_elements.rb', line 2927

def __js_method_names__
  super + JS_METHOD_NAMES
end

#__js_set__(key, value) ⇒ Object



3092
3093
3094
3095
3096
3097
3098
3099
# File 'lib/dommy/html_elements.rb', line 3092

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

#captionObject



2931
2932
2933
# File 'lib/dommy/html_elements.rb', line 2931

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

#caption=(new_caption) ⇒ Object



2935
2936
2937
2938
2939
2940
2941
# File 'lib/dommy/html_elements.rb', line 2935

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



2980
2981
2982
2983
2984
2985
2986
2987
2988
# File 'lib/dommy/html_elements.rb', line 2980

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



3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
# File 'lib/dommy/html_elements.rb', line 3031

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



3017
3018
3019
3020
3021
3022
3023
3024
# File 'lib/dommy/html_elements.rb', line 3017

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



2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
# File 'lib/dommy/html_elements.rb', line 2996

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



2990
2991
2992
2993
2994
# File 'lib/dommy/html_elements.rb', line 2990

def delete_caption
  cap = caption
  cap&.remove
  nil
end

#delete_row(index) ⇒ Object



3070
3071
3072
3073
# File 'lib/dommy/html_elements.rb', line 3070

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

#delete_t_footObject



3026
3027
3028
3029
# File 'lib/dommy/html_elements.rb', line 3026

def delete_t_foot
  t_foot&.remove
  nil
end

#delete_t_headObject



3012
3013
3014
3015
# File 'lib/dommy/html_elements.rb', line 3012

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>`.



3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
# File 'lib/dommy/html_elements.rb', line 3047

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



2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
# File 'lib/dommy/html_elements.rb', line 2963

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



2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
# File 'lib/dommy/html_elements.rb', line 2951

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



2947
2948
2949
# File 'lib/dommy/html_elements.rb', line 2947

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

#t_headObject



2943
2944
2945
# File 'lib/dommy/html_elements.rb', line 2943

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