summaryrefslogtreecommitdiff
path: root/chromium/third_party/blink/renderer/core/css/parser/css_property_parser.cc
blob: 1f6e911c2f116eaaf16b3f1bea9b64665b6b96a8 (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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
// Copyright 2015 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/parser/css_property_parser.h"

#include "third_party/blink/renderer/core/css/css_inherited_value.h"
#include "third_party/blink/renderer/core/css/css_initial_value.h"
#include "third_party/blink/renderer/core/css/css_pending_substitution_value.h"
#include "third_party/blink/renderer/core/css/css_unicode_range_value.h"
#include "third_party/blink/renderer/core/css/css_unset_value.h"
#include "third_party/blink/renderer/core/css/css_variable_reference_value.h"
#include "third_party/blink/renderer/core/css/hash_tools.h"
#include "third_party/blink/renderer/core/css/parser/at_rule_descriptor_parser.h"
#include "third_party/blink/renderer/core/css/parser/css_parser_local_context.h"
#include "third_party/blink/renderer/core/css/parser/css_property_parser_helpers.h"
#include "third_party/blink/renderer/core/css/parser/css_variable_parser.h"
#include "third_party/blink/renderer/core/css/properties/css_parsing_utils.h"
#include "third_party/blink/renderer/core/css/properties/css_property.h"
#include "third_party/blink/renderer/core/css/properties/shorthand.h"
#include "third_party/blink/renderer/core/style_property_shorthand.h"
#include "third_party/blink/renderer/platform/runtime_enabled_features.h"

namespace blink {

using css_property_parser_helpers::ConsumeIdent;
using css_property_parser_helpers::IsImplicitProperty;
using css_property_parser_helpers::ParseLonghand;

class CSSIdentifierValue;

namespace {

const CSSValue* MaybeConsumeCSSWideKeyword(CSSParserTokenRange& range) {
  CSSParserTokenRange local_range = range;

  CSSValueID id = local_range.ConsumeIncludingWhitespace().Id();
  if (!local_range.AtEnd())
    return nullptr;

  const CSSValue* value = nullptr;
  if (id == CSSValueID::kInitial)
    value = CSSInitialValue::Create();
  if (id == CSSValueID::kInherit)
    value = CSSInheritedValue::Create();
  if (id == CSSValueID::kUnset)
    value = cssvalue::CSSUnsetValue::Create();

  if (value)
    range = local_range;

  return value;
}

}  // namespace

CSSPropertyParser::CSSPropertyParser(
    const CSSParserTokenRange& range,
    const CSSParserContext* context,
    HeapVector<CSSPropertyValue, 256>* parsed_properties)
    : range_(range), context_(context), parsed_properties_(parsed_properties) {
  range_.ConsumeWhitespace();
}

bool CSSPropertyParser::ParseValue(
    CSSPropertyID unresolved_property,
    bool important,
    const CSSParserTokenRange& range,
    const CSSParserContext* context,
    HeapVector<CSSPropertyValue, 256>& parsed_properties,
    StyleRule::RuleType rule_type) {
  int parsed_properties_size = parsed_properties.size();

  CSSPropertyParser parser(range, context, &parsed_properties);
  CSSPropertyID resolved_property = resolveCSSPropertyID(unresolved_property);
  bool parse_success;
  if (rule_type == StyleRule::kViewport) {
    parse_success =
        (RuntimeEnabledFeatures::CSSViewportEnabled() ||
         IsUASheetBehavior(context->Mode())) &&
        parser.ParseViewportDescriptor(resolved_property, important);
  } else if (rule_type == StyleRule::kFontFace) {
    parse_success = parser.ParseFontFaceDescriptor(resolved_property);
  } else {
    parse_success = parser.ParseValueStart(unresolved_property, important);
  }

  // This doesn't count UA style sheets
  if (parse_success)
    context->Count(context->Mode(), unresolved_property);

  if (!parse_success)
    parsed_properties.Shrink(parsed_properties_size);

  return parse_success;
}

const CSSValue* CSSPropertyParser::ParseSingleValue(
    CSSPropertyID property,
    const CSSParserTokenRange& range,
    const CSSParserContext* context) {
  DCHECK(context);
  CSSPropertyParser parser(range, context, nullptr);

  if (const CSSValue* value = MaybeConsumeCSSWideKeyword(parser.range_))
    return value;

  const CSSValue* value = ParseLonghand(property, CSSPropertyID::kInvalid,
                                        *parser.context_, parser.range_);
  if (!value || !parser.range_.AtEnd())
    return nullptr;
  return value;
}

bool CSSPropertyParser::ParseValueStart(CSSPropertyID unresolved_property,
                                        bool important) {
  if (ConsumeCSSWideKeyword(unresolved_property, important))
    return true;

  CSSParserTokenRange original_range = range_;
  CSSPropertyID property_id = resolveCSSPropertyID(unresolved_property);
  const CSSProperty& property = CSSProperty::Get(property_id);
  // If a CSSPropertyID is only a known descriptor (@fontface, @viewport), not a
  // style property, it will not be a valid declaration.
  if (!property.IsProperty())
    return false;
  bool is_shorthand = property.IsShorthand();
  DCHECK(context_);
  if (is_shorthand) {
    const auto local_context =
        CSSParserLocalContext()
            .WithAliasParsing(isPropertyAlias(unresolved_property))
            .WithCurrentShorthand(property_id);
    // Variable references will fail to parse here and will fall out to the
    // variable ref parser below.
    if (To<Shorthand>(property).ParseShorthand(
            important, range_, *context_, local_context, *parsed_properties_))
      return true;
  } else {
    if (const CSSValue* parsed_value = ParseLonghand(
            unresolved_property, CSSPropertyID::kInvalid, *context_, range_)) {
      if (range_.AtEnd()) {
        AddProperty(property_id, CSSPropertyID::kInvalid, *parsed_value,
                    important, IsImplicitProperty::kNotImplicit,
                    *parsed_properties_);
        return true;
      }
    }
  }

  if (CSSVariableParser::ContainsValidVariableReferences(original_range)) {
    bool is_animation_tainted = false;
    auto* variable = MakeGarbageCollected<CSSVariableReferenceValue>(
        CSSVariableData::Create(original_range, is_animation_tainted, true,
                                context_->BaseURL(), context_->Charset()),
        *context_);

    if (is_shorthand) {
      const cssvalue::CSSPendingSubstitutionValue& pending_value =
          *cssvalue::CSSPendingSubstitutionValue::Create(property_id, variable);
      css_property_parser_helpers::AddExpandedPropertyForValue(
          property_id, pending_value, important, *parsed_properties_);
    } else {
      AddProperty(property_id, CSSPropertyID::kInvalid, *variable, important,
                  IsImplicitProperty::kNotImplicit, *parsed_properties_);
    }
    return true;
  }

  return false;
}

static inline bool IsExposedInMode(const CSSProperty& property,
                                   CSSParserMode mode) {
  return mode == kUASheetMode ? property.IsUAExposed()
                              : property.IsWebExposed();
}

template <typename CharacterType>
static CSSPropertyID UnresolvedCSSPropertyID(const CharacterType* property_name,
                                             unsigned length,
                                             CSSParserMode mode) {
  if (length == 0)
    return CSSPropertyID::kInvalid;
  if (length >= 2 && property_name[0] == '-' && property_name[1] == '-')
    return CSSPropertyID::kVariable;
  if (length > maxCSSPropertyNameLength)
    return CSSPropertyID::kInvalid;

  char buffer[maxCSSPropertyNameLength + 1];  // 1 for null character

  for (unsigned i = 0; i != length; ++i) {
    CharacterType c = property_name[i];
    if (c == 0 || c >= 0x7F)
      return CSSPropertyID::kInvalid;  // illegal character
    buffer[i] = ToASCIILower(c);
  }
  buffer[length] = '\0';

  const char* name = buffer;
  const Property* hash_table_entry = FindProperty(name, length);
  if (!hash_table_entry)
    return CSSPropertyID::kInvalid;
  CSSPropertyID property_id = static_cast<CSSPropertyID>(hash_table_entry->id);
  const CSSProperty& property =
      CSSProperty::Get(resolveCSSPropertyID(property_id));
  bool exposed = IsExposedInMode(property, mode);
  return exposed ? property_id : CSSPropertyID::kInvalid;
}

CSSPropertyID unresolvedCSSPropertyID(const String& string) {
  unsigned length = string.length();
  CSSParserMode mode = kHTMLStandardMode;
  return string.Is8Bit()
             ? UnresolvedCSSPropertyID(string.Characters8(), length, mode)
             : UnresolvedCSSPropertyID(string.Characters16(), length, mode);
}

CSSPropertyID UnresolvedCSSPropertyID(StringView string, CSSParserMode mode) {
  unsigned length = string.length();
  return string.Is8Bit()
             ? UnresolvedCSSPropertyID(string.Characters8(), length, mode)
             : UnresolvedCSSPropertyID(string.Characters16(), length, mode);
}

template <typename CharacterType>
static CSSValueID CssValueKeywordID(const CharacterType* value_keyword,
                                    unsigned length) {
  char buffer[maxCSSValueKeywordLength + 1];  // 1 for null character

  for (unsigned i = 0; i != length; ++i) {
    CharacterType c = value_keyword[i];
    if (c == 0 || c >= 0x7F)
      return CSSValueID::kInvalid;  // illegal character
    buffer[i] = WTF::ToASCIILower(c);
  }
  buffer[length] = '\0';

  const Value* hash_table_entry = FindValue(buffer, length);
  return hash_table_entry ? static_cast<CSSValueID>(hash_table_entry->id)
                          : CSSValueID::kInvalid;
}

CSSValueID CssValueKeywordID(StringView string) {
  unsigned length = string.length();
  if (!length)
    return CSSValueID::kInvalid;
  if (length > maxCSSValueKeywordLength)
    return CSSValueID::kInvalid;

  return string.Is8Bit() ? CssValueKeywordID(string.Characters8(), length)
                         : CssValueKeywordID(string.Characters16(), length);
}

bool CSSPropertyParser::ConsumeCSSWideKeyword(CSSPropertyID unresolved_property,
                                              bool important) {
  CSSParserTokenRange range_copy = range_;

  const CSSValue* value = MaybeConsumeCSSWideKeyword(range_copy);
  if (!value)
    return false;

  CSSPropertyID property = resolveCSSPropertyID(unresolved_property);
  const StylePropertyShorthand& shorthand = shorthandForProperty(property);
  if (!shorthand.length()) {
    if (!CSSProperty::Get(property).IsProperty())
      return false;
    AddProperty(property, CSSPropertyID::kInvalid, *value, important,
                IsImplicitProperty::kNotImplicit, *parsed_properties_);
  } else {
    css_property_parser_helpers::AddExpandedPropertyForValue(
        property, *value, important, *parsed_properties_);
  }
  range_ = range_copy;
  return true;
}

static CSSValue* ConsumeSingleViewportDescriptor(
    CSSParserTokenRange& range,
    CSSPropertyID prop_id,
    CSSParserMode css_parser_mode) {
  CSSValueID id = range.Peek().Id();
  switch (prop_id) {
    case CSSPropertyID::kMinWidth:
    case CSSPropertyID::kMaxWidth:
    case CSSPropertyID::kMinHeight:
    case CSSPropertyID::kMaxHeight:
      if (id == CSSValueID::kAuto || id == CSSValueID::kInternalExtendToZoom)
        return ConsumeIdent(range);
      return css_property_parser_helpers::ConsumeLengthOrPercent(
          range, css_parser_mode, kValueRangeNonNegative);
    case CSSPropertyID::kMinZoom:
    case CSSPropertyID::kMaxZoom:
    case CSSPropertyID::kZoom: {
      if (id == CSSValueID::kAuto)
        return ConsumeIdent(range);
      CSSValue* parsed_value = css_property_parser_helpers::ConsumeNumber(
          range, kValueRangeNonNegative);
      if (parsed_value)
        return parsed_value;
      return css_property_parser_helpers::ConsumePercent(
          range, kValueRangeNonNegative);
    }
    case CSSPropertyID::kUserZoom:
      return ConsumeIdent<CSSValueID::kZoom, CSSValueID::kFixed>(range);
    case CSSPropertyID::kOrientation:
      return ConsumeIdent<CSSValueID::kAuto, CSSValueID::kPortrait,
                          CSSValueID::kLandscape>(range);
    case CSSPropertyID::kViewportFit:
      return ConsumeIdent<CSSValueID::kAuto, CSSValueID::kContain,
                          CSSValueID::kCover>(range);
    default:
      NOTREACHED();
      break;
  }

  NOTREACHED();
  return nullptr;
}

bool CSSPropertyParser::ParseViewportDescriptor(CSSPropertyID prop_id,
                                                bool important) {
  DCHECK(RuntimeEnabledFeatures::CSSViewportEnabled() ||
         IsUASheetBehavior(context_->Mode()));

  switch (prop_id) {
    case CSSPropertyID::kWidth: {
      CSSValue* min_width = ConsumeSingleViewportDescriptor(
          range_, CSSPropertyID::kMinWidth, context_->Mode());
      if (!min_width)
        return false;
      CSSValue* max_width = min_width;
      if (!range_.AtEnd()) {
        max_width = ConsumeSingleViewportDescriptor(
            range_, CSSPropertyID::kMaxWidth, context_->Mode());
      }
      if (!max_width || !range_.AtEnd())
        return false;
      AddProperty(CSSPropertyID::kMinWidth, CSSPropertyID::kInvalid, *min_width,
                  important, IsImplicitProperty::kNotImplicit,
                  *parsed_properties_);
      AddProperty(CSSPropertyID::kMaxWidth, CSSPropertyID::kInvalid, *max_width,
                  important, IsImplicitProperty::kNotImplicit,
                  *parsed_properties_);
      return true;
    }
    case CSSPropertyID::kHeight: {
      CSSValue* min_height = ConsumeSingleViewportDescriptor(
          range_, CSSPropertyID::kMinHeight, context_->Mode());
      if (!min_height)
        return false;
      CSSValue* max_height = min_height;
      if (!range_.AtEnd()) {
        max_height = ConsumeSingleViewportDescriptor(
            range_, CSSPropertyID::kMaxHeight, context_->Mode());
      }
      if (!max_height || !range_.AtEnd())
        return false;
      AddProperty(CSSPropertyID::kMinHeight, CSSPropertyID::kInvalid,
                  *min_height, important, IsImplicitProperty::kNotImplicit,
                  *parsed_properties_);
      AddProperty(CSSPropertyID::kMaxHeight, CSSPropertyID::kInvalid,
                  *max_height, important, IsImplicitProperty::kNotImplicit,
                  *parsed_properties_);
      return true;
    }
    case CSSPropertyID::kViewportFit:
    case CSSPropertyID::kMinWidth:
    case CSSPropertyID::kMaxWidth:
    case CSSPropertyID::kMinHeight:
    case CSSPropertyID::kMaxHeight:
    case CSSPropertyID::kMinZoom:
    case CSSPropertyID::kMaxZoom:
    case CSSPropertyID::kZoom:
    case CSSPropertyID::kUserZoom:
    case CSSPropertyID::kOrientation: {
      CSSValue* parsed_value =
          ConsumeSingleViewportDescriptor(range_, prop_id, context_->Mode());
      if (!parsed_value || !range_.AtEnd())
        return false;
      AddProperty(prop_id, CSSPropertyID::kInvalid, *parsed_value, important,
                  IsImplicitProperty::kNotImplicit, *parsed_properties_);
      return true;
    }
    default:
      return false;
  }
}

bool CSSPropertyParser::ParseFontFaceDescriptor(
    CSSPropertyID resolved_property) {
  // TODO(meade): This function should eventually take an AtRuleDescriptorID.
  const AtRuleDescriptorID id =
      CSSPropertyIDAsAtRuleDescriptor(resolved_property);
  if (id == AtRuleDescriptorID::Invalid)
    return false;
  CSSValue* parsed_value =
      AtRuleDescriptorParser::ParseFontFaceDescriptor(id, range_, *context_);
  if (!parsed_value)
    return false;

  AddProperty(resolved_property,
              CSSPropertyID::kInvalid /* current_shorthand */, *parsed_value,
              false /* important */, IsImplicitProperty::kNotImplicit,
              *parsed_properties_);
  return true;
}

}  // namespace blink