Skip to content

Commit

Permalink
Merge branch 'main' into carlosff/json/unwrappedtables
Browse files Browse the repository at this point in the history
  • Loading branch information
CarlosFigueiraMSFT authored Mar 12, 2024
2 parents f71381d + 00b0b14 commit a5a0a23
Show file tree
Hide file tree
Showing 8 changed files with 106 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/libraries/Microsoft.PowerFx.Core/Localization/Strings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -528,6 +528,9 @@ internal static class TexlStrings
public static StringGetter AboutEncodeUrl = (b) => StringResources.Get("AboutEncodeUrl", b);
public static StringGetter EncodeUrlArg1 = (b) => StringResources.Get("EncodeUrlArg1", b);

public static StringGetter AboutEncodeHTML = (b) => StringResources.Get("AboutEncodeHTML", b);
public static StringGetter EncodeHTMLArg1 = (b) => StringResources.Get("EncodeHTMLArg1", b);

public static StringGetter AboutPlainText = (b) => StringResources.Get("AboutPlainText", b);
public static StringGetter PlainTextArg1 = (b) => StringResources.Get("PlainTextArg1", b);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ internal class BuiltinFunctionsCore
public static readonly TexlFunction DropColumns = _library.Add(new DropColumnsFunction());
public static readonly TexlFunction EDate = _library.Add(new EDateFunction());
public static readonly TexlFunction EOMonth = _library.Add(new EOMonthFunction());
public static readonly TexlFunction EncodeHTML = _library.Add(new EncodeHTMLFunction());
public static readonly TexlFunction EncodeUrl = _library.Add(new EncodeUrlFunction());
public static readonly TexlFunction EndsWith = _library.Add(new EndsWithFunction());
public static readonly TexlFunction Error = _library.Add(new ErrorFunction());
Expand Down
23 changes: 23 additions & 0 deletions src/libraries/Microsoft.PowerFx.Core/Texl/Builtins/EncodeHTML.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

using System.Collections.Generic;
using Microsoft.PowerFx.Core.Localization;
using Microsoft.PowerFx.Core.Texl.Builtins;
using Microsoft.PowerFx.Core.Types;

