StylePropertyMap.cpp (4432B)
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/. */ 6 7 #include "mozilla/dom/StylePropertyMap.h" 8 9 #include "CSSUnsupportedValue.h" 10 #include "mozilla/ErrorResult.h" 11 #include "mozilla/RefPtr.h" 12 #include "mozilla/dom/BindingDeclarations.h" 13 #include "mozilla/dom/CSSKeywordValue.h" 14 #include "mozilla/dom/CSSMathSum.h" 15 #include "mozilla/dom/CSSStyleValue.h" 16 #include "mozilla/dom/CSSUnitValue.h" 17 #include "mozilla/dom/StylePropertyMapBinding.h" 18 #include "nsCOMPtr.h" 19 #include "nsCSSProps.h" 20 #include "nsDOMCSSDeclaration.h" 21 #include "nsQueryObject.h" 22 #include "nsString.h" 23 #include "nsStyledElement.h" 24 25 namespace mozilla::dom { 26 27 StylePropertyMap::StylePropertyMap(Element* aElement, bool aComputed) 28 : StylePropertyMapReadOnly(aElement, aComputed) { 29 MOZ_DIAGNOSTIC_ASSERT(!aComputed); 30 } 31 32 StylePropertyMap::StylePropertyMap(CSSStyleRule* aRule) 33 : StylePropertyMapReadOnly(aRule) {} 34 35 JSObject* StylePropertyMap::WrapObject(JSContext* aCx, 36 JS::Handle<JSObject*> aGivenProto) { 37 return StylePropertyMap_Binding::Wrap(aCx, this, aGivenProto); 38 } 39 40 // start of StylePropertyMap Web IDL implementation 41 42 // https://drafts.css-houdini.org/css-typed-om/#dom-stylepropertymap-set 43 // 44 // XXX This is not yet fully implemented and optimized! 45 void StylePropertyMap::Set( 46 const nsACString& aProperty, 47 const Sequence<OwningCSSStyleValueOrUTF8String>& aValues, 48 ErrorResult& aRv) { 49 // Step 2. 50 51 NonCustomCSSPropertyId id = nsCSSProps::LookupProperty(aProperty); 52 if (id == eCSSProperty_UNKNOWN) { 53 aRv.ThrowTypeError("Invalid property: "_ns + aProperty); 54 return; 55 } 56 57 auto propertyId = CSSPropertyId::FromIdOrCustomProperty(id, aProperty); 58 59 if (aValues.Length() != 1) { 60 aRv.Throw(NS_ERROR_NOT_IMPLEMENTED); 61 return; 62 } 63 64 const auto& styleValueOrString = aValues[0]; 65 66 if (!styleValueOrString.IsCSSStyleValue()) { 67 aRv.Throw(NS_ERROR_NOT_IMPLEMENTED); 68 return; 69 } 70 71 CSSStyleValue& styleValue = styleValueOrString.GetAsCSSStyleValue(); 72 73 // Step 4 74 75 const auto* valuePropertyId = styleValue.GetPropertyId(); 76 77 if (valuePropertyId && *valuePropertyId != propertyId) { 78 aRv.ThrowTypeError("Invalid type for property"_ns); 79 return; 80 } 81 82 nsAutoCString cssText; 83 84 switch (styleValue.GetValueType()) { 85 case CSSStyleValue::ValueType::MathSum: { 86 CSSMathSum& mathSum = styleValue.GetAsCSSMathSum(); 87 88 mathSum.ToCssTextWithProperty(propertyId, cssText); 89 break; 90 } 91 92 case CSSStyleValue::ValueType::UnitValue: { 93 CSSUnitValue& unitValue = styleValue.GetAsCSSUnitValue(); 94 95 unitValue.ToCssTextWithProperty(propertyId, cssText); 96 break; 97 } 98 99 case CSSStyleValue::ValueType::KeywordValue: { 100 CSSKeywordValue& keywordValue = styleValue.GetAsCSSKeywordValue(); 101 102 keywordValue.ToCssTextWithProperty(propertyId, cssText); 103 break; 104 } 105 106 case CSSStyleValue::ValueType::UnsupportedValue: { 107 CSSUnsupportedValue& unsupportedValue = 108 styleValue.GetAsCSSUnsupportedValue(); 109 110 unsupportedValue.ToCssTextWithProperty(propertyId, cssText); 111 break; 112 } 113 114 case CSSStyleValue::ValueType::Uninitialized: 115 break; 116 } 117 118 if (cssText.IsEmpty()) { 119 aRv.Throw(NS_ERROR_NOT_IMPLEMENTED); 120 return; 121 } 122 123 // Step 6. 124 125 RefPtr<nsStyledElement> styledElement = do_QueryObject(mParent); 126 if (!styledElement) { 127 aRv.Throw(NS_ERROR_NOT_IMPLEMENTED); 128 return; 129 } 130 131 nsCOMPtr<nsDOMCSSDeclaration> declaration = styledElement->Style(); 132 133 declaration->SetProperty(aProperty, cssText, ""_ns, aRv); 134 } 135 136 void StylePropertyMap::Append( 137 const nsACString& aProperty, 138 const Sequence<OwningCSSStyleValueOrUTF8String>& aValues, 139 ErrorResult& aRv) { 140 aRv.Throw(NS_ERROR_NOT_IMPLEMENTED); 141 } 142 143 void StylePropertyMap::Delete(const nsACString& aProperty, ErrorResult& aRv) { 144 aRv.Throw(NS_ERROR_NOT_IMPLEMENTED); 145 } 146 147 void StylePropertyMap::Clear() {} 148 149 // end of StylePropertyMap Web IDL implementation 150 151 size_t StylePropertyMap::SizeOfIncludingThis(MallocSizeOf aMallocSizeOf) const { 152 return StylePropertyMapReadOnly::SizeOfExcludingThis(aMallocSizeOf) + 153 aMallocSizeOf(this); 154 } 155 156 } // namespace mozilla::dom