Skip to content
Merged
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
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