namespace Microsoft.PowerFx.Core.Texl
{
internal sealed class EncodeHTMLFunction : StringOneArgFunction
{
public EncodeHTMLFunction()
: base("EncodeHTML", TexlStrings.AboutEncodeHTML, FunctionCategories.Text)
{
}

public override IEnumerable<TexlStrings.StringGetter[]> GetSignatures()
{
yield return new[] { TexlStrings.EncodeHTMLArg1 };
}
}
}
11 changes: 11 additions & 0 deletions src/libraries/Microsoft.PowerFx.Interpreter/Functions/Library.cs
Original file line number Diff line number Diff line change
Expand Up @@ -652,6 +652,17 @@ static Library()
returnBehavior: ReturnBehavior.AlwaysEvaluateAndReturnResult,
targetFunction: EOMonth)
},
{
BuiltinFunctionsCore.EncodeHTML,
StandardErrorHandling<StringValue>(
BuiltinFunctionsCore.EncodeUrl.Name,
expandArguments: NoArgExpansion,
replaceBlankValues: NoOpAlreadyHandledByIR,
checkRuntimeTypes: ExactValueType<StringValue>,
checkRuntimeValues: DeferRuntimeValueChecking,
returnBehavior: ReturnBehavior.AlwaysEvaluateAndReturnResult,
targetFunction: EncodeHTML)
},
{
BuiltinFunctionsCore.EncodeUrl,
StandardErrorHandling<StringValue>(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
using System.Web;
using Microsoft.PowerFx.Core.Functions;
using Microsoft.PowerFx.Core.IR;
using Microsoft.PowerFx.Core.Localization;
Expand Down Expand Up @@ -826,6 +827,12 @@ public static FormulaValue EncodeUrl(IRContext irContext, StringValue[] args)
return new StringValue(irContext, Uri.EscapeDataString(args[0].Value));
}

public static FormulaValue EncodeHTML(IRContext irContext, StringValue[] args)
{
var encoded = HttpUtility.HtmlEncode(args[0].Value);
return new StringValue(irContext, encoded);
}

public static FormulaValue Proper(EvalVisitor runner, EvalVisitorContext context, IRContext irContext, StringValue[] args)
{
return new StringValue(irContext, runner.CultureInfo.TextInfo.ToTitleCase(runner.CultureInfo.TextInfo.ToLower(args[0].Value)));
Expand Down
11 changes: 11 additions & 0 deletions src/strings/PowerFxResources.en-US.resx
Original file line number Diff line number Diff line change
Expand Up @@ -4136,6 +4136,17 @@
<data name="AboutEncodeUrl_url" xml:space="preserve">
<value>A url to be encoded.</value>
</data>
<data name="AboutEncodeHTML" xml:space="preserve">
<value>Converts a text to an HTML-encoded text.</value>
<comment>Description of 'EncodeHTML' function.</comment>
</data>
<data name="EncodeHTMLArg1" xml:space="preserve">
<value>value</value>
<comment>Function_parameter - First parameter for the EncodeHTML function - the value (string) to encode.</comment>
</data>
<data name="AboutEncodeHTML_value" xml:space="preserve">
<value>A text to be encoded.</value>
</data>
<data name="ErrUnimplementedFunction" xml:space="preserve">
<value>'{0}' is a recognized but not supported function.</value>
<comment>Error Message.</comment>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// Escaping
>> EncodeHTML("<p>A paragraph</p>")
"&lt;p&gt;A paragraph&lt;/p&gt;"

// Multiple encodings
>> EncodeHTML("<h1>Mac & cheese</h1>")
"&lt;h1&gt;Mac &amp; cheese&lt;/h1&gt;"

// Quotes
>> EncodeHTML("A value with ""double"" and 'single' quotes")
"A value with &quot;double&quot; and &#39;single&#39; quotes"

// \u00A0 to \u00af - escaped
>> EncodeHTML(Concat(Sequence(16, 160), UniChar(Value)))
"&#160;&#161;&#162;&#163;&#164;&#165;&#166;&#167;&#168;&#169;&#170;&#171;&#172;&#173;&#174;&#175;"

// \u0090 to \u009f - not escaped
>> With({str:Concat(Sequence(16, 144), UniChar(Value))}, str = EncodeHTML(str))
true

// \u00A0 to \u00ff - escaped
>> With({str:EncodeHTML(Concat(Sequence(96, 160), UniChar(Value))), expectedEscaped:Concat(Sequence(96, 160), $"&#{Value};")}, str = expectedEscaped)
true

// \u0100... not escaped
>> EncodeHTML("<b>ĀāĂă</b>")
"&lt;b&gt;ĀāĂă&lt;/b&gt;"

// Blanks
>> EncodeHTML(Blank())
""

>> EncodeHTML("")
""

// Errors
>> EncodeHTML(Char(-1))
Error({Kind:ErrorKind.InvalidArgument})

// Escaping surrogate pairs
>> EncodeHTML("<ul><li>not a pair: ❤</li><li>a surrogate pair: 💩</li></ul>")
"&lt;ul&gt;&lt;li&gt;not a pair: ❤&lt;/li&gt;&lt;li&gt;a surrogate pair: &#128169;&lt;/li&gt;&lt;/ul&gt;"

>> EncodeHTML("Osage alphabet (start): 𐒰𐒱𐒲𐒳𐒴𐒵𐒶")
"Osage alphabet (start): &#66736;&#66737;&#66738;&#66739;&#66740;&#66741;&#66742;"

// Unpaired surrogate characters become the replacement character U+FFFD
>> EncodeHTML($"Unpaired: {Mid(UniChar(Hex2Dec("1f600")),1,1)} {Mid(UniChar(Hex2Dec("1f600")),2,1)}") = $"Unpaired: {UniChar(65533)} {UniChar(65533)}"
true
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
"Distinct",
"DropColumns",
"EDate",
"EncodeHTML",
"EncodeUrl",
"EndsWith",
"EOMonth",
Expand Down

0 comments on commit a5a0a23

Please sign in to comment.