diff --git a/dom/base/nsINode.cpp b/dom/base/nsINode.cpp index f1668596d197..bf4e639fb6a4 100644 --- a/dom/base/nsINode.cpp +++ b/dom/base/nsINode.cpp @@ -29,6 +29,7 @@ #include "mozilla/TimeStamp.h" #include "mozilla/dom/CharacterData.h" #include "mozilla/dom/DocumentType.h" +#include "mozilla/dom/DOMOverlaysBinding.h" #include "mozilla/dom/Element.h" #include "mozilla/dom/Event.h" #include "mozilla/dom/L10nUtilsBinding.h" @@ -37,6 +38,7 @@ #include "mozilla/dom/ShadowRoot.h" #include "mozilla/dom/SVGUseElement.h" #include "mozilla/dom/ScriptSettings.h" +#include "mozilla/dom/l10n/DOMOverlays.h" #include "nsAttrValueOrString.h" #include "nsBindingManager.h" #include "nsCCUncollectableMarker.h" @@ -2770,59 +2772,30 @@ class LocalizationHandler : public PromiseNativeHandler { return; } - JS::Rooted untranslatedElements( - aCx, JS_NewArrayObject(aCx, mElements.Length())); - if (!untranslatedElements) { - mReturnValuePromise->MaybeRejectWithUndefined(); - return; - } - ErrorResult rv; + nsTArray errors; for (size_t i = 0; i < l10nData.Length(); ++i) { Element* elem = mElements[i]; - nsString& content = l10nData[i].mValue; - if (!content.IsVoid()) { - elem->SetTextContent(content, rv); - if (NS_WARN_IF(rv.Failed())) { - mReturnValuePromise->MaybeRejectWithUndefined(); - return; - } - } - - Nullable>& attributes = - l10nData[i].mAttributes; - if (!attributes.IsNull()) { - for (size_t j = 0; j < attributes.Value().Length(); ++j) { - nsString& name = attributes.Value()[j].mName; - nsString& value = attributes.Value()[j].mValue; - RefPtr nameAtom = NS_Atomize(name); - if (!elem->AttrValueIs(kNameSpaceID_None, nameAtom, value, - eCaseMatters)) { - rv = elem->SetAttr(kNameSpaceID_None, nameAtom, value, true); - if (rv.Failed()) { - mReturnValuePromise->MaybeRejectWithUndefined(); - return; - } - } - } - } - - if (content.IsVoid() && attributes.IsNull()) { - JS::Rooted wrappedElem(aCx); - if (!ToJSValue(aCx, elem, &wrappedElem)) { - mReturnValuePromise->MaybeRejectWithUndefined(); - return; - } - - if (!JS_DefineElement(aCx, untranslatedElements, i, wrappedElem, - JSPROP_ENUMERATE)) { - mReturnValuePromise->MaybeRejectWithUndefined(); - return; - } + mozilla::dom::l10n::DOMOverlays::TranslateElement(*elem, l10nData[i], + errors, rv); + if (NS_WARN_IF(rv.Failed())) { + mReturnValuePromise->MaybeRejectWithUndefined(); + return; } } - JS::Rooted result(aCx, JS::ObjectValue(*untranslatedElements)); - mReturnValuePromise->MaybeResolveWithClone(aCx, result); + + nsTArray jsErrors; + SequenceRooter rooter(aCx, &jsErrors); + for (auto& error : errors) { + JS::RootedValue jsError(aCx); + if (!ToJSValue(aCx, error, &jsError)) { + mReturnValuePromise->MaybeRejectWithUndefined(); + return; + } + jsErrors.AppendElement(jsError); + } + + mReturnValuePromise->MaybeResolve(jsErrors); } virtual void RejectedCallback(JSContext* aCx, diff --git a/dom/base/test/chrome/test_node_localize.xul b/dom/base/test/chrome/test_node_localize.xul index ae39d5e1d2c0..ddf42c8cb4fd 100644 --- a/dom/base/test/chrome/test_node_localize.xul +++ b/dom/base/test/chrome/test_node_localize.xul @@ -39,8 +39,7 @@ https://bugzilla.mozilla.org/show_bug.cgi?id=1363862 {name: "accesskey", value: "V"}, ] }, - "key4": undefined, - "key5": { + "key4": { value: null, attributes: [ {name: "value", value: "Submit Value"}, @@ -97,24 +96,16 @@ https://bugzilla.mozilla.org/show_bug.cgi?id=1363862 async function testLocalization() { const container = document.getElementById("testContainer"); - const untranslatedElements = await translateFragment(container); + await translateFragment(container); // We will walk through all translations and check if they // were correctly populated onto the DOM. for (const [l10nId, translation] of Object.entries(translations)) { const elem = document.querySelector(`[data-l10n-id=${l10nId}]`); - // If there is no translation then the element should be returned - // as part of the `untranslatedElements`. - if (translation === undefined) { - const i = Object.keys(translations).indexOf(l10nId); - ok(untranslatedElements[i] === elem); - continue; - } - if (translation.value !== null) { - ok(elem.textContent === translation.value, - "element's textContent should be populated with the translation value"); + ok(elem.textContent === translation.value, + "element's textContent should be populated with the translation value"); } if (translation.attributes !== null) { @@ -136,7 +127,6 @@ https://bugzilla.mozilla.org/show_bug.cgi?id=1363862