summaryrefslogtreecommitdiffstats
path: root/layout/style/ShadowParts.cpp
blob: adb4d932f605ec5606fbc5cfd4daaaeea57d7247 (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
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "ShadowParts.h"
#include "nsContentUtils.h"
#include "nsString.h"

namespace mozilla {

static bool IsSpace(char16_t aChar) {
  return nsContentUtils::IsHTMLWhitespace(aChar);
};

using SingleMapping = std::pair<RefPtr<nsAtom>, RefPtr<nsAtom>>;

// https://drafts.csswg.org/css-shadow-parts/#parsing-mapping
//
// Returns null on both tokens to signal an error.
static SingleMapping ParseSingleMapping(const nsAString& aString) {
  const char16_t* c = aString.BeginReading();
  const char16_t* end = aString.EndReading();

  const auto CollectASequenceOfSpaces = [&c, end]() {
    while (c != end && IsSpace(*c)) {
      ++c;
    }
  };

  const auto CollectToken = [&c, end]() -> RefPtr<nsAtom> {
    const char16_t* t = c;
    while (c != end && !IsSpace(*c) && *c != ':') {
      ++c;
    }
    if (c == t) {
      return nullptr;
    }
    return NS_AtomizeMainThread(Substring(t, c));
  };

  // Steps 1 and 2 are variable declarations.
  //
  // 3. Collect a sequence of code points that are space characters.
  CollectASequenceOfSpaces();

  // 4. Collect a sequence of code points that are not space characters or
  // U+003A COLON characters, and call the result first token.
  RefPtr<nsAtom> firstToken = CollectToken();

  // 5. If first token is empty then return error.
  if (!firstToken) {
    return {nullptr, nullptr};
  }

  // 6. Collect a sequence of code points that are space characters.
  CollectASequenceOfSpaces();

  // 7. If the end of the input has been reached, return the pair first
  // token/first token.
  if (c == end) {
    return {firstToken, firstToken};
  }

  // 8. If character at position is not a U+003A COLON character, return error.
  if (*c != ':') {
    return {nullptr, nullptr};
  }

  // 9. Consume the U+003A COLON character.
  ++c;

  // 10. Collect a sequence of code points that are space characters.
  CollectASequenceOfSpaces();

  // 11. Collect a sequence of code points that are not space characters or
  // U+003A COLON characters. and let second token be the result.
  RefPtr<nsAtom> secondToken = CollectToken();

  // 12. If second token is empty then return error.
  if (!secondToken) {
    return {nullptr, nullptr};
  }

  // 13. Collect a sequence of code points that are space characters.
  CollectASequenceOfSpaces();

  // 14. If position is not past the end of input then return error.
  if (c != end) {
    return {nullptr, nullptr};
  }

  // 15. Return the pair first token/second token.
  return {std::move(firstToken), std::move(secondToken)};
}

// https://drafts.csswg.org/css-shadow-parts/#parsing-mapping-list
ShadowParts ShadowParts::Parse(const nsAString& aString) {
  ShadowParts parts;

  for (const auto& substring : aString.Split(',')) {
    auto mapping = ParseSingleMapping(substring);
    if (!mapping.first) {
      MOZ_ASSERT(!mapping.second);
      continue;
    }
    nsAtom* second = mapping.second.get();
    parts.mMappings.GetOrInsertNew(mapping.first)
        ->AppendElement(std::move(mapping.second));
    parts.mReverseMappings.InsertOrUpdate(second, std::move(mapping.first));
  }

  return parts;
}

#ifdef DEBUG
void ShadowParts::Dump() const {
  if (mMappings.IsEmpty()) {
    printf("  (empty)\n");
    return;
  }
  for (auto& entry : mMappings) {
    nsAutoCString key;
    entry.GetKey()->ToUTF8String(key);
    printf("  %s: ", key.get());

    bool first = true;
    for (nsAtom* part : *entry.GetData()) {
      if (!first) {
        printf(", ");
      }
      first = false;
      nsAutoCString value;
      part->ToUTF8String(value);
      printf("%s", value.get());
    }
    printf("\n");
  }
}
#endif
}  // namespace mozilla