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
|
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "core/css/cssom/InlineStylePropertyMap.h"
#include "bindings/core/v8/Iterable.h"
#include "core/CSSPropertyNames.h"
#include "core/css/CSSCustomIdentValue.h"
#include "core/css/CSSCustomPropertyDeclaration.h"
#include "core/css/CSSPrimitiveValue.h"
#include "core/css/CSSPropertyMetadata.h"
#include "core/css/CSSValueList.h"
#include "core/css/StylePropertySet.h"
#include "core/css/cssom/CSSOMTypes.h"
#include "core/css/cssom/CSSSimpleLength.h"
#include "core/css/cssom/CSSUnsupportedStyleValue.h"
#include "core/css/cssom/StyleValueFactory.h"
namespace blink {
namespace {
const CSSValue* styleValueToCSSValue(CSSPropertyID propertyID,
const CSSStyleValue& styleValue) {
if (!CSSOMTypes::propertyCanTake(propertyID, styleValue))
return nullptr;
return styleValue.toCSSValueWithProperty(propertyID);
}
const CSSValue* singleStyleValueAsCSSValue(CSSPropertyID propertyID,
const CSSStyleValue& styleValue) {
if (!CSSPropertyMetadata::propertyIsRepeated(propertyID))
return styleValueToCSSValue(propertyID, styleValue);
const CSSValue* cssValue = styleValueToCSSValue(propertyID, styleValue);
if (!cssValue)
return nullptr;
// TODO(meade): Determine the correct separator for each property.
CSSValueList* valueList = CSSValueList::createSpaceSeparated();
valueList->append(*cssValue);
return valueList;
}
CSSValueList* asCSSValueList(CSSPropertyID propertyID,
const CSSStyleValueVector& styleValueVector) {
// TODO(meade): Determine the correct separator for each property.
CSSValueList* valueList = CSSValueList::createSpaceSeparated();
for (const CSSStyleValue* value : styleValueVector) {
const CSSValue* cssValue = styleValueToCSSValue(propertyID, *value);
if (!cssValue) {
return nullptr;
}
valueList->append(*cssValue);
}
return valueList;
}
} // namespace
CSSStyleValueVector InlineStylePropertyMap::getAllInternal(
CSSPropertyID propertyID) {
const CSSValue* cssValue =
m_ownerElement->ensureMutableInlineStyle().getPropertyCSSValue(
propertyID);
if (!cssValue)
return CSSStyleValueVector();
return StyleValueFactory::cssValueToStyleValueVector(propertyID, *cssValue);
}
CSSStyleValueVector InlineStylePropertyMap::getAllInternal(
AtomicString customPropertyName) {
const CSSValue* cssValue =
m_ownerElement->ensureMutableInlineStyle().getPropertyCSSValue(
customPropertyName);
if (!cssValue)
return CSSStyleValueVector();
return StyleValueFactory::cssValueToStyleValueVector(CSSPropertyInvalid,
*cssValue);
}
Vector<String> InlineStylePropertyMap::getProperties() {
DEFINE_STATIC_LOCAL(const String, kAtApply, ("@apply"));
Vector<String> result;
bool containsAtApply = false;
StylePropertySet& inlineStyleSet = m_ownerElement->ensureMutableInlineStyle();
for (unsigned i = 0; i < inlineStyleSet.propertyCount(); i++) {
CSSPropertyID propertyID = inlineStyleSet.propertyAt(i).id();
if (propertyID == CSSPropertyVariable) {
StylePropertySet::PropertyReference propertyReference =
inlineStyleSet.propertyAt(i);
const CSSCustomPropertyDeclaration& customDeclaration =
toCSSCustomPropertyDeclaration(propertyReference.value());
result.push_back(customDeclaration.name());
} else if (propertyID == CSSPropertyApplyAtRule) {
if (!containsAtApply) {
result.push_back(kAtApply);
containsAtApply = true;
}
} else {
result.push_back(getPropertyNameString(propertyID));
}
}
return result;
}
void InlineStylePropertyMap::set(
CSSPropertyID propertyID,
CSSStyleValueOrCSSStyleValueSequenceOrString& item,
ExceptionState& exceptionState) {
const CSSValue* cssValue = nullptr;
if (item.isCSSStyleValue()) {
cssValue =
singleStyleValueAsCSSValue(propertyID, *item.getAsCSSStyleValue());
} else if (item.isCSSStyleValueSequence()) {
if (!CSSPropertyMetadata::propertyIsRepeated(propertyID)) {
exceptionState.throwTypeError(
"Property does not support multiple values");
return;
}
cssValue = asCSSValueList(propertyID, item.getAsCSSStyleValueSequence());
} else {
// Parse it.
DCHECK(item.isString());
// TODO(meade): Implement this.
exceptionState.throwTypeError("Not implemented yet");
return;
}
if (!cssValue) {
exceptionState.throwTypeError("Invalid type for property");
return;
}
m_ownerElement->setInlineStyleProperty(propertyID, cssValue);
}
void InlineStylePropertyMap::append(
CSSPropertyID propertyID,
CSSStyleValueOrCSSStyleValueSequenceOrString& item,
ExceptionState& exceptionState) {
if (!CSSPropertyMetadata::propertyIsRepeated(propertyID)) {
exceptionState.throwTypeError("Property does not support multiple values");
return;
}
const CSSValue* cssValue =
m_ownerElement->ensureMutableInlineStyle().getPropertyCSSValue(
propertyID);
CSSValueList* cssValueList = nullptr;
if (!cssValue) {
// TODO(meade): Determine the correct separator for each property.
cssValueList = CSSValueList::createSpaceSeparated();
} else if (cssValue->isValueList()) {
cssValueList = toCSSValueList(cssValue)->copy();
} else {
// TODO(meade): Figure out what the correct behaviour here is.
exceptionState.throwTypeError("Property is not already list valued");
return;
}
if (item.isCSSStyleValue()) {
const CSSValue* cssValue =
styleValueToCSSValue(propertyID, *item.getAsCSSStyleValue());
if (!cssValue) {
exceptionState.throwTypeError("Invalid type for property");
return;
}
cssValueList->append(*cssValue);
} else if (item.isCSSStyleValueSequence()) {
for (CSSStyleValue* styleValue : item.getAsCSSStyleValueSequence()) {
const CSSValue* cssValue = styleValueToCSSValue(propertyID, *styleValue);
if (!cssValue) {
exceptionState.throwTypeError("Invalid type for property");
return;
}
cssValueList->append(*cssValue);
}
} else {
// Parse it.
DCHECK(item.isString());
// TODO(meade): Implement this.
exceptionState.throwTypeError("Not implemented yet");
return;
}
m_ownerElement->setInlineStyleProperty(propertyID, cssValueList);
}
void InlineStylePropertyMap::remove(CSSPropertyID propertyID,
ExceptionState& exceptionState) {
m_ownerElement->removeInlineStyleProperty(propertyID);
}
HeapVector<StylePropertyMap::StylePropertyMapEntry>
InlineStylePropertyMap::getIterationEntries() {
DEFINE_STATIC_LOCAL(const String, kAtApply, ("@apply"));
HeapVector<StylePropertyMap::StylePropertyMapEntry> result;
StylePropertySet& inlineStyleSet = m_ownerElement->ensureMutableInlineStyle();
for (unsigned i = 0; i < inlineStyleSet.propertyCount(); i++) {
StylePropertySet::PropertyReference propertyReference =
inlineStyleSet.propertyAt(i);
CSSPropertyID propertyID = propertyReference.id();
String name;
CSSStyleValueOrCSSStyleValueSequence value;
if (propertyID == CSSPropertyVariable) {
const CSSCustomPropertyDeclaration& customDeclaration =
toCSSCustomPropertyDeclaration(propertyReference.value());
name = customDeclaration.name();
// TODO(meade): Eventually custom properties will support other types, so
// actually return them instead of always returning a
// CSSUnsupportedStyleValue.
value.setCSSStyleValue(
CSSUnsupportedStyleValue::create(customDeclaration.customCSSText()));
} else if (propertyID == CSSPropertyApplyAtRule) {
name = kAtApply;
value.setCSSStyleValue(CSSUnsupportedStyleValue::create(
toCSSCustomIdentValue(propertyReference.value()).value()));
} else {
name = getPropertyNameString(propertyID);
CSSStyleValueVector styleValueVector =
StyleValueFactory::cssValueToStyleValueVector(
propertyID, propertyReference.value());
if (styleValueVector.size() == 1)
value.setCSSStyleValue(styleValueVector[0]);
else
value.setCSSStyleValueSequence(styleValueVector);
}
result.push_back(std::make_pair(name, value));
}
return result;
}
} // namespace blink
|