RadioNodeList.cpp 1.6 KB

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