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::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

#__node__, #document

Instance Method Summary collapse

Methods inherited from Element

#[], #[]=, #__shadow_root__, #after, #anchor_href, #append, #append_child, #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_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, #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

Methods included from EventTarget

#__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



3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
# File 'lib/dommy/html_elements.rb', line 3050

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



3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
# File 'lib/dommy/html_elements.rb', line 3024

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



3041
3042
3043
3044
3045
3046
3047
3048
# File 'lib/dommy/html_elements.rb', line 3041

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

#captionObject



2880
2881
2882
# File 'lib/dommy/html_elements.rb', line 2880

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

#caption=(new_caption) ⇒ Object



2884
2885
2886
2887
2888
2889
2890
# File 'lib/dommy/html_elements.rb', line 2884

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

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

#create_captionObject



2929
2930
2931
2932
2933
2934
2935
2936
2937
# File 'lib/dommy/html_elements.rb', line 2929

def create_caption
  existing = caption
  return existing if existing

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

#create_t_bodyObject



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

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

  tb
end

#create_t_footObject



2966
2967
2968
2969
2970
2971
2972
2973
# File 'lib/dommy/html_elements.rb', line 2966

def create_t_foot
  existing = t_foot
  return existing if existing

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

#create_t_headObject



2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
# File 'lib/dommy/html_elements.rb', line 2945

def create_t_head
  existing = t_head
  return existing if existing

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

  head
end

#delete_captionObject



2939
2940
2941
2942
2943
# File 'lib/dommy/html_elements.rb', line 2939

def delete_caption
  cap = caption
  cap&.remove
  nil
end

#delete_row(index) ⇒ Object



3019
3020
3021
3022
# File 'lib/dommy/html_elements.rb', line 3019

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

#delete_t_footObject



2975
2976
2977
2978
# File 'lib/dommy/html_elements.rb', line 2975

def delete_t_foot
  t_foot&.remove
  nil
end

#delete_t_headObject



2961
2962
2963
2964
# File 'lib/dommy/html_elements.rb', line 2961

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



2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
# File 'lib/dommy/html_elements.rb', line 2996

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.__node__.parent
    if section
      anchor.__node__.add_previous_sibling(tr.__node__)
      @document.notify_child_list_mutation(target_node: section, added_nodes: [tr.__node__], removed_nodes: [])
    end
  end

  tr
end

#rowsObject



2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
# File 'lib/dommy/html_elements.rb', line 2912

def rows
  el = self
  HTMLCollection.new do
    ordered = []
    head = el.__node__.element_children.find { |n| n.name == "thead" }
    bodies = el.__node__.element_children.select { |n| n.name == "tbody" }
    direct = el.__node__.element_children.select { |n| n.name == "tr" }
    foot = el.__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



2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
# File 'lib/dommy/html_elements.rb', line 2900

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

#t_footObject



2896
2897
2898
# File 'lib/dommy/html_elements.rb', line 2896

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

#t_headObject



2892
2893
2894
# File 'lib/dommy/html_elements.rb', line 2892

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