1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "js/ForOfIterator.h" // JS::ForOfIterator
#include "js/JSON.h" // JS_ParseJSON
#include "nsContentUtils.h"
#include "nsIScriptError.h"
#include "DOMLocalization.h"
#include "mozilla/intl/L10nRegistry.h"
#include "mozilla/intl/LocaleService.h"
#include "mozilla/dom/AutoEntryScript.h"
#include "mozilla/dom/Element.h"
#include "mozilla/dom/L10nOverlays.h"
using namespace mozilla;
using namespace mozilla::dom;
using namespace mozilla::intl;
NS_IMPL_CYCLE_COLLECTION_CLASS(DOMLocalization)
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN_INHERITED(DOMLocalization, Localization)
tmp->DisconnectMutations();
NS_IMPL_CYCLE_COLLECTION_UNLINK(mMutations)
NS_IMPL_CYCLE_COLLECTION_UNLINK(mRoots)
NS_IMPL_CYCLE_COLLECTION_UNLINK_PRESERVED_WRAPPER
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(DOMLocalization, Localization)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mMutations)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mRoots)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
NS_IMPL_ADDREF_INHERITED(DOMLocalization, Localization)
NS_IMPL_RELEASE_INHERITED(DOMLocalization, Localization)
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(DOMLocalization)
NS_INTERFACE_MAP_END_INHERITING(Localization)
DOMLocalization::DOMLocalization(nsIGlobalObject* aGlobal, bool aSync)
: Localization(aGlobal, aSync) {
mMutations = new L10nMutations(this);
}
DOMLocalization::DOMLocalization(nsIGlobalObject* aGlobal, bool aIsSync,
const ffi::LocalizationRc* aRaw)
: Localization(aGlobal, aIsSync, aRaw) {
mMutations = new L10nMutations(this);
}
already_AddRefed<DOMLocalization> DOMLocalization::Constructor(
const GlobalObject& aGlobal,
const Sequence<dom::OwningUTF8StringOrResourceId>& aResourceIds,
bool aIsSync, const Optional<NonNull<L10nRegistry>>& aRegistry,
const Optional<Sequence<nsCString>>& aLocales, ErrorResult& aRv) {
auto ffiResourceIds{L10nRegistry::ResourceIdsToFFI(aResourceIds)};
Maybe<nsTArray<nsCString>> locales;
if (aLocales.WasPassed()) {
locales.emplace();
locales->SetCapacity(aLocales.Value().Length());
for (const auto& locale : aLocales.Value()) {
locales->AppendElement(locale);
}
}
RefPtr<const ffi::LocalizationRc> raw;
bool result;
if (aRegistry.WasPassed()) {
result = ffi::localization_new_with_locales(
&ffiResourceIds, aIsSync, aRegistry.Value().Raw(),
locales.ptrOr(nullptr), getter_AddRefs(raw));
} else {
result = ffi::localization_new_with_locales(&ffiResourceIds, aIsSync,
nullptr, locales.ptrOr(nullptr),
getter_AddRefs(raw));
}
if (result) {
nsCOMPtr<nsIGlobalObject> global =
do_QueryInterface(aGlobal.GetAsSupports());
return do_AddRef(new DOMLocalization(global, aIsSync, raw));
}
aRv.ThrowInvalidStateError(
"Failed to create the Localization. Check the locales arguments.");
return nullptr;
}
JSObject* DOMLocalization::WrapObject(JSContext* aCx,
JS::Handle<JSObject*> aGivenProto) {
return DOMLocalization_Binding::Wrap(aCx, this, aGivenProto);
}
void DOMLocalization::Destroy() { DisconnectMutations(); }
DOMLocalization::~DOMLocalization() { Destroy(); }
bool DOMLocalization::HasPendingMutations() const {
return mMutations && mMutations->HasPendingMutations();
}
/**
* DOMLocalization API
*/
void DOMLocalization::ConnectRoot(nsINode& aNode) {
nsCOMPtr<nsIGlobalObject> global = aNode.GetOwnerGlobal();
if (!global) {
return;
}
MOZ_ASSERT(global == mGlobal,
"Cannot add a root that overlaps with existing root.");
#ifdef DEBUG
for (nsINode* root : mRoots) {
MOZ_ASSERT(
root != &aNode && !root->Contains(&aNode) && !aNode.Contains(root),
"Cannot add a root that overlaps with existing root.");
}
#endif
mRoots.Insert(&aNode);
aNode.AddMutationObserverUnlessExists(mMutations);
}
void DOMLocalization::DisconnectRoot(nsINode& aNode) {
if (mRoots.Contains(&aNode)) {
aNode.RemoveMutationObserver(mMutations);
mRoots.Remove(&aNode);
}
}
void DOMLocalization::PauseObserving() { mMutations->PauseObserving(); }
void DOMLocalization::ResumeObserving() { mMutations->ResumeObserving(); }
void DOMLocalization::SetAttributes(
JSContext* aCx, Element& aElement, const nsAString& aId,
const Optional<JS::Handle<JSObject*>>& aArgs, ErrorResult& aRv) {
if (aArgs.WasPassed() && aArgs.Value()) {
nsAutoString data;
JS::Rooted<JS::Value> val(aCx, JS::ObjectValue(*aArgs.Value()));
if (!nsContentUtils::StringifyJSON(aCx, val, data,
UndefinedIsNullStringLiteral)) {
aRv.NoteJSContextException(aCx);
return;
}
if (!aElement.AttrValueIs(kNameSpaceID_None, nsGkAtoms::datal10nargs, data,
eCaseMatters)) {
aElement.SetAttr(kNameSpaceID_None, nsGkAtoms::datal10nargs, data, true);
}
} else {
aElement.UnsetAttr(kNameSpaceID_None, nsGkAtoms::datal10nargs, true);
}
if (!aElement.AttrValueIs(kNameSpaceID_None, nsGkAtoms::datal10nid, aId,
eCaseMatters)) {
aElement.SetAttr(kNameSpaceID_None, nsGkAtoms::datal10nid, aId, true);
}
}
void DOMLocalization::GetAttributes(Element& aElement, L10nIdArgs& aResult,
ErrorResult& aRv) {
nsAutoString l10nId;
nsAutoString l10nArgs;
if (aElement.GetAttr(kNameSpaceID_None, nsGkAtoms::datal10nid, l10nId)) {
CopyUTF16toUTF8(l10nId, aResult.mId);
}
if (aElement.GetAttr(kNameSpaceID_None, nsGkAtoms::datal10nargs, l10nArgs)) {
ConvertStringToL10nArgs(l10nArgs, aResult.mArgs.SetValue(), aRv);
}
}
void DOMLocalization::SetArgs(JSContext* aCx, Element& aElement,
const Optional<JS::Handle<JSObject*>>& aArgs,
ErrorResult& aRv) {
if (aArgs.WasPassed() && aArgs.Value()) {
nsAutoString data;
JS::Rooted<JS::Value> val(aCx, JS::ObjectValue(*aArgs.Value()));
if (!nsContentUtils::StringifyJSON(aCx, val, data,
UndefinedIsNullStringLiteral)) {
aRv.NoteJSContextException(aCx);
return;
}
if (!aElement.AttrValueIs(kNameSpaceID_None, nsGkAtoms::datal10nargs, data,
eCaseMatters)) {
aElement.SetAttr(kNameSpaceID_None, nsGkAtoms::datal10nargs, data, true);
}
} else {
aElement.UnsetAttr(kNameSpaceID_None, nsGkAtoms::datal10nargs, true);
}
}
already_AddRefed<Promise> DOMLocalization::TranslateFragment(nsINode& aNode,
ErrorResult& aRv) {
Sequence<OwningNonNull<Element>> elements;
GetTranslatables(aNode, elements, aRv);
if (NS_WARN_IF(aRv.Failed())) {
return nullptr;
}
return TranslateElements(elements, aRv);
}
/**
* A Promise Handler used to apply the result of
* a call to Localization::FormatMessages onto the list
* of translatable elements.
*/
class ElementTranslationHandler : public PromiseNativeHandler {
public:
explicit ElementTranslationHandler(DOMLocalization* aDOMLocalization,
nsXULPrototypeDocument* aProto)
: mDOMLocalization(aDOMLocalization), mProto(aProto){};
NS_DECL_CYCLE_COLLECTING_ISUPPORTS
NS_DECL_CYCLE_COLLECTION_CLASS(ElementTranslationHandler)
nsTArray<nsCOMPtr<Element>>& Elements() { return mElements; }
void SetReturnValuePromise(Promise* aReturnValuePromise) {
mReturnValuePromise = aReturnValuePromise;
}
virtual void ResolvedCallback(JSContext* aCx, JS::Handle<JS::Value> aValue,
ErrorResult& aRv) override {
ErrorResult rv;
nsTArray<Nullable<L10nMessage>> l10nData;
if (aValue.isObject()) {
JS::ForOfIterator iter(aCx);
if (!iter.init(aValue, JS::ForOfIterator::AllowNonIterable)) {
mReturnValuePromise->MaybeRejectWithUndefined();
return;
}
if (!iter.valueIsIterable()) {
mReturnValuePromise->MaybeRejectWithUndefined();
return;
}
JS::Rooted<JS::Value> temp(aCx);
while (true) {
bool done;
if (!iter.next(&temp, &done)) {
mReturnValuePromise->MaybeRejectWithUndefined();
return;
}
if (done) {
break;
}
Nullable<L10nMessage>* slotPtr =
l10nData.AppendElement(mozilla::fallible);
if (!slotPtr) {
mReturnValuePromise->MaybeRejectWithUndefined();
return;
}
if (!temp.isNull()) {
if (!slotPtr->SetValue().Init(aCx, temp)) {
mReturnValuePromise->MaybeRejectWithUndefined();
return;
}
}
}
}
bool allTranslated =
mDOMLocalization->ApplyTranslations(mElements, l10nData, mProto, rv);
if (NS_WARN_IF(rv.Failed()) || !allTranslated) {
mReturnValuePromise->MaybeRejectWithUndefined();
return;
}
mReturnValuePromise->MaybeResolveWithUndefined();
}
virtual void RejectedCallback(JSContext* aCx, JS::Handle<JS::Value> aValue,
ErrorResult& aRv) override {
mReturnValuePromise->MaybeRejectWithClone(aCx, aValue);
}
private:
~ElementTranslationHandler() = default;
nsTArray<nsCOMPtr<Element>> mElements;
RefPtr<DOMLocalization> mDOMLocalization;
RefPtr<Promise> mReturnValuePromise;
RefPtr<nsXULPrototypeDocument> mProto;
};
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(ElementTranslationHandler)
NS_INTERFACE_MAP_ENTRY(nsISupports)
NS_INTERFACE_MAP_END
NS_IMPL_CYCLE_COLLECTION_CLASS(ElementTranslationHandler)
NS_IMPL_CYCLE_COLLECTING_ADDREF(ElementTranslationHandler)
NS_IMPL_CYCLE_COLLECTING_RELEASE(ElementTranslationHandler)
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(ElementTranslationHandler)
NS_IMPL_CYCLE_COLLECTION_UNLINK(mElements)
NS_IMPL_CYCLE_COLLECTION_UNLINK(mDOMLocalization)
NS_IMPL_CYCLE_COLLECTION_UNLINK(mReturnValuePromise)
NS_IMPL_CYCLE_COLLECTION_UNLINK(mProto)
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(ElementTranslationHandler)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mElements)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mDOMLocalization)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mReturnValuePromise)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mProto)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
already_AddRefed<Promise> DOMLocalization::TranslateElements(
const nsTArray<OwningNonNull<Element>>& aElements, ErrorResult& aRv) {
return TranslateElements(aElements, nullptr, aRv);
}
already_AddRefed<Promise> DOMLocalization::TranslateElements(
const nsTArray<OwningNonNull<Element>>& aElements,
nsXULPrototypeDocument* aProto, ErrorResult& aRv) {
Sequence<OwningUTF8StringOrL10nIdArgs> l10nKeys;
RefPtr<ElementTranslationHandler> nativeHandler =
new ElementTranslationHandler(this, aProto);
nsTArray<nsCOMPtr<Element>>& domElements = nativeHandler->Elements();
domElements.SetCapacity(aElements.Length());
if (!mGlobal) {
aRv.Throw(NS_ERROR_UNEXPECTED);
return nullptr;
}
for (auto& domElement : aElements) {
if (!domElement->HasAttr(nsGkAtoms::datal10nid)) {
continue;
}
OwningUTF8StringOrL10nIdArgs* key = l10nKeys.AppendElement(fallible);
if (!key) {
aRv.Throw(NS_ERROR_OUT_OF_MEMORY);
return nullptr;
}
GetAttributes(*domElement, key->SetAsL10nIdArgs(), aRv);
if (NS_WARN_IF(aRv.Failed())) {
return nullptr;
}
if (!domElements.AppendElement(domElement, fallible)) {
// This can't really happen, we SetCapacity'd above...
aRv.Throw(NS_ERROR_OUT_OF_MEMORY);
return nullptr;
}
}
RefPtr<Promise> promise = Promise::Create(mGlobal, aRv);
if (NS_WARN_IF(aRv.Failed())) {
return nullptr;
}
if (IsSync()) {
nsTArray<Nullable<L10nMessage>> l10nMessages;
FormatMessagesSync(l10nKeys, l10nMessages, aRv);
if (NS_WARN_IF(aRv.Failed())) {
promise->MaybeRejectWithUndefined();
return promise.forget();
}
bool allTranslated =
ApplyTranslations(domElements, l10nMessages, aProto, aRv);
if (NS_WARN_IF(aRv.Failed()) || !allTranslated) {
promise->MaybeRejectWithUndefined();
return promise.forget();
}
promise->MaybeResolveWithUndefined();
return promise.forget();
}
RefPtr<Promise> callbackResult = FormatMessages(l10nKeys, aRv);
if (NS_WARN_IF(aRv.Failed())) {
return nullptr;
}
nativeHandler->SetReturnValuePromise(promise);
callbackResult->AppendNativeHandler(nativeHandler);
return MaybeWrapPromise(promise);
}
/**
* Promise handler used to set localization data on
* roots of elements that got successfully translated.
*/
class L10nRootTranslationHandler final : public PromiseNativeHandler {
public:
NS_DECL_CYCLE_COLLECTING_ISUPPORTS
NS_DECL_CYCLE_COLLECTION_CLASS(L10nRootTranslationHandler)
explicit L10nRootTranslationHandler(Element* aRoot) : mRoot(aRoot) {}
void ResolvedCallback(JSContext* aCx, JS::Handle<JS::Value> aValue,
ErrorResult& aRv) override {
DOMLocalization::SetRootInfo(mRoot);
}
void RejectedCallback(JSContext* aCx, JS::Handle<JS::Value> aValue,
ErrorResult& aRv) override {}
private:
~L10nRootTranslationHandler() = default;
RefPtr<Element> mRoot;
};
NS_IMPL_CYCLE_COLLECTION(L10nRootTranslationHandler, mRoot)
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(L10nRootTranslationHandler)
NS_INTERFACE_MAP_ENTRY(nsISupports)
NS_INTERFACE_MAP_END
NS_IMPL_CYCLE_COLLECTING_ADDREF(L10nRootTranslationHandler)
NS_IMPL_CYCLE_COLLECTING_RELEASE(L10nRootTranslationHandler)
already_AddRefed<Promise> DOMLocalization::TranslateRoots(ErrorResult& aRv) {
nsTArray<RefPtr<Promise>> promises;
for (nsINode* root : mRoots) {
RefPtr<Promise> promise = TranslateFragment(*root, aRv);
if (MOZ_UNLIKELY(aRv.Failed())) {
return nullptr;
}
// If the root is an element, we'll add a native handler
// to set root info (language, direction etc.) on it
// once the localization finishes.
if (root->IsElement()) {
RefPtr<L10nRootTranslationHandler> nativeHandler =
new L10nRootTranslationHandler(root->AsElement());
promise->AppendNativeHandler(nativeHandler);
}
promises.AppendElement(promise);
}
AutoEntryScript aes(mGlobal, "DOMLocalization TranslateRoots");
return Promise::All(aes.cx(), promises, aRv);
}
/**
* Helper methods
*/
/* static */
void DOMLocalization::GetTranslatables(
nsINode& aNode, Sequence<OwningNonNull<Element>>& aElements,
ErrorResult& aRv) {
nsIContent* node =
aNode.IsContent() ? aNode.AsContent() : aNode.GetFirstChild();
for (; node; node = node->GetNextNode(&aNode)) {
if (!node->IsElement()) {
continue;
}
Element* domElement = node->AsElement();
if (!domElement->HasAttr(kNameSpaceID_None, nsGkAtoms::datal10nid)) {
continue;
}
if (!aElements.AppendElement(*domElement, fallible)) {
aRv.Throw(NS_ERROR_OUT_OF_MEMORY);
return;
}
}
}
/* static */
void DOMLocalization::SetRootInfo(Element* aElement) {
nsAutoCString primaryLocale;
LocaleService::GetInstance()->GetAppLocaleAsBCP47(primaryLocale);
aElement->SetAttr(kNameSpaceID_None, nsGkAtoms::lang,
NS_ConvertUTF8toUTF16(primaryLocale), true);
nsAutoString dir;
if (LocaleService::GetInstance()->IsAppLocaleRTL()) {
nsGkAtoms::rtl->ToString(dir);
} else {
nsGkAtoms::ltr->ToString(dir);
}
uint32_t nameSpace = aElement->GetNameSpaceID();
nsAtom* dirAtom =
nameSpace == kNameSpaceID_XUL ? nsGkAtoms::localedir : nsGkAtoms::dir;
aElement->SetAttr(kNameSpaceID_None, dirAtom, dir, true);
}
bool DOMLocalization::ApplyTranslations(
nsTArray<nsCOMPtr<Element>>& aElements,
nsTArray<Nullable<L10nMessage>>& aTranslations,
nsXULPrototypeDocument* aProto, ErrorResult& aRv) {
if (aElements.Length() != aTranslations.Length()) {
aRv.Throw(NS_ERROR_FAILURE);
return false;
}
PauseObserving();
bool hasMissingTranslation = false;
nsTArray<L10nOverlaysError> errors;
for (size_t i = 0; i < aTranslations.Length(); ++i) {
nsCOMPtr elem = aElements[i];
if (aTranslations[i].IsNull()) {
hasMissingTranslation = true;
continue;
}
// If we have a proto, we expect all elements are connected up.
// If they're not, they may have been removed by earlier translations.
// We will have added an error in L10nOverlays in this case.
// This is an error in fluent use, but shouldn't be crashing. There's
// also no point translating the element - skip it:
if (aProto && !elem->IsInComposedDoc()) {
continue;
}
// It is possible that someone removed the `data-l10n-id` from the element
// before the async translation completed. In that case, skip applying
// the translation.
if (!elem->HasAttr(kNameSpaceID_None, nsGkAtoms::datal10nid)) {
continue;
}
L10nOverlays::TranslateElement(*elem, aTranslations[i].Value(), errors,
aRv);
if (NS_WARN_IF(aRv.Failed())) {
hasMissingTranslation = true;
continue;
}
if (aProto) {
// We only need to rebuild deep if the translation has a value.
// Otherwise we'll only rebuild the attributes.
aProto->RebuildL10nPrototype(elem,
!aTranslations[i].Value().mValue.IsVoid());
}
}
ReportL10nOverlaysErrors(errors);
ResumeObserving();
return !hasMissingTranslation;
}
/* Protected */
void DOMLocalization::OnChange() {
Localization::OnChange();
RefPtr<Promise> promise = TranslateRoots(IgnoreErrors());
}
void DOMLocalization::DisconnectMutations() {
if (mMutations) {
mMutations->Disconnect();
DisconnectRoots();
}
}
void DOMLocalization::DisconnectRoots() {
for (nsINode* node : mRoots) {
node->RemoveMutationObserver(mMutations);
}
mRoots.Clear();
}
void DOMLocalization::ReportL10nOverlaysErrors(
nsTArray<L10nOverlaysError>& aErrors) {
nsAutoString msg;
for (auto& error : aErrors) {
if (error.mCode.WasPassed()) {
msg = u"[fluent-dom] "_ns;
switch (error.mCode.Value()) {
case L10nOverlays_Binding::ERROR_FORBIDDEN_TYPE:
msg += u"An element of forbidden type \""_ns +
error.mTranslatedElementName.Value() +
nsLiteralString(
u"\" was found in the translation. Only safe text-level "
"elements and elements with data-l10n-name are allowed.");
break;
case L10nOverlays_Binding::ERROR_NAMED_ELEMENT_MISSING:
msg += u"An element named \""_ns + error.mL10nName.Value() +
u"\" wasn't found in the source."_ns;
break;
case L10nOverlays_Binding::ERROR_NAMED_ELEMENT_TYPE_MISMATCH:
msg += u"An element named \""_ns + error.mL10nName.Value() +
nsLiteralString(
u"\" was found in the translation but its type ") +
error.mTranslatedElementName.Value() +
nsLiteralString(
u" didn't match the element found in the source ") +
error.mSourceElementName.Value() + u"."_ns;
break;
case L10nOverlays_Binding::ERROR_TRANSLATED_ELEMENT_DISCONNECTED:
msg += u"The element using message \""_ns + error.mL10nName.Value() +
nsLiteralString(
u"\" was removed from the DOM when translating its \"") +
error.mTranslatedElementName.Value() + u"\" parent."_ns;
break;
case L10nOverlays_Binding::ERROR_TRANSLATED_ELEMENT_DISALLOWED_DOM:
msg += nsLiteralString(
u"While translating an element with fluent ID \"") +
error.mL10nName.Value() + u"\" a child element of type \""_ns +
error.mTranslatedElementName.Value() +
nsLiteralString(
u"\" was removed. Either the fluent message "
"does not contain markup, or it does not contain markup "
"of this type.");
break;
case L10nOverlays_Binding::ERROR_UNKNOWN:
default:
msg += nsLiteralString(
u"Unknown error happened while translating an element.");
break;
}
nsPIDOMWindowInner* innerWindow = GetParentObject()->AsInnerWindow();
Document* doc = innerWindow ? innerWindow->GetExtantDoc() : nullptr;
if (doc) {
nsContentUtils::ReportToConsoleNonLocalized(
msg, nsIScriptError::warningFlag, "DOM"_ns, doc);
} else {
NS_WARNING("Failed to report l10n DOM Overlay errors to console.");
}
printf_stderr("%s\n", NS_ConvertUTF16toUTF8(msg).get());
}
}
}
void DOMLocalization::ConvertStringToL10nArgs(const nsString& aInput,
intl::L10nArgs& aRetVal,
ErrorResult& aRv) {
if (aInput.IsEmpty()) {
// There are no properties.
return;
}
// This method uses a temporary dictionary to automate
// converting a JSON string into an IDL Record via a dictionary.
//
// Once we get Record::Init(const nsAString& aJSON), we'll switch to
// that.
L10nArgsHelperDict helperDict;
if (!helperDict.Init(u"{\"args\": "_ns + aInput + u"}"_ns)) {
nsTArray<nsCString> errors{
"[dom/l10n] Failed to parse l10n-args JSON: "_ns +
NS_ConvertUTF16toUTF8(aInput),
};
MaybeReportErrorsToGecko(errors, aRv, GetParentObject());
return;
}
for (auto& entry : helperDict.mArgs.Entries()) {
L10nArgs::EntryType* newEntry = aRetVal.Entries().AppendElement(fallible);
if (!newEntry) {
aRv.Throw(NS_ERROR_OUT_OF_MEMORY);
return;
}
newEntry->mKey = entry.mKey;
newEntry->mValue = entry.mValue;
}
}
|