File: names_map.cc

package info (click to toggle)
chromium-browser 70.0.3538.110-1~deb9u1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 1,619,476 kB
  • sloc: cpp: 13,024,755; ansic: 1,349,823; python: 916,672; xml: 314,489; java: 280,047; asm: 276,936; perl: 75,771; objc: 66,634; sh: 45,860; cs: 28,354; php: 11,064; makefile: 10,911; yacc: 9,109; tcl: 8,403; ruby: 4,065; lex: 1,779; pascal: 1,411; lisp: 1,055; awk: 41; jsp: 39; sed: 17; sql: 3
file content (151 lines) | stat: -rw-r--r-- 4,810 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
// Copyright 2018 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 <memory>

#include "third_party/blink/renderer/core/dom/names_map.h"
#include "third_party/blink/renderer/core/html/parser/html_parser_idioms.h"
#include "third_party/blink/renderer/platform/wtf/text/string_hash.h"
#include "third_party/blink/renderer/platform/wtf/text/wtf_string.h"

namespace blink {

NamesMap::NamesMap(const AtomicString& string) {
  Set(string);
}

void NamesMap::Set(const AtomicString& source) {
  if (source.IsNull()) {
    Clear();
    return;
  }
  if (source.Is8Bit()) {
    Set(source, source.Characters8());
    return;
  }

  Set(source, source.Characters16());
}

void NamesMap::Add(const AtomicString& key, const AtomicString& value) {
  // AddResult
  auto add_result = data_.insert(key, base::Optional<SpaceSplitString>());
  if (add_result.is_new_entry) {
    add_result.stored_value->value =
        base::make_optional<SpaceSplitString>(SpaceSplitString());
  }
  add_result.stored_value->value.value().Add(value);
}

// Parser for HTML partmap attribute. See
// http://drafts.csswg.org/css-shadow-parts/ but with modifications.
//
// This implements a modified version where the key is first and the value is
// second and => is not used to separate key and value. It also allows an ident
// token on its own as a short-hand for forwarding with the same name.

// The states that can occur while parsing the part map. {...} denotes the new
// states that can be reached from this state.
enum State {
  kPreKey,    // Searching for the start of a key. {kPreKey, kKey}
  kKey,       // Searching for the end of a key. {kKey, kPreValue}
  kPreValue,  // Searching for the start of a value. {kPreValue, kPreKey,
              // kValue}
  kValue,     // Searching for the end of a value. {kValue, kPreKey, kPostValue}
  kPostValue,  // Searching for the comma after the value. {kPostValue, kPreKey}
};

template <typename CharacterType>
void NamesMap::Set(const AtomicString& source,
                   const CharacterType* characters) {
  Clear();
  unsigned length = source.length();

  // The character we are examining.
  unsigned cur = 0;
  // The start of the current token.
  unsigned start = 0;
  State state = kPreKey;
  AtomicString key;
  while (cur < length) {
    switch (state) {
      case kPreKey:
        // Skip any number of spaces and commas. When we find something else, it
        // is the start of a key.
        if (!IsHTMLSpaceOrComma<CharacterType>(characters[cur])) {
          start = cur;
          state = kKey;
        }
        break;
      case kKey:
        // At a space or comma, we have found the end of the key.
        if (IsHTMLSpaceOrComma<CharacterType>(characters[cur])) {
          key = AtomicString(characters + start, cur - start);
          state = kPreValue;
        } else {
          break;
        }
        FALLTHROUGH;
      case kPreValue:
        // At a comma this was a key without a value, the implicit value is the
        // same as the key.
        if (IsComma<CharacterType>(characters[cur])) {
          Add(key, key);
          state = kPreKey;
          // If we reach a non-space character, we have found the start of the
          // value.
        } else if (IsNotHTMLSpace<CharacterType>(characters[cur])) {
          start = cur;
          state = kValue;
        }
        break;
      case kValue:
        // At a comma or space, we have found the end of the value.
        if (IsHTMLSpaceOrComma<CharacterType>(characters[cur])) {
          Add(key, AtomicString(characters + start, cur - start));
          if (IsComma<CharacterType>(characters[cur])) {
            state = kPreKey;
          } else {
            state = kPostValue;
          }
        }
        break;
      case kPostValue:
        // At a comma, we start looking for the next key.
        if (IsComma<CharacterType>(characters[cur])) {
          state = kPreKey;
        }
        break;
    }

    ++cur;
  }

  // We have reached the end of the string, add whatever we had into the map.
  switch (state) {
    case kPreKey:
      break;
    case kKey:
      // The string ends with a key.
      key = AtomicString(characters + start, cur - start);
      FALLTHROUGH;
    case kPreValue:
      // The string ends after a key but with nothing else useful.
      Add(key, key);
      break;
    case kValue:
      // The string ends with a value.
      Add(key, AtomicString(characters + start, cur - start));
      break;
    case kPostValue:
      break;
  }
}

base::Optional<SpaceSplitString> NamesMap::Get(const AtomicString& key) const {
  auto it = data_.find(key);
  return it != data_.end() ? it->value : base::nullopt;
}

}  // namespace blink