Bug 1906015 - part 2: Make the most do_QueryInterface users for nsIFormControl use new getter methods r=smaug,credential-management-reviewers,sessionstore-reviewers,sclements

Unfortunately, the following QIs are still required.
https://searchfox.org/mozilla-central/rev/cbdfa503a87597b20719aae5f6a1efccd6cb3b7b/dom/html/nsIConstraintValidation.cpp#101,121

Depends on D215576

Differential Revision: https://phabricator.services.mozilla.com/D215577
This commit is contained in:
Masayuki Nakano 2024-07-10 00:46:59 +00:00
parent f8a1753d60
commit 518f238fa3
23 changed files with 57 additions and 75 deletions

View File

@ -2390,8 +2390,7 @@ Relation LocalAccessible::RelationByType(RelationType aType) const {
case RelationType::DEFAULT_BUTTON: {
if (mContent->IsHTMLElement()) {
// HTML form controls implements nsIFormControl interface.
nsCOMPtr<nsIFormControl> control(do_QueryInterface(mContent));
if (control) {
if (auto* control = nsIFormControl::FromNode(mContent)) {
if (dom::HTMLFormElement* form = control->GetForm()) {
return Relation(mDoc, form->GetDefaultSubmitElement());
}

View File

@ -117,8 +117,7 @@ Relation HTMLRadioButtonAccessible::ComputeGroupAttributes(
RefPtr<nsContentList> inputElms;
nsCOMPtr<nsIFormControl> formControlNode(do_QueryInterface(mContent));
if (dom::Element* formElm = formControlNode->GetForm()) {
if (dom::Element* formElm = nsIFormControl::FromNode(mContent)->GetForm()) {
inputElms = NS_GetContentList(formElm, namespaceId, tagName);
} else {
inputElms = NS_GetContentList(mContent->OwnerDoc(), namespaceId, tagName);

View File

@ -312,7 +312,7 @@ already_AddRefed<FormData> FormData::Constructor(
if (aFormElement.WasPassed()) {
// 1.1. If submitter is non-null, then:
if (aSubmitter) {
nsCOMPtr<nsIFormControl> fc = do_QueryObject(aSubmitter);
const nsIFormControl* fc = nsIFormControl::FromNode(aSubmitter);
// 1.1.1. If submitter is not a submit button, then throw a TypeError.
if (!fc || !fc->IsSubmitControl()) {

View File

@ -463,7 +463,7 @@ nsresult DragDataProducer::Produce(DataTransfer* aDataTransfer, bool* aCanDrag,
nsCOMPtr<nsIContent> findFormNode = mSelectionTargetNode;
nsIContent* findFormParent = findFormNode->GetParent();
while (findFormParent) {
nsCOMPtr<nsIFormControl> form(do_QueryInterface(findFormParent));
const auto* form = nsIFormControl::FromNode(findFormParent);
if (form && !form->AllowDraggableChildren()) {
return NS_OK;
}
@ -499,26 +499,23 @@ nsresult DragDataProducer::Produce(DataTransfer* aDataTransfer, bool* aCanDrag,
bool haveSelectedContent = false;
// possible parent link node
nsCOMPtr<nsIContent> parentLink;
nsCOMPtr<nsIContent> draggedNode;
// only drag form elements by using the alt key,
// otherwise buttons and select widgets are hard to use
{
// only drag form elements by using the alt key,
// otherwise buttons and select widgets are hard to use
// Note that while <object> elements implement nsIFormControl, we should
// really allow dragging them if they happen to be images.
nsCOMPtr<nsIFormControl> form(do_QueryInterface(mTarget));
if (form && !mIsAltKeyPressed &&
form->ControlType() != FormControlType::Object) {
// Note that while <object> elements implement nsIFormControl, we should
// really allow dragging them if they happen to be images.
if (!mIsAltKeyPressed) {
const auto* form = nsIFormControl::FromNodeOrNull(mTarget);
if (form && form->ControlType() != FormControlType::Object) {
*aCanDrag = false;
return NS_OK;
}
draggedNode = FindDragTarget(mTarget);
}
// possible parent link node
nsCOMPtr<nsIContent> parentLink;
nsCOMPtr<nsIContent> draggedNode = FindDragTarget(mTarget);
nsCOMPtr<nsIImageLoadingContent> image;
nsCOMPtr<nsIContent> selectedImageOrLinkNode;

View File

@ -3478,8 +3478,7 @@ void nsContentUtils::GenerateStateKey(nsIContent* aContent, Document* aDocument,
// XXX We don't need to use index if name is there
// XXXbz We don't? Why not? I don't follow.
//
nsCOMPtr<nsIFormControl> control(do_QueryInterface(aContent));
if (control) {
if (const auto* control = nsIFormControl::FromNode(aContent)) {
// Get the control number if this was a parser inserted element from the
// network.
int32_t controlNumber =

View File

@ -2271,9 +2271,8 @@ void EventStateManager::FireContextClick() {
}
}
} else if (mGestureDownContent->IsHTMLElement()) {
nsCOMPtr<nsIFormControl> formCtrl(do_QueryInterface(mGestureDownContent));
if (formCtrl) {
if (const auto* formCtrl =
nsIFormControl::FromNode(mGestureDownContent)) {
allowedToDispatch =
formCtrl->IsTextControl(/*aExcludePassword*/ false) ||
formCtrl->ControlType() == FormControlType::InputFile;

View File

@ -1617,7 +1617,7 @@ MOZ_CAN_RUN_SCRIPT static bool IsNextFocusableElementTextControl(
return false;
}
nextContent = nextContent->FindFirstNonChromeOnlyAccessContent();
nsCOMPtr<nsIFormControl> nextControl = do_QueryInterface(nextContent);
const auto* nextControl = nsIFormControl::FromNode(nextContent);
if (!nextControl || !nextControl->IsTextControl(false)) {
return false;
}

View File

@ -300,8 +300,7 @@ nsresult KeyEventHandler::DispatchXBLCommand(dom::EventTarget* aTarget,
// If the focus is in a form control, don't scroll.
for (nsIContent* c = focusedContent; c; c = c->GetParent()) {
nsCOMPtr<nsIFormControl> formControl = do_QueryInterface(c);
if (formControl) {
if (nsIFormControl::FromNode(c)) {
return NS_OK;
}
}

View File

@ -90,8 +90,7 @@ void HTMLFieldSetElement::GetType(nsAString& aType) const {
bool HTMLFieldSetElement::MatchListedElements(Element* aElement,
int32_t aNamespaceID,
nsAtom* aAtom, void* aData) {
nsCOMPtr<nsIFormControl> formControl = do_QueryInterface(aElement);
return formControl;
return nsIFormControl::FromNodeOrNull(aElement) != nullptr;
}
nsIHTMLCollection* HTMLFieldSetElement::Elements() {

View File

@ -22,7 +22,7 @@ namespace mozilla::dom {
/* static */
bool HTMLFormControlsCollection::ShouldBeInElements(
nsIFormControl* aFormControl) {
const nsIFormControl* aFormControl) {
// For backwards compatibility (with 4.x and IE) we must not add
// <input type=image> elements to the list of form controls in a
// form.
@ -87,14 +87,14 @@ void HTMLFormControlsCollection::DropFormReference() {
void HTMLFormControlsCollection::Clear() {
// Null out childrens' pointer to me. No refcounting here
for (nsGenericHTMLFormElement* element : Reversed(mElements.AsList())) {
nsCOMPtr<nsIFormControl> formControl = do_QueryObject(element);
nsCOMPtr<nsIFormControl> formControl = nsIFormControl::FromNode(element);
MOZ_ASSERT(formControl);
formControl->ClearForm(false, false);
}
mElements.Clear();
for (nsGenericHTMLFormElement* element : Reversed(mNotInElements.AsList())) {
nsCOMPtr<nsIFormControl> formControl = do_QueryObject(element);
nsCOMPtr<nsIFormControl> formControl = nsIFormControl::FromNode(element);
MOZ_ASSERT(formControl);
formControl->ClearForm(false, false);
}
@ -140,7 +140,7 @@ nsISupports* HTMLFormControlsCollection::NamedItemInternal(
nsresult HTMLFormControlsCollection::AddElementToTable(
nsGenericHTMLFormElement* aChild, const nsAString& aName) {
nsCOMPtr<nsIFormControl> formControl = do_QueryObject(aChild);
const auto* formControl = nsIFormControl::FromNode(aChild);
MOZ_ASSERT(formControl);
if (!ShouldBeInElements(formControl)) {
return NS_OK;
@ -160,7 +160,7 @@ nsresult HTMLFormControlsCollection::IndexOfContent(nsIContent* aContent,
nsresult HTMLFormControlsCollection::RemoveElementFromTable(
nsGenericHTMLFormElement* aChild, const nsAString& aName) {
nsCOMPtr<nsIFormControl> formControl = do_QueryObject(aChild);
const auto* formControl = nsIFormControl::FromNode(aChild);
MOZ_ASSERT(formControl);
if (!ShouldBeInElements(formControl)) {
return NS_OK;

View File

@ -92,7 +92,7 @@ class HTMLFormControlsCollection final : public nsIHTMLCollection,
}
public:
static bool ShouldBeInElements(nsIFormControl* aFormControl);
static bool ShouldBeInElements(const nsIFormControl* aFormControl);
HTMLFormElement* mForm; // WEAK - the form owns me

View File

@ -242,7 +242,7 @@ void HTMLFormElement::ReportInvalidUnfocusableElements(
void HTMLFormElement::MaybeSubmit(Element* aSubmitter) {
#ifdef DEBUG
if (aSubmitter) {
nsCOMPtr<nsIFormControl> fc = do_QueryInterface(aSubmitter);
const auto* fc = nsIFormControl::FromNode(aSubmitter);
MOZ_ASSERT(fc);
MOZ_ASSERT(fc->IsSubmitControl(), "aSubmitter is not a submit control?");
}
@ -338,7 +338,7 @@ void HTMLFormElement::RequestSubmit(nsGenericHTMLElement* aSubmitter,
ErrorResult& aRv) {
// 1. If submitter is not null, then:
if (aSubmitter) {
nsCOMPtr<nsIFormControl> fc = do_QueryObject(aSubmitter);
const auto* fc = nsIFormControl::FromNodeOrNull(aSubmitter);
// 1.1. If submitter is not a submit button, then throw a TypeError.
if (!fc || !fc->IsSubmitControl()) {
@ -428,7 +428,7 @@ static void CollectOrphans(nsINode* aRemovalRoot,
if (node->HasFlag(MAYBE_ORPHAN_FORM_ELEMENT)) {
node->UnsetFlags(MAYBE_ORPHAN_FORM_ELEMENT);
if (!node->IsInclusiveDescendantOf(aRemovalRoot)) {
nsCOMPtr<nsIFormControl> fc = do_QueryInterface(node);
nsCOMPtr<nsIFormControl> fc = nsIFormControl::FromNode(node);
MOZ_ASSERT(fc);
fc->ClearForm(true, false);
#ifdef DEBUG
@ -439,7 +439,7 @@ static void CollectOrphans(nsINode* aRemovalRoot,
#ifdef DEBUG
if (!removed) {
nsCOMPtr<nsIFormControl> fc = do_QueryInterface(node);
const auto* fc = nsIFormControl::FromNode(node);
MOZ_ASSERT(fc);
HTMLFormElement* form = fc->GetForm();
NS_ASSERTION(form == aThisForm, "How did that happen?");
@ -645,7 +645,7 @@ nsresult HTMLFormElement::DoReset() {
uint32_t numElements = mControls->Length();
for (uint32_t elementX = 0; elementX < numElements; ++elementX) {
// Hold strong ref in case the reset does something weird
nsCOMPtr<nsIFormControl> controlNode = do_QueryInterface(
nsCOMPtr<nsIFormControl> controlNode = nsIFormControl::FromNodeOrNull(
mControls->mElements->SafeElementAt(elementX, nullptr));
if (controlNode) {
controlNode->Reset();
@ -1067,7 +1067,7 @@ nsresult HTMLFormElement::ConstructEntryList(FormData* aFormData) {
for (nsGenericHTMLFormElement* control : sortedControls) {
// Disabled elements don't submit
if (!control->IsDisabled()) {
nsCOMPtr<nsIFormControl> fc = do_QueryInterface(control);
nsCOMPtr<nsIFormControl> fc = nsIFormControl::FromNode(control);
MOZ_ASSERT(fc);
// Tell the control to submit its name/value pairs to the submission
fc->SubmitNamesValues(aFormData);
@ -1186,7 +1186,7 @@ nsresult HTMLFormElement::AddElement(nsGenericHTMLFormElement* aChild,
// a parent and still be in the form.
NS_ASSERTION(aChild->HasAttr(nsGkAtoms::form) || aChild->GetParent(),
"Form control should have a parent");
nsCOMPtr<nsIFormControl> fc = do_QueryObject(aChild);
nsCOMPtr<nsIFormControl> fc = nsIFormControl::FromNode(aChild);
MOZ_ASSERT(fc);
// Determine whether to add the new element to the elements or
// the not-in-elements list.
@ -1288,7 +1288,7 @@ nsresult HTMLFormElement::RemoveElement(nsGenericHTMLFormElement* aChild,
// Remove it from the radio group if it's a radio button
//
nsresult rv = NS_OK;
nsCOMPtr<nsIFormControl> fc = do_QueryInterface(aChild);
nsCOMPtr<nsIFormControl> fc = nsIFormControl::FromNode(aChild);
MOZ_ASSERT(fc);
if (fc->ControlType() == FormControlType::InputRadio) {
RefPtr<HTMLInputElement> radio = static_cast<HTMLInputElement*>(aChild);
@ -1317,8 +1317,8 @@ nsresult HTMLFormElement::RemoveElement(nsGenericHTMLFormElement* aChild,
// We are removing the first submit in this list, find the new first submit
uint32_t length = controls->Length();
for (uint32_t i = index; i < length; ++i) {
nsCOMPtr<nsIFormControl> currentControl =
do_QueryInterface(controls->ElementAt(i));
const auto* currentControl =
nsIFormControl::FromNode(controls->ElementAt(i));
MOZ_ASSERT(currentControl);
if (currentControl->IsSubmitControl()) {
*firstSubmitSlot = controls->ElementAt(i);
@ -1554,8 +1554,7 @@ nsresult HTMLFormElement::GetActionURL(nsIURI** aActionURL,
if (aOriginatingElement &&
aOriginatingElement->HasAttr(nsGkAtoms::formaction)) {
#ifdef DEBUG
nsCOMPtr<nsIFormControl> formControl =
do_QueryInterface(aOriginatingElement);
const auto* formControl = nsIFormControl::FromNode(aOriginatingElement);
NS_ASSERTION(formControl && formControl->IsSubmitControl(),
"The originating element must be a submit form control!");
#endif // DEBUG
@ -1707,8 +1706,8 @@ bool HTMLFormElement::ImplicitSubmissionIsDisabled() const {
uint32_t numDisablingControlsFound = 0;
uint32_t length = mControls->mElements->Length();
for (uint32_t i = 0; i < length && numDisablingControlsFound < 2; ++i) {
nsCOMPtr<nsIFormControl> fc =
do_QueryInterface(mControls->mElements->ElementAt(i));
const auto* fc =
nsIFormControl::FromNode(mControls->mElements->ElementAt(i));
MOZ_ASSERT(fc);
if (fc->IsSingleLineTextControl(false)) {
numDisablingControlsFound++;
@ -1722,7 +1721,7 @@ bool HTMLFormElement::IsLastActiveElement(
MOZ_ASSERT(aElement, "Unexpected call");
for (auto* element : Reversed(mControls->mElements.AsList())) {
nsCOMPtr<nsIFormControl> fc = do_QueryInterface(element);
const auto* fc = nsIFormControl::FromNode(element);
MOZ_ASSERT(fc);
// XXX How about date/time control?
if (fc->IsTextControl(false) && !element->IsDisabled()) {

View File

@ -36,13 +36,8 @@ JSObject* HTMLLabelElement::WrapNode(JSContext* aCx,
NS_IMPL_ELEMENT_CLONE(HTMLLabelElement)
HTMLFormElement* HTMLLabelElement::GetForm() const {
nsGenericHTMLElement* control = GetControl();
if (!control) {
return nullptr;
}
// Not all labeled things have a form association. Stick to the ones that do.
nsCOMPtr<nsIFormControl> formControl = do_QueryObject(control);
const auto* formControl = nsIFormControl::FromNodeOrNull(GetControl());
if (!formControl) {
return nullptr;
}

View File

@ -128,7 +128,7 @@ HTMLLegendElement::LegendAlignValue HTMLLegendElement::LogicalAlign(
}
HTMLFormElement* HTMLLegendElement::GetForm() const {
nsCOMPtr<nsIFormControl> fieldsetControl = do_QueryInterface(GetFieldSet());
const auto* fieldsetControl = nsIFormControl::FromNodeOrNull(GetFieldSet());
return fieldsetControl ? fieldsetControl->GetForm() : nullptr;
}

View File

@ -447,7 +447,7 @@ bool nsGenericHTMLElement::Spellcheck() {
}
// Anything else that's not a form control is not spellchecked by default
nsCOMPtr<nsIFormControl> formControl = do_QueryObject(this);
const nsIFormControl* formControl = GetAsFormControl();
if (!formControl) {
return false; // Not spellchecked by default
}

View File

@ -1093,8 +1093,7 @@ PersistNodeFixup::FixupNode(nsINode* aNodeIn, bool* aSerializeCloneKids,
// Update element node attributes with user-entered form state
RefPtr<dom::HTMLInputElement> outElt =
dom::HTMLInputElement::FromNode((*aNodeOut)->AsContent());
nsCOMPtr<nsIFormControl> formControl = do_QueryInterface(*aNodeOut);
switch (formControl->ControlType()) {
switch (nsIFormControl::FromNode(*aNodeOut)->ControlType()) {
case FormControlType::InputEmail:
case FormControlType::InputSearch:
case FormControlType::InputText:

View File

@ -1195,8 +1195,8 @@ bool EditorEventListener::IsFileControlTextBox() {
if (!parent || !parent->IsHTMLElement(nsGkAtoms::input)) {
return false;
}
nsCOMPtr<nsIFormControl> formControl = do_QueryInterface(parent);
return formControl->ControlType() == FormControlType::InputFile;
return nsIFormControl::FromNode(parent)->ControlType() ==
FormControlType::InputFile;
}
bool EditorEventListener::ShouldHandleNativeKeyBindings(

View File

@ -114,8 +114,8 @@ nsresult nsColorControlFrame::AttributeChanged(int32_t aNameSpaceID,
// If the value attribute is set, update the color box, but only if we're
// still a color control, which might not be the case if the type attribute
// was removed/changed.
nsCOMPtr<nsIFormControl> fctrl = do_QueryInterface(GetContent());
if (fctrl->ControlType() == FormControlType::InputColor &&
if (nsIFormControl::FromNode(GetContent())->ControlType() ==
FormControlType::InputColor &&
aNameSpaceID == kNameSpaceID_None && nsGkAtoms::value == aAttribute) {
UpdateColor();
}

View File

@ -74,7 +74,7 @@ NS_QUERYFRAME_TAIL_INHERITING(nsHTMLButtonControlFrame)
// label from a string bundle as is done for all other UI strings.
// See bug 16999 for further details.
nsresult nsGfxButtonControlFrame::GetDefaultLabel(nsAString& aString) const {
nsCOMPtr<nsIFormControl> form = do_QueryInterface(mContent);
const auto* form = nsIFormControl::FromNodeOrNull(mContent);
NS_ENSURE_TRUE(form, NS_ERROR_UNEXPECTED);
auto type = form->ControlType();

View File

@ -630,7 +630,7 @@ void nsHtml5TreeOperation::SetFormElement(nsIContent* aNode,
RefPtr formElement = HTMLFormElement::FromNodeOrNull(aParent);
NS_ASSERTION(formElement,
"The form element doesn't implement HTMLFormElement.");
nsCOMPtr<nsIFormControl> formControl(do_QueryInterface(aNode));
nsCOMPtr<nsIFormControl> formControl = nsIFormControl::FromNodeOrNull(aNode);
if (formControl &&
formControl->ControlType() !=
FormControlType::FormAssociatedCustomElement &&

View File

@ -164,7 +164,7 @@ static bool ShouldFindAnonymousContent(const nsIContent& aContent) {
MOZ_ASSERT(aContent.IsInNativeAnonymousSubtree());
nsIContent& host = AnonymousSubtreeRootParentOrHost(aContent);
if (nsCOMPtr<nsIFormControl> formControl = do_QueryInterface(&host)) {
if (const auto* formControl = nsIFormControl::FromNode(&host)) {
if (formControl->IsTextControl(/* aExcludePassword = */ true)) {
// Only editable NAC in textfields should be findable. That is, we want to
// find "bar" in `<input value="bar">`, but not in `<input

View File

@ -828,7 +828,7 @@ void nsFormFillController::RemoveForDocument(Document* aDoc) {
}
bool nsFormFillController::IsTextControl(nsINode* aNode) {
nsCOMPtr<nsIFormControl> formControl = do_QueryInterface(aNode);
const auto* formControl = nsIFormControl::FromNodeOrNull(aNode);
return formControl && formControl->IsSingleLineTextControl(false);
}

View File

@ -658,9 +658,8 @@ static uint32_t CollectInputElement(Document* aDocument,
uint32_t length = inputlist->Length();
for (uint32_t i = 0; i < length; ++i) {
MOZ_ASSERT(inputlist->Item(i), "null item in node list!");
nsCOMPtr<nsIFormControl> formControl =
do_QueryInterface(inputlist->Item(i));
if (formControl) {
if (const auto* formControl =
nsIFormControl::FromNodeOrNull(inputlist->Item(i))) {
auto controlType = formControl->ControlType();
if (controlType == FormControlType::InputPassword ||
controlType == FormControlType::InputHidden ||
@ -916,9 +915,8 @@ void SessionStoreUtils::CollectFromInputElement(Document& aDocument,
uint32_t length = inputlist->Length(true);
for (uint32_t i = 0; i < length; ++i) {
MOZ_ASSERT(inputlist->Item(i), "null item in node list!");
nsCOMPtr<nsIFormControl> formControl =
do_QueryInterface(inputlist->Item(i));
if (formControl) {
if (const auto* formControl =
nsIFormControl::FromNodeOrNull(inputlist->Item(i))) {
auto controlType = formControl->ControlType();
if (controlType == FormControlType::InputPassword ||
controlType == FormControlType::InputHidden ||