Add JavaScript bindings for Errata API #1260

Closed
NickGerleman wants to merge 4 commits from export-D45297215 into main
9 changed files with 99 additions and 6 deletions

View File

@@ -175,6 +175,7 @@ static YGConfigRef globalConfig;
globalConfig = YGConfigNew(); globalConfig = YGConfigNew();
YGConfigSetExperimentalFeatureEnabled( YGConfigSetExperimentalFeatureEnabled(
globalConfig, YGExperimentalFeatureWebFlexBasis, true); globalConfig, YGExperimentalFeatureWebFlexBasis, true);
YGConfigSetErrata(globalConfig, YGErrataClassic);
YGConfigSetPointScaleFactor(globalConfig, [UIScreen mainScreen].scale); YGConfigSetPointScaleFactor(globalConfig, [UIScreen mainScreen].scale);
} }

View File

@@ -73,6 +73,12 @@ namespace Facebook.Yoga
[DllImport(DllName, ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)] [DllImport(DllName, ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]
public static extern bool YGConfigGetUseLegacyStretchBehaviour(YGConfigHandle config); public static extern bool YGConfigGetUseLegacyStretchBehaviour(YGConfigHandle config);
[DllImport(DllName, ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]
public static extern void YGConfigSetErrata(YGConfigHandle config, YogaErrata errata);
[DllImport(DllName, ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]
public static extern YogaErrata YGConfigGetErrata(YGConfigHandle config);
[DllImport(DllName, ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)] [DllImport(DllName, ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]
public static extern void YGConfigSetPointScaleFactor( public static extern void YGConfigSetPointScaleFactor(
YGConfigHandle config, YGConfigHandle config,

View File

@@ -117,8 +117,12 @@ namespace Facebook.Yoga
} }
} }
public bool UseLegacyStretchBehaviour [ObsoleteAttribute("\"UseLegacyStretchBehaviour\" will be removed in the next release. " +
{ "Usage should be replaced with \"Errata\" set to \"YogaErrata.All\" to opt out of all " +
"future breaking conformance fixes, or \"YogaErrata.StretchFlexBasis\" toopt out of " +
"the specific conformance fix previously disabled by \"UseLegacyStretchBehaviour\".",
true /*error*/)]
public bool UseLegacyStretchBehaviour {
get get
{ {
return Native.YGConfigGetUseLegacyStretchBehaviour(_ygConfig); return Native.YGConfigGetUseLegacyStretchBehaviour(_ygConfig);
@@ -130,6 +134,19 @@ namespace Facebook.Yoga
} }
} }
public YogaErrata Errata
{
get
{
return Native.YGConfigGetErrata(_ygConfig);
}
set
{
Native.YGConfigSetErrata(_ygConfig, value);
}
}
public float PointScaleFactor public float PointScaleFactor
{ {
set set

View File

@@ -22,7 +22,13 @@ public abstract class YogaConfig {
* Yoga previously had an error where containers would take the maximum space possible instead of the minimum * Yoga previously had an error where containers would take the maximum space possible instead of the minimum
* like they are supposed to. In practice this resulted in implicit behaviour similar to align-self: stretch; * like they are supposed to. In practice this resulted in implicit behaviour similar to align-self: stretch;
* Because this was such a long-standing bug we must allow legacy users to switch back to this behaviour. * Because this was such a long-standing bug we must allow legacy users to switch back to this behaviour.
*
* @deprecated "setUseLegacyStretchBehaviour" will be removed in the next release. Usage should be replaced with
* "setErrata(YogaErrata.ALL)" to opt out of all future breaking conformance fixes, or
* "setErrata(YogaErrata.STRETCH_FLEX_BASIS)" to opt out of the specific conformance fix previously disabled by
* "UseLegacyStretchBehaviour".
*/ */
@Deprecated
public abstract void setUseLegacyStretchBehaviour(boolean useLegacyStretchBehaviour); public abstract void setUseLegacyStretchBehaviour(boolean useLegacyStretchBehaviour);
public abstract void setErrata(YogaErrata errata); public abstract void setErrata(YogaErrata errata);

View File

@@ -12,6 +12,7 @@ import type {
Direction, Direction,
Display, Display,
Edge, Edge,
Errata,
ExperimentalFeature, ExperimentalFeature,
FlexDirection, FlexDirection,
Gutter, Gutter,
@@ -52,8 +53,20 @@ export type Config = {
enabled: boolean enabled: boolean
): void; ): void;
setPointScaleFactor(factor: number): void; setPointScaleFactor(factor: number): void;
/**
* @deprecated Please use "getErrata()"
*/
useLegacyStretchBehaviour(): boolean; useLegacyStretchBehaviour(): boolean;
/**
* @deprecated "setUseLegacyStretchBehaviour" will be removed in the next
* release. Usage should be replaced with "setErrata(ERRATA_ALL)" to opt out
* of all future breaking conformance fixes, or
* "setErrata(ERRATA_STRETCH_FLEX_BASIS)" to opt out of the specific
* conformance fix previously disabled by "UseLegacyStretchBehaviour".
*/
setUseLegacyStretchBehaviour(useLegacyStretchBehaviour: boolean): void; setUseLegacyStretchBehaviour(useLegacyStretchBehaviour: boolean): void;
getErrata(): Errata,
setErrata(errata: Errata): void,
useWebDefaults(): boolean; useWebDefaults(): boolean;
setUseWebDefaults(useWebDefaults: boolean): void; setUseWebDefaults(useWebDefaults: boolean): void;
}; };

View File

@@ -36,6 +36,10 @@ void Config::setUseLegacyStretchBehaviour(bool useLegacyStretchBehaviour) {
YGConfigSetUseLegacyStretchBehaviour(m_config, useLegacyStretchBehaviour); YGConfigSetUseLegacyStretchBehaviour(m_config, useLegacyStretchBehaviour);
} }
void Config::setErrata(int errata) {
YGConfigSetErrata(m_config, static_cast<YGErrata>(errata));
}
void Config::setUseWebDefaults(bool useWebDefaults) { void Config::setUseWebDefaults(bool useWebDefaults) {
YGConfigSetUseWebDefaults(m_config, useWebDefaults); YGConfigSetUseWebDefaults(m_config, useWebDefaults);
} }
@@ -45,10 +49,14 @@ bool Config::isExperimentalFeatureEnabled(int feature) const {
m_config, static_cast<YGExperimentalFeature>(feature)); m_config, static_cast<YGExperimentalFeature>(feature));
} }
bool Config::useLegacyStretchBehaviour() { bool Config::useLegacyStretchBehaviour() const {
return YGConfigGetUseLegacyStretchBehaviour(m_config); return YGConfigGetUseLegacyStretchBehaviour(m_config);
} }
bool Config::useWebDefaults() { int Config::getErrata() const {
return static_cast<int>(YGConfigGetErrata(m_config));
}
bool Config::useWebDefaults() const {
return YGConfigGetUseWebDefaults(m_config); return YGConfigGetUseWebDefaults(m_config);
} }

