Skip to content

Commit

Permalink
Implement EncodeHTML function and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
CarlosFigueiraMSFT committed Mar 8, 2024
1 parent 685aa72 commit ed43022
Show file tree
Hide file tree
Showing 9 changed files with 63 additions and 56 deletions.
4 changes: 2 additions & 2 deletions src/libraries/Microsoft.PowerFx.Core/Localization/Strings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -518,8 +518,8 @@ 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 AboutEscapeHtml = (b) => StringResources.Get("AboutEscapeHtml", b);
public static StringGetter EscapeHtmlArg1 = (b) => StringResources.Get("EscapeHtmlArg1", 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 @@ -94,10 +94,10 @@ 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());
public static readonly TexlFunction EscapeHtml = _library.Add(new EscapeHtmlFunction());
public static readonly TexlFunction Exp = _library.Add(new ExpFunction());
public static readonly TexlFunction ExpT = _library.Add(new ExpTableFunction());
public static readonly TexlFunction Filter = _library.Add(new FilterFunction());
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 };
}
}
}
23 changes: 0 additions & 23 deletions src/libraries/Microsoft.PowerFx.Core/Texl/Builtins/EscapeHtml.cs

This file was deleted.

22 changes: 11 additions & 11 deletions src/libraries/Microsoft.PowerFx.Interpreter/Functions/Library.cs
Original file line number Diff line number Diff line change
Expand Up @@ -641,6 +641,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 Expand Up @@ -674,17 +685,6 @@ static Library()
returnBehavior: ReturnBehavior.ReturnFalseIfAnyArgIsBlank,
targetFunction: Error)
},
{
BuiltinFunctionsCore.EscapeHtml,
StandardErrorHandling<StringValue>(
BuiltinFunctionsCore.EncodeUrl.Name,
expandArguments: NoArgExpansion,
replaceBlankValues: NoOpAlreadyHandledByIR,
checkRuntimeTypes: ExactValueType<StringValue>,
checkRuntimeValues: DeferRuntimeValueChecking,
returnBehavior: ReturnBehavior.AlwaysEvaluateAndReturnResult,
targetFunction: EscapeHtml)
},
{
BuiltinFunctionsCore.Exp,
StandardErrorHandling<NumberValue>(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -802,7 +802,7 @@ public static FormulaValue EncodeUrl(IRContext irContext, StringValue[] args)
return new StringValue(irContext, Uri.EscapeDataString(args[0].Value));
}

public static FormulaValue EscapeHtml(IRContext irContext, StringValue[] args)
public static FormulaValue EncodeHTML(IRContext irContext, StringValue[] args)
{
var encoded = HttpUtility.HtmlEncode(args[0].Value);
return new StringValue(irContext, encoded);
Expand Down
12 changes: 6 additions & 6 deletions src/strings/PowerFxResources.en-US.resx
Original file line number Diff line number Diff line change
Expand Up @@ -4092,16 +4092,16 @@
<data name="AboutEncodeUrl_url" xml:space="preserve">
<value>A url to be encoded.</value>
</data>
<data name="AboutEscapeHtml" xml:space="preserve">
<data name="AboutEncodeHTML" xml:space="preserve">
<value>Converts a text to an HTML-encoded text.</value>
<comment>Description of 'EncodeUrl' function.</comment>
<comment>Description of 'EncodeHTML' function.</comment>
</data>
<data name="EscapeHtmlArg1" xml:space="preserve">
<data name="EncodeHTMLArg1" xml:space="preserve">
<value>value</value>
<comment>Function_parameter - First parameter for the EscapeHtml function - the value (string) to escape.</comment>
<comment>Function_parameter - First parameter for the EncodeHTML function - the value (string) to encode.</comment>
</data>
<data name="AboutEscapeHtml_value" xml:space="preserve">
<value>A text to be escaped.</value>
<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>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,42 +1,49 @@
// Escaping
>> EscapeHtml("<p>A paragraph</p>")
>> EncodeHTML("<p>A paragraph</p>")
"&lt;p&gt;A paragraph&lt;/p&gt;"

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

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

// \u00A0 to \u00af - escaped
>> EscapeHtml(Concat(Sequence(16, 160), UniChar(Value)))
>> 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 = EscapeHtml(str))
>> With({str:Concat(Sequence(16, 144), UniChar(Value))}, str = EncodeHTML(str))
true

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

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

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

>> EncodeHTML("")
""

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

// Escaping surrogate pairs
>> EscapeHtml("<ul><li>not a pair: ❤</li><li>a surrogate pair: 💩</li></ul>")
>> 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;"

>> EscapeHtml("Osage alphabet (start): 𐒰𐒱𐒲𐒳𐒴𐒵𐒶")
>> 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: {UniChar(Hex2Dec("df32"))} {UniChar(Hex2Dec("d823"))}"))
"Unpaired: � �"
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,11 @@
"Distinct",
"DropColumns",
"EDate",
"EncodeHTML",
"EncodeUrl",
"EndsWith",
"EOMonth",
"Error",
"EscapeHtml",
"Exp",
"Filter",
"Find",
Expand Down

0 comments on commit ed43022

Please sign in to comment.