HTMLLegendElement.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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/HTMLLegendElement.h"
  6. #include "mozilla/dom/HTMLLegendElementBinding.h"
  7. #include "nsIDOMHTMLFormElement.h"
  8. #include "nsFocusManager.h"
  9. #include "nsIFrame.h"
  10. NS_IMPL_NS_NEW_HTML_ELEMENT(Legend)
  11. namespace mozilla {
  12. namespace dom {
  13. HTMLLegendElement::~HTMLLegendElement()
  14. {
  15. }
  16. NS_IMPL_ELEMENT_CLONE(HTMLLegendElement)
  17. nsIContent*
  18. HTMLLegendElement::GetFieldSet() const
  19. {
  20. nsIContent* parent = GetParent();
  21. if (parent && parent->IsHTMLElement(nsGkAtoms::fieldset)) {
  22. return parent;
  23. }
  24. return nullptr;
  25. }
  26. bool
  27. HTMLLegendElement::ParseAttribute(int32_t aNamespaceID,
  28. nsIAtom* aAttribute,
  29. const nsAString& aValue,
  30. nsAttrValue& aResult)
  31. {
  32. // this contains center, because IE4 does
  33. static const nsAttrValue::EnumTable kAlignTable[] = {
  34. { "left", NS_STYLE_TEXT_ALIGN_LEFT },
  35. { "right", NS_STYLE_TEXT_ALIGN_RIGHT },
  36. { "center", NS_STYLE_TEXT_ALIGN_CENTER },
  37. { "bottom", NS_STYLE_VERTICAL_ALIGN_BOTTOM },
  38. { "top", NS_STYLE_VERTICAL_ALIGN_TOP },
  39. { nullptr, 0 }
  40. };
  41. if (aAttribute == nsGkAtoms::align && aNamespaceID == kNameSpaceID_None) {
  42. return aResult.ParseEnumValue(aValue, kAlignTable, false);
  43. }
  44. return nsGenericHTMLElement::ParseAttribute(aNamespaceID, aAttribute, aValue,
  45. aResult);
  46. }
  47. nsChangeHint
  48. HTMLLegendElement::GetAttributeChangeHint(const nsIAtom* aAttribute,
  49. int32_t aModType) const
  50. {
  51. nsChangeHint retval =
  52. nsGenericHTMLElement::GetAttributeChangeHint(aAttribute, aModType);
  53. if (aAttribute == nsGkAtoms::align) {
  54. retval |= NS_STYLE_HINT_REFLOW;
  55. }
  56. return retval;
  57. }
  58. nsresult
  59. HTMLLegendElement::BindToTree(nsIDocument* aDocument, nsIContent* aParent,
  60. nsIContent* aBindingParent,
  61. bool aCompileEventHandlers)
  62. {
  63. return nsGenericHTMLElement::BindToTree(aDocument, aParent,
  64. aBindingParent,
  65. aCompileEventHandlers);
  66. }
  67. void
  68. HTMLLegendElement::UnbindFromTree(bool aDeep, bool aNullParent)
  69. {
  70. nsGenericHTMLElement::UnbindFromTree(aDeep, aNullParent);
  71. }
  72. void
  73. HTMLLegendElement::Focus(ErrorResult& aError)
  74. {
  75. nsIFrame* frame = GetPrimaryFrame();
  76. if (!frame) {
  77. return;
  78. }
  79. int32_t tabIndex;
  80. if (frame->IsFocusable(&tabIndex, false)) {
  81. nsGenericHTMLElement::Focus(aError);
  82. return;
  83. }
  84. // If the legend isn't focusable, focus whatever is focusable following
  85. // the legend instead, bug 81481.
  86. nsIFocusManager* fm = nsFocusManager::GetFocusManager();
  87. if (!fm) {
  88. return;
  89. }
  90. nsCOMPtr<nsIDOMElement> result;
  91. aError = fm->MoveFocus(nullptr, this, nsIFocusManager::MOVEFOCUS_FORWARD,
  92. nsIFocusManager::FLAG_NOPARENTFRAME,
  93. getter_AddRefs(result));
  94. }
  95. bool
  96. HTMLLegendElement::PerformAccesskey(bool aKeyCausesActivation,
  97. bool aIsTrustedEvent)
  98. {
  99. // just use the same behaviour as the focus method
  100. ErrorResult rv;
  101. Focus(rv);
  102. return NS_SUCCEEDED(rv.StealNSResult());
  103. }
  104. already_AddRefed<HTMLFormElement>
  105. HTMLLegendElement::GetForm()
  106. {
  107. Element* form = GetFormElement();
  108. MOZ_ASSERT_IF(form, form->IsHTMLElement(nsGkAtoms::form));
  109. RefPtr<HTMLFormElement> ret = static_cast<HTMLFormElement*>(form);
  110. return ret.forget();
  111. }
  112. JSObject*
  113. HTMLLegendElement::WrapNode(JSContext* aCx, JS::Handle<JSObject*> aGivenProto)
  114. {
  115. return HTMLLegendElementBinding::Wrap(aCx, this, aGivenProto);
  116. }
  117. } // namespace dom
  118. } // namespace mozilla