Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed string conversion bug with global function #1711

Merged
merged 2 commits into from
Oct 22, 2023
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
Original file line number Diff line number Diff line change
Expand Up @@ -760,7 +760,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 @@ -773,7 +773,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 @@ -999,8 +999,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 @@ -1261,6 +1261,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
Loading