1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "mozilla/dom/StylePropertyMapReadOnly.h"
#include "CSSUnsupportedValue.h"
#include "mozilla/Assertions.h"
#include "mozilla/ComputedStyle.h"
#include "mozilla/DeclarationBlock.h"
#include "mozilla/ErrorResult.h"
#include "mozilla/RefPtr.h"
#include "mozilla/ServoStyleConsts.h"
#include "mozilla/dom/CSSKeywordValue.h"
#include "mozilla/dom/CSSMathSum.h"
#include "mozilla/dom/CSSNumericArray.h"
#include "mozilla/dom/CSSStyleValue.h"
#include "mozilla/dom/CSSUnitValue.h"
#include "mozilla/dom/Element.h"
#include "mozilla/dom/StylePropertyMapReadOnlyBinding.h"
#include "nsCSSProps.h"
#include "nsComputedDOMStyle.h"
#include "nsCycleCollectionParticipant.h"
#include "nsQueryObject.h"
#include "nsReadableUtils.h"
namespace mozilla::dom {
namespace {
template <typename Source>
struct DeclarationTraits;
// Specialization for inline style (specified values)
struct InlineStyleDeclarations {};
template <>
struct DeclarationTraits<InlineStyleDeclarations> {
static StylePropertyTypedValueResult Get(Element* aElement,
const nsACString& aProperty,
ErrorResult& aRv) {
MOZ_ASSERT(aElement);
auto result = StylePropertyTypedValueResult::None();
RefPtr<DeclarationBlock> block = aElement->GetInlineStyleDeclaration();
if (!block) {
return result;
}
if (!block->GetPropertyTypedValue(aProperty, result)) {
return result;
}
return result;
}
};
// Specialization for computed style (computed values)
struct ComputedStyleDeclarations {};
template <>
struct DeclarationTraits<ComputedStyleDeclarations> {
static StylePropertyTypedValueResult Get(Element* aElement,
const nsACString& aProperty,
ErrorResult& aRv) {
MOZ_ASSERT(aElement);
auto result = StylePropertyTypedValueResult::None();
RefPtr<const ComputedStyle> style =
nsComputedDOMStyle::GetComputedStyle(aElement);
if (!style) {
return result;
}
if (!style->GetPropertyTypedValue(aProperty, result)) {
return result;
}
return result;
}
};
// XXX StyleRuleDeclarations go here
} // namespace
StylePropertyMapReadOnly::StylePropertyMapReadOnly(
nsCOMPtr<nsISupports> aParent, bool aComputed)
: mParent(std::move(aParent)), mDeclarations(aComputed) {
MOZ_ASSERT(mParent);
}
NS_IMPL_CYCLE_COLLECTING_ADDREF(StylePropertyMapReadOnly)
NS_IMPL_CYCLE_COLLECTING_RELEASE(StylePropertyMapReadOnly)
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(StylePropertyMapReadOnly)
NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
NS_INTERFACE_MAP_ENTRY(nsISupports)
NS_INTERFACE_MAP_END
NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(StylePropertyMapReadOnly, mParent)
nsISupports* StylePropertyMapReadOnly::GetParentObject() const {
return mParent;
}
JSObject* StylePropertyMapReadOnly::WrapObject(
JSContext* aCx, JS::Handle<JSObject*> aGivenProto) {
return StylePropertyMapReadOnly_Binding::Wrap(aCx, this, aGivenProto);
}
// start of StylePropertyMapReadOnly Web IDL implementation
// https://drafts.css-houdini.org/css-typed-om-1/#dom-stylepropertymapreadonly-get
//
// XXX This is not yet fully implemented and optimized!
void StylePropertyMapReadOnly::Get(const nsACString& aProperty,
OwningUndefinedOrCSSStyleValue& aRetVal,
ErrorResult& aRv) const {
// XXX This QO wouldn't be needed if we had RefPtr<Element> mElement
RefPtr<Element> element = do_QueryObject(mParent);
if (!element) {
aRv.Throw(NS_ERROR_NOT_IMPLEMENTED);
return;
}
// Step 2.
NonCustomCSSPropertyId id = nsCSSProps::LookupProperty(aProperty);
if (id == eCSSProperty_UNKNOWN) {
aRv.ThrowTypeError("Invalid property: "_ns + aProperty);
return;
}
// Step 3.
const Declarations& declarations = mDeclarations;
// Step 4.
auto result = declarations.Get(element, aProperty, aRv);
if (aRv.Failed()) {
return;
}
// XXX Move the creation of CSSStyleValue to a dedicated class for example
// CSSStyleValueFactory and eventually split the handling of tags into
// separate methods to make the code more readable and accessible for
// CSSStyleValue::Parse. See bug 2004057
RefPtr<CSSStyleValue> styleValue;
switch (result.tag) {
case StylePropertyTypedValueResult::Tag::Typed: {
const auto& typedValue = result.AsTyped();
switch (typedValue.tag) {
case StyleTypedValue::Tag::Keyword:
styleValue =
MakeRefPtr<CSSKeywordValue>(mParent, typedValue.AsKeyword());
break;
case StyleTypedValue::Tag::Numeric: {
auto numericValue = typedValue.AsNumeric();
switch (numericValue.tag) {
case StyleNumericValue::Tag::Unit: {
auto unitValue = numericValue.AsUnit();
styleValue = MakeRefPtr<CSSUnitValue>(mParent, unitValue.value,
unitValue.unit);
break;
}
case StyleNumericValue::Tag::Sum: {
auto mathSum = numericValue.AsSum();
nsTArray<RefPtr<CSSNumericValue>> values;
for (const auto& value : mathSum.values) {
// XXX Only supporting units for now
if (value.IsUnit()) {
auto unitValue = value.AsUnit();
values.AppendElement(MakeRefPtr<CSSUnitValue>(
mParent, unitValue.value, unitValue.unit));
}
}
auto array =
MakeRefPtr<CSSNumericArray>(mParent, std::move(values));
styleValue = MakeAndAddRef<CSSMathSum>(mParent, std::move(array));
break;
}
}
break;
}
}
break;
}
case StylePropertyTypedValueResult::Tag::Unsupported: {
auto propertyId = CSSPropertyId::FromIdOrCustomProperty(id, aProperty);
auto rawBlock = result.AsUnsupported();
auto block = MakeRefPtr<DeclarationBlock>(rawBlock.Consume());
styleValue = MakeRefPtr<CSSUnsupportedValue>(mParent, propertyId,
std::move(block));
break;
}
case StylePropertyTypedValueResult::Tag::None:
break;
}
if (styleValue) {
aRetVal.SetAsCSSStyleValue() = std::move(styleValue);
} else {
aRetVal.SetUndefined();
}
}
// https://drafts.css-houdini.org/css-typed-om-1/#dom-stylepropertymapreadonly-getall
//
// XXX This is not yet fully implemented and optimized!
void StylePropertyMapReadOnly::GetAll(const nsACString& aProperty,
nsTArray<RefPtr<CSSStyleValue>>& aRetVal,
ErrorResult& aRv) const {
OwningUndefinedOrCSSStyleValue retVal;
Get(aProperty, retVal, aRv);
if (aRv.Failed()) {
return;
}
if (retVal.IsCSSStyleValue()) {
auto styleValue = retVal.GetAsCSSStyleValue();
aRetVal.AppendElement(styleValue);
}
}
bool StylePropertyMapReadOnly::Has(const nsACString& aProperty,
ErrorResult& aRv) const {
aRv.Throw(NS_ERROR_NOT_IMPLEMENTED);
return false;
}
uint32_t StylePropertyMapReadOnly::Size() const { return 0; }
uint32_t StylePropertyMapReadOnly::GetIterableLength() const { return 0; }
const nsACString& StylePropertyMapReadOnly::GetKeyAtIndex(
uint32_t aIndex) const {
return EmptyCString();
}
nsTArray<RefPtr<CSSStyleValue>> StylePropertyMapReadOnly::GetValueAtIndex(
uint32_t aIndex) const {
return nsTArray<RefPtr<CSSStyleValue>>();
}
// end of StylePropertyMapReadOnly Web IDL implementation
size_t StylePropertyMapReadOnly::SizeOfExcludingThis(
MallocSizeOf aMallocSizeOf) const {
return 0;
}
size_t StylePropertyMapReadOnly::SizeOfIncludingThis(
MallocSizeOf aMallocSizeOf) const {
return SizeOfExcludingThis(aMallocSizeOf) + aMallocSizeOf(this);
}
StylePropertyTypedValueResult StylePropertyMapReadOnly::Declarations::Get(
Element* aElement, const nsACString& aProperty, ErrorResult& aRv) const {
if (mComputed) {
return DeclarationTraits<ComputedStyleDeclarations>::Get(aElement,
aProperty, aRv);
}
return DeclarationTraits<InlineStyleDeclarations>::Get(aElement, aProperty,
aRv);
}
} // namespace mozilla::dom
|