gecko-dev/layout/style/ShadowParts.cpp
Emilio Cobos Álvarez 74823e8b90 Bug 1559076 - Implement shadow part forwarding (minus invalidation). r=heycam
Some of the stuff, in particular inside GeckoBindings stuff should be
refactored to be less ugly and duplicate a bit less code, but the rest of the
code should be landable as is.

Some invalidation changes are already needed because we weren't matching with
the right shadow host during invalidation (which made existing ::part() tests
fail).

Pending invalidation work:

 * Making exportparts work right on the snapshots.
 * Invalidating parts from descendant hosts.

They're not very hard but I need to think how to best implement it:

 * Maybe get rid of ShadowRoot::mParts and just walk DOM descendants in the
   Shadow DOM.

 * Maybe implement a ElementHasExportPartsAttr much like HasPartAttr and use
   that to keep the list of elements.

 * Maybe invalidate :host and ::part() together in here[1]

 * Maybe something else.

Opinions?

[1]: https://searchfox.org/mozilla-central/rev/131338e5017bc0283d86fb73844407b9a2155c98/servo/components/style/invalidation/element/invalidator.rs#561

Differential Revision: https://phabricator.services.mozilla.com/D53730

--HG--
extra : moz-landing-system : lando
2019-11-21 10:32:32 +00:00

132 lines
3.7 KiB
C++

/* -*- 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.GetOrInsert(mapping.first) = std::move(mapping.second);
parts.mReverseMappings.GetOrInsert(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);
nsAutoCString value;
entry.GetData()->ToUTF8String(value);
printf(" %s: %s\n", key.get(), value.get());
}
}
#endif
} // namespace mozilla