Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(354)

Unified Diff: third_party/WebKit/Source/core/css/cssom/CSSCalcLength.cpp

Issue 2371673002: [CSS Typed OM] Implement support for CSSValue -> CSSCalcLength (Closed)
Patch Set: sync Created 3 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: third_party/WebKit/Source/core/css/cssom/CSSCalcLength.cpp
diff --git a/third_party/WebKit/Source/core/css/cssom/CSSCalcLength.cpp b/third_party/WebKit/Source/core/css/cssom/CSSCalcLength.cpp
index c574e2166a332e2469785132ffbecdddee114a7d..9ba993e9071f4997d49ccf828b9348c5972f1d15 100644
--- a/third_party/WebKit/Source/core/css/cssom/CSSCalcLength.cpp
+++ b/third_party/WebKit/Source/core/css/cssom/CSSCalcLength.cpp
@@ -16,6 +16,7 @@ namespace blink {
namespace {
static CSSPrimitiveValue::UnitType unitFromIndex(int index) {
+ DCHECK(index < CSSLengthValue::kNumSupportedUnits);
int lowestValue = static_cast<int>(CSSPrimitiveValue::UnitType::Percentage);
return static_cast<CSSPrimitiveValue::UnitType>(index + lowestValue);
}
@@ -141,7 +142,66 @@ CSSValue* CSSCalcLength::toCSSValue() const {
std::unique_ptr<CSSCalcLength::UnitData>
CSSCalcLength::UnitData::fromExpressionNode(
const CSSCalcExpressionNode* expressionNode) {
- return nullptr;
+ CalculationCategory category = expressionNode->category();
+ DCHECK(category == CalculationCategory::CalcLength ||
+ category == CalculationCategory::CalcPercentLength ||
+ category == CalculationCategory::CalcPercent);
+
+ if (expressionNode->getType() ==
+ CSSCalcExpressionNode::Type::CssCalcPrimitiveValue) {
+ CSSPrimitiveValue::UnitType unit = expressionNode->typeWithCalcResolved();
+ DCHECK(CSSLengthValue::isSupportedLengthUnit(unit));
+ std::unique_ptr<UnitData> result(new UnitData());
+ result->set(unit, expressionNode->doubleValue());
+ return result;
+ }
+
+ const CSSCalcExpressionNode* left = expressionNode->leftExpressionNode();
+ const CSSCalcExpressionNode* right = expressionNode->rightExpressionNode();
+ CalcOperator op = expressionNode->operatorType();
+
+ if (op == CalcMultiply) {
+ std::unique_ptr<UnitData> unitData = nullptr;
+ double argument = 0;
+ // One side should be a number.
+ if (left->category() == CalculationCategory::CalcNumber) {
+ unitData = UnitData::fromExpressionNode(right);
+ argument = left->doubleValue();
+ } else if (right->category() == CalculationCategory::CalcNumber) {
+ unitData = UnitData::fromExpressionNode(left);
+ argument = right->doubleValue();
+ } else {
+ NOTREACHED();
+ return nullptr;
+ }
+ DCHECK(unitData);
+ unitData->multiply(argument);
+ return unitData;
+ }
+
+ if (op == CalcDivide) {
+ // Divisor must always be on the RHS.
+ DCHECK_EQ(right->category(), CalculationCategory::CalcNumber);
+ std::unique_ptr<UnitData> unitData = UnitData::fromExpressionNode(left);
+ DCHECK(unitData);
+ unitData->divide(right->doubleValue());
+ return unitData;
+ }
+
+ // Add and subtract.
+ std::unique_ptr<UnitData> leftUnitData = UnitData::fromExpressionNode(left);
+ std::unique_ptr<UnitData> rightUnitData = UnitData::fromExpressionNode(right);
+ DCHECK(leftUnitData);
+ DCHECK(rightUnitData);
+
+ if (op == CalcAdd)
+ leftUnitData->add(*rightUnitData);
+ else if (op == CalcSubtract)
+ leftUnitData->subtract(*rightUnitData);
+ else
+ NOTREACHED();
+
+ return leftUnitData;
}
CSSCalcExpressionNode* CSSCalcLength::UnitData::toCSSCalcExpressionNode()
« no previous file with comments | « third_party/WebKit/Source/core/css/CSSCalculationValue.cpp ('k') | third_party/WebKit/Source/core/css/cssom/CSSLengthValue.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698