A Civilization-style strategy game about governing Israel from 15 May 1948. Real ministers, real map, every fact sourced. Unity + deterministic C# sim.
Unity: flags after the URL fragment are ignored everywhere (Fixes #2265) (#2308)
Beriff55810b7383
diff --git a/changelog.d/2265-url-fragment.md b/changelog.d/2265-url-fragment.mdnew file mode 100644index 000000000..00b5e8830--- /dev/null+++ b/changelog.d/2265-url-fragment.md@@ -0,0 +1,3 @@+### Fixed++- A language, border-variant or companion flag in the part of the page URL after the `#` no longer counts; only the query is read, as for the other flags (Fixes #2265).diff --git a/unity/Assets/Scripts/Boot/UrlFlags.cs b/unity/Assets/Scripts/Boot/UrlFlags.csindex 24b10577d..0e22b5332 100644--- a/unity/Assets/Scripts/Boot/UrlFlags.cs+++ b/unity/Assets/Scripts/Boot/UrlFlags.cs@@ -12,7 +12,8 @@ namespace Statecraft.Game.Boot{public static bool Has(string? url, string name) => Regex.IsMatch(BeforeFragment(url), @"[?&]" + Regex.Escape(name) + @"(?=[=&]|$)");- static string BeforeFragment(string? url)+ /// <summary>The page address without its fragment: everything before the first '#'. The other URL parsers cut with it too (#2265).</summary>+ internal static string BeforeFragment(string? url){url ??= "";var hash = url.IndexOf('#');diff --git a/unity/Assets/Scripts/Map/MapBorders.cs b/unity/Assets/Scripts/Map/MapBorders.csindex f76cde1bf..918b95585 100644--- a/unity/Assets/Scripts/Map/MapBorders.cs+++ b/unity/Assets/Scripts/Map/MapBorders.cs@@ -191,7 +191,7 @@ namespace Statecraft.Game.Mapstring? v = null;if (webgl){- var m = System.Text.RegularExpressions.Regex.Match(url ?? "", @"[?&]bordervariant=((?i:legacy|[abc]2?))(?=[&#]|$)");+ var m = System.Text.RegularExpressions.Regex.Match(UrlFlags.BeforeFragment(url), @"[?&]bordervariant=((?i:legacy|[abc]2?))(?=[&#]|$)");if (m.Success) v = m.Groups[1].Value;}elsediff --git a/unity/Assets/Scripts/UI/UiLanguage.cs b/unity/Assets/Scripts/UI/UiLanguage.csindex c88a149ed..ce5d22801 100644--- a/unity/Assets/Scripts/UI/UiLanguage.cs+++ b/unity/Assets/Scripts/UI/UiLanguage.cs@@ -26,13 +26,13 @@ namespace Statecraft.Game.UI/// link) holds for that visit, over the choice <paramref name="stored"/> in Settings; with neither, English.</summary>public static string AtStart(string? stored, string? url, string[] args, bool webgl){- bool flag = webgl ? AnyQuery.IsMatch(url ?? "") : Array.IndexOf(args, "-" + Flag) >= 0;+ bool flag = webgl ? AnyQuery.IsMatch(Boot.UrlFlags.BeforeFragment(url)) : Array.IndexOf(args, "-" + Flag) >= 0;return flag ? Requested(url, args, webgl) : stored == Hebrew ? Hebrew : English;}public static string Requested(string? url, string[] args, bool webgl){- if (webgl) return HebrewQuery.IsMatch(url ?? "") ? Hebrew : English;+ if (webgl) return HebrewQuery.IsMatch(Boot.UrlFlags.BeforeFragment(url)) ? Hebrew : English;var i = Array.IndexOf(args, "-" + Flag);return i >= 0 && i + 1 < args.Length && args[i + 1] == Hebrew ? Hebrew : English;}diff --git a/unity/Assets/Scripts/UI/WebCompanion.cs b/unity/Assets/Scripts/UI/WebCompanion.csindex b21593366..a7e99af56 100644--- a/unity/Assets/Scripts/UI/WebCompanion.cs+++ b/unity/Assets/Scripts/UI/WebCompanion.cs@@ -57,7 +57,7 @@ namespace Statecraft.Game.UI/// "on") or "?companion=1" in the page address turns it on. The address wins over a stored "off" for that visit and/// writes nothing: the owner's ruling of 2 November 2026, "Link wins, that visit (Recommended)". None of them counts/// while the build does not offer it (<see cref="Available"/>).</summary>- public static bool On(bool? picked, string? stored, string? url) => Offered() && (picked ?? (stored == "on" || Asked.IsMatch(url ?? "")));+ public static bool On(bool? picked, string? stored, string? url) => Offered() && (picked ?? (stored == "on" || Asked.IsMatch(Boot.UrlFlags.BeforeFragment(url))));readonly VisualElement root;readonly UiText text;diff --git a/unity/Assets/Tests/EditMode/BorderCivTests.cs b/unity/Assets/Tests/EditMode/BorderCivTests.csindex 8952996a4..80b1bad85 100644--- a/unity/Assets/Tests/EditMode/BorderCivTests.cs+++ b/unity/Assets/Tests/EditMode/BorderCivTests.cs@@ -138,6 +138,10 @@ public class BorderCivTestsAssert.AreEqual(BorderVariant.Legacy, MapBorders.VariantRequested("https://x.invalid/?bordervariant=legacy", new string[0], webgl: true));Assert.AreEqual(BorderVariant.Legacy, MapBorders.VariantRequested("https://x.invalid/?golden&bordervariant=Legacy#x", new string[0], webgl: true));Assert.AreEqual(BorderVariant.Legacy, MapBorders.VariantRequested(null, new[] { "Statecraft", "-bordervariant", "legacy" }, webgl: false));+ // #2265: only the query is read; a flag in the fragment is not a query parameter.+ Assert.AreEqual(BorderVariant.A2, MapBorders.VariantRequested("https://x.invalid/?x=1#&bordervariant=legacy", new string[0], webgl: true));+ Assert.AreEqual(BorderVariant.A2, MapBorders.VariantRequested("https://x.invalid/#?bordervariant=b", new string[0], webgl: true));+ Assert.AreEqual(BorderVariant.B, MapBorders.VariantRequested("https://x.invalid/?bordervariant=b#&bordervariant=c", new string[0], webgl: true));Assert.AreEqual(BorderVariant.A, MapBorders.VariantRequested("https://x.invalid/?bordervariant=a", new string[0], webgl: true));Assert.AreEqual(BorderVariant.B, MapBorders.VariantRequested("https://x.invalid/?golden&bordervariant=b", new string[0], webgl: true));Assert.AreEqual(BorderVariant.C, MapBorders.VariantRequested("https://x.invalid/?bordervariant=C#x", new string[0], webgl: true));diff --git a/unity/Assets/Tests/EditMode/HebrewUiTests.cs b/unity/Assets/Tests/EditMode/HebrewUiTests.csindex a5e01ea5e..56fd7b98f 100644--- a/unity/Assets/Tests/EditMode/HebrewUiTests.cs+++ b/unity/Assets/Tests/EditMode/HebrewUiTests.cs@@ -40,6 +40,21 @@ public class HebrewUiTests// --- The flag ---------------------------------------------------------------------------------------------------+ // #2265: only the query is read; a flag in the fragment (after the first '#') is not a query parameter.+ [Test]+ public void ALanguageFlagAfterTheFragmentIsIgnored()+ {+ var none = new string[0];+ foreach (var q in new[] { "?x=1#&lang=he", "#?lang=he" })+ Assert.AreEqual(UiLanguage.English, UiLanguage.Requested("http://localhost:8000/index.html" + q, none, webgl: true), q);+ // A fragment-only flag is no flag: the stored choice stands, in both directions.+ Assert.AreEqual(UiLanguage.Hebrew, UiLanguage.AtStart(UiLanguage.Hebrew, "http://localhost:8000/index.html#&lang=en", none, webgl: true));+ Assert.AreEqual(UiLanguage.English, UiLanguage.AtStart(null, "http://localhost:8000/index.html?x=1#?lang=he", none, webgl: true));+ // The query still counts when a fragment follows.+ Assert.AreEqual(UiLanguage.Hebrew, UiLanguage.Requested("http://localhost:8000/index.html?lang=he#x", none, webgl: true));+ Assert.AreEqual(UiLanguage.English, UiLanguage.AtStart(UiLanguage.Hebrew, "http://localhost:8000/index.html?lang=en#x", none, webgl: true));+ }+[Test]public void HebrewIsOnlyAskedForByHand(){diff --git a/unity/Assets/Tests/EditMode/WebCompanionTests.cs b/unity/Assets/Tests/EditMode/WebCompanionTests.csindex 0852fd145..43243b016 100644--- a/unity/Assets/Tests/EditMode/WebCompanionTests.cs+++ b/unity/Assets/Tests/EditMode/WebCompanionTests.cs@@ -301,6 +301,9 @@ public sealed class WebCompanionTests[TestCase(null, null, "https://example.org/statecraft/index.html?companion=1", true)][TestCase(null, null, "https://example.org/statecraft/index.html?lang=he&companion=1#x", true)][TestCase(null, "off", "https://example.org/statecraft/index.html?companion=1", true)]+ [TestCase(null, null, "https://example.org/statecraft/index.html?x=1#&companion=1", false)]+ [TestCase(null, null, "https://example.org/statecraft/index.html#?companion=1", false)]+ [TestCase(null, null, "https://example.org/statecraft/index.html?companion=1#&companion=0", true)][TestCase(null, null, "https://example.org/statecraft/index.html?companion=0", false)][TestCase(null, null, "https://example.org/statecraft/index.html?companion", false)][TestCase(null, null, "https://example.org/statecraft/index.html?companion=10", false)]