Backed out changeset 2450366cf7ca (bug 1891629) for causing win msix mochitest failures
[gecko.git] / intl / l10n / FluentResource.cpp
blob38582138a5d43fdbe39f6a4689c36fa793b1aaf7
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "nsContentUtils.h"
8 #include "FluentResource.h"
10 using namespace mozilla::dom;
12 namespace mozilla {
13 namespace intl {
15 NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(FluentResource, mParent)
17 FluentResource::FluentResource(nsISupports* aParent,
18 const ffi::FluentResource* aRaw)
19 : mParent(aParent), mRaw(std::move(aRaw)), mHasErrors(false) {}
21 FluentResource::FluentResource(nsISupports* aParent, const nsACString& aSource)
22 : mParent(aParent), mHasErrors(false) {
23 mRaw = dont_AddRef(ffi::fluent_resource_new(&aSource, &mHasErrors));
26 already_AddRefed<FluentResource> FluentResource::Constructor(
27 const GlobalObject& aGlobal, const nsACString& aSource) {
28 RefPtr<FluentResource> res =
29 new FluentResource(aGlobal.GetAsSupports(), aSource);
31 if (res->mHasErrors) {
32 nsContentUtils::LogSimpleConsoleError(
33 u"Errors encountered while parsing Fluent Resource."_ns, "chrome"_ns,
34 false, true /* from chrome context*/);
36 return res.forget();
39 void FluentResource::TextElements(
40 nsTArray<dom::FluentTextElementItem>& aElements, ErrorResult& aRv) {
41 if (mHasErrors) {
42 aRv.ThrowInvalidStateError("textElements don't exist due to parse error");
43 return;
46 nsTArray<ffi::TextElementInfo> elements;
47 ffi::fluent_resource_get_text_elements(mRaw, &elements);
49 auto maybeAssign = [](dom::Optional<nsCString>& aDest, nsCString&& aSrc) {
50 if (!aSrc.IsEmpty()) {
51 aDest.Construct() = std::move(aSrc);
55 for (auto& info : elements) {
56 dom::FluentTextElementItem item;
57 maybeAssign(item.mId, std::move(info.id));
58 maybeAssign(item.mAttr, std::move(info.attr));
59 maybeAssign(item.mText, std::move(info.text));
61 aElements.AppendElement(item);
65 JSObject* FluentResource::WrapObject(JSContext* aCx,
66 JS::Handle<JSObject*> aGivenProto) {
67 return FluentResource_Binding::Wrap(aCx, this, aGivenProto);
70 } // namespace intl
71 } // namespace mozilla