View File

@@ -33,12 +33,14 @@ public: // Setters
void setExperimentalFeatureEnabled(int feature, bool enabled); void setExperimentalFeatureEnabled(int feature, bool enabled);
void setPointScaleFactor(float pixelsInPoint); void setPointScaleFactor(float pixelsInPoint);
void setUseLegacyStretchBehaviour(bool useLegacyStretchBehaviour); void setUseLegacyStretchBehaviour(bool useLegacyStretchBehaviour);
void setErrata(int errata);
void setUseWebDefaults(bool useWebDefaults); void setUseWebDefaults(bool useWebDefaults);
public: // Getters public: // Getters
bool isExperimentalFeatureEnabled(int feature) const; bool isExperimentalFeatureEnabled(int feature) const;
bool useLegacyStretchBehaviour(); bool useLegacyStretchBehaviour() const;
bool useWebDefaults(); int getErrata() const;
bool useWebDefaults() const;
private: private:
YGConfigRef m_config; YGConfigRef m_config;

View File

@@ -34,10 +34,12 @@ EMSCRIPTEN_BINDINGS(YOGA_LAYOUT) {
.function("setPointScaleFactor", &Config::setPointScaleFactor) .function("setPointScaleFactor", &Config::setPointScaleFactor)
.function( .function(
"setUseLegacyStretchBehaviour", &Config::setUseLegacyStretchBehaviour) "setUseLegacyStretchBehaviour", &Config::setUseLegacyStretchBehaviour)
.function("setErrata", &Config::setErrata)
.function("setUseWebDefaults", &Config::setUseWebDefaults) .function("setUseWebDefaults", &Config::setUseWebDefaults)
.function( .function(
"isExperimentalFeatureEnabled", &Config::isExperimentalFeatureEnabled) "isExperimentalFeatureEnabled", &Config::isExperimentalFeatureEnabled)
.function("useLegacyStretchBehaviour", &Config::useLegacyStretchBehaviour) .function("useLegacyStretchBehaviour", &Config::useLegacyStretchBehaviour)
.function("getErrata", &Config::getErrata)
.function("useWebDefaults", &Config::useWebDefaults); .function("useWebDefaults", &Config::useWebDefaults);
value_object<Layout>("Layout") value_object<Layout>("Layout")

View File

@@ -0,0 +1,38 @@
/**
* 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.
*/
test("errata_all_contains_example_errata", () => {
const config = Yoga.Config.create();
config.setErrata(Yoga.ERRATA_ALL);
expect(config.getErrata()).toBe(Yoga.ERRATA_ALL);
expect(config.getErrata() & Yoga.ERRATA_STRETCH_FLEX_BASIS).not.toBe(0);
config.free();
});
test("errata_none_omits_example_errata", () => {
const config = Yoga.Config.create();
config.setErrata(Yoga.ERRATA_NONE);
expect(config.getErrata()).toBe(Yoga.ERRATA_NONE);
expect(config.getErrata() & Yoga.ERRATA_STRETCH_FLEX_BASIS).toBe(0);
config.free();
});
test("errata_is_settable", () => {
const config = Yoga.Config.create();
config.setErrata(Yoga.ERRATA_ALL);
expect(config.getErrata()).toBe(Yoga.ERRATA_ALL);
config.setErrata(Yoga.ERRATA_NONE);
expect(config.getErrata()).toBe(Yoga.ERRATA_NONE);
config.free();
});