From 327c7e2b154996577ded17173836960679b40eb8 Mon Sep 17 00:00:00 2001 From: Adam Vandolder Date: Tue, 13 Jun 2023 15:51:46 +0000 Subject: [PATCH] Bug 1556358 - Part 1: Add formStateRestore CE lifecycle callback. r=edgar Depends on D176127 Differential Revision: https://phabricator.services.mozilla.com/D174114 --- dom/base/CustomElementRegistry.cpp | 30 +++++++++++++++++++ dom/base/CustomElementRegistry.h | 6 ++++ dom/html/ElementInternals.h | 1 + dom/webidl/CustomElementRegistry.webidl | 8 +++++ .../CustomElementRegistry.html.ini | 4 --- 5 files changed, 45 insertions(+), 4 deletions(-) delete mode 100644 testing/web-platform/meta/custom-elements/CustomElementRegistry.html.ini diff --git a/dom/base/CustomElementRegistry.cpp b/dom/base/CustomElementRegistry.cpp index d8b40b5bc5b6..d16dcb44ee6f 100644 --- a/dom/base/CustomElementRegistry.cpp +++ b/dom/base/CustomElementRegistry.cpp @@ -20,6 +20,7 @@ #include "mozilla/dom/DocGroup.h" #include "mozilla/dom/CustomEvent.h" #include "mozilla/dom/ShadowRoot.h" +#include "mozilla/dom/UnionTypes.h" #include "mozilla/AutoRestore.h" #include "mozilla/HoldDropJSObjects.h" #include "mozilla/UseCounter.h" @@ -189,6 +190,14 @@ UniquePtr CustomElementCallback::Create( } break; + case ElementCallbackType::eFormStateRestore: + if (aDefinition->mFormAssociatedCallbacks->mFormStateRestoreCallback + .WasPassed()) { + func = aDefinition->mFormAssociatedCallbacks->mFormStateRestoreCallback + .Value(); + } + break; + case ElementCallbackType::eGetCustomInterface: MOZ_ASSERT_UNREACHABLE("Don't call GetCustomInterface through callback"); break; @@ -234,6 +243,27 @@ void CustomElementCallback::Call() { static_cast(mCallback.get()) ->Call(mThisObject, mArgs.mDisabled); break; + case ElementCallbackType::eFormStateRestore: { + if (mArgs.mState.IsNull()) { + MOZ_ASSERT_UNREACHABLE( + "A null state should never be restored to a form-associated " + "custom element"); + return; + } + + const OwningFileOrUSVStringOrFormData& owningValue = mArgs.mState.Value(); + Nullable value; + if (owningValue.IsFormData()) { + value.SetValue().SetAsFormData() = owningValue.GetAsFormData(); + } else if (owningValue.IsFile()) { + value.SetValue().SetAsFile() = owningValue.GetAsFile(); + } else { + value.SetValue().SetAsUSVString().ShareOrDependUpon( + owningValue.GetAsUSVString()); + } + static_cast(mCallback.get()) + ->Call(mThisObject, value, mArgs.mReason); + } break; case ElementCallbackType::eGetCustomInterface: MOZ_ASSERT_UNREACHABLE("Don't call GetCustomInterface through callback"); break; diff --git a/dom/base/CustomElementRegistry.h b/dom/base/CustomElementRegistry.h index 5cf3285b461d..cd6f120de4e0 100644 --- a/dom/base/CustomElementRegistry.h +++ b/dom/base/CustomElementRegistry.h @@ -16,6 +16,7 @@ #include "mozilla/dom/Document.h" #include "mozilla/dom/Element.h" #include "mozilla/dom/ElementInternals.h" +#include "mozilla/dom/ElementInternalsBinding.h" #include "mozilla/dom/HTMLFormElement.h" #include "mozilla/RefPtr.h" #include "nsCycleCollectionParticipant.h" @@ -43,6 +44,7 @@ enum class ElementCallbackType { eFormAssociated, eFormReset, eFormDisabled, + eFormStateRestore, eGetCustomInterface }; @@ -63,6 +65,10 @@ struct LifecycleCallbackArgs { // Used by the form disabled callback. bool mDisabled; + // Used by the form state restore callback. + Nullable mState; + RestoreReason mReason; + size_t SizeOfExcludingThis(MallocSizeOf aMallocSizeOf) const; }; diff --git a/dom/html/ElementInternals.h b/dom/html/ElementInternals.h index 4f0008421c6b..191437c29562 100644 --- a/dom/html/ElementInternals.h +++ b/dom/html/ElementInternals.h @@ -10,6 +10,7 @@ #include "js/TypeDecls.h" #include "mozilla/ErrorResult.h" #include "mozilla/dom/ElementInternalsBinding.h" +#include "mozilla/dom/UnionTypes.h" #include "nsCycleCollectionParticipant.h" #include "nsIConstraintValidation.h" #include "nsIFormControl.h" diff --git a/dom/webidl/CustomElementRegistry.webidl b/dom/webidl/CustomElementRegistry.webidl index 8c10f2f41fac..07c855ea1f5a 100644 --- a/dom/webidl/CustomElementRegistry.webidl +++ b/dom/webidl/CustomElementRegistry.webidl @@ -20,6 +20,11 @@ dictionary ElementDefinitionOptions { DOMString extends; }; +enum RestoreReason { + "restore", + "autocomplete", +}; + callback constructor CustomElementConstructor = any (); [MOZ_CAN_RUN_SCRIPT_BOUNDARY] @@ -44,6 +49,8 @@ callback LifecycleFormResetCallback = undefined(); [MOZ_CAN_RUN_SCRIPT_BOUNDARY] callback LifecycleFormDisabledCallback = undefined(boolean disabled); [MOZ_CAN_RUN_SCRIPT_BOUNDARY] +callback LifecycleFormStateRestoreCallback = undefined((File or USVString or FormData)? state, RestoreReason reason); +[MOZ_CAN_RUN_SCRIPT_BOUNDARY] callback LifecycleGetCustomInterfaceCallback = object?(any iid); // Unsorted is necessary until https://github.com/whatwg/html/issues/3580 is resolved. @@ -61,4 +68,5 @@ dictionary FormAssociatedLifecycleCallbacks { LifecycleFormAssociatedCallback formAssociatedCallback; LifecycleFormResetCallback formResetCallback; LifecycleFormDisabledCallback formDisabledCallback; + LifecycleFormStateRestoreCallback formStateRestoreCallback; }; diff --git a/testing/web-platform/meta/custom-elements/CustomElementRegistry.html.ini b/testing/web-platform/meta/custom-elements/CustomElementRegistry.html.ini deleted file mode 100644 index d2e9f567945b..000000000000 --- a/testing/web-platform/meta/custom-elements/CustomElementRegistry.html.ini +++ /dev/null @@ -1,4 +0,0 @@ -[CustomElementRegistry.html] - [customElements.define must get four additional callbacks on the prototype if formAssociated is converted to true] - expected: FAIL -