summaryrefslogtreecommitdiff
path: root/chromium/third_party/blink/renderer/core/css/parser/font_variant_east_asian_parser.h
blob: d8f4be1589ded5e75ff7bad748c092495458e982 (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
// Copyright 2017 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.

#ifndef THIRD_PARTY_BLINK_RENDERER_CORE_CSS_PARSER_FONT_VARIANT_EAST_ASIAN_PARSER_H_
#define THIRD_PARTY_BLINK_RENDERER_CORE_CSS_PARSER_FONT_VARIANT_EAST_ASIAN_PARSER_H_

#include "third_party/blink/renderer/core/css/css_value_list.h"
#include "third_party/blink/renderer/core/css/parser/css_parser_token_range.h"
#include "third_party/blink/renderer/core/css/parser/css_property_parser_helpers.h"

namespace blink {

class FontVariantEastAsianParser {
  STACK_ALLOCATED();

 public:
  FontVariantEastAsianParser()
      : east_asian_form_value_(nullptr),
        east_asian_width_value_(nullptr),
        ruby_value_(nullptr) {}

  enum class ParseResult { kConsumedValue, kDisallowedValue, kUnknownValue };

  ParseResult ConsumeEastAsian(CSSParserTokenRange& range) {
    CSSValueID value_id = range.Peek().Id();
    switch (value_id) {
      case CSSValueID::kJis78:
      case CSSValueID::kJis83:
      case CSSValueID::kJis90:
      case CSSValueID::kJis04:
      case CSSValueID::kSimplified:
      case CSSValueID::kTraditional:
        if (east_asian_form_value_)
          return ParseResult::kDisallowedValue;
        east_asian_form_value_ =
            css_property_parser_helpers::ConsumeIdent(range);
        return ParseResult::kConsumedValue;
      case CSSValueID::kFullWidth:
      case CSSValueID::kProportionalWidth:
        if (east_asian_width_value_)
          return ParseResult::kDisallowedValue;
        east_asian_width_value_ =
            css_property_parser_helpers::ConsumeIdent(range);
        return ParseResult::kConsumedValue;
      case CSSValueID::kRuby:
        if (ruby_value_)
          return ParseResult::kDisallowedValue;
        ruby_value_ = css_property_parser_helpers::ConsumeIdent(range);
        return ParseResult::kConsumedValue;
      default:
        return ParseResult::kUnknownValue;
    }
  }

  CSSValue* FinalizeValue() {
    CSSValueList* result = CSSValueList::CreateSpaceSeparated();
    if (east_asian_form_value_) {
      result->Append(*east_asian_form_value_);
      east_asian_form_value_ = nullptr;
    }
    if (east_asian_width_value_) {
      result->Append(*east_asian_width_value_);
      east_asian_width_value_ = nullptr;
    }
    if (ruby_value_) {
      result->Append(*ruby_value_);
      ruby_value_ = nullptr;
    }

    if (!result->length())
      return CSSIdentifierValue::Create(CSSValueID::kNormal);
    return result;
  }

 private:
  CSSIdentifierValue* east_asian_form_value_;
  CSSIdentifierValue* east_asian_width_value_;
  CSSIdentifierValue* ruby_value_;
};

}  // namespace blink

#endif  // THIRD_PARTY_BLINK_RENDERER_CORE_CSS_PARSER_FONT_VARIANT_EAST_ASIAN_PARSER_H_