Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,15 @@ private static void validateDirectEventNames(
}
for (String oldKey : keysToNormalize) {
Object value = events.get(oldKey);
String newKey = "top" + oldKey.substring(0, 1).toUpperCase() + oldKey.substring(1);
String baseKey = "";
if (oldKey.startsWith("on")) {
// Drop "on" prefix.
baseKey = oldKey.substring(2);
} else {
// Capitalize first letter.
baseKey = oldKey.substring(0, 1).toUpperCase() + oldKey.substring(1);
}
String newKey = "top" + baseKey;
events.put(newKey, value);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class UIManagerModuleConstantsHelperTest {
val onClickMap: Map<String, String> =
MapBuilder.builder<String, String>().put("onClick", "¯\\_(ツ)_/¯").build()
UIManagerModuleConstantsHelper.normalizeEventTypes(onClickMap)
assertTrue(onClickMap.containsKey("topOnClick"))
assertTrue(onClickMap.containsKey("topClick"))
assertTrue(onClickMap.containsKey("onClick"))
}

Expand All @@ -58,16 +58,16 @@ class UIManagerModuleConstantsHelperTest {
"bubbled", "onColorChanged", "captured", "onColorChangedCapture")))
.build()
UIManagerModuleConstantsHelper.normalizeEventTypes(nestedObjects)
assertTrue(nestedObjects.containsKey("topOnColorChanged"))
var innerMap = nestedObjects["topOnColorChanged"] as? Map<String, Any?>
assertTrue(nestedObjects.containsKey("topColorChanged"))
var innerMap = nestedObjects["topColorChanged"] as? Map<String, Any?>
assertNotNull(innerMap)
assertTrue(innerMap!!.containsKey("phasedRegistrationNames"))
var innerInnerMap = innerMap.get("phasedRegistrationNames") as? Map<String, Any?>
assertNotNull(innerInnerMap)
assertEquals("onColorChanged", innerInnerMap!!.get("bubbled"))
assertEquals("onColorChangedCapture", innerInnerMap.get("captured"))
assertTrue(nestedObjects.containsKey("onColorChanged"))
innerMap = nestedObjects.get("topOnColorChanged") as? Map<String, Any?>
innerMap = nestedObjects.get("topColorChanged") as? Map<String, Any?>
assertNotNull(innerMap)
assertTrue(innerMap!!.containsKey("phasedRegistrationNames"))
innerInnerMap = innerMap.get("phasedRegistrationNames") as? Map<String, Any?>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,25 @@

namespace facebook::react {

static bool hasPrefix(const std::string& str, const std::string& prefix) {
return str.compare(0, prefix.length(), prefix) == 0;
}

// TODO(T29874519): Get rid of "top" prefix once and for all.
/*
* Capitalizes the first letter of the event type and adds "top" prefix if
* necessary (e.g. "layout" becames "topLayout").
*/
static std::string normalizeEventType(std::string type) {
auto prefixedType = std::move(type);
if (prefixedType.find("top", 0) != 0) {
prefixedType.insert(0, "top");
prefixedType[3] = static_cast<char>(toupper(prefixedType[3]));
if (facebook::react::hasPrefix(prefixedType, "top")) {
return prefixedType;
}
if (facebook::react::hasPrefix(prefixedType, "on")) {
return "top" + prefixedType.substr(2);
}
return prefixedType;
prefixedType[0] = static_cast<char>(toupper(prefixedType[0]));
return "top" + prefixedType;
}

std::mutex& EventEmitter::DispatchMutex() {
Expand Down