Skip to content

Commit

Permalink
Merge pull request #1711 from riganti/fix/tostring-global-function
Browse files Browse the repository at this point in the history
Fixed string conversion bug with global function
  • Loading branch information
tomasherceg authored Oct 22, 2023
2 parents c5ef35b + 4a8c020 commit 09d893c
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -790,7 +790,7 @@ JsExpression wrapInRound(JsExpression a) =>
}
else if (p[0].ParameterType == typeof(string) || p[0].ParameterType == typeof(bool))
{
AddMethodTranslator(m, translator: new GenericMethodCompiler(args => wrapInRound(new JsIdentifierExpression("Number").Invoke(args[1]))));
AddMethodTranslator(m, translator: new GenericMethodCompiler(args => wrapInRound(new JsIdentifierExpression("window").Member("Number").Invoke(args[1]))));
}
}

Expand All @@ -803,7 +803,7 @@ JsExpression wrapInRound(JsExpression a) =>
continue;
if (p[0].ParameterType.IsNumericType() && p[0].ParameterType != typeof(char))
{
AddMethodTranslator(m, translator: new GenericMethodCompiler(args => new JsIdentifierExpression("Boolean").Invoke(args[1])));
AddMethodTranslator(m, translator: new GenericMethodCompiler(args => new JsIdentifierExpression("window").Member("Boolean").Invoke(args[1])));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ internal static bool CanBeNull(Expression expr)
if (isNullable)
js = new JsBinaryExpression(js, BinaryOperatorType.NullishCoalescing, new JsLiteral(""));
if (!isStringAlready)
js = new JsIdentifierExpression("String").Invoke(js);
js = new JsIdentifierExpression("window").Member("String").Invoke(js);
return js;
}
}
Expand Down
1 change: 1 addition & 0 deletions src/Samples/Common/DotVVM.Samples.Common.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@
<None Remove="Views\FeatureSamples\CustomPrimitiveTypes\RouteLink.dothtml" />
<None Remove="Views\FeatureSamples\CustomPrimitiveTypes\TextBox.dothtml" />
<None Remove="Views\FeatureSamples\CustomPrimitiveTypes\UsedInControls.dothtml" />
<None Remove="Views\FeatureSamples\Formatting\ToStringGlobalFunctionBug.dothtml" />
<None Remove="Views\FeatureSamples\HotReload\ViewChanges.dothtml" />
<None Remove="Views\FeatureSamples\JavascriptTranslation\ArrayTranslation.dothtml" />
<None Remove="Views\FeatureSamples\JavascriptTranslation\DateTimeTranslations.dothtml" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DotVVM.Framework.ViewModel;
using DotVVM.Framework.Hosting;

namespace DotVVM.Samples.Common.ViewModels.FeatureSamples.Formatting
{
public class ToStringGlobalFunctionBugViewModel : DotvvmViewModelBase
{
public bool Boolean { get; set; }

public string String { get; set; }
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
@viewModel DotVVM.Samples.Common.ViewModels.FeatureSamples.Formatting.ToStringGlobalFunctionBugViewModel, DotVVM.Samples.Common

<!DOCTYPE html>

<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8" />
<title></title>
</head>
<body>
<dot:Button Text="Set to true" Click="{staticCommand: Boolean = true}" />

<p class="result-boolean">Boolean: {{value: Boolean}}</p>
<p class="result-string">String: {{value: String}}</p>

</body>
</html>


Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 15 additions & 1 deletion src/Samples/Tests/Tests/Feature/FormattingTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,22 @@ public void Feature_Formatting_Formatting()
});
}

[Fact]
public void Feature_Formatting_ToStringGlobalFunctionBug()
{
RunInAllBrowsers(browser => {
browser.NavigateToUrl(SamplesRouteUrls.FeatureSamples_Formatting_ToStringGlobalFunctionBug);

AssertUI.TextEquals(browser.Single(".result-boolean"), "Boolean: false");
AssertUI.TextEquals(browser.Single(".result-string"), "String:");
browser.Single("input[type=button]").Click();
AssertUI.TextEquals(browser.Single(".result-boolean"), "Boolean: true");
AssertUI.TextEquals(browser.Single(".result-string"), "String:");
});
}

public FormattingTests(ITestOutputHelper output) : base(output)
{
}
}
}
}
5 changes: 3 additions & 2 deletions src/Tests/Binding/JavascriptCompilationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1020,8 +1020,8 @@ public void JsTranslator_MathMethods(string binding, string expected)
}

[TestMethod]
[DataRow("Convert.ToBoolean(IntProp)", "Boolean(IntProp())")]
[DataRow("Convert.ToBoolean(DoubleProp)", "Boolean(DoubleProp())")]
[DataRow("Convert.ToBoolean(IntProp)", "window.Boolean(IntProp())")]
[DataRow("Convert.ToBoolean(DoubleProp)", "window.Boolean(DoubleProp())")]
[DataRow("Convert.ToDecimal(DoubleProp)", "DoubleProp")]
[DataRow("Convert.ToInt32(DoubleProp)", "Math.round(DoubleProp())")]
[DataRow("Convert.ToByte(DoubleProp)", "Math.round(DoubleProp())")]
Expand Down Expand Up @@ -1282,6 +1282,7 @@ public void JavascriptCompilation_ApiRefreshOn()
[TestMethod]
public void JavascriptCompilation_MarkupControlProperty()
{
_ = TestMarkupControl.SomePropertyProperty;
var dataContext = bindingHelper.CreateDataContext(new [] { typeof(object) }, markupControl: typeof(TestMarkupControl));
var result = bindingHelper.ValueBindingToJs("_control.SomeProperty + 'aa'", dataContext, niceMode: false);
Assert.AreEqual("($control.SomeProperty()??\"\")+\"aa\"", result);
Expand Down

0 comments on commit 09d893c

Please sign in to comment.