Skip to content

Commit

Permalink
Fix unescape at end of string (#831)
Browse files Browse the repository at this point in the history
  • Loading branch information
lahma committed Jan 27, 2021
1 parent 238c308 commit 5567920
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 4 deletions.
15 changes: 15 additions & 0 deletions Jint.Tests.Test262/BuiltIns/AnnexB/EscapeTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using Xunit;

namespace Jint.Tests.Test262.BuiltIns.AnnexB
{
public class EscapeTests : Test262Test
{
[Theory(DisplayName = "annexB\\built-ins\\escape")]
[MemberData(nameof(SourceFiles), "annexB\\built-ins\\escape", false)]
[MemberData(nameof(SourceFiles), "annexB\\built-ins\\escape", true, Skip = "Skipped")]
protected void RunTest(SourceFile sourceFile)
{
RunTestInternal(sourceFile);
}
}
}
15 changes: 15 additions & 0 deletions Jint.Tests.Test262/BuiltIns/AnnexB/UnescapeTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using Xunit;

namespace Jint.Tests.Test262.BuiltIns.AnnexB
{
public class UnescapeTests : Test262Test
{
[Theory(DisplayName = "annexB\\built-ins\\unescape")]
[MemberData(nameof(SourceFiles), "annexB\\built-ins\\unescape", false)]
[MemberData(nameof(SourceFiles), "annexB\\built-ins\\unescape", true, Skip = "Skipped")]
protected void RunTest(SourceFile sourceFile)
{
RunTestInternal(sourceFile);
}
}
}
19 changes: 19 additions & 0 deletions Jint.Tests/Runtime/GlobalTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using Xunit;

namespace Jint.Tests.Runtime
{
public class GlobalTests
{
[Fact]
public void UnescapeAtEndOfString()
{
var e = new Engine();

Assert.Equal("@", e.Execute("unescape('%40');").GetCompletionValue().AsString());
Assert.Equal("@_", e.Execute("unescape('%40_');").GetCompletionValue().AsString());
Assert.Equal("@@", e.Execute("unescape('%40%40');").GetCompletionValue().AsString());
Assert.Equal("@", e.Execute("unescape('%u0040');").GetCompletionValue().AsString());
Assert.Equal("@@", e.Execute("unescape('%u0040%u0040');").GetCompletionValue().AsString());
}
}
}
8 changes: 4 additions & 4 deletions Jint/Native/Global/GlobalObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ protected override void Initialize()
["decodeURIComponent"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "decodeURIComponent", DecodeUriComponent, 1, lengthFlags), propertyFlags),
["encodeURI"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "encodeURI", EncodeUri, 1, lengthFlags), propertyFlags),
["encodeURIComponent"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "encodeURIComponent", EncodeUriComponent, 1, lengthFlags), propertyFlags),
["escape"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "escape", Escape, 1), propertyFlags),
["unescape"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "unescape", Unescape, 1), propertyFlags),
["escape"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "escape", Escape, 1, lengthFlags), propertyFlags),
["unescape"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "unescape", Unescape, 1, lengthFlags), propertyFlags),
["globalThis"] = new PropertyDescriptor(this, propertyFlags),

// toString is not mentioned or actually required in spec, but some tests rely on it
Expand Down Expand Up @@ -679,7 +679,7 @@ public JsValue Unescape(JsValue thisObject, JsValue[] arguments)
var c = uriString[k];
if (c == '%')
{
if (k < strLen - 6
if (k <= strLen - 6
&& uriString[k + 1] == 'u'
&& uriString.Skip(k + 2).Take(4).All(IsValidHexaChar))
{
Expand All @@ -689,7 +689,7 @@ public JsValue Unescape(JsValue thisObject, JsValue[] arguments)

k += 5;
}
else if (k < strLen - 3
else if (k <= strLen - 3
&& uriString.Skip(k + 1).Take(2).All(IsValidHexaChar))
{
c = (char)int.Parse(
Expand Down

0 comments on commit 5567920

Please sign in to comment.