RadioNodeList.cpp (1765B)
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */ 3 /* This Source Code Form is subject to the terms of the Mozilla Public 4 * License, v. 2.0. If a copy of the MPL was not distributed with this 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 7 #include "mozilla/dom/RadioNodeList.h" 8 9 #include "HTMLInputElement.h" 10 #include "js/TypeDecls.h" 11 #include "mozilla/dom/BindingUtils.h" 12 #include "mozilla/dom/RadioNodeListBinding.h" 13 14 namespace mozilla::dom { 15 16 /* virtual */ 17 JSObject* RadioNodeList::WrapObject(JSContext* aCx, 18 JS::Handle<JSObject*> aGivenProto) { 19 return RadioNodeList_Binding::Wrap(aCx, this, aGivenProto); 20 } 21 22 HTMLInputElement* GetAsRadio(nsIContent* node) { 23 auto* el = HTMLInputElement::FromNode(node); 24 if (el && el->ControlType() == FormControlType::InputRadio) { 25 return el; 26 } 27 return nullptr; 28 } 29 30 void RadioNodeList::GetValue(nsString& retval, CallerType aCallerType) { 31 for (uint32_t i = 0; i < Length(); i++) { 32 HTMLInputElement* maybeRadio = GetAsRadio(Item(i)); 33 if (maybeRadio && maybeRadio->Checked()) { 34 maybeRadio->GetValue(retval, aCallerType); 35 return; 36 } 37 } 38 retval.Truncate(); 39 } 40 41 void RadioNodeList::SetValue(const nsAString& value, CallerType aCallerType) { 42 for (uint32_t i = 0; i < Length(); i++) { 43 HTMLInputElement* maybeRadio = GetAsRadio(Item(i)); 44 if (!maybeRadio) { 45 continue; 46 } 47 48 nsString curval = nsString(); 49 maybeRadio->GetValue(curval, aCallerType); 50 if (curval.Equals(value)) { 51 maybeRadio->SetChecked(true); 52 return; 53 } 54 } 55 } 56 57 NS_IMPL_ISUPPORTS_INHERITED(RadioNodeList, nsSimpleContentList, RadioNodeList) 58 59 } // namespace mozilla::dom