Skip to content

Commit

Permalink
Correct bugs in JavaScript string escaping. Add JavaScriptConvertTest.
Browse files Browse the repository at this point in the history
  • Loading branch information
JamesNK committed Sep 10, 2007
1 parent e65bc2b commit 487dc81
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 6 deletions.
71 changes: 71 additions & 0 deletions Src/Newtonsoft.Json.Tests/JavaScriptConvertTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
using System;
using System.Collections.Generic;
using System.Text;
using Newtonsoft.Json.Utilities;
using NUnit.Framework;

namespace Newtonsoft.Json.Tests
{
[TestFixture]
public class JavaScriptConvertTest
{
[Test]
public void EscapeJavaScriptString()
{
string result;

result = JavaScriptUtils.ToEscapedJavaScriptString("How now brown cow?", '"', true);
Assert.AreEqual(@"""How now brown cow?""", result);

result = JavaScriptUtils.ToEscapedJavaScriptString("How now 'brown' cow?", '"', true);
Assert.AreEqual(@"""How now 'brown' cow?""", result);

result = JavaScriptUtils.ToEscapedJavaScriptString("How now <brown> cow?", '"', true);
Assert.AreEqual(@"""How now <brown> cow?""", result);

result = JavaScriptUtils.ToEscapedJavaScriptString(@"How
now brown cow?", '"', true);
Assert.AreEqual(@"""How \r\nnow brown cow?""", result);

result = JavaScriptUtils.ToEscapedJavaScriptString("\u0000\u0001\u0002\u0003\u0004\u0005\u0006\u0007", '"', true);
Assert.AreEqual(@"""\u0000\u0001\u0002\u0003\u0004\u0005\u0006\u0007""", result);

result =
JavaScriptUtils.ToEscapedJavaScriptString("\b\t\n\u000b\f\r\u000e\u000f\u0010\u0011\u0012\u0013", '"', true);
Assert.AreEqual(@"""\b\t\n\u000b\f\r\u000e\u000f\u0010\u0011\u0012\u0013""", result);

result =
JavaScriptUtils.ToEscapedJavaScriptString(
"\u0014\u0015\u0016\u0017\u0018\u0019\u001a\u001b\u001c\u001d\u001e\u001f ", '"', true);
Assert.AreEqual(@"""\u0014\u0015\u0016\u0017\u0018\u0019\u001a\u001b\u001c\u001d\u001e\u001f """, result);

result =
JavaScriptUtils.ToEscapedJavaScriptString(
"!\"#$%&\u0027()*+,-./0123456789:;\u003c=\u003e?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]", '"', true);
Assert.AreEqual(@"""!\""#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]""", result);

result = JavaScriptUtils.ToEscapedJavaScriptString("^_`abcdefghijklmnopqrstuvwxyz{|}~", '"', true);
Assert.AreEqual(@"""^_`abcdefghijklmnopqrstuvwxyz{|}~""", result);

string data =
"\u0000\u0001\u0002\u0003\u0004\u0005\u0006\u0007\b\t\n\u000b\f\r\u000e\u000f\u0010\u0011\u0012\u0013\u0014\u0015\u0016\u0017\u0018\u0019\u001a\u001b\u001c\u001d\u001e\u001f !\"#$%&\u0027()*+,-./0123456789:;\u003c=\u003e?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~";
string expected =
@"""\u0000\u0001\u0002\u0003\u0004\u0005\u0006\u0007\b\t\n\u000b\f\r\u000e\u000f\u0010\u0011\u0012\u0013\u0014\u0015\u0016\u0017\u0018\u0019\u001a\u001b\u001c\u001d\u001e\u001f !\""#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~""";

result = JavaScriptUtils.ToEscapedJavaScriptString(data, '"', true);
Assert.AreEqual(expected, result);

result = JavaScriptUtils.ToEscapedJavaScriptString("Fred's cat.", '\'', true);
Assert.AreEqual(result, @"'Fred\'s cat.'");

result = JavaScriptUtils.ToEscapedJavaScriptString(@"""How are you gentlemen?"" said Cats.", '"', true);
Assert.AreEqual(result, @"""\""How are you gentlemen?\"" said Cats.""");

result = JavaScriptUtils.ToEscapedJavaScriptString(@"""How are' you gentlemen?"" said Cats.", '"', true);
Assert.AreEqual(result, @"""\""How are' you gentlemen?\"" said Cats.""");

result = JavaScriptUtils.ToEscapedJavaScriptString(@"Fred's ""cat"".", '\'', true);
Assert.AreEqual(result, @"'Fred\'s ""cat"".'");
}
}
}
20 changes: 20 additions & 0 deletions Src/Newtonsoft.Json.Tests/JsonSerializerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -498,6 +498,26 @@ public void ReadOnlyCollectionSerialize()
CollectionAssert.AreEqual(r1, r2);
}

public class Person
{
private Guid _internalId;
private string _firstName;

[JsonIgnore]
public Guid InternalId
{
get { return _internalId; }
set { _internalId = value; }
}

[JsonProperty("first_name")]
public string FirstName
{
get { return _firstName; }
set { _firstName = value; }
}
}

//public abstract class Foo
//{
//}
Expand Down
1 change: 1 addition & 0 deletions Src/Newtonsoft.Json.Tests/Newtonsoft.Json.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
</ItemGroup>
<ItemGroup>
<Compile Include="AspNetAjaxDateTimeConverterTest.cs" />
<Compile Include="JavaScriptConvertTest.cs" />
<Compile Include="JsonReaderTest.cs" />
<Compile Include="JsonSerializerTest.cs" />
<Compile Include="JsonWriterTest.cs" />
Expand Down
1 change: 1 addition & 0 deletions Src/Newtonsoft.Json/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

[assembly: InternalsVisibleTo("Newtonsoft.Json.Tests")]
[assembly: AllowPartiallyTrustedCallers]

// Setting ComVisible to false makes the types in this assembly not visible
Expand Down
12 changes: 6 additions & 6 deletions Src/Newtonsoft.Json/Utilities/JavaScriptUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,13 @@ public static void WriteEscapedJavaScriptChar(TextWriter writer, char c, char de
//case '\'':
// StringUtils.WriteCharAsUnicode(writer, c);
// break;
case '\'':
// only escape if this charater is being used as the delimiter
writer.Write((delimiter == '\'') ? @"\'" : @"'");
break;
case '"':
// only escape if this charater is being used as the delimiter
writer.Write((delimiter == '"') ? "\\\"" : null);
writer.Write((delimiter == '"') ? "\\\"" : @"""");
break;
default:
if (c > '\u001f')
Expand Down Expand Up @@ -130,8 +134,4 @@ public static string ToEscapedJavaScriptString(string value, char delimiter, boo
}
}
}
}




}

0 comments on commit 487dc81

Please sign in to comment.