Skip to content

Commit

Permalink
Merge pull request #100 from xiaohai-huang/main
Browse files Browse the repository at this point in the history
Fixed URI handling functions does not match JS spec
  • Loading branch information
KurtGokhan authored Nov 11, 2023
2 parents 6e05e71 + 2a4b3ff commit 3072bc4
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 24 deletions.
25 changes: 4 additions & 21 deletions Runtime/Scripting/DomProxies/EncodingHelpers.cs
Original file line number Diff line number Diff line change
@@ -1,44 +1,27 @@
using System;
using System.Reflection;
using UnityEngine.Networking;

namespace ReactUnity
{
public static class EncodingHelpers
{
private static Type WWWTranscoder;
private static MethodInfo URLEncode;
private static MethodInfo URLDecode;
private static MethodInfo DataEncode;
private static MethodInfo DataDecode;

static EncodingHelpers()
{
WWWTranscoder = typeof(UnityWebRequest).Assembly?.GetType("UnityEngine.WWWTranscoder");
}

public static string encodeURI(string input)
{
URLEncode = URLEncode ?? WWWTranscoder?.GetMethod("URLEncode", new Type[] { typeof(string) });
return URLEncode?.Invoke(null, new object[] { input }) as string ?? input;
return Uri.EscapeUriString(input);
}

public static string decodeURI(string input)
{
URLDecode = URLDecode ?? WWWTranscoder?.GetMethod("URLDecode", new Type[] { typeof(string) });
return URLDecode?.Invoke(null, new object[] { input }) as string ?? input;
return Uri.UnescapeDataString(input);
}

public static string encodeURIComponent(string input)
{
DataEncode = DataEncode ?? WWWTranscoder?.GetMethod("DataEncode", new Type[] { typeof(string) });
return DataEncode?.Invoke(null, new object[] { input }) as string ?? input;
return Uri.EscapeDataString(input);
}

public static string decodeURIComponent(string input)
{
DataDecode = DataDecode ?? WWWTranscoder?.GetMethod("DataDecode", new Type[] { typeof(string) });
return DataDecode?.Invoke(null, new object[] { input }) as string ?? input;
return Uri.UnescapeDataString(input);
}
}
}
6 changes: 3 additions & 3 deletions Tests/Editor/Renderer/MiscTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public IEnumerator EncodingHelpersWork()
yield return null;

Assert.AreEqual(
"bla%26bla%26bla",
"bla&bla&bla",
Context.Script.EvaluateScript(@"encodeURI('bla&bla&bla')")
);

Expand All @@ -40,13 +40,13 @@ public IEnumerator EncodingHelpersWork()


Assert.AreEqual(
"%5c%25+%2b",
"%5C%25%20+",
Context.Script.EvaluateScript(@"encodeURI('\\% +')")
);


Assert.AreEqual(
"%5c%25%20%2b",
"%5C%25%20%2B",
Context.Script.EvaluateScript(@"encodeURIComponent('\\% +')")
);

Expand Down

0 comments on commit 3072bc4

Please sign in to comment.