Skip to content

Commit

Permalink
remove HtmlFormatter.MaxProperties
Browse files Browse the repository at this point in the history
  • Loading branch information
jonsequitur committed Feb 1, 2023
1 parent e95e742 commit 0931d22
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 154 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System;
using System.ComponentModel;
using System.Dynamic;
using System.IO;
using System.Linq;
Expand All @@ -12,8 +11,6 @@
using Microsoft.CSharp.RuntimeBinder;
using Microsoft.DotNet.Interactive.Formatting.Tests.Utility;
using Xunit;
using static Microsoft.DotNet.Interactive.Formatting.PocketViewTags;
using static System.Net.Mime.MediaTypeNames;

namespace Microsoft.DotNet.Interactive.Formatting.Tests;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -423,113 +423,7 @@ public void It_shows_null_items_in_the_sequence_as_null()
</tbody>
</table>");
}

[Fact]
public void It_shows_properties_up_to_default_max()
{
var formatter = HtmlFormatter.GetPreferredFormatterFor(typeof(Dummy.ClassWithManyProperties));

var writer = new StringWriter();

formatter.Format(new Dummy.ClassWithManyProperties(), writer);

writer.ToString().RemoveStyleElement().Should()
.BeEquivalentHtmlTo($@"<table>
<thead>
<tr>
<th>X1</th>
<th>X2</th>
<th>X3</th>
<th>X4</th>
<th>X5</th>
<th>X6</th>
<th>X7</th>
<th>X8</th>
<th>X9</th>
<th>X10</th>
<th>X11</th>
<th>X12</th>
<th>X13</th>
<th>X14</th>
<th>X15</th>
<th>X16</th>
<th>X17</th>
<th>X18</th>
<th>X19</th>
<th>X20</th>
<th>..</th>
</tr>
</thead>
<tbody>
<tr>
<td>{Tags.PlainTextBegin}1{Tags.PlainTextEnd}</td>
<td>{Tags.PlainTextBegin}2{Tags.PlainTextEnd}</td>
<td>{Tags.PlainTextBegin}3{Tags.PlainTextEnd}</td>
<td>{Tags.PlainTextBegin}4{Tags.PlainTextEnd}</td>
<td>{Tags.PlainTextBegin}5{Tags.PlainTextEnd}</td>
<td>{Tags.PlainTextBegin}6{Tags.PlainTextEnd}</td>
<td>{Tags.PlainTextBegin}7{Tags.PlainTextEnd}</td>
<td>{Tags.PlainTextBegin}8{Tags.PlainTextEnd}</td>
<td>{Tags.PlainTextBegin}9{Tags.PlainTextEnd}</td>
<td>{Tags.PlainTextBegin}10{Tags.PlainTextEnd}</td>
<td>{Tags.PlainTextBegin}11{Tags.PlainTextEnd}</td>
<td>{Tags.PlainTextBegin}12{Tags.PlainTextEnd}</td>
<td>{Tags.PlainTextBegin}13{Tags.PlainTextEnd}</td>
<td>{Tags.PlainTextBegin}14{Tags.PlainTextEnd}</td>
<td>{Tags.PlainTextBegin}15{Tags.PlainTextEnd}</td>
<td>{Tags.PlainTextBegin}16{Tags.PlainTextEnd}</td>
<td>{Tags.PlainTextBegin}17{Tags.PlainTextEnd}</td>
<td>{Tags.PlainTextBegin}18{Tags.PlainTextEnd}</td>
<td>{Tags.PlainTextBegin}19{Tags.PlainTextEnd}</td>
<td>{Tags.PlainTextBegin}20{Tags.PlainTextEnd}</td>
</tr>
</tbody>
</table>");
}

[Fact]
public void It_shows_properties_up_to_custom_max()
{
var formatter = HtmlFormatter.GetPreferredFormatterFor(typeof(Dummy.ClassWithManyProperties));

var writer = new StringWriter();
HtmlFormatter.MaxProperties = 1;

formatter.Format(new Dummy.ClassWithManyProperties(), writer);

writer.ToString().RemoveStyleElement().Should()
.BeEquivalentHtmlTo($@"<table>
<thead>
<tr>
<th>X1</th>
<th>..</th>
</tr>
</thead>
<tbody>
<tr>
<td>{Tags.PlainTextBegin}1{Tags.PlainTextEnd}</td>
</tr>
</tbody>
</table>");
}

[Fact]
public void Setting_properties_to_zero_means_no_table_formatting_and_plaintext_gets_used()
{
var formatter = HtmlFormatter.GetPreferredFormatterFor(typeof(Dummy.ClassWithManyProperties));

var writer = new StringWriter();
HtmlFormatter.MaxProperties = 0;
PlainTextFormatter.MaxProperties = 0;

formatter.Format(new Dummy.ClassWithManyProperties(), writer);

writer.ToString()
.RemoveStyleElement()
.Should()
.Be($"{Tags.PlainTextBegin}Dummy.ClassWithManyProperties{Tags.PlainTextEnd}");
}


[Fact]
public void Sequences_can_contain_different_types_of_elements()
{
Expand Down
16 changes: 3 additions & 13 deletions src/Microsoft.DotNet.Interactive.Formatting/HtmlFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,26 +25,16 @@ static HtmlFormatter()
void Initialize()
{
#if (false) // FIX: delete
FormattersForAnyObject = new FormatterMapByType(typeof(HtmlFormatter<>), nameof(HtmlFormatter<object>.CreateTableFormatterForAnyObject));
FormattersForAnyObject = new FormatterMapByType(typeof(HtmlFormatter<>), nameof(HtmlFormatter<object>.CreateTableFormatterForAnyObject));
#else
FormattersForAnyObject = new FormatterMapByType(typeof(HtmlFormatter<>), nameof(HtmlFormatter<object>.CreateTreeViewFormatterForAnyObject));
FormattersForAnyObject = new FormatterMapByType(typeof(HtmlFormatter<>), nameof(HtmlFormatter<object>.CreateTreeViewFormatterForAnyObject));
#endif
MaxProperties = DefaultMaxProperties;
}
}

// FIX: (HtmlFormatter.UseTreeView) delete this
public static bool UseTreeView { get; set; }

// FIX: (MaxProperties) this can likely be removed
/// <summary>
/// Indicates the maximum number of properties to show in the default HTML display of arbitrary objects.
/// If set to zero no properties are shown.
/// </summary>
public static int MaxProperties { get; set; } = DefaultMaxProperties;

internal const int DefaultMaxProperties = 20;


// FIX: (HtmlFormatter) this can return a formatter with the wrong MIME type
public static ITypeFormatter GetPreferredFormatterFor(Type type) =>
Formatter.GetPreferredFormatterFor(type, MimeType);
Expand Down
38 changes: 8 additions & 30 deletions src/Microsoft.DotNet.Interactive.Formatting/HtmlFormatter{T}.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,7 @@ internal static HtmlFormatter<T> CreateTableFormatterForAnyObject()

static bool BuildTable(T instance, FormatContext context, MemberAccessor<T>[] memberAccessors)
{
// Note the order of members is declaration order
var reducedMembers =
memberAccessors
.Take(Math.Max(0, HtmlFormatter.MaxProperties))
.ToArray();

if (reducedMembers.Length == 0)
if (memberAccessors.Length == 0)
{
// This formatter refuses to format objects without members, and
// refused to produce nested tables, or if no members are selected
Expand All @@ -81,19 +75,13 @@ static bool BuildTable(T instance, FormatContext context, MemberAccessor<T>[] me
{
// Note, embeds the keys and values as arbitrary objects into the HTML content,
List<IHtmlContent> headers =
reducedMembers.Select(m => (IHtmlContent)th(m.Member.Name))
.ToList();

// Add a '..' column if we elided some members due to size limitations
if (reducedMembers.Length < memberAccessors.Length)
{
headers.Add(th(".."));
}
memberAccessors.Select(m => (IHtmlContent)th(m.Member.Name))
.ToList();

// FIX: (CreateTableFormatterForAnyObject) should this use a tree view?
IEnumerable<object> values =
reducedMembers.Select(m => m.GetValueOrException(instance))
.Select(v => td(div[@class: "dni-plaintext"](pre(v.ToDisplayString(PlainTextFormatter.MimeType)))));
memberAccessors.Select(m => m.GetValueOrException(instance))
.Select(v => td(div[@class: "dni-plaintext"](pre(v.ToDisplayString(PlainTextFormatter.MimeType)))));

PocketView t =
table(
Expand Down Expand Up @@ -242,17 +230,8 @@ bool BuildTable(T source, FormatContext context)
.OrderBy(x => headerToSortIndex[x])
.ToArray();

var valueKeysLimited =
valueKeys
.Take(Math.Max(0, HtmlFormatter.MaxProperties))
.ToArray();

headers.AddRange(valueKeysLimited.Select(k => (IHtmlContent)th(k)));
if (valueKeysLimited.Length < valueKeys.Length)
{
headers.Add((IHtmlContent)th(".."));
}

headers.AddRange(valueKeys.Select(k => (IHtmlContent)th(k)));

var rows = new List<IHtmlContent>();

for (var rowIndex = 0; rowIndex < rowData.Count; rowIndex++)
Expand All @@ -269,7 +248,7 @@ bool BuildTable(T source, FormatContext context)
rowValues.Add(type);
}

foreach (var key in valueKeysLimited)
foreach (var key in valueKeys)
{
if (valuesByHeader[key].TryGetValue(rowIndex, out var cellData))
{
Expand Down Expand Up @@ -320,7 +299,6 @@ static bool BuildTreeView(T source, FormatContext context, MemberAccessor<T>[] m
using var _ = context.IncrementTableDepth();
using var __ = context.IncrementDepth();

// FIX: (CreateTreeViewFormatterForAnyObject)
if (!context.AllowRecursion)
{
HtmlFormatter.FormatAndStyleAsPlainText(source, context);
Expand Down
1 change: 0 additions & 1 deletion src/Microsoft.DotNet.Interactive.Formatting/HtmlTag.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ public HtmlAttributes HtmlAttributes
/// <inheritdoc />
public virtual void WriteTo(TextWriter writer, HtmlEncoder encoder = null)
{
// FIX: (WriteTo) who's calling this?
WriteTo(new FormatContext(writer));
}

Expand Down

0 comments on commit 0931d22

Please sign in to comment.