diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/convert-HTML.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/convert-HTML.cs index 89debd225e0..af48588e02f 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/convert-HTML.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/convert-HTML.cs @@ -326,9 +326,11 @@ internal class ConvertHTMLExpressionParameterDefinition : CommandParameterDefini protected override void SetEntries() { this.hashEntries.Add(new ExpressionEntryDefinition()); - this.hashEntries.Add(new HashtableEntryDefinition(ConvertHTMLParameterDefinitionKeys.LabelEntryKey, new Type[] { typeof(string) })); - this.hashEntries.Add(new HashtableEntryDefinition(ConvertHTMLParameterDefinitionKeys.AlignmentEntryKey, new Type[] { typeof(string) })); - this.hashEntries.Add(new HashtableEntryDefinition(ConvertHTMLParameterDefinitionKeys.WidthEntryKey, new Type[] { typeof(string) })); + this.hashEntries.Add(new LabelEntryDefinition()); + this.hashEntries.Add(new HashtableEntryDefinition(ConvertHTMLParameterDefinitionKeys.AlignmentEntryKey, new[] { typeof(string) })); + + // Note: We accept "width" as either string or int. + this.hashEntries.Add(new HashtableEntryDefinition(ConvertHTMLParameterDefinitionKeys.WidthEntryKey, new[] { typeof(string), typeof(int) })); } } @@ -362,7 +364,11 @@ private void InitializeResolvedNameMshParameters() { string label = p.GetEntry(ConvertHTMLParameterDefinitionKeys.LabelEntryKey) as string; string alignment = p.GetEntry(ConvertHTMLParameterDefinitionKeys.AlignmentEntryKey) as string; - string width = p.GetEntry(ConvertHTMLParameterDefinitionKeys.WidthEntryKey) as string; + + // Accept the width both as a string and as an int. + string width; + int? widthNum = p.GetEntry(ConvertHTMLParameterDefinitionKeys.WidthEntryKey) as int?; + width = widthNum != null ? widthNum.Value.ToString() : p.GetEntry(ConvertHTMLParameterDefinitionKeys.WidthEntryKey) as string; PSPropertyExpression ex = p.GetEntry(FormatParameterDefinitionKeys.ExpressionEntryKey) as PSPropertyExpression; List resolvedNames = ex.ResolveNames(_inputObject); foreach (PSPropertyExpression resolvedName in resolvedNames) diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/ConvertTo-Html.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/ConvertTo-Html.Tests.ps1 index bbf3fa1b74f..7cfaed62aa5 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/ConvertTo-Html.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/ConvertTo-Html.Tests.ps1 @@ -169,15 +169,29 @@ After the object $returnString | Should -Be '' } - It "Test ConvertTo-HTML supports scriptblock-based calculated properties: by hashtable"{ - $returnString = ($customObject | ConvertTo-HTML @{ l='NewAge'; e={ $_.Age + 1 } }) -join $newLine + It "Test ConvertTo-HTML supports scriptblock-based calculated properties: by hashtable" { + $returnString = ($customObject | ConvertTo-HTML @{ l = 'NewAge'; e = { $_.Age + 1 } }) -join $newLine $returnString | Should -Match '\b43\b' } - It "Test ConvertTo-HTML supports scriptblock-based calculated properties: directly"{ + It "Test ConvertTo-HTML supports scriptblock-based calculated properties: directly" { $returnString = ($customObject | ConvertTo-HTML { $_.Age + 1 }) -join $newLine $returnString | Should -Match '\b43\b' } -} + It "Test ConvertTo-HTML calculated property supports 'name' key as alias of 'label'" { + $returnString = ($customObject | ConvertTo-Html @{ name = 'AgeRenamed'; e = 'Age'}) -join $newLine + $returnString | Should -Match 'AgeRenamed' + } + + It "Test ConvertTo-HTML calculated property supports integer 'width' entry" { + $returnString = ($customObject | ConvertTo-Html @{ e = 'Age'; width = 10 }) -join $newLine + $returnString | Should -Match '\swidth\s*=\s*(["''])10\1' + } + It "Test ConvertTo-HTML calculated property supports string 'width' entry" { + $returnString = ($customObject | ConvertTo-Html @{ e = 'Age'; width = '10' }) -join $newLine + $returnString | Should -Match '\swidth\s*=\s*(["''])10\1' + } + +}