Skip to content

Commit

Permalink
Bug 1588715 - Refactor matches the spec term 'submitter'; r=smaug
Browse files Browse the repository at this point in the history
Differential Revision: https://phabricator.services.mozilla.com/D62035

--HG--
extra : moz-landing-system : lando
  • Loading branch information
johndai1984 committed Feb 11, 2020
1 parent 5683fcf commit 82370e0
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 60 deletions.
7 changes: 3 additions & 4 deletions dom/base/FormData.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,13 @@ using namespace mozilla;
using namespace mozilla::dom;

FormData::FormData(nsISupports* aOwner, NotNull<const Encoding*> aEncoding,
Element* aOriginatingElement)
: HTMLFormSubmission(nullptr, EmptyString(), aEncoding,
aOriginatingElement),
Element* aSubmitter)
: HTMLFormSubmission(nullptr, EmptyString(), aEncoding, aSubmitter),
mOwner(aOwner) {}

FormData::FormData(const FormData& aFormData)
: HTMLFormSubmission(aFormData.mActionURL, aFormData.mTarget,
aFormData.mEncoding, aFormData.mOriginatingElement) {
aFormData.mEncoding, aFormData.mSubmitter) {
mOwner = aFormData.mOwner;
mFormData = aFormData.mFormData;
}
Expand Down
2 changes: 1 addition & 1 deletion dom/base/FormData.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class FormData final : public nsISupports,
public:
explicit FormData(nsISupports* aOwner = nullptr,
NotNull<const Encoding*> aEncoding = UTF_8_ENCODING,
Element* aOriginatingElement = nullptr);
Element* aSubmitter = nullptr);

already_AddRefed<FormData> Clone();

Expand Down
2 changes: 1 addition & 1 deletion dom/html/HTMLButtonElement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ HTMLButtonElement::SubmitNamesValues(HTMLFormSubmission* aFormSubmission) {
//
// We only submit if we were the button pressed
//
if (aFormSubmission->GetOriginatingElement() != this) {
if (aFormSubmission->GetSubmitterElement() != this) {
return NS_OK;
}

Expand Down
10 changes: 5 additions & 5 deletions dom/html/HTMLFormElement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -688,12 +688,12 @@ nsresult HTMLFormElement::BuildSubmission(HTMLFormSubmission** aFormSubmission,
Event* aEvent) {
NS_ASSERTION(!mPendingSubmission, "tried to build two submissions!");

// Get the originating frame
nsGenericHTMLElement* originatingElement = nullptr;
// Get the submitter element
nsGenericHTMLElement* submitter = nullptr;
if (aEvent) {
SubmitEvent* submitEvent = aEvent->AsSubmitEvent();
if (submitEvent) {
originatingElement = submitEvent->GetSubmitter();
submitter = submitEvent->GetSubmitter();
}
}

Expand All @@ -705,7 +705,7 @@ nsresult HTMLFormElement::BuildSubmission(HTMLFormSubmission** aFormSubmission,
//
auto encoding = GetSubmitEncoding()->OutputEncoding();
RefPtr<FormData> formData =
new FormData(GetOwnerGlobal(), encoding, originatingElement);
new FormData(GetOwnerGlobal(), encoding, submitter);
rv = ConstructEntryList(formData);
NS_ENSURE_SUBMIT_SUCCESS(rv);

Expand All @@ -718,7 +718,7 @@ nsresult HTMLFormElement::BuildSubmission(HTMLFormSubmission** aFormSubmission,
//
// Get the submission object
//
rv = HTMLFormSubmission::GetFromForm(this, originatingElement, encoding,
rv = HTMLFormSubmission::GetFromForm(this, submitter, encoding,
aFormSubmission);
NS_ENSURE_SUBMIT_SUCCESS(rv);

Expand Down
71 changes: 33 additions & 38 deletions dom/html/HTMLFormSubmission.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,8 @@ class FSURLEncoded : public EncodingFormSubmission {
*/
FSURLEncoded(nsIURI* aActionURL, const nsAString& aTarget,
NotNull<const Encoding*> aEncoding, int32_t aMethod,
Document* aDocument, Element* aOriginatingElement)
: EncodingFormSubmission(aActionURL, aTarget, aEncoding,
aOriginatingElement),
Document* aDocument, Element* aSubmitter)
: EncodingFormSubmission(aActionURL, aTarget, aEncoding, aSubmitter),
mMethod(aMethod),
mDocument(aDocument),
mWarnedFileControl(false) {}
Expand Down Expand Up @@ -344,9 +343,8 @@ nsresult FSURLEncoded::URLEncode(const nsAString& aStr, nsACString& aEncoded) {
FSMultipartFormData::FSMultipartFormData(nsIURI* aActionURL,
const nsAString& aTarget,
NotNull<const Encoding*> aEncoding,
Element* aOriginatingElement)
: EncodingFormSubmission(aActionURL, aTarget, aEncoding,
aOriginatingElement) {
Element* aSubmitter)
: EncodingFormSubmission(aActionURL, aTarget, aEncoding, aSubmitter) {
mPostData = do_CreateInstance("@mozilla.org/io/multiplex-input-stream;1");

nsCOMPtr<nsIInputStream> inputStream = do_QueryInterface(mPostData);
Expand Down Expand Up @@ -598,9 +596,8 @@ namespace {
class FSTextPlain : public EncodingFormSubmission {
public:
FSTextPlain(nsIURI* aActionURL, const nsAString& aTarget,
NotNull<const Encoding*> aEncoding, Element* aOriginatingElement)
: EncodingFormSubmission(aActionURL, aTarget, aEncoding,
aOriginatingElement) {}
NotNull<const Encoding*> aEncoding, Element* aSubmitter)
: EncodingFormSubmission(aActionURL, aTarget, aEncoding, aSubmitter) {}

virtual nsresult AddNameValuePair(const nsAString& aName,
const nsAString& aValue) override;
Expand Down Expand Up @@ -711,16 +708,15 @@ nsresult FSTextPlain::GetEncodedSubmission(nsIURI* aURI,

EncodingFormSubmission::EncodingFormSubmission(
nsIURI* aActionURL, const nsAString& aTarget,
NotNull<const Encoding*> aEncoding, Element* aOriginatingElement)
: HTMLFormSubmission(aActionURL, aTarget, aEncoding, aOriginatingElement) {
NotNull<const Encoding*> aEncoding, Element* aSubmitter)
: HTMLFormSubmission(aActionURL, aTarget, aEncoding, aSubmitter) {
if (!aEncoding->CanEncodeEverything()) {
nsAutoCString name;
aEncoding->Name(name);
AutoTArray<nsString, 1> args;
CopyUTF8toUTF16(name, *args.AppendElement());
SendJSWarning(
aOriginatingElement ? aOriginatingElement->GetOwnerDocument() : nullptr,
"CannotEncodeAllUnicode", args);
SendJSWarning(aSubmitter ? aSubmitter->GetOwnerDocument() : nullptr,
"CannotEncodeAllUnicode", args);
}
}

Expand Down Expand Up @@ -762,9 +758,10 @@ void GetEnumAttr(nsGenericHTMLElement* aContent, nsAtom* atom,
} // anonymous namespace

/* static */
nsresult HTMLFormSubmission::GetFromForm(
HTMLFormElement* aForm, nsGenericHTMLElement* aOriginatingElement,
NotNull<const Encoding*>& aEncoding, HTMLFormSubmission** aFormSubmission) {
nsresult HTMLFormSubmission::GetFromForm(HTMLFormElement* aForm,
nsGenericHTMLElement* aSubmitter,
NotNull<const Encoding*>& aEncoding,
HTMLFormSubmission** aFormSubmission) {
// Get all the information necessary to encode the form data
NS_ASSERTION(aForm->GetComposedDoc(),
"Should have doc if we're building submission!");
Expand All @@ -773,7 +770,7 @@ nsresult HTMLFormSubmission::GetFromForm(

// Get action
nsCOMPtr<nsIURI> actionURL;
rv = aForm->GetActionURL(getter_AddRefs(actionURL), aOriginatingElement);
rv = aForm->GetActionURL(getter_AddRefs(actionURL), aSubmitter);
NS_ENSURE_SUCCESS(rv, rv);

// Check if CSP allows this form-action
Expand All @@ -794,66 +791,64 @@ nsresult HTMLFormSubmission::GetFromForm(
}

// Get target
// The target is the originating element formtarget attribute if the element
// The target is the submitter element formtarget attribute if the element
// is a submit control and has such an attribute.
// Otherwise, the target is the form owner's target attribute,
// if it has such an attribute.
// Finally, if one of the child nodes of the head element is a base element
// with a target attribute, then the value of the target attribute of the
// first such base element; or, if there is no such element, the empty string.
nsAutoString target;
if (!(aOriginatingElement &&
aOriginatingElement->GetAttr(kNameSpaceID_None, nsGkAtoms::formtarget,
target)) &&
if (!(aSubmitter && aSubmitter->GetAttr(kNameSpaceID_None,
nsGkAtoms::formtarget, target)) &&
!aForm->GetAttr(kNameSpaceID_None, nsGkAtoms::target, target)) {
aForm->GetBaseTarget(target);
}

// Get encoding type (default: urlencoded)
int32_t enctype = NS_FORM_ENCTYPE_URLENCODED;
if (aOriginatingElement &&
aOriginatingElement->HasAttr(kNameSpaceID_None, nsGkAtoms::formenctype)) {
GetEnumAttr(aOriginatingElement, nsGkAtoms::formenctype, &enctype);
if (aSubmitter &&
aSubmitter->HasAttr(kNameSpaceID_None, nsGkAtoms::formenctype)) {
GetEnumAttr(aSubmitter, nsGkAtoms::formenctype, &enctype);
} else {
GetEnumAttr(aForm, nsGkAtoms::enctype, &enctype);
}

// Get method (default: GET)
int32_t method = NS_FORM_METHOD_GET;
if (aOriginatingElement &&
aOriginatingElement->HasAttr(kNameSpaceID_None, nsGkAtoms::formmethod)) {
GetEnumAttr(aOriginatingElement, nsGkAtoms::formmethod, &method);
if (aSubmitter &&
aSubmitter->HasAttr(kNameSpaceID_None, nsGkAtoms::formmethod)) {
GetEnumAttr(aSubmitter, nsGkAtoms::formmethod, &method);
} else {
GetEnumAttr(aForm, nsGkAtoms::method, &method);
}

// Choose encoder
if (method == NS_FORM_METHOD_POST && enctype == NS_FORM_ENCTYPE_MULTIPART) {
*aFormSubmission = new FSMultipartFormData(actionURL, target, aEncoding,
aOriginatingElement);
*aFormSubmission =
new FSMultipartFormData(actionURL, target, aEncoding, aSubmitter);
} else if (method == NS_FORM_METHOD_POST &&
enctype == NS_FORM_ENCTYPE_TEXTPLAIN) {
*aFormSubmission =
new FSTextPlain(actionURL, target, aEncoding, aOriginatingElement);
new FSTextPlain(actionURL, target, aEncoding, aSubmitter);
} else {
Document* doc = aForm->OwnerDoc();
if (enctype == NS_FORM_ENCTYPE_MULTIPART ||
enctype == NS_FORM_ENCTYPE_TEXTPLAIN) {
AutoTArray<nsString, 1> args;
nsString& enctypeStr = *args.AppendElement();
if (aOriginatingElement &&
aOriginatingElement->HasAttr(kNameSpaceID_None,
nsGkAtoms::formenctype)) {
aOriginatingElement->GetAttr(kNameSpaceID_None, nsGkAtoms::formenctype,
enctypeStr);
if (aSubmitter &&
aSubmitter->HasAttr(kNameSpaceID_None, nsGkAtoms::formenctype)) {
aSubmitter->GetAttr(kNameSpaceID_None, nsGkAtoms::formenctype,
enctypeStr);
} else {
aForm->GetAttr(kNameSpaceID_None, nsGkAtoms::enctype, enctypeStr);
}

SendJSWarning(doc, "ForgotPostWarning", args);
}
*aFormSubmission = new FSURLEncoded(actionURL, target, aEncoding, method,
doc, aOriginatingElement);
*aFormSubmission =
new FSURLEncoded(actionURL, target, aEncoding, method, doc, aSubmitter);
}

return NS_OK;
Expand Down
20 changes: 10 additions & 10 deletions dom/html/HTMLFormSubmission.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,11 @@ class HTMLFormSubmission {
* METHOD)
*
* @param aForm the form to get a submission object based on
* @param aOriginatingElement the originating element (can be null)
* @param aSubmitter the submitter element (can be null)
* @param aFormSubmission the form submission object (out param)
*/
static nsresult GetFromForm(HTMLFormElement* aForm,
nsGenericHTMLElement* aOriginatingElement,
nsGenericHTMLElement* aSubmitter,
NotNull<const Encoding*>& aEncoding,
HTMLFormSubmission** aFormSubmission);

Expand Down Expand Up @@ -93,7 +93,7 @@ class HTMLFormSubmission {
*/
void GetCharset(nsACString& aCharset) { mEncoding->Name(aCharset); }

Element* GetOriginatingElement() const { return mOriginatingElement.get(); }
Element* GetSubmitterElement() const { return mSubmitter.get(); }

/**
* Get the action URI that will be used for submission.
Expand All @@ -115,15 +115,15 @@ class HTMLFormSubmission {
* Can only be constructed by subclasses.
*
* @param aEncoding the character encoding of the form
* @param aOriginatingElement the originating element (can be null)
* @param aSubmitter the submitter element (can be null)
*/
HTMLFormSubmission(nsIURI* aActionURL, const nsAString& aTarget,
mozilla::NotNull<const mozilla::Encoding*> aEncoding,
Element* aOriginatingElement)
Element* aSubmitter)
: mActionURL(aActionURL),
mTarget(aTarget),
mEncoding(aEncoding),
mOriginatingElement(aOriginatingElement),
mSubmitter(aSubmitter),
mInitiatedFromUserInput(UserActivation::IsHandlingUserInput()) {
MOZ_COUNT_CTOR(HTMLFormSubmission);
}
Expand All @@ -137,8 +137,8 @@ class HTMLFormSubmission {
// The character encoding of this form submission
mozilla::NotNull<const mozilla::Encoding*> mEncoding;

// Originating element.
RefPtr<Element> mOriginatingElement;
// Submitter element.
RefPtr<Element> mSubmitter;

// Keep track of whether this form submission was user-initiated or not
bool mInitiatedFromUserInput;
Expand All @@ -148,7 +148,7 @@ class EncodingFormSubmission : public HTMLFormSubmission {
public:
EncodingFormSubmission(nsIURI* aActionURL, const nsAString& aTarget,
mozilla::NotNull<const mozilla::Encoding*> aEncoding,
Element* aOriginatingElement);
Element* aSubmitter);

virtual ~EncodingFormSubmission();

Expand Down Expand Up @@ -176,7 +176,7 @@ class FSMultipartFormData : public EncodingFormSubmission {
*/
FSMultipartFormData(nsIURI* aActionURL, const nsAString& aTarget,
mozilla::NotNull<const mozilla::Encoding*> aEncoding,
Element* aOriginatingElement);
Element* aSubmitter);
~FSMultipartFormData();

virtual nsresult AddNameValuePair(const nsAString& aName,
Expand Down
2 changes: 1 addition & 1 deletion dom/html/HTMLInputElement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5588,7 +5588,7 @@ HTMLInputElement::SubmitNamesValues(HTMLFormSubmission* aFormSubmission) {
if (IsDisabled() || mType == NS_FORM_INPUT_RESET ||
mType == NS_FORM_INPUT_BUTTON ||
((mType == NS_FORM_INPUT_SUBMIT || mType == NS_FORM_INPUT_IMAGE) &&
aFormSubmission->GetOriginatingElement() != this) ||
aFormSubmission->GetSubmitterElement() != this) ||
((mType == NS_FORM_INPUT_RADIO || mType == NS_FORM_INPUT_CHECKBOX) &&
!mChecked)) {
return NS_OK;
Expand Down

0 comments on commit 82370e0

Please sign in to comment.