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



340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
# File 'lib/dommy/html_elements.rb', line 340

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



294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
# File 'lib/dommy/html_elements.rb', line 294

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



321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
# File 'lib/dommy/html_elements.rb', line 321

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



204
205
206
# File 'lib/dommy/html_elements.rb', line 204

def accept_charset
  reflected_string("accept-charset")
end

#actionObject



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

def action
  reflected_string("action")
end

#action=(v) ⇒ Object



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

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

#autocompleteObject



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

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.



275
276
277
278
279
280
281
282
283
284
285
286
287
288
# File 'lib/dommy/html_elements.rb', line 275

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.



221
222
223
224
225
226
227
228
229
230
231
232
# File 'lib/dommy/html_elements.rb', line 221

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



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

def enctype
  reflected_string("enctype")
end

#lengthObject



234
235
236
# File 'lib/dommy/html_elements.rb', line 234

def length
  elements.size
end

#method_attrObject



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

def method_attr
  reflected_string("method")
end

#method_attr=(v) ⇒ Object



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

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

#nameObject



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

def name
  reflected_string("name")
end

#name=(v) ⇒ Object



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

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

#no_validateObject



208
209
210
# File 'lib/dommy/html_elements.rb', line 208

def no_validate
  reflected_boolean("novalidate")
end

#no_validate=(v) ⇒ Object



212
213
214
# File 'lib/dommy/html_elements.rb', line 212

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

#report_validityObject



290
291
292
# File 'lib/dommy/html_elements.rb', line 290

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.



257
258
259
260
261
262
263
264
265
266
267
268
269
270
# File 'lib/dommy/html_elements.rb', line 257

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.



249
250
251
# File 'lib/dommy/html_elements.rb', line 249

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



242
243
244
# File 'lib/dommy/html_elements.rb', line 242

def submit
  nil
end

#targetObject



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

def target
  reflected_string("target")
end