summaryrefslogtreecommitdiff
path: root/chromium/third_party/blink/renderer/core/css/property_registration.cc
blob: c27045c2d2065c390199751a2fc7fa8a59e870e9 (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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
// 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/bindings/core/v8/v8_property_definition.h"
#include "third_party/blink/renderer/core/animation/css_interpolation_types_map.h"
#include "third_party/blink/renderer/core/css/css_custom_property_declaration.h"
#include "third_party/blink/renderer/core/css/css_identifier_value.h"
#include "third_party/blink/renderer/core/css/css_string_value.h"
#include "third_party/blink/renderer/core/css/css_style_sheet.h"
#include "third_party/blink/renderer/core/css/css_syntax_definition.h"
#include "third_party/blink/renderer/core/css/css_syntax_string_parser.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_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_rule.h"
#include "third_party/blink/renderer/core/css/style_sheet_contents.h"
#include "third_party/blink/renderer/core/dom/document.h"
#include "third_party/blink/renderer/core/frame/local_dom_window.h"

namespace blink {

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

PropertyRegistration::PropertyRegistration(
    const AtomicString& name,
    const CSSSyntaxDefinition& 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) {
}

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

  if (auto* variable_reference_value =
          DynamicTo<CSSVariableReferenceValue>(value)) {
    return !variable_reference_value->VariableDataValue()
                ->NeedsVariableResolution();
  }

  if (auto* value_list = DynamicTo<CSSValueList>(value)) {
    for (const CSSValue* inner_value : *value_list) {
      if (!ComputationallyIndependent(*inner_value))
        return false;
    }
    return true;
  }

  if (const auto* primitive_value = DynamicTo<CSSPrimitiveValue>(value))
    return primitive_value->IsComputationallyIndependent();

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

  return true;
}

static base::Optional<CSSSyntaxDefinition> ConvertSyntax(
    const CSSValue& value) {
  return CSSSyntaxStringParser(To<CSSStringValue>(value).Value()).Parse();
}

static bool ConvertInherts(const CSSValue& value) {
  CSSValueID inherits_id = To<CSSIdentifierValue>(value).GetValueID();
  DCHECK(inherits_id == CSSValueID::kTrue || inherits_id == CSSValueID::kFalse);
  return inherits_id == CSSValueID::kTrue;
}

static scoped_refptr<CSSVariableData> ConvertInitialVariableData(
    const CSSValue* value) {
  if (!value)
    return nullptr;
  return To<CSSCustomPropertyDeclaration>(*value).Value();
}

void PropertyRegistration::DeclareProperty(Document& document,
                                           const AtomicString& name,
                                           StyleRuleProperty& rule) {
  // https://drafts.css-houdini.org/css-properties-values-api-1/#the-syntax-descriptor
  const CSSValue* syntax_value = rule.GetSyntax();
  if (!syntax_value)
    return;
  base::Optional<CSSSyntaxDefinition> syntax = ConvertSyntax(*syntax_value);
  if (!syntax)
    return;

  // https://drafts.css-houdini.org/css-properties-values-api-1/#inherits-descriptor
  const CSSValue* inherits_value = rule.Inherits();
  if (!inherits_value)
    return;
  bool inherits = ConvertInherts(*inherits_value);

  // https://drafts.css-houdini.org/css-properties-values-api-1/#initial-value-descriptor
  const CSSValue* initial_value = rule.GetInitialValue();
  scoped_refptr<CSSVariableData> initial_variable_data =
      ConvertInitialVariableData(initial_value);

  // Parse initial value, if we have it.
  const CSSValue* initial = nullptr;
  if (initial_variable_data) {
    const CSSParserContext* parser_context =
        document.ElementSheet().Contents()->ParserContext();
    const bool is_animation_tainted = false;
    initial = syntax->Parse(initial_variable_data->TokenRange(),
                            *parser_context, is_animation_tainted);
    if (!initial)
      return;
    if (!ComputationallyIndependent(*initial))
      return;
    initial = &StyleBuilderConverter::ConvertRegisteredPropertyInitialValue(
        document, *initial);
    initial_variable_data =
        StyleBuilderConverter::ConvertRegisteredPropertyVariableData(
            *initial, is_animation_tainted);
  }

  // For non-universal @property rules, the initial value is required for the
  // the rule to be valid.
  if (!initial && !syntax->IsUniversal())
    return;

  document.EnsurePropertyRegistry().DeclareProperty(
      name, *MakeGarbageCollected<PropertyRegistration>(
                name, *syntax, inherits, initial, initial_variable_data));

  document.GetStyleEngine().PropertyRegistryChanged();
}

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

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

  base::Optional<CSSSyntaxDefinition> syntax_definition =
      CSSSyntaxStringParser(property_definition->syntax()).Parse();
  if (!syntax_definition) {
    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 (property_definition->hasInitialValue()) {
    CSSTokenizer tokenizer(property_definition->initialValue());
    const auto tokens = tokenizer.TokenizeToEOF();
    bool is_animation_tainted = false;
    initial = syntax_definition->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 =
        StyleBuilderConverter::ConvertRegisteredPropertyVariableData(
            *initial, is_animation_tainted);
  } else {
    if (!syntax_definition->IsUniversal()) {
      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_definition, property_definition->inherits(),
          initial, std::move(initial_variable_data)));

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

void PropertyRegistration::RemoveDeclaredProperties(Document& document) {
  document.EnsurePropertyRegistry().RemoveDeclaredProperties();
  document.GetStyleEngine().PropertyRegistryChanged();
}

}  // namespace blink