summaryrefslogtreecommitdiff
path: root/chromium/third_party/blink/renderer/core/css/cssom/css_math_invert.cc
blob: 4c041c637229745e1b90cb28aa61d6b883305a78 (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
// 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_invert.h"

#include "third_party/blink/renderer/bindings/core/v8/v8_union_cssnumericvalue_double.h"
#include "third_party/blink/renderer/core/css/css_math_expression_node.h"
#include "third_party/blink/renderer/core/css/cssom/css_numeric_sum_value.h"
#include "third_party/blink/renderer/platform/wtf/text/string_builder.h"

namespace blink {

V8CSSNumberish* CSSMathInvert::value() {
  return MakeGarbageCollected<V8CSSNumberish>(value_);
}

absl::optional<CSSNumericSumValue> CSSMathInvert::SumValue() const {
  auto sum = value_->SumValue();
  if (!sum || sum->terms.size() != 1)
    return absl::nullopt;

  for (auto& unit_exponent : sum->terms[0].units)
    unit_exponent.value *= -1;

  sum->terms[0].value = 1.0 / sum->terms[0].value;
  return sum;
}

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

  result.Append("1 / ");
  value_->BuildCSSText(Nested::kYes, ParenLess::kNo, result);

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

CSSMathExpressionNode* CSSMathInvert::ToCalcExpressionNode() const {
  CSSMathExpressionNode* right_side = value_->ToCalcExpressionNode();
  if (!right_side)
    return nullptr;
  return CSSMathExpressionBinaryOperation::Create(
      CSSMathExpressionNumericLiteral::Create(
          1, CSSPrimitiveValue::UnitType::kNumber, false),
      right_side, CSSMathOperator::kDivide);
}

}  // namespace blink