CSSUnitValue.cpp (3963B)
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/CSSUnitValue.h" 8 9 #include "mozilla/AlreadyAddRefed.h" 10 #include "mozilla/CSSPropertyId.h" 11 #include "mozilla/ErrorResult.h" 12 #include "mozilla/RefPtr.h" 13 #include "mozilla/dom/BindingDeclarations.h" 14 #include "mozilla/dom/CSSUnitValueBinding.h" 15 16 namespace mozilla::dom { 17 18 CSSUnitValue::CSSUnitValue(nsCOMPtr<nsISupports> aParent, double aValue, 19 const nsACString& aUnit) 20 : CSSNumericValue(std::move(aParent), ValueType::UnitValue), 21 mValue(aValue), 22 mUnit(aUnit) {} 23 24 JSObject* CSSUnitValue::WrapObject(JSContext* aCx, 25 JS::Handle<JSObject*> aGivenProto) { 26 return CSSUnitValue_Binding::Wrap(aCx, this, aGivenProto); 27 } 28 29 // start of CSSUnitValue Web IDL implementation 30 31 // https://drafts.css-houdini.org/css-typed-om-1/#dom-cssunitvalue-cssunitvalue 32 // 33 // static 34 already_AddRefed<CSSUnitValue> CSSUnitValue::Constructor( 35 const GlobalObject& aGlobal, double aValue, const nsACString& aUnit, 36 ErrorResult& aRv) { 37 // XXX Units should be normalized to lowercase. The Typed OM spec doesn’t 38 // state this explicitly, but WPT requires lowercase normalization and it 39 // can also be deduced from the CSS Values spec. Besides fixing it here, 40 // a spec issue may be needed to clarify this. 41 42 // Step 1. 43 44 // XXX A type should be created from unit and if that fails, the failure 45 // should be propagated here 46 47 // Step 2. 48 49 return MakeAndAddRef<CSSUnitValue>(aGlobal.GetAsSupports(), aValue, aUnit); 50 } 51 52 double CSSUnitValue::Value() const { return mValue; } 53 54 void CSSUnitValue::SetValue(double aArg) { mValue = aArg; } 55 56 void CSSUnitValue::GetUnit(nsCString& aRetVal) const { aRetVal = mUnit; } 57 58 // end of CSSUnitValue Web IDL implementation 59 60 void CSSUnitValue::ToCssTextWithProperty(const CSSPropertyId& aPropertyId, 61 nsACString& aDest) const { 62 // XXX See: 63 // https://drafts.css-houdini.org/css-typed-om-1/#create-an-internal-representation 64 // 65 // The current |isValueOutOfRange| check implements only a minimal subset of 66 // the algorithm step that wraps out-of-range numeric values (e.g. negative 67 // perspective) in a calc() expression to keep them representable. 68 // 69 // This is a temporary solution until a more robust mechanism is added for 70 // handling range checks and value wrapping. The long-term plan is to move 71 // this logic to a dedicated FromTyped trait or similar infrastructure that 72 // can validate and construct internal representations in a property-aware 73 // and fully spec-compliant manner. See bug 2005142 74 const bool isValueOutOfRange = [](NonCustomCSSPropertyId aId, double aValue) { 75 switch (aId) { 76 case eCSSProperty_column_width: 77 case eCSSProperty_perspective: 78 case eCSSProperty_border_block_end_width: 79 case eCSSProperty_border_block_start_width: 80 case eCSSProperty_border_bottom_width: 81 case eCSSProperty_border_inline_end_width: 82 case eCSSProperty_border_inline_start_width: 83 case eCSSProperty_border_left_width: 84 case eCSSProperty_border_right_width: 85 case eCSSProperty_border_top_width: 86 case eCSSProperty_outline_width: 87 return aValue < 0; 88 89 default: 90 return false; 91 } 92 }(aPropertyId.mId, mValue); 93 94 if (isValueOutOfRange) { 95 aDest.Append("calc("_ns); 96 } 97 98 aDest.AppendFloat(mValue); 99 aDest.Append(mUnit); 100 101 if (isValueOutOfRange) { 102 aDest.Append(")"_ns); 103 } 104 } 105 106 CSSUnitValue& CSSStyleValue::GetAsCSSUnitValue() { 107 MOZ_DIAGNOSTIC_ASSERT(mValueType == ValueType::UnitValue); 108 109 return *static_cast<CSSUnitValue*>(this); 110 } 111 112 } // namespace mozilla::dom