CSSMathSum.cpp (3220B)
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 2 /* vim:set ts=2 sw=2 sts=2 et cindent: */ 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/. */ 6 7 #include "mozilla/dom/CSSMathSum.h" 8 9 #include "mozilla/AlreadyAddRefed.h" 10 #include "mozilla/ErrorResult.h" 11 #include "mozilla/dom/BindingDeclarations.h" 12 #include "mozilla/dom/CSSMathSumBinding.h" 13 #include "mozilla/dom/CSSNumericArray.h" 14 #include "mozilla/dom/CSSNumericValueBinding.h" 15 #include "mozilla/dom/CSSUnitValue.h" 16 #include "nsString.h" 17 18 namespace mozilla::dom { 19 20 CSSMathSum::CSSMathSum(nsCOMPtr<nsISupports> aParent, 21 RefPtr<CSSNumericArray> aValues) 22 : CSSMathValue(std::move(aParent), ValueType::MathSum), 23 mValues(std::move(aValues)) {} 24 25 NS_IMPL_ISUPPORTS_CYCLE_COLLECTION_INHERITED_0(CSSMathSum, CSSMathValue) 26 NS_IMPL_CYCLE_COLLECTION_INHERITED(CSSMathSum, CSSMathValue, mValues) 27 28 JSObject* CSSMathSum::WrapObject(JSContext* aCx, 29 JS::Handle<JSObject*> aGivenProto) { 30 return CSSMathSum_Binding::Wrap(aCx, this, aGivenProto); 31 } 32 33 // start of CSSMathSum Web IDL implementation 34 35 // https://www.w3.org/TR/css-typed-om-1/#dom-cssmathsum-cssmathsum 36 // 37 // static 38 already_AddRefed<CSSMathSum> CSSMathSum::Constructor( 39 const GlobalObject& aGlobal, const Sequence<OwningCSSNumberish>& aArgs, 40 ErrorResult& aRv) { 41 nsCOMPtr<nsISupports> global = aGlobal.GetAsSupports(); 42 43 // Step 1. 44 45 nsTArray<RefPtr<CSSNumericValue>> values; 46 47 for (const OwningCSSNumberish& arg : aArgs) { 48 RefPtr<CSSNumericValue> value; 49 50 if (arg.IsDouble()) { 51 value = MakeRefPtr<CSSUnitValue>(global, arg.GetAsDouble(), "number"_ns); 52 } else { 53 MOZ_ASSERT(arg.IsCSSNumericValue()); 54 55 value = arg.GetAsCSSNumericValue(); 56 } 57 58 values.AppendElement(std::move(value)); 59 } 60 61 // Step 2. 62 63 if (values.IsEmpty()) { 64 aRv.ThrowSyntaxError("Arguments can't be empty"); 65 return nullptr; 66 } 67 68 // XXX Step 3 is not yet implemented! 69 70 // Step 4. 71 72 auto array = MakeRefPtr<CSSNumericArray>(global, std::move(values)); 73 74 return MakeAndAddRef<CSSMathSum>(global, std::move(array)); 75 } 76 77 CSSNumericArray* CSSMathSum::Values() const { return mValues; } 78 79 // end of CSSMathSum Web IDL implementation 80 81 void CSSMathSum::ToCssTextWithProperty(const CSSPropertyId& aPropertyId, 82 nsACString& aDest) const { 83 aDest.Append("calc("_ns); 84 85 bool written = false; 86 87 for (uint32_t index = 0; index < mValues->Length(); index++) { 88 bool found; 89 CSSNumericValue* value = mValues->IndexedGetter(index, found); 90 MOZ_ASSERT(found); 91 92 if (value->IsCSSUnitValue()) { 93 CSSUnitValue& unitValue = value->GetAsCSSUnitValue(); 94 95 if (written) { 96 aDest.Append(" + "_ns); 97 } 98 99 unitValue.ToCssTextWithProperty(aPropertyId, aDest); 100 written = true; 101 } 102 } 103 104 aDest.Append(")"_ns); 105 } 106 107 CSSMathSum& CSSStyleValue::GetAsCSSMathSum() { 108 MOZ_DIAGNOSTIC_ASSERT(mValueType == ValueType::MathSum); 109 110 return *static_cast<CSSMathSum*>(this); 111 } 112 113 } // namespace mozilla::dom