mirror of
https://github.com/facebook/react-native.git
synced 2026-11-18 20:19:07 +00:00
Default invalid pointerEvents values to auto
Summary: Invalid Android pointerEvents values fall back to `AUTO` and log a warning instead of throwing. Null still resets the default silently, and valid case/hyphen variants retain their existing behaviour. Changelog: [Android][Fixed] - Default invalid pointerEvents values to auto with a warning Differential Revision: D129603915 Pull Request resolved: https://github.com/react/react-native/pull/59794
This commit is contained in:
1 parent
480fe97660
commit
3d3a339012
7 files changed
+181
-25
No files matched your search
+4
-4
@@ -402,7 +402,7 @@ static inline void fromRawValue(const PropsParserContext& context, const RawValu
|
||||
if (string == \\"top\\") { result = EnumPropNativeComponentViewAlignment::Top; return; }
|
||||
if (string == \\"center\\") { result = EnumPropNativeComponentViewAlignment::Center; return; }
|
||||
if (string == \\"bottom-right\\") { result = EnumPropNativeComponentViewAlignment::BottomRight; return; }
|
||||
abort();
|
||||
result = EnumPropNativeComponentViewAlignment::Center;
|
||||
}
|
||||
|
||||
static inline std::string toString(const EnumPropNativeComponentViewAlignment &value) {
|
||||
@@ -439,7 +439,7 @@ static inline void fromRawValue(const PropsParserContext& context, const RawValu
|
||||
result = EnumPropNativeComponentViewIntervals::Intervals60;
|
||||
return;
|
||||
default:
|
||||
abort();
|
||||
result = EnumPropNativeComponentViewIntervals::Intervals0;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -902,7 +902,7 @@ static inline void fromRawValue(const PropsParserContext& context, const RawValu
|
||||
auto string = (std::string)value;
|
||||
if (string == \\"small\\") { result = ObjectPropsNativeComponentStringEnumProp::Small; return; }
|
||||
if (string == \\"large\\") { result = ObjectPropsNativeComponentStringEnumProp::Large; return; }
|
||||
abort();
|
||||
result = ObjectPropsNativeComponentStringEnumProp::Small;
|
||||
}
|
||||
|
||||
static inline std::string toString(const ObjectPropsNativeComponentStringEnumProp &value) {
|
||||
@@ -932,7 +932,7 @@ static inline void fromRawValue(const PropsParserContext& context, const RawValu
|
||||
result = ObjectPropsNativeComponentIntEnumProp::IntEnumProp1;
|
||||
return;
|
||||
default:
|
||||
abort();
|
||||
result = ObjectPropsNativeComponentIntEnumProp::IntEnumProp0;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+4
-4
@@ -402,7 +402,7 @@ static inline void fromRawValue(const PropsParserContext& context, const RawValu
|
||||
if (string == \\"top\\") { result = EnumPropNativeComponentViewAlignment::Top; return; }
|
||||
if (string == \\"center\\") { result = EnumPropNativeComponentViewAlignment::Center; return; }
|
||||
if (string == \\"bottom-right\\") { result = EnumPropNativeComponentViewAlignment::BottomRight; return; }
|
||||
abort();
|
||||
result = EnumPropNativeComponentViewAlignment::Center;
|
||||
}
|
||||
|
||||
static inline std::string toString(const EnumPropNativeComponentViewAlignment &value) {
|
||||
@@ -439,7 +439,7 @@ static inline void fromRawValue(const PropsParserContext& context, const RawValu
|
||||
result = EnumPropNativeComponentViewIntervals::Intervals60;
|
||||
return;
|
||||
default:
|
||||
abort();
|
||||
result = EnumPropNativeComponentViewIntervals::Intervals0;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -902,7 +902,7 @@ static inline void fromRawValue(const PropsParserContext& context, const RawValu
|
||||
auto string = (std::string)value;
|
||||
if (string == \\"small\\") { result = ObjectPropsNativeComponentStringEnumProp::Small; return; }
|
||||
if (string == \\"large\\") { result = ObjectPropsNativeComponentStringEnumProp::Large; return; }
|
||||
abort();
|
||||
result = ObjectPropsNativeComponentStringEnumProp::Small;
|
||||
}
|
||||
|
||||
static inline std::string toString(const ObjectPropsNativeComponentStringEnumProp &value) {
|
||||
@@ -932,7 +932,7 @@ static inline void fromRawValue(const PropsParserContext& context, const RawValu
|
||||
result = ObjectPropsNativeComponentIntEnumProp::IntEnumProp1;
|
||||
return;
|
||||
default:
|
||||
abort();
|
||||
result = ObjectPropsNativeComponentIntEnumProp::IntEnumProp0;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -23,6 +23,7 @@ const {
|
||||
getNativeTypeFromAnnotation,
|
||||
} = require('./ComponentsGeneratorUtils.js');
|
||||
const {
|
||||
convertDefaultTypeToString,
|
||||
generateStructName,
|
||||
getDefaultInitializerString,
|
||||
getEnumMaskName,
|
||||
@@ -106,11 +107,13 @@ const EnumTemplate = ({
|
||||
values,
|
||||
fromCases,
|
||||
toCases,
|
||||
defaultValue,
|
||||
}: {
|
||||
enumName: string,
|
||||
values: string,
|
||||
fromCases: string,
|
||||
toCases: string,
|
||||
defaultValue: string,
|
||||
}) =>
|
||||
`
|
||||
enum class ${enumName} { ${values} };
|
||||
@@ -118,7 +121,7 @@ enum class ${enumName} { ${values} };
|
||||
static inline void fromRawValue(const PropsParserContext& context, const RawValue &value, ${enumName} &result) {
|
||||
auto string = (std::string)value;
|
||||
${fromCases}
|
||||
abort();
|
||||
result = ${defaultValue};
|
||||
}
|
||||
|
||||
static inline std::string toString(const ${enumName} &value) {
|
||||
@@ -142,12 +145,14 @@ const IntEnumTemplate = ({
|
||||
fromCases,
|
||||
toCases,
|
||||
toDynamicCases,
|
||||
defaultValue,
|
||||
}: {
|
||||
enumName: string,
|
||||
values: string,
|
||||
fromCases: string,
|
||||
toCases: string,
|
||||
toDynamicCases: string,
|
||||
defaultValue: string,
|
||||
}) =>
|
||||
`
|
||||
enum class ${enumName} { ${values} };
|
||||
@@ -157,7 +162,7 @@ static inline void fromRawValue(const PropsParserContext& context, const RawValu
|
||||
auto integerValue = (int)value;
|
||||
switch (integerValue) {${fromCases}
|
||||
default:
|
||||
abort();
|
||||
result = ${defaultValue};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -419,6 +424,7 @@ function generateStringEnum(
|
||||
values: values.map(toSafeCppString).join(', '),
|
||||
fromCases: fromCases,
|
||||
toCases: toCases,
|
||||
defaultValue: convertDefaultTypeToString(componentName, prop),
|
||||
});
|
||||
}
|
||||
|
||||
@@ -474,6 +480,7 @@ function generateIntEnum(
|
||||
fromCases,
|
||||
toCases,
|
||||
toDynamicCases,
|
||||
defaultValue: convertDefaultTypeToString(componentName, prop),
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
+76
@@ -10,6 +10,12 @@
|
||||
|
||||
'use strict';
|
||||
|
||||
import type {
|
||||
NamedShape,
|
||||
PropTypeAnnotation,
|
||||
SchemaType,
|
||||
} from '../../../CodegenSchema';
|
||||
|
||||
const fixtures = require('../__test_fixtures__/fixtures.js');
|
||||
const generator = require('../GeneratePropsH.js');
|
||||
|
||||
@@ -23,4 +29,74 @@ describe('GeneratePropsH', () => {
|
||||
expect(generator.generate(fixtureName, fixture)).toMatchSnapshot();
|
||||
});
|
||||
});
|
||||
|
||||
it.each(['auto', 'box-none'])(
|
||||
'uses the declared string enum default %s for unknown values',
|
||||
defaultValue => {
|
||||
const header = generatePropsHeader({
|
||||
name: 'pointerEvents',
|
||||
optional: true,
|
||||
typeAnnotation: {
|
||||
type: 'StringEnumTypeAnnotation',
|
||||
default: defaultValue,
|
||||
options: ['none', 'auto', 'box-none'],
|
||||
},
|
||||
});
|
||||
const conversion = header.match(
|
||||
/static inline void fromRawValue[\s\S]*?\n}/,
|
||||
)?.[0];
|
||||
|
||||
expect(conversion).toContain(
|
||||
`result = TestComponentPointerEvents::${defaultValue === 'auto' ? 'Auto' : 'BoxNone'};\n}`,
|
||||
);
|
||||
expect(conversion).not.toContain('abort()');
|
||||
},
|
||||
);
|
||||
|
||||
it.each([0, 2])(
|
||||
'uses the declared integer enum default %s for unknown values',
|
||||
defaultValue => {
|
||||
const header = generatePropsHeader({
|
||||
name: 'mode',
|
||||
optional: true,
|
||||
typeAnnotation: {
|
||||
type: 'Int32EnumTypeAnnotation',
|
||||
default: defaultValue,
|
||||
options: [0, 1, 2],
|
||||
},
|
||||
});
|
||||
const conversion = header.match(
|
||||
/static inline void fromRawValue[\s\S]*?\n}/,
|
||||
)?.[0];
|
||||
|
||||
expect(conversion).toContain(
|
||||
`default:\n result = TestComponentMode::Mode${defaultValue};`,
|
||||
);
|
||||
expect(conversion).not.toContain('abort()');
|
||||
},
|
||||
);
|
||||
});
|
||||
|
||||
function generatePropsHeader(prop: NamedShape<PropTypeAnnotation>): string {
|
||||
const schema: SchemaType = {
|
||||
modules: {
|
||||
TestModule: {
|
||||
type: 'Component',
|
||||
components: {
|
||||
TestComponent: {
|
||||
extendsProps: [
|
||||
{
|
||||
type: 'ReactNativeBuiltInType',
|
||||
knownTypeName: 'ReactNativeCoreViewProps',
|
||||
},
|
||||
],
|
||||
events: [],
|
||||
props: [prop],
|
||||
commands: [],
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
};
|
||||
return generator.generate('TestModule', schema).get('Props.h') ?? '';
|
||||
}
|
||||
+14
-14
@@ -27,7 +27,7 @@ enum class AllPropStringEnumProp { Option1 };
|
||||
static inline void fromRawValue(const PropsParserContext& context, const RawValue &value, AllPropStringEnumProp &result) {
|
||||
auto string = (std::string)value;
|
||||
if (string == \\"option1\\") { result = AllPropStringEnumProp::Option1; return; }
|
||||
abort();
|
||||
result = AllPropStringEnumProp::Option1;
|
||||
}
|
||||
|
||||
static inline std::string toString(const AllPropStringEnumProp &value) {
|
||||
@@ -53,7 +53,7 @@ static inline void fromRawValue(const PropsParserContext& context, const RawValu
|
||||
result = AllPropIntEnumProp::IntEnumProp0;
|
||||
return;
|
||||
default:
|
||||
abort();
|
||||
result = AllPropIntEnumProp::IntEnumProp0;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -401,7 +401,7 @@ enum class AllPropWithOptionalGenStringEnumProp { Option1 };
|
||||
static inline void fromRawValue(const PropsParserContext& context, const RawValue &value, AllPropWithOptionalGenStringEnumProp &result) {
|
||||
auto string = (std::string)value;
|
||||
if (string == \\"option1\\") { result = AllPropWithOptionalGenStringEnumProp::Option1; return; }
|
||||
abort();
|
||||
result = AllPropWithOptionalGenStringEnumProp::Option1;
|
||||
}
|
||||
|
||||
static inline std::string toString(const AllPropWithOptionalGenStringEnumProp &value) {
|
||||
@@ -427,7 +427,7 @@ static inline void fromRawValue(const PropsParserContext& context, const RawValu
|
||||
result = AllPropWithOptionalGenIntEnumProp::IntEnumProp0;
|
||||
return;
|
||||
default:
|
||||
abort();
|
||||
result = AllPropWithOptionalGenIntEnumProp::IntEnumProp0;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -775,7 +775,7 @@ enum class AllPropWithOptionalObjectGenStringEnumProp { Option1 };
|
||||
static inline void fromRawValue(const PropsParserContext& context, const RawValue &value, AllPropWithOptionalObjectGenStringEnumProp &result) {
|
||||
auto string = (std::string)value;
|
||||
if (string == \\"option1\\") { result = AllPropWithOptionalObjectGenStringEnumProp::Option1; return; }
|
||||
abort();
|
||||
result = AllPropWithOptionalObjectGenStringEnumProp::Option1;
|
||||
}
|
||||
|
||||
static inline std::string toString(const AllPropWithOptionalObjectGenStringEnumProp &value) {
|
||||
@@ -801,7 +801,7 @@ static inline void fromRawValue(const PropsParserContext& context, const RawValu
|
||||
result = AllPropWithOptionalObjectGenIntEnumProp::IntEnumProp0;
|
||||
return;
|
||||
default:
|
||||
abort();
|
||||
result = AllPropWithOptionalObjectGenIntEnumProp::IntEnumProp0;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3013,7 +3013,7 @@ static inline void fromRawValue(const PropsParserContext& context, const RawValu
|
||||
result = Int32EnumPropsNativeComponentMaxInterval::MaxInterval2;
|
||||
return;
|
||||
default:
|
||||
abort();
|
||||
result = Int32EnumPropsNativeComponentMaxInterval::MaxInterval0;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3301,7 +3301,7 @@ enum class ObjectPropsStringEnumProp { Option1 };
|
||||
static inline void fromRawValue(const PropsParserContext& context, const RawValue &value, ObjectPropsStringEnumProp &result) {
|
||||
auto string = (std::string)value;
|
||||
if (string == \\"option1\\") { result = ObjectPropsStringEnumProp::Option1; return; }
|
||||
abort();
|
||||
result = ObjectPropsStringEnumProp::Option1;
|
||||
}
|
||||
|
||||
static inline std::string toString(const ObjectPropsStringEnumProp &value) {
|
||||
@@ -3327,7 +3327,7 @@ static inline void fromRawValue(const PropsParserContext& context, const RawValu
|
||||
result = ObjectPropsIntEnumProp::IntEnumProp0;
|
||||
return;
|
||||
default:
|
||||
abort();
|
||||
result = ObjectPropsIntEnumProp::IntEnumProp0;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3735,7 +3735,7 @@ enum class ObjectPropsWithOptionalGenStringEnumProp { Option1 };
|
||||
static inline void fromRawValue(const PropsParserContext& context, const RawValue &value, ObjectPropsWithOptionalGenStringEnumProp &result) {
|
||||
auto string = (std::string)value;
|
||||
if (string == \\"option1\\") { result = ObjectPropsWithOptionalGenStringEnumProp::Option1; return; }
|
||||
abort();
|
||||
result = ObjectPropsWithOptionalGenStringEnumProp::Option1;
|
||||
}
|
||||
|
||||
static inline std::string toString(const ObjectPropsWithOptionalGenStringEnumProp &value) {
|
||||
@@ -3761,7 +3761,7 @@ static inline void fromRawValue(const PropsParserContext& context, const RawValu
|
||||
result = ObjectPropsWithOptionalGenIntEnumProp::IntEnumProp0;
|
||||
return;
|
||||
default:
|
||||
abort();
|
||||
result = ObjectPropsWithOptionalGenIntEnumProp::IntEnumProp0;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4169,7 +4169,7 @@ enum class ObjectPropsWithOptionalObjectGenStringEnumProp { Option1 };
|
||||
static inline void fromRawValue(const PropsParserContext& context, const RawValue &value, ObjectPropsWithOptionalObjectGenStringEnumProp &result) {
|
||||
auto string = (std::string)value;
|
||||
if (string == \\"option1\\") { result = ObjectPropsWithOptionalObjectGenStringEnumProp::Option1; return; }
|
||||
abort();
|
||||
result = ObjectPropsWithOptionalObjectGenStringEnumProp::Option1;
|
||||
}
|
||||
|
||||
static inline std::string toString(const ObjectPropsWithOptionalObjectGenStringEnumProp &value) {
|
||||
@@ -4195,7 +4195,7 @@ static inline void fromRawValue(const PropsParserContext& context, const RawValu
|
||||
result = ObjectPropsWithOptionalObjectGenIntEnumProp::IntEnumProp0;
|
||||
return;
|
||||
default:
|
||||
abort();
|
||||
result = ObjectPropsWithOptionalObjectGenIntEnumProp::IntEnumProp0;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4651,7 +4651,7 @@ static inline void fromRawValue(const PropsParserContext& context, const RawValu
|
||||
if (string == \\"top\\") { result = StringEnumPropsNativeComponentAlignment::Top; return; }
|
||||
if (string == \\"center\\") { result = StringEnumPropsNativeComponentAlignment::Center; return; }
|
||||
if (string == \\"bottom-right\\") { result = StringEnumPropsNativeComponentAlignment::BottomRight; return; }
|
||||
abort();
|
||||
result = StringEnumPropsNativeComponentAlignment::Center;
|
||||
}
|
||||
|
||||
static inline std::string toString(const StringEnumPropsNativeComponentAlignment &value) {
|
||||
|
||||
+12
-1
@@ -7,6 +7,8 @@
|
||||
|
||||
package com.facebook.react.uimanager
|
||||
|
||||
import com.facebook.common.logging.FLog
|
||||
import com.facebook.react.common.ReactConstants
|
||||
import java.util.Locale
|
||||
|
||||
/**
|
||||
@@ -28,12 +30,21 @@ public enum class PointerEvents {
|
||||
|
||||
public companion object {
|
||||
|
||||
/**
|
||||
* Parses pointer events case-insensitively, treating hyphens as underscores. Null and invalid
|
||||
* values return [AUTO]; invalid values also log a warning.
|
||||
*/
|
||||
@JvmStatic
|
||||
public fun parsePointerEvents(pointerEventsStr: String?): PointerEvents =
|
||||
if (pointerEventsStr == null) {
|
||||
AUTO
|
||||
} else {
|
||||
PointerEvents.valueOf(pointerEventsStr.uppercase(Locale.US).replace("-", "_"))
|
||||
try {
|
||||
PointerEvents.valueOf(pointerEventsStr.uppercase(Locale.US).replace("-", "_"))
|
||||
} catch (e: IllegalArgumentException) {
|
||||
FLog.w(ReactConstants.TAG, "Invalid pointerEvents value: $pointerEventsStr")
|
||||
AUTO
|
||||
}
|
||||
}
|
||||
|
||||
@JvmStatic
|
||||
|
||||
+62
@@ -0,0 +1,62 @@
|
||||
/*
|
||||
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
||||
*
|
||||
* This source code is licensed under the MIT license found in the
|
||||
* LICENSE file in the root directory of this source tree.
|
||||
*/
|
||||
|
||||
package com.facebook.react.uimanager
|
||||
|
||||
import com.facebook.common.logging.FLog
|
||||
import com.facebook.react.common.ReactConstants
|
||||
import org.assertj.core.api.Assertions.assertThat
|
||||
import org.junit.Test
|
||||
import org.mockito.Mockito.mockStatic
|
||||
|
||||
class PointerEventsTest {
|
||||
|
||||
@Test
|
||||
fun parsePointerEvents_acceptsAllValuesAndNormalisationVariants() {
|
||||
val values =
|
||||
listOf(
|
||||
"auto" to PointerEvents.AUTO,
|
||||
"AUTO" to PointerEvents.AUTO,
|
||||
"AuTo" to PointerEvents.AUTO,
|
||||
"none" to PointerEvents.NONE,
|
||||
"NONE" to PointerEvents.NONE,
|
||||
"NoNe" to PointerEvents.NONE,
|
||||
"box-none" to PointerEvents.BOX_NONE,
|
||||
"BOX-NONE" to PointerEvents.BOX_NONE,
|
||||
"BoX-NoNe" to PointerEvents.BOX_NONE,
|
||||
"box_none" to PointerEvents.BOX_NONE,
|
||||
"BOX_NONE" to PointerEvents.BOX_NONE,
|
||||
"box-only" to PointerEvents.BOX_ONLY,
|
||||
"BOX-ONLY" to PointerEvents.BOX_ONLY,
|
||||
"BoX-OnLy" to PointerEvents.BOX_ONLY,
|
||||
"box_only" to PointerEvents.BOX_ONLY,
|
||||
"BOX_ONLY" to PointerEvents.BOX_ONLY,
|
||||
)
|
||||
for ((value, expected) in values) {
|
||||
assertThat(PointerEvents.parsePointerEvents(value)).describedAs(value).isEqualTo(expected)
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun parsePointerEvents_nullDefaultsToAutoWithoutWarning() {
|
||||
mockStatic(FLog::class.java).use { flog ->
|
||||
assertThat(PointerEvents.parsePointerEvents(null)).isEqualTo(PointerEvents.AUTO)
|
||||
flog.verifyNoInteractions()
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun parsePointerEvents_invalidValuesDefaultToAutoAndWarnWithOriginalValue() {
|
||||
mockStatic(FLog::class.java).use { flog ->
|
||||
for (value in listOf("", "BoX-InVaLiD", " auto ")) {
|
||||
assertThat(PointerEvents.parsePointerEvents(value)).isEqualTo(PointerEvents.AUTO)
|
||||
flog.verify { FLog.w(ReactConstants.TAG, "Invalid pointerEvents value: $value") }
|
||||
}
|
||||
flog.verifyNoMoreInteractions()
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user