HTMLOptGroupElement.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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/EventDispatcher.h"
  6. #include "mozilla/EventStates.h"
  7. #include "mozilla/dom/HTMLOptGroupElement.h"
  8. #include "mozilla/dom/HTMLOptGroupElementBinding.h"
  9. #include "mozilla/dom/HTMLSelectElement.h" // SafeOptionListMutation
  10. #include "nsGkAtoms.h"
  11. #include "nsStyleConsts.h"
  12. #include "nsIFrame.h"
  13. #include "nsIFormControlFrame.h"
  14. NS_IMPL_NS_NEW_HTML_ELEMENT(OptGroup)
  15. namespace mozilla {
  16. namespace dom {
  17. /**
  18. * The implementation of <optgroup>
  19. */
  20. HTMLOptGroupElement::HTMLOptGroupElement(already_AddRefed<mozilla::dom::NodeInfo>& aNodeInfo)
  21. : nsGenericHTMLElement(aNodeInfo)
  22. {
  23. // We start off enabled
  24. AddStatesSilently(NS_EVENT_STATE_ENABLED);
  25. }
  26. HTMLOptGroupElement::~HTMLOptGroupElement()
  27. {
  28. }
  29. NS_IMPL_ISUPPORTS_INHERITED(HTMLOptGroupElement, nsGenericHTMLElement,
  30. nsIDOMHTMLOptGroupElement)
  31. NS_IMPL_ELEMENT_CLONE(HTMLOptGroupElement)
  32. NS_IMPL_BOOL_ATTR(HTMLOptGroupElement, Disabled, disabled)
  33. NS_IMPL_STRING_ATTR(HTMLOptGroupElement, Label, label)
  34. nsresult
  35. HTMLOptGroupElement::GetEventTargetParent(EventChainPreVisitor& aVisitor)
  36. {
  37. aVisitor.mCanHandle = false;
  38. // Do not process any DOM events if the element is disabled
  39. // XXXsmaug This is not the right thing to do. But what is?
  40. if (HasAttr(kNameSpaceID_None, nsGkAtoms::disabled)) {
  41. return NS_OK;
  42. }
  43. nsIFrame* frame = GetPrimaryFrame();
  44. if (frame) {
  45. const nsStyleUserInterface* uiStyle = frame->StyleUserInterface();
  46. if (uiStyle->mUserInput == StyleUserInput::None ||
  47. uiStyle->mUserInput == StyleUserInput::Disabled) {
  48. return NS_OK;
  49. }
  50. }
  51. return nsGenericHTMLElement::GetEventTargetParent(aVisitor);
  52. }
  53. Element*
  54. HTMLOptGroupElement::GetSelect()
  55. {
  56. Element* parent = nsINode::GetParentElement();
  57. if (!parent || !parent->IsHTMLElement(nsGkAtoms::select)) {
  58. return nullptr;
  59. }
  60. return parent;
  61. }
  62. nsresult
  63. HTMLOptGroupElement::InsertChildAt(nsIContent* aKid,
  64. uint32_t aIndex,
  65. bool aNotify)
  66. {
  67. SafeOptionListMutation safeMutation(GetSelect(), this, aKid, aIndex, aNotify);
  68. nsresult rv = nsGenericHTMLElement::InsertChildAt(aKid, aIndex, aNotify);
  69. if (NS_FAILED(rv)) {
  70. safeMutation.MutationFailed();
  71. }
  72. return rv;
  73. }
  74. void
  75. HTMLOptGroupElement::RemoveChildAt(uint32_t aIndex, bool aNotify)
  76. {
  77. SafeOptionListMutation safeMutation(GetSelect(), this, nullptr, aIndex,
  78. aNotify);
  79. nsGenericHTMLElement::RemoveChildAt(aIndex, aNotify);
  80. }
  81. nsresult
  82. HTMLOptGroupElement::AfterSetAttr(int32_t aNameSpaceID, nsIAtom* aName,
  83. const nsAttrValue* aValue,
  84. const nsAttrValue* aOldValue, bool aNotify)
  85. {
  86. if (aNameSpaceID == kNameSpaceID_None && aName == nsGkAtoms::disabled) {
  87. // All our children <option> have their :disabled state depending on our
  88. // disabled attribute. We should make sure their state is updated.
  89. for (nsIContent* child = nsINode::GetFirstChild(); child;
  90. child = child->GetNextSibling()) {
  91. if (child->IsHTMLElement(nsGkAtoms::option)) {
  92. // No need to call |IsElement()| because it's an HTML element.
  93. child->AsElement()->UpdateState(true);
  94. }
  95. }
  96. }
  97. return nsGenericHTMLElement::AfterSetAttr(aNameSpaceID, aName, aValue,
  98. aOldValue, aNotify);
  99. }
  100. EventStates
  101. HTMLOptGroupElement::IntrinsicState() const
  102. {
  103. EventStates state = nsGenericHTMLElement::IntrinsicState();
  104. if (HasAttr(kNameSpaceID_None, nsGkAtoms::disabled)) {
  105. state |= NS_EVENT_STATE_DISABLED;
  106. state &= ~NS_EVENT_STATE_ENABLED;
  107. } else {
  108. state &= ~NS_EVENT_STATE_DISABLED;
  109. state |= NS_EVENT_STATE_ENABLED;
  110. }
  111. return state;
  112. }
  113. JSObject*
  114. HTMLOptGroupElement::WrapNode(JSContext* aCx, JS::Handle<JSObject*> aGivenProto)
  115. {
  116. return HTMLOptGroupElementBinding::Wrap(aCx, this, aGivenProto);
  117. }
  118. } // namespace dom
  119. } // namespace mozilla