<select> — exposes value (selected option's value), options,
selectedIndex, and dispatches change events. Minimal compared to
happy-dom's full HTMLSelectElement, but covers common test cases.
The option(s) that display as selected, applying the selectedness rules at read time: a single-select shows the LAST option whose selectedness is true (last-selected wins), or — if none is — its first option ("ask for reset"); a multiple select shows every selected option (or none).
The option(s) that display as selected, applying the selectedness rules at
read time: a single-select shows the LAST option whose selectedness is
true (last-selected wins), or — if none is — its first option ("ask for
reset"); a multiple select shows every selected option (or none).
# File 'lib/dommy/html_elements.rb', line 2811def__display_selected__opts=options.to_achosen=opts.select{|o|o.respond_to?(:selected)&&o.selected}ifmultiplechosenelsif!chosen.empty?# Single-select: the most recently property-selected option wins over an
# attribute-selected one; otherwise the last selected in document order.
dirty=chosen.select{|o|o.respond_to?(:__selectedness_dirty__)&&o.__selectedness_dirty__}[(dirty.empty??chosen:dirty).last]elsif!opts.empty?[opts.first]else[]endend
# File 'lib/dommy/html_elements.rb', line 2979def__js_call__(method,args)casemethodwhen"item"item(args[0])when"namedItem"named_item(args[0])when"add"add(args[0],args[1])when"remove"# HTMLSelectElement.remove(index) removes an option; with no argument it
# is ChildNode.remove() (removes the <select> itself).
args.empty??super:remove_option(args[0])when"checkValidity"check_validitywhen"reportValidity"report_validitywhen"setCustomValidity"set_custom_validity(args[0])elsesuperendend
# File 'lib/dommy/html_elements.rb', line 2926def__js_get__(key)casekeywhen"options"optionswhen"length"lengthwhen"value"valuewhen"size"sizewhen"selectedIndex"selected_indexwhen"selectedOptions"selected_optionswhen"form"formwhen"labels"labelswhen"type"typewhen"validity"validitywhen"willValidate"will_validatewhen"validationMessage"validation_messageelse# Indexed getter: `select[i]` is the option at index i (WebIDL).
returnitem(key)ifkey.is_a?(Integer)returnitem(key.to_i)ifkey.is_a?(String)&&key.match?(/\A\d+\z/)superendend
# File 'lib/dommy/html_elements.rb', line 2859defadd(option,before=nil)returnnilunlessoption.respond_to?(:__dommy_backend_node__)ifbefore.respond_to?(:__dommy_backend_node__)insert_before(option,before)elseappend_child(option)endnilend
#check_validity ⇒ Object
2911
2912
2913
2914
2915
# File 'lib/dommy/html_elements.rb', line 2911defcheck_validityok=!will_validate||validity.validdispatch_event(Event.new("invalid","bubbles"=>false,"cancelable"=>true))unlessokokend
#form ⇒ Object
2803
2804
2805
# File 'lib/dommy/html_elements.rb', line 2803defformclosest("form")end
#item(i) ⇒ Object
select.item(i) — returns the option at index i.
2854
2855
2856
# File 'lib/dommy/html_elements.rb', line 2854defitem(i)options[i.to_i]end
#labels ⇒ Object
2883
2884
2885
2886
2887
# File 'lib/dommy/html_elements.rb', line 2883deflabelsreturn[]ifid.empty?@document.query_selector_all("label[for='#{id}']")end
#length ⇒ Object
2788
2789
2790
# File 'lib/dommy/html_elements.rb', line 2788deflengthoptions.sizeend
#length=(n) ⇒ Object
select.length = n resizes the options list (delegates to the collection):
shrinks by removing trailing options, grows by appending blank ones.
2794
2795
2796
# File 'lib/dommy/html_elements.rb', line 2794deflength=(n)options.length=nend
#named_item(name) ⇒ Object
select.namedItem(name) — the first option whose id or name matches.
2799
2800
2801
# File 'lib/dommy/html_elements.rb', line 2799defnamed_item(name)options.named_item(name)end
#options ⇒ Object
options — all
2773
2774
2775
2776
2777
2778
# File 'lib/dommy/html_elements.rb', line 2773defoptionsel=selfHTMLOptionsCollection.new(self)doel.__dommy_backend_node__.css("option").map{|n|el.document.wrap_node(n)}.compactendend
#remove_option(i) ⇒ Object
select.remove(i) — removes the option at index i. (Note: also
inherits remove() from ChildNode for self-removal; spec lets
both forms coexist via overloading.)
2874
2875
2876
2877
2878
2879
2880
2881
# File 'lib/dommy/html_elements.rb', line 2874defremove_option(i)idx=i.to_i# An index out of range (including a negative one) is a no-op — NOT Ruby's
# from-the-end negative indexing.
returnifidx.negative?||idx>=options.lengthoptions[idx]&.removeend
#report_validity ⇒ Object
2917
2918
2919
# File 'lib/dommy/html_elements.rb', line 2917defreport_validitycheck_validityend
#selected_index ⇒ Object
2828
2829
2830
2831
2832
2833
2834
# File 'lib/dommy/html_elements.rb', line 2828defselected_indexopts=options.to_asel=__display_selected__.firstreturn-1unlessselopts.find_index{|o|o.__dommy_backend_node__.equal?(sel.__dommy_backend_node__)}||-1end
#selected_index=(i) ⇒ Object
2836
2837
2838
# File 'lib/dommy/html_elements.rb', line 2836defselected_index=(i)options.to_a.each_with_index{|o,idx|o.selected=(idx==i.to_i)}end
#selected_options ⇒ Object
selectedOptions — live collection of options with selected
attribute. When nothing is explicitly selected, browsers fall
back to the first option for non-multiple selects.
2783
2784
2785
2786
# File 'lib/dommy/html_elements.rb', line 2783defselected_optionsel=self@selected_options||=HTMLCollection.new{el.__display_selected__}end
#set_custom_validity(msg) ⇒ Object
2921
2922
2923
2924
# File 'lib/dommy/html_elements.rb', line 2921defset_custom_validity(msg)@custom_validity_message=msg.to_snilend
#size ⇒ Object
Own js_call methods, on top of Element's.
2766
2767
2768
# File 'lib/dommy/html_elements.rb', line 2766defsize@__node__["size"].to_s.to_iend
#type ⇒ Object
2889
2890
2891
# File 'lib/dommy/html_elements.rb', line 2889deftypemultiple?"select-multiple":"select-one"end
#validation_message ⇒ Object
2901
2902
2903
2904
2905
2906
2907
2908
2909
# File 'lib/dommy/html_elements.rb', line 2901defvalidation_messagereturn""unlesswill_validatemsg=(@custom_validity_message||"").to_sreturnmsgunlessmsg.empty?return"Please select an item in the list."ifvalidity.value_missing""end
#validity ⇒ Object
2893
2894
2895
# File 'lib/dommy/html_elements.rb', line 2893defvalidity@__validity||=ValidityState.new(self)end
#value ⇒ Object
value of the select = value of the (displayed) selected option, or "".
2841
2842
2843
2844
# File 'lib/dommy/html_elements.rb', line 2841defvaluesel=__display_selected__.firstsel?sel.value.to_s:""end
#value=(new_value) ⇒ Object
2846
2847
2848
2849
2850
2851
# File 'lib/dommy/html_elements.rb', line 2846defvalue=(new_value)opts=options.to_atarget=opts.find{|o|o.value.to_s==new_value.to_s}opts.each{|o|o.selected=false}target.selected=trueiftargetend
#will_validate ⇒ Object
2897
2898
2899
# File 'lib/dommy/html_elements.rb', line 2897defwill_validate!reflected_boolean("disabled")&&!disabled_by_ancestor_fieldset?&&closest("datalist").nil?end