Class: Dommy::HTMLFormElement

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

Overview

‘<form>` — element collection, submit/reset, and a stubbed validation surface.

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 HTMLElement

#case_sensitive_attribute_names?

Methods inherited from Element

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



329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
# File 'lib/dommy/html_elements.rb', line 329

def __js_call__(method, args)
  case method
  when "submit"
    submit
  when "reset"
    reset
  when "requestSubmit"
    request_submit(args[0])
  when "checkValidity"
    check_validity
  when "reportValidity"
    report_validity
  else
    super
  end
end

#__js_get__(key) ⇒ Object



283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
# File 'lib/dommy/html_elements.rb', line 283

def __js_get__(key)
  case key
  when "elements"
    elements
  when "length"
    length
  when "name"
    name
  when "action"
    action
  when "method"
    method_attr
  when "enctype"
    enctype
  when "target"
    target
  when "autocomplete"
    autocomplete
  when "acceptCharset"
    accept_charset
  when "noValidate"
    no_validate
  else
    super
  end
end

#__js_set__(key, value) ⇒ Object



310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
# File 'lib/dommy/html_elements.rb', line 310

def __js_set__(key, value)
  case key
  when "name"
    set_reflected_string("name", value)
  when "action"
    set_reflected_string("action", value)
  when "method"
    set_reflected_string("method", value)
  when "enctype"
    set_reflected_string("enctype", value)
  when "target"
    set_reflected_string("target", value)
  when "noValidate"
    set_reflected_boolean("novalidate", value)
  else
    super
  end
end

#accept_charsetObject



193
194
195
# File 'lib/dommy/html_elements.rb', line 193

def accept_charset
  reflected_string("accept-charset")
end

#actionObject



165
166
167
# File 'lib/dommy/html_elements.rb', line 165

def action
  reflected_string("action")
end

#action=(v) ⇒ Object



169
170
171
# File 'lib/dommy/html_elements.rb', line 169

def action=(v)
  set_reflected_string("action", v)
end

#autocompleteObject



189
190
191
# File 'lib/dommy/html_elements.rb', line 189

def autocomplete
  reflected_string("autocomplete")
end

#check_validityObject

Walk all listed elements; the form is “valid” iff every candidate control passes its own checkValidity. Dispatches a non-bubbling ‘invalid` event on each failing control.



264
265
266
267
268
269
270
271
272
273
274
275
276
277
# File 'lib/dommy/html_elements.rb', line 264

def check_validity
  ok = true
  elements.each do |el|
    next unless el.respond_to?(:will_validate)
    next unless el.will_validate
    next if el.validity.valid && (el.instance_variable_get(:@custom_validity_message) || "").empty?

    # Fire invalid event on this control (matches spec).
    el.dispatch_event(Event.new("invalid", "bubbles" => false, "cancelable" => true))
    ok = false
  end

  ok
end

#elementsObject

‘form.elements` — listed elements inside the form (excludes nested forms per spec; we approximate by walking input/select/textarea/button/output/fieldset). Returned as a live HTMLCollection so listening to `submit`/`reset` and adding fields between accesses works as expected.



210
211
212
213
214
215
216
217
218
219
220
221
# File 'lib/dommy/html_elements.rb', line 210

def elements
  el = self
  HTMLCollection.new do
    el
      .__node__
      .css("input, select, textarea, button, output, fieldset")
      .map do |n|
        el.document.wrap_node(n)
      end
      .compact
  end
end

#enctypeObject



181
182
183
# File 'lib/dommy/html_elements.rb', line 181

def enctype
  reflected_string("enctype")
end

#lengthObject



223
224
225
# File 'lib/dommy/html_elements.rb', line 223

def length
  elements.size
end

#method_attrObject



173
174
175
# File 'lib/dommy/html_elements.rb', line 173

def method_attr
  reflected_string("method")
end

#method_attr=(v) ⇒ Object



177
178
179
# File 'lib/dommy/html_elements.rb', line 177

def method_attr=(v)
  set_reflected_string("method", v)
end

#nameObject



157
158
159
# File 'lib/dommy/html_elements.rb', line 157

def name
  reflected_string("name")
end

#name=(v) ⇒ Object



161
162
163
# File 'lib/dommy/html_elements.rb', line 161

def name=(v)
  set_reflected_string("name", v)
end

#no_validateObject



197
198
199
# File 'lib/dommy/html_elements.rb', line 197

def no_validate
  reflected_boolean("novalidate")
end

#no_validate=(v) ⇒ Object



201
202
203
# File 'lib/dommy/html_elements.rb', line 201

def no_validate=(v)
  set_reflected_boolean("novalidate", v)
end

#report_validityObject



279
280
281
# File 'lib/dommy/html_elements.rb', line 279

def report_validity
  check_validity
end

#request_submit(submitter = nil) ⇒ Object

Spec: ‘requestSubmit(submitter?)` is the JS counterpart that MIRRORS user-initiated submission — it runs constraint validation and fires a `submit` event. Returns true if not default-prevented. `submitter` (if given) must be a button inside this form.



246
247
248
249
250
251
252
253
254
255
256
257
258
259
# File 'lib/dommy/html_elements.rb', line 246

def request_submit(submitter = nil)
  if submitter
    unless submitter.respond_to?(:__node__) && submitter.__node__.ancestors.include?(@__node__)
      raise DOMException::NotFoundError, "submitter is not a descendant of this form"
    end

    type = submitter.respond_to?(:type) ? submitter.type.to_s.downcase : ""
    unless %w[submit image].include?(type)
      raise TypeError, "submitter must be a submit button"
    end
  end

  dispatch_event(Event.new("submit", "bubbles" => true, "cancelable" => true))
end

#resetObject

Spec: ‘reset()` does fire a `reset` event; if the event is default-prevented, no reset happens. Dommy has no built-in control re-init logic, so we just dispatch the event.



238
239
240
# File 'lib/dommy/html_elements.rb', line 238

def reset
  dispatch_event(Event.new("reset", "bubbles" => true, "cancelable" => true))
end

#submitObject

Spec: ‘submit()` performs form submission directly WITHOUT firing a `submit` event. This is the JS-only entry point —browsers don’t run constraint validation either. Dommy has no navigation engine, so this is effectively a no-op (returns nil).



231
232
233
# File 'lib/dommy/html_elements.rb', line 231

def submit
  nil
end

#targetObject



185
186
187
# File 'lib/dommy/html_elements.rb', line 185

def target
  reflected_string("target")
end