File: PropertyRegistration.cpp

package info (click to toggle)
chromium-browser 57.0.2987.98-1~deb8u1
  • links: PTS, VCS
  • area: main
  • in suites: jessie
  • size: 2,637,852 kB
  • ctags: 2,544,394
  • sloc: cpp: 12,815,961; ansic: 3,676,222; python: 1,147,112; asm: 526,608; java: 523,212; xml: 286,794; perl: 92,654; sh: 86,408; objc: 73,271; makefile: 27,698; cs: 18,487; yacc: 13,031; tcl: 12,957; pascal: 4,875; ml: 4,716; lex: 3,904; sql: 3,862; ruby: 1,982; lisp: 1,508; php: 1,368; exp: 404; awk: 325; csh: 117; jsp: 39; sed: 37
file content (155 lines) | stat: -rw-r--r-- 5,525 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
// 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 "core/css/PropertyRegistration.h"

#include "core/animation/CSSValueInterpolationType.h"
#include "core/css/CSSStyleSheet.h"
#include "core/css/CSSSyntaxDescriptor.h"
#include "core/css/CSSValueList.h"
#include "core/css/CSSVariableReferenceValue.h"
#include "core/css/PropertyDescriptor.h"
#include "core/css/PropertyRegistry.h"
#include "core/css/StyleSheetContents.h"
#include "core/css/parser/CSSParserContext.h"
#include "core/css/parser/CSSTokenizer.h"
#include "core/css/parser/CSSVariableParser.h"
#include "core/css/resolver/StyleBuilderConverter.h"
#include "core/dom/Document.h"
#include "core/dom/ExceptionCode.h"
#include "core/dom/StyleChangeReason.h"

namespace blink {

static bool computationallyIndependent(const CSSValue& value) {
  DCHECK(!value.isCSSWideKeyword());

  if (value.isVariableReferenceValue())
    return !toCSSVariableReferenceValue(value)
                .variableDataValue()
                ->needsVariableResolution();

  if (value.isValueList()) {
    for (const CSSValue* innerValue : toCSSValueList(value)) {
      if (!computationallyIndependent(*innerValue))
        return false;
    }
    return true;
  }

  if (value.isPrimitiveValue()) {
    const CSSPrimitiveValue& primitiveValue = toCSSPrimitiveValue(value);
    if (!primitiveValue.isLength() &&
        !primitiveValue.isCalculatedPercentageWithLength())
      return true;

    CSSPrimitiveValue::CSSLengthArray lengthArray;
    primitiveValue.accumulateLengthArray(lengthArray);
    for (size_t i = 0; i < lengthArray.values.size(); i++) {
      if (lengthArray.typeFlags.get(i) &&
          i != CSSPrimitiveValue::UnitTypePixels &&
          i != CSSPrimitiveValue::UnitTypePercentage)
        return false;
    }
    return true;
  }

  // TODO(timloh): Images and transform-function values can also contain
  // lengths.

  return true;
}

InterpolationTypes interpolationTypesForSyntax(const AtomicString& propertyName,
                                               const CSSSyntaxDescriptor&) {
  PropertyHandle property(propertyName);
  InterpolationTypes interpolationTypes;
  // TODO(alancutter): Read the syntax descriptor and add the appropriate
  // CSSInterpolationType subclasses.
  interpolationTypes.push_back(
      WTF::makeUnique<CSSValueInterpolationType>(property));
  return interpolationTypes;
}

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

  String name = descriptor.name();
  if (!CSSVariableParser::isValidVariableName(name)) {
    exceptionState.throwDOMException(
        SyntaxError, "Custom property names must start with '--'.");
    return;
  }
  AtomicString atomicName(name);
  Document* document = toDocument(executionContext);
  PropertyRegistry& registry = *document->propertyRegistry();
  if (registry.registration(atomicName)) {
    exceptionState.throwDOMException(
        InvalidModificationError,
        "The name provided has already been registered.");
    return;
  }

  CSSSyntaxDescriptor syntaxDescriptor(descriptor.syntax());
  if (!syntaxDescriptor.isValid()) {
    exceptionState.throwDOMException(
        SyntaxError,
        "The syntax provided is not a valid custom property syntax.");
    return;
  }

  InterpolationTypes interpolationTypes =
      interpolationTypesForSyntax(atomicName, syntaxDescriptor);

  if (descriptor.hasInitialValue()) {
    CSSTokenizer tokenizer(descriptor.initialValue());
    bool isAnimationTainted = false;
    const CSSValue* initial = syntaxDescriptor.parse(
        tokenizer.tokenRange(),
        document->elementSheet().contents()->parserContext(),
        isAnimationTainted);
    if (!initial) {
      exceptionState.throwDOMException(
          SyntaxError,
          "The initial value provided does not parse for the given syntax.");
      return;
    }
    if (!computationallyIndependent(*initial)) {
      exceptionState.throwDOMException(
          SyntaxError,
          "The initial value provided is not computationally independent.");
      return;
    }
    initial =
        &StyleBuilderConverter::convertRegisteredPropertyInitialValue(*initial);
    RefPtr<CSSVariableData> initialVariableData = CSSVariableData::create(
        tokenizer.tokenRange(), isAnimationTainted, false);
    registry.registerProperty(
        atomicName, syntaxDescriptor, descriptor.inherits(), initial,
        std::move(initialVariableData), std::move(interpolationTypes));
  } else {
    if (!syntaxDescriptor.isTokenStream()) {
      exceptionState.throwDOMException(
          SyntaxError,
          "An initial value must be provided if the syntax is not '*'");
      return;
    }
    registry.registerProperty(atomicName, syntaxDescriptor,
                              descriptor.inherits(), nullptr, nullptr,
                              std::move(interpolationTypes));
  }

  // TODO(timloh): Invalidate only elements with this custom property set
  document->setNeedsStyleRecalc(SubtreeStyleChange,
                                StyleChangeReasonForTracing::create(
                                    StyleChangeReason::PropertyRegistration));
}

}  // namespace blink