summaryrefslogtreecommitdiff
path: root/chromium/third_party/blink/renderer/core/css/cssom/css_math_product.cc
blob: 39f7bb929808b0c6d1f6af762517c423c2a60769 (plain)
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
// Copyright 2017 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 "third_party/blink/renderer/core/css/cssom/css_math_product.h"

#include "third_party/blink/renderer/core/css/css_calculation_value.h"
#include "third_party/blink/renderer/core/css/cssom/css_math_invert.h"
#include "third_party/blink/renderer/platform/wtf/text/string_builder.h"

namespace blink {

namespace {

CSSNumericSumValue::UnitMap MultiplyUnitMaps(
    CSSNumericSumValue::UnitMap a,
    const CSSNumericSumValue::UnitMap& b) {
  for (const auto& unit_exponent : b) {
    DCHECK_NE(unit_exponent.value, 0);
    const auto old_value =
        a.Contains(unit_exponent.key) ? a.at(unit_exponent.key) : 0;

    // Remove any zero entries
    if (old_value + unit_exponent.value == 0)
      a.erase(unit_exponent.key);
    else
      a.Set(unit_exponent.key, old_value + unit_exponent.value);
  }
  return a;
}

}  // namespace

CSSMathProduct* CSSMathProduct::Create(const HeapVector<CSSNumberish>& args,
                                       ExceptionState& exception_state) {
  if (args.IsEmpty()) {
    exception_state.ThrowDOMException(DOMExceptionCode::kSyntaxError,
                                      "Arguments can't be empty");
    return nullptr;
  }

  CSSMathProduct* result = Create(CSSNumberishesToNumericValues(args));
  if (!result) {
    exception_state.ThrowTypeError("Incompatible types");
    return nullptr;
  }

  return result;
}

CSSMathProduct* CSSMathProduct::Create(CSSNumericValueVector values) {
  bool error = false;
  CSSNumericValueType final_type =
      CSSMathVariadic::TypeCheck(values, CSSNumericValueType::Multiply, error);
  return error ? nullptr
               : MakeGarbageCollected<CSSMathProduct>(
                     MakeGarbageCollected<CSSNumericArray>(std::move(values)),
                     final_type);
}

base::Optional<CSSNumericSumValue> CSSMathProduct::SumValue() const {
  CSSNumericSumValue sum;
  // Start with the number '1', which is the multiplicative identity.
  sum.terms.push_back(CSSNumericSumValue::Term{1, {}});

  for (const auto& value : NumericValues()) {
    const auto child_sum = value->SumValue();
    if (!child_sum)
      return base::nullopt;

    CSSNumericSumValue new_sum;
    for (const auto& a : sum.terms) {
      for (const auto& b : child_sum->terms) {
        new_sum.terms.emplace_back(a.value * b.value,
                                   MultiplyUnitMaps(a.units, b.units));
      }
    }

    sum = new_sum;
  }
  return sum;
}

CSSCalcExpressionNode* CSSMathProduct::ToCalcExpressionNode() const {
  // TODO(crbug.com/782103): Handle the single value case correctly.
  if (NumericValues().size() == 1)
    return NumericValues()[0]->ToCalcExpressionNode();

  CSSCalcExpressionNode* node = CSSCalcValue::CreateExpressionNode(
      NumericValues()[0]->ToCalcExpressionNode(),
      NumericValues()[1]->ToCalcExpressionNode(), kCalcMultiply);

  for (wtf_size_t i = 2; i < NumericValues().size(); i++) {
    node = CSSCalcValue::CreateExpressionNode(
        node, NumericValues()[i]->ToCalcExpressionNode(), kCalcMultiply);
  }

  return node;
}

void CSSMathProduct::BuildCSSText(Nested nested,
                                  ParenLess paren_less,
                                  StringBuilder& result) const {
  if (paren_less == ParenLess::kNo)
    result.Append(nested == Nested::kYes ? "(" : "calc(");

  const auto& values = NumericValues();
  DCHECK(!values.IsEmpty());
  values[0]->BuildCSSText(Nested::kYes, ParenLess::kNo, result);

  for (wtf_size_t i = 1; i < values.size(); i++) {
    const auto& arg = *values[i];
    if (arg.GetType() == CSSStyleValue::kInvertType) {
      result.Append(" / ");
      static_cast<const CSSMathInvert&>(arg).Value().BuildCSSText(
          Nested::kYes, ParenLess::kNo, result);
    } else {
      result.Append(" * ");
      arg.BuildCSSText(Nested::kYes, ParenLess::kNo, result);
    }
  }

  if (paren_less == ParenLess::kNo)
    result.Append(")");
}

}  // namespace blink