summaryrefslogtreecommitdiff
path: root/chromium/third_party/blink/renderer/core/css/property_registration.cc
blob: 883f87f96ec9b9780f82f82699edfc1a59b4f888 (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
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
// 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 "third_party/blink/renderer/core/css/property_registration.h"

#include "third_party/blink/renderer/core/animation/css_interpolation_types_map.h"
#include "third_party/blink/renderer/core/css/css_style_sheet.h"
#include "third_party/blink/renderer/core/css/css_syntax_descriptor.h"
#include "third_party/blink/renderer/core/css/css_value_list.h"
#include "third_party/blink/renderer/core/css/css_variable_reference_value.h"
#include "third_party/blink/renderer/core/css/parser/css_parser_context.h"
#include "third_party/blink/renderer/core/css/parser/css_tokenizer.h"
#include "third_party/blink/renderer/core/css/parser/css_variable_parser.h"
#include "third_party/blink/renderer/core/css/property_descriptor.h"
#include "third_party/blink/renderer/core/css/property_registry.h"
#include "third_party/blink/renderer/core/css/resolver/style_builder_converter.h"
#include "third_party/blink/renderer/core/css/style_change_reason.h"
#include "third_party/blink/renderer/core/css/style_engine.h"
#include "third_party/blink/renderer/core/css/style_sheet_contents.h"
#include "third_party/blink/renderer/core/dom/document.h"

namespace blink {

const PropertyRegistration* PropertyRegistration::From(
    const ExecutionContext* execution_context,
    const AtomicString& property_name) {
  const auto* document = DynamicTo<Document>(execution_context);
  if (!document)
    return nullptr;
  const PropertyRegistry* registry = document->GetPropertyRegistry();
  return registry ? registry->Registration(property_name) : nullptr;
}

PropertyRegistration::PropertyRegistration(
    const AtomicString& name,
    const CSSSyntaxDescriptor& syntax,
    bool inherits,
    const CSSValue* initial,
    scoped_refptr<CSSVariableData> initial_variable_data)
    : syntax_(syntax),
      inherits_(inherits),
      initial_(initial),
      initial_variable_data_(std::move(initial_variable_data)),
      interpolation_types_(
          CSSInterpolationTypesMap::CreateInterpolationTypesForCSSSyntax(
              name,
              syntax,
              *this)),
      referenced_(false) {
  DCHECK(RuntimeEnabledFeatures::CSSVariables2Enabled());
}

static bool ComputationallyIndependent(const CSSValue& value) {
  DCHECK(!value.IsCSSWideKeyword());

  if (value.IsVariableReferenceValue())
    return !ToCSSVariableReferenceValue(value)
                .VariableDataValue()
                ->NeedsVariableResolution();

  if (value.IsValueList()) {
    for (const CSSValue* inner_value : ToCSSValueList(value)) {
      if (!ComputationallyIndependent(*inner_value))
        return false;
    }
    return true;
  }

  if (value.IsPrimitiveValue()) {
    const CSSPrimitiveValue& primitive_value = ToCSSPrimitiveValue(value);
    if (!primitive_value.IsLength() &&
        !primitive_value.IsCalculatedPercentageWithLength())
      return true;

    CSSPrimitiveValue::CSSLengthArray length_array;
    primitive_value.AccumulateLengthArray(length_array);
    for (size_t i = 0; i < length_array.values.size(); i++) {
      if (length_array.type_flags.Get(i) &&
          i != CSSPrimitiveValue::kUnitTypePixels &&
          i != CSSPrimitiveValue::kUnitTypePercentage)
        return false;
    }
    return true;
  }

  // TODO(timloh): Images values can also contain lengths.

  return true;
}

void PropertyRegistration::registerProperty(
    ExecutionContext* execution_context,
    const PropertyDescriptor* descriptor,
    ExceptionState& exception_state) {
  // Bindings code ensures these are set.
  DCHECK(descriptor->hasName());
  DCHECK(descriptor->hasInherits());
  DCHECK(descriptor->hasSyntax());

  String name = descriptor->name();
  if (!CSSVariableParser::IsValidVariableName(name)) {
    exception_state.ThrowDOMException(
        DOMExceptionCode::kSyntaxError,
        "Custom property names must start with '--'.");
    return;
  }
  AtomicString atomic_name(name);
  Document* document = To<Document>(execution_context);
  PropertyRegistry& registry = *document->GetPropertyRegistry();
  if (registry.Registration(atomic_name)) {
    exception_state.ThrowDOMException(
        DOMExceptionCode::kInvalidModificationError,
        "The name provided has already been registered.");
    return;
  }

  CSSSyntaxDescriptor syntax_descriptor(descriptor->syntax());
  if (!syntax_descriptor.IsValid()) {
    exception_state.ThrowDOMException(
        DOMExceptionCode::kSyntaxError,
        "The syntax provided is not a valid custom property syntax.");
    return;
  }

  const CSSParserContext* parser_context =
      document->ElementSheet().Contents()->ParserContext();

  const CSSValue* initial = nullptr;
  scoped_refptr<CSSVariableData> initial_variable_data;
  if (descriptor->hasInitialValue()) {
    CSSTokenizer tokenizer(descriptor->initialValue());
    const auto tokens = tokenizer.TokenizeToEOF();
    bool is_animation_tainted = false;
    initial = syntax_descriptor.Parse(CSSParserTokenRange(tokens),
                                      parser_context, is_animation_tainted);
    if (!initial) {
      exception_state.ThrowDOMException(
          DOMExceptionCode::kSyntaxError,
          "The initial value provided does not parse for the given syntax.");
      return;
    }
    if (!ComputationallyIndependent(*initial)) {
      exception_state.ThrowDOMException(
          DOMExceptionCode::kSyntaxError,
          "The initial value provided is not computationally independent.");
      return;
    }
    initial = &StyleBuilderConverter::ConvertRegisteredPropertyInitialValue(
        *document, *initial);
    initial_variable_data = CSSVariableData::Create(
        CSSParserTokenRange(tokens), is_animation_tainted, false,
        parser_context->BaseURL(), parser_context->Charset());
  } else {
    if (!syntax_descriptor.IsTokenStream()) {
      exception_state.ThrowDOMException(
          DOMExceptionCode::kSyntaxError,
          "An initial value must be provided if the syntax is not '*'");
      return;
    }
  }
  registry.RegisterProperty(
      atomic_name, *MakeGarbageCollected<PropertyRegistration>(
                       atomic_name, syntax_descriptor, descriptor->inherits(),
                       initial, std::move(initial_variable_data)));

  document->GetStyleEngine().CustomPropertyRegistered();
}

}  // namespace blink