diff --git a/dom/base/nsDOMWindowUtils.cpp b/dom/base/nsDOMWindowUtils.cpp index 926003c7b074..e92c3fcfe368 100644 --- a/dom/base/nsDOMWindowUtils.cpp +++ b/dom/base/nsDOMWindowUtils.cpp @@ -2316,6 +2316,16 @@ nsDOMWindowUtils::GetFocusedAutocapitalize(nsAString& aAutocapitalize) { return NS_OK; } +NS_IMETHODIMP +nsDOMWindowUtils::GetFocusedAutocorrect(bool* aAutocorrect) { + nsCOMPtr widget = GetWidget(); + if (!widget) { + return NS_ERROR_FAILURE; + } + *aAutocorrect = widget->GetInputContext().mAutocorrect; + return NS_OK; +} + NS_IMETHODIMP nsDOMWindowUtils::GetViewId(Element* aElement, nsViewID* aResult) { if (aElement && nsLayoutUtils::FindIDFor(aElement, aResult)) { diff --git a/dom/events/IMEStateManager.cpp b/dom/events/IMEStateManager.cpp index 7a16751ce506..db8f5e8fcec2 100644 --- a/dom/events/IMEStateManager.cpp +++ b/dom/events/IMEStateManager.cpp @@ -1904,6 +1904,31 @@ static void GetAutocapitalize(const IMEState& aState, const Element& aElement, } } +static bool GetAutocorrect(const IMEState& aState, const Element& aElement, + const InputContext& aInputContext) { + if (!StaticPrefs::dom_forms_autocorrect()) { +#ifdef ANDROID + // Autocorrect was on-by-default on Android by bug 806349, despite + // autocorrect preference. + return true; +#else + return false; +#endif + } + + if (aElement.IsHTMLElement() && aState.IsEditable()) { + if (nsContentUtils::IsChromeDoc(aElement.OwnerDoc()) && + !aElement.HasAttr(nsGkAtoms::autocorrect)) { + // Since Chrome UI may not want to enable autocorrect by default such as + // bug 1881783. + return false; + } + + return nsGenericHTMLElement::FromNode(&aElement)->Autocorrect(); + } + return true; +} + // static void IMEStateManager::SetIMEState(const IMEState& aState, const nsPresContext* aPresContext, @@ -1972,6 +1997,7 @@ void IMEStateManager::SetIMEState(const IMEState& aState, GetInputMode(aState, *focusedElement, context.mHTMLInputMode); GetAutocapitalize(aState, *focusedElement, context, context.mAutocapitalize); + context.mAutocorrect = GetAutocorrect(aState, *focusedElement, context); } if (aAction.mCause == InputContextAction::CAUSE_UNKNOWN && diff --git a/dom/interfaces/base/nsIDOMWindowUtils.idl b/dom/interfaces/base/nsIDOMWindowUtils.idl index 4f0fc82a36c5..7f87e544c37c 100644 --- a/dom/interfaces/base/nsIDOMWindowUtils.idl +++ b/dom/interfaces/base/nsIDOMWindowUtils.idl @@ -1779,6 +1779,11 @@ interface nsIDOMWindowUtils : nsISupports { */ readonly attribute AString focusedAutocapitalize; + /** + * Get the autocorrect of the currently focused editing host, if any. + */ + readonly attribute boolean focusedAutocorrect; + /** * Find the view ID for a given element. This is the reverse of * findElementWithViewId(). diff --git a/widget/IMEData.cpp b/widget/IMEData.cpp index f9497a648d5c..75a0ffddbebb 100644 --- a/widget/IMEData.cpp +++ b/widget/IMEData.cpp @@ -244,7 +244,9 @@ std::ostream& operator<<(std::ostream& aStream, const InputContext& aContext) { << aContext.mHTMLInputType << "\", mHTMLInputMode=\"" << aContext.mHTMLInputMode << "\", mActionHint=\"" << aContext.mActionHint << "\", mAutocapitalize=\"" - << aContext.mAutocapitalize << "\", mIsPrivateBrowsing=" + << aContext.mAutocapitalize + << "\", mAutocorrect=" << (aContext.mAutocorrect ? "true" : "false") + << ", mIsPrivateBrowsing=" << (aContext.mInPrivateBrowsing ? "true" : "false") << " }"; return aStream; } diff --git a/widget/IMEData.h b/widget/IMEData.h index 1ea33e18b9e9..fd12a2907a87 100644 --- a/widget/IMEData.h +++ b/widget/IMEData.h @@ -425,6 +425,7 @@ struct InputContext final { mHTMLInputMode.Truncate(); mActionHint.Truncate(); mAutocapitalize.Truncate(); + mAutocorrect = true; } bool IsPasswordEditor() const { @@ -466,6 +467,11 @@ struct InputContext final { #if defined(ANDROID) || defined(XP_IOS) // enterkeyhint is only supported by Android IME API and iOS API. mActionHint != aOldContext.mActionHint || +#endif +#if defined(ANDROID) || defined(XP_DARWIN) + // autocorrect is only supported by Android IME API, macOS text + // substitution and iOS API. + mAutocorrect != aOldContext.mAutocorrect || #endif false; } @@ -487,6 +493,9 @@ struct InputContext final { /* A hint for autocapitalize */ nsString mAutocapitalize; + /* A hint for autocorrect */ + bool mAutocorrect = true; // on-by-default + /** * mOrigin indicates whether this focus event refers to main or remote * content. diff --git a/widget/nsGUIEventIPC.h b/widget/nsGUIEventIPC.h index cfededb82aa7..a1f48167403f 100644 --- a/widget/nsGUIEventIPC.h +++ b/widget/nsGUIEventIPC.h @@ -846,6 +846,7 @@ struct ParamTraits { WriteParam(aWriter, aParam.mHTMLInputMode); WriteParam(aWriter, aParam.mActionHint); WriteParam(aWriter, aParam.mAutocapitalize); + WriteParam(aWriter, aParam.mAutocorrect); WriteParam(aWriter, aParam.mOrigin); WriteParam(aWriter, aParam.mHasHandledUserInput); WriteParam(aWriter, aParam.mInPrivateBrowsing); @@ -858,6 +859,7 @@ struct ParamTraits { ReadParam(aReader, &aResult->mHTMLInputMode) && ReadParam(aReader, &aResult->mActionHint) && ReadParam(aReader, &aResult->mAutocapitalize) && + ReadParam(aReader, &aResult->mAutocorrect) && ReadParam(aReader, &aResult->mOrigin) && ReadParam(aReader, &aResult->mHasHandledUserInput) && ReadParam(aReader, &aResult->mInPrivateBrowsing) && diff --git a/widget/tests/chrome.toml b/widget/tests/chrome.toml index 79857320aa59..74805c5da043 100644 --- a/widget/tests/chrome.toml +++ b/widget/tests/chrome.toml @@ -8,6 +8,8 @@ tags = "os_integration" ["test_alwaysontop_focus.xhtml"] +["test_autocorrect_in_parent.html"] + # Privacy relevant ["test_bug343416.xhtml"] diff --git a/widget/tests/mochitest.toml b/widget/tests/mochitest.toml index 81be915d02ee..f9f4843469fd 100644 --- a/widget/tests/mochitest.toml +++ b/widget/tests/mochitest.toml @@ -18,6 +18,8 @@ skip-if = [ ["test_autocapitalize.html"] +["test_autocorrect.html"] + ["test_clipboard.html"] skip-if = [ "headless", # bug 1852983 diff --git a/widget/tests/test_autocorrect.html b/widget/tests/test_autocorrect.html new file mode 100644 index 000000000000..60d80bf1afd2 --- /dev/null +++ b/widget/tests/test_autocorrect.html @@ -0,0 +1,66 @@ + + + + Tests for autocorrect + + + + + +

+ + +
+
+
+
+
+
+
+
+
+
+

+

+

+

+

+

+
+ +
+
+
+ + diff --git a/widget/tests/test_autocorrect_in_parent.html b/widget/tests/test_autocorrect_in_parent.html new file mode 100644 index 000000000000..4eb223e6fced --- /dev/null +++ b/widget/tests/test_autocorrect_in_parent.html @@ -0,0 +1,47 @@ + + + +Tests for default of autocorrect in parent + + + + +

+ + +
+
+
+
+
+

+

+
+ +
+
+
+ +