No bug - tagging b4d3227540c9ebc43d64aac6168fdca7019c22d8 with FIREFOX_BETA_126_BASE...
[gecko.git] / layout / style / FontFaceSetIterator.cpp
blobca05ee87d756314721df506c96f6269500fe33a8
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 "mozilla/dom/FontFaceSetIterator.h"
9 #include "js/Array.h" // JS::NewArrayObject
11 namespace mozilla {
12 namespace dom {
14 NS_IMPL_CYCLE_COLLECTION(FontFaceSetIterator, mFontFaceSet)
16 FontFaceSetIterator::FontFaceSetIterator(FontFaceSet* aFontFaceSet,
17 bool aIsKeyAndValue)
18 : mFontFaceSet(aFontFaceSet),
19 mNextIndex(0),
20 mIsKeyAndValue(aIsKeyAndValue) {
21 MOZ_COUNT_CTOR(FontFaceSetIterator);
24 FontFaceSetIterator::~FontFaceSetIterator() {
25 MOZ_COUNT_DTOR(FontFaceSetIterator);
28 bool FontFaceSetIterator::WrapObject(JSContext* aCx,
29 JS::Handle<JSObject*> aGivenProto,
30 JS::MutableHandle<JSObject*> aReflector) {
31 return FontFaceSetIterator_Binding::Wrap(aCx, this, aGivenProto, aReflector);
34 void FontFaceSetIterator::Next(JSContext* aCx,
35 FontFaceSetIteratorResult& aResult,
36 ErrorResult& aRv) {
37 if (!mFontFaceSet) {
38 aResult.mDone = true;
39 return;
42 // Skip over non-Author origin fonts (GetFontFaceAt returns nullptr
43 // for those).
44 FontFace* face;
45 while (!(face = mFontFaceSet->GetFontFaceAt(mNextIndex++))) {
46 if (mNextIndex >= mFontFaceSet->SizeIncludingNonAuthorOrigins()) {
47 break; // this iterator is done
51 if (!face) {
52 aResult.mValue.setUndefined();
53 aResult.mDone = true;
54 mFontFaceSet = nullptr;
55 return;
58 JS::Rooted<JS::Value> value(aCx);
59 if (!ToJSValue(aCx, face, &value)) {
60 aRv.Throw(NS_ERROR_FAILURE);
61 return;
64 if (mIsKeyAndValue) {
65 JS::RootedValueArray<2> values(aCx);
66 values[0].set(value);
67 values[1].set(value);
69 JS::Rooted<JSObject*> array(aCx);
70 array = JS::NewArrayObject(aCx, values);
71 if (array) {
72 aResult.mValue.setObject(*array);
74 } else {
75 aResult.mValue = value;
78 aResult.mDone = false;
81 } // namespace dom
82 } // namespace mozilla