From 19243ff95eae41b9e7430666b8a146e76d09b2d6 Mon Sep 17 00:00:00 2001 From: Jessica Jong Date: Mon, 17 Jul 2017 14:17:19 +0800 Subject: [PATCH] Bug 1366361 - Part 1: .action/formAction should return the document's URL if @action/formaction is missing or empty. r=smaug MozReview-Commit-ID: H4CK0SVpaCv --- dom/html/HTMLButtonElement.h | 1 + dom/html/HTMLFormElement.cpp | 21 +++++++++++++++++++++ dom/html/HTMLFormElement.h | 3 ++- dom/html/HTMLInputElement.h | 1 + dom/html/nsGenericHTMLElement.cpp | 26 ++++++++++++++++++++++++++ dom/html/nsGenericHTMLElement.h | 2 ++ 6 files changed, 53 insertions(+), 1 deletion(-) diff --git a/dom/html/HTMLButtonElement.h b/dom/html/HTMLButtonElement.h index 57334e70b716..9a3c8f18b7fe 100644 --- a/dom/html/HTMLButtonElement.h +++ b/dom/html/HTMLButtonElement.h @@ -23,6 +23,7 @@ class HTMLButtonElement final : public nsGenericHTMLFormElementWithState, { public: using nsIConstraintValidation::GetValidationMessage; + using nsGenericHTMLFormElementWithState::GetFormAction; explicit HTMLButtonElement(already_AddRefed& aNodeInfo, FromParser aFromParser = NOT_FROM_PARSER); diff --git a/dom/html/HTMLFormElement.cpp b/dom/html/HTMLFormElement.cpp index ebda80c484b8..ebe0deca9216 100644 --- a/dom/html/HTMLFormElement.cpp +++ b/dom/html/HTMLFormElement.cpp @@ -1585,6 +1585,27 @@ HTMLFormElement::FlushPendingSubmission() } } +void +HTMLFormElement::GetAction(nsString& aValue) +{ + if (!GetAttr(kNameSpaceID_None, nsGkAtoms::action, aValue) || + aValue.IsEmpty()) { + nsIDocument* document = OwnerDoc(); + nsIURI* docURI = document->GetDocumentURI(); + if (docURI) { + nsAutoCString spec; + nsresult rv = docURI->GetSpec(spec); + if (NS_FAILED(rv)) { + return; + } + + CopyUTF8toUTF16(spec, aValue); + } + } else { + GetURIAttr(nsGkAtoms::action, nullptr, aValue); + } +} + nsresult HTMLFormElement::GetActionURL(nsIURI** aActionURL, nsIContent* aOriginatingElement) diff --git a/dom/html/HTMLFormElement.h b/dom/html/HTMLFormElement.h index 4f806b75f222..8f6225962aa0 100644 --- a/dom/html/HTMLFormElement.h +++ b/dom/html/HTMLFormElement.h @@ -314,7 +314,8 @@ public: SetHTMLAttr(nsGkAtoms::acceptcharset, aValue, aRv); } - // XPCOM GetAction() is OK + void GetAction(nsString& aValue); + void SetAction(const nsAString& aValue, ErrorResult& aRv) { SetHTMLAttr(nsGkAtoms::action, aValue, aRv); diff --git a/dom/html/HTMLInputElement.h b/dom/html/HTMLInputElement.h index e83d3fdfabb5..f42743d342c1 100644 --- a/dom/html/HTMLInputElement.h +++ b/dom/html/HTMLInputElement.h @@ -137,6 +137,7 @@ class HTMLInputElement final : public nsGenericHTMLFormElementWithState, public: using nsIConstraintValidation::GetValidationMessage; using nsGenericHTMLFormElementWithState::GetForm; + using nsGenericHTMLFormElementWithState::GetFormAction; enum class FromClone { no, yes }; diff --git a/dom/html/nsGenericHTMLElement.cpp b/dom/html/nsGenericHTMLElement.cpp index 34a642104652..b87ed1e28ef3 100644 --- a/dom/html/nsGenericHTMLElement.cpp +++ b/dom/html/nsGenericHTMLElement.cpp @@ -2428,6 +2428,32 @@ nsGenericHTMLFormElement::IsLabelable() const type == NS_FORM_TEXTAREA; } +void +nsGenericHTMLFormElement::GetFormAction(nsString& aValue) +{ + uint32_t type = ControlType(); + if (!(type & NS_FORM_INPUT_ELEMENT) && !(type & NS_FORM_BUTTON_ELEMENT)) { + return; + } + + if (!GetAttr(kNameSpaceID_None, nsGkAtoms::formaction, aValue) || + aValue.IsEmpty()) { + nsIDocument* document = OwnerDoc(); + nsIURI* docURI = document->GetDocumentURI(); + if (docURI) { + nsAutoCString spec; + nsresult rv = docURI->GetSpec(spec); + if (NS_FAILED(rv)) { + return; + } + + CopyUTF8toUTF16(spec, aValue); + } + } else { + GetURIAttr(nsGkAtoms::formaction, nullptr, aValue); + } +} + //---------------------------------------------------------------------- void diff --git a/dom/html/nsGenericHTMLElement.h b/dom/html/nsGenericHTMLElement.h index 1e4aa8de3e95..59a823496b1f 100644 --- a/dom/html/nsGenericHTMLElement.h +++ b/dom/html/nsGenericHTMLElement.h @@ -1120,6 +1120,8 @@ public: virtual bool IsLabelable() const override; + void GetFormAction(nsString& aValue); + protected: virtual ~nsGenericHTMLFormElement();