File: CSSUnitValue.cpp

package info (click to toggle)
firefox 149.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 4,767,760 kB
  • sloc: cpp: 7,416,064; javascript: 6,752,859; ansic: 3,774,850; python: 1,250,473; xml: 641,578; asm: 439,191; java: 186,617; sh: 56,634; makefile: 18,856; objc: 13,092; perl: 12,763; pascal: 5,960; yacc: 4,583; cs: 3,846; lex: 1,720; ruby: 1,002; php: 436; lisp: 258; awk: 105; sql: 66; sed: 53; csh: 10; exp: 6
file content (169 lines) | stat: -rw-r--r-- 6,078 bytes parent folder | download
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
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim:set ts=2 sw=2 sts=2 et cindent: */
/* 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/CSSUnitValue.h"

#include "mozilla/AlreadyAddRefed.h"
#include "mozilla/CSSPropertyId.h"
#include "mozilla/ErrorResult.h"
#include "mozilla/RefPtr.h"
#include "mozilla/ServoStyleConsts.h"
#include "mozilla/dom/BindingDeclarations.h"
#include "mozilla/dom/CSSUnitValueBinding.h"

namespace mozilla::dom {

CSSUnitValue::CSSUnitValue(nsCOMPtr<nsISupports> aParent, double aValue,
                           const nsACString& aUnit)
    : CSSNumericValue(std::move(aParent), NumericValueType::UnitValue),
      mValue(aValue),
      mUnit(aUnit) {}

// static
RefPtr<CSSUnitValue> CSSUnitValue::Create(nsCOMPtr<nsISupports> aParent,
                                          const StyleUnitValue& aUnitValue) {
  return MakeRefPtr<CSSUnitValue>(aParent, aUnitValue.value, aUnitValue.unit);
}

JSObject* CSSUnitValue::WrapObject(JSContext* aCx,
                                   JS::Handle<JSObject*> aGivenProto) {
  return CSSUnitValue_Binding::Wrap(aCx, this, aGivenProto);
}

// start of CSSUnitValue Web IDL implementation

// https://drafts.css-houdini.org/css-typed-om-1/#dom-cssunitvalue-cssunitvalue
//
// static
already_AddRefed<CSSUnitValue> CSSUnitValue::Constructor(
    const GlobalObject& aGlobal, double aValue, const nsACString& aUnit,
    ErrorResult& aRv) {
  // XXX Units should be normalized to lowercase. The Typed OM spec doesn’t
  // state this explicitly, but WPT requires lowercase normalization and it
  // can also be deduced from the CSS Values spec. Besides fixing it here,
  // a spec issue may be needed to clarify this.

  // Step 1.

  // XXX A type should be created from unit and if that fails, the failure
  // should be propagated here

  // Step 2.

  return MakeAndAddRef<CSSUnitValue>(aGlobal.GetAsSupports(), aValue, aUnit);
}

double CSSUnitValue::Value() const { return mValue; }

void CSSUnitValue::SetValue(double aArg) { mValue = aArg; }

void CSSUnitValue::GetUnit(nsCString& aRetVal) const { aRetVal = mUnit; }

// end of CSSUnitValue Web IDL implementation

void CSSUnitValue::ToCssTextWithProperty(const CSSPropertyId& aPropertyId,
                                         nsACString& aDest) const {
  // XXX See:
  // https://drafts.css-houdini.org/css-typed-om-1/#create-an-internal-representation
  //
  // The current |isValueOutOfRange| check implements only a minimal subset of
  // the algorithm step that wraps out-of-range numeric values (e.g. negative
  // perspective) in a calc() expression to keep them representable.
  //
  // This is a temporary solution until a more robust mechanism is added for
  // handling range checks and value wrapping. The long-term plan is to move
  // this logic to a dedicated FromTyped trait or similar infrastructure that
  // can validate and construct internal representations in a property-aware
  // and fully spec-compliant manner. See bug 2005142
  const bool isValueOutOfRange = [](NonCustomCSSPropertyId aId, double aValue) {
    switch (aId) {
      case eCSSProperty_font_stretch:
      case eCSSProperty_column_width:
      case eCSSProperty_flex_basis:
      case eCSSProperty_font_size:
      case eCSSProperty_perspective:
      case eCSSProperty_max_block_size:
      case eCSSProperty_max_height:
      case eCSSProperty_max_inline_size:
      case eCSSProperty_max_width:
      case eCSSProperty_block_size:
      case eCSSProperty_height:
      case eCSSProperty_inline_size:
      case eCSSProperty_min_block_size:
      case eCSSProperty_min_height:
      case eCSSProperty_min_inline_size:
      case eCSSProperty_min_width:
      case eCSSProperty_width:
      case eCSSProperty_border_block_end_width:
      case eCSSProperty_border_block_start_width:
      case eCSSProperty_border_bottom_width:
      case eCSSProperty_border_inline_end_width:
      case eCSSProperty_border_inline_start_width:
      case eCSSProperty_border_left_width:
      case eCSSProperty_border_right_width:
      case eCSSProperty_border_top_width:
      case eCSSProperty_outline_width:
      case eCSSProperty_padding_block_end:
      case eCSSProperty_padding_block_start:
      case eCSSProperty_padding_bottom:
      case eCSSProperty_padding_inline_end:
      case eCSSProperty_padding_inline_start:
      case eCSSProperty_padding_left:
      case eCSSProperty_padding_right:
      case eCSSProperty_padding_top:
      case eCSSProperty_r:
      case eCSSProperty_shape_margin:
      case eCSSProperty_rx:
      case eCSSProperty_ry:
      case eCSSProperty_scroll_padding_block_end:
      case eCSSProperty_scroll_padding_block_start:
      case eCSSProperty_scroll_padding_bottom:
      case eCSSProperty_scroll_padding_inline_end:
      case eCSSProperty_scroll_padding_inline_start:
      case eCSSProperty_scroll_padding_left:
      case eCSSProperty_scroll_padding_right:
      case eCSSProperty_scroll_padding_top:
        return aValue < 0;

      default:
        return false;
    }
  }(aPropertyId.mId, mValue);

  if (isValueOutOfRange) {
    aDest.Append("calc("_ns);
  }

  aDest.AppendFloat(mValue);

  if (mUnit.Equals("percent"_ns)) {
    aDest.Append("%"_ns);
  } else if (!mUnit.Equals("number"_ns)) {
    aDest.Append(mUnit);
  }

  if (isValueOutOfRange) {
    aDest.Append(")"_ns);
  }
}

StyleUnitValue CSSUnitValue::ToStyleUnitValue() const {
  return StyleUnitValue(mValue, StyleCssString(mUnit));
}

const CSSUnitValue& CSSNumericValue::GetAsCSSUnitValue() const {
  MOZ_DIAGNOSTIC_ASSERT(mNumericValueType == NumericValueType::UnitValue);

  return *static_cast<const CSSUnitValue*>(this);
}

CSSUnitValue& CSSNumericValue::GetAsCSSUnitValue() {
  MOZ_DIAGNOSTIC_ASSERT(mNumericValueType == NumericValueType::UnitValue);

  return *static_cast<CSSUnitValue*>(this);
}

}  // namespace mozilla::dom