diff --git a/API/Context.cs b/API/Context.cs
index 490b137..406674b 100644
--- a/API/Context.cs
+++ b/API/Context.cs
@@ -25,7 +25,7 @@ public object RunFile(string filepath)
throw new Exception(String.Format("File does not exist {0}.", filepath));
}
// Read file
- string script = File.ReadAllText(file.FullName);
+ string script = File.ReadAllText(file.FullName, Context.Encoding ?? Encoding.UTF8);
// Execute file
return RunScript(script, file.Name);
}
@@ -75,6 +75,12 @@ public static Context Current
get { return Program.Context; }
}
+ ///
+ /// Encoding for reading script files
+ ///
+ ///
+ public static Encoding Encoding { get; set; }
+
///
/// Handles an exception
///
diff --git a/API/Phantom.cs b/API/Phantom.cs
index 8491c30..50b29c6 100644
--- a/API/Phantom.cs
+++ b/API/Phantom.cs
@@ -2,6 +2,7 @@
using System.Net;
using System.Collections.Generic;
using System.Text;
+using System.Text.RegularExpressions;
using System.IO;
namespace TrifleJS.API
@@ -132,12 +133,52 @@ public static string outputEncoding {
{
try
{
- System.Console.OutputEncoding = Encoding.GetEncoding(value);
+ string encoding = SanitizeEncoding(value);
+ System.Console.OutputEncoding = Encoding.GetEncoding(encoding);
}
- catch {
+ catch
+ {
Console.error(String.Format("Unknown Encoding '{0}'", value));
- };
+ }
+ }
+ }
+
+ ///
+ /// Note that windows uses "UTF-8" instead of "UTF8"
+ /// so we have to sanitize these strings
+ ///
+ internal static string scriptEncoding
+ {
+ get { return Context.Encoding.WebName.ToUpper(); }
+ set
+ {
+ try
+ {
+ string encoding = SanitizeEncoding(value);
+ Context.Encoding = Encoding.GetEncoding(encoding);
+ }
+ catch
+ {
+ Console.error(String.Format("Unknown Encoding '{0}'", value));
+ }
+ }
+ }
+
+ ///
+ /// Note that windows uses "UTF-8" instead of "UTF8"
+ /// so we have to sanitize these strings
+ ///
+ internal static string SanitizeEncoding(string encoding)
+ {
+ if (encoding != null)
+ {
+ if (encoding.IndexOf("utf", StringComparison.InvariantCultureIgnoreCase) == 0
+ && !encoding.Contains("-"))
+ {
+ return Regex.Replace(encoding, "utf", "UTF-", RegexOptions.IgnoreCase);
+ }
}
+ return encoding;
}
#region Cookies
diff --git a/Program.cs b/Program.cs
index 8ce67d4..80962ab 100644
--- a/Program.cs
+++ b/Program.cs
@@ -106,6 +106,12 @@ static void Main(string[] args)
case "--emulate":
isVersionSet = Browser.Emulate(arg.Replace("--emulate=", "").ToUpper());
break;
+ case "--output-encoding":
+ API.Phantom.outputEncoding = arg.Replace("--output-encoding=", "");
+ break;
+ case "--script-encoding":
+ API.Phantom.scriptEncoding = arg.Replace("--script-encoding=", "");
+ break;
case "--proxy":
Proxy.server = arg.Replace("--proxy=", "");
break;