Skip to content

Commit

Permalink
change default HTML formatting to a tree layout rather than a table (#…
Browse files Browse the repository at this point in the history
…2671)

* split HTML formatter tests into smaller files

* split PlainTextFormatterTests into smaller files

* refactoring

* implement tree views for objects

* remove HtmlFormatter.MaxProperties

* improve dictionary and sequence rendering, open top level of treeview

* remove old object table formatters, clean up
  • Loading branch information
jonsequitur authored Feb 4, 2023
1 parent 9b75e3c commit 276ca26
Show file tree
Hide file tree
Showing 25 changed files with 2,052 additions and 1,872 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ Microsoft.DotNet.Interactive.Formatting
public static TTag Prepend<TTag>(Microsoft.AspNetCore.Html.IHtmlContent content)
public static TTag PrependTo<TTag>(HtmlTag toTag)
public static TTag SelfClosing<TTag>()
public static PocketView Table(System.Collections.Generic.IReadOnlyList<Microsoft.AspNetCore.Html.IHtmlContent> headers, System.Collections.Generic.IReadOnlyList<Microsoft.AspNetCore.Html.IHtmlContent> rows)
public static HtmlTag Tag()
public static Microsoft.AspNetCore.Html.IHtmlContent ToHtmlContent()
public static TTag WithAttributes<TTag>(System.Collections.Generic.IDictionary<System.String,System.Object> htmlAttributes)
Expand Down Expand Up @@ -94,7 +93,6 @@ Microsoft.DotNet.Interactive.Formatting
public static System.Boolean HasClass(System.String class)
public static HtmlAttributes Selected(System.Boolean value = True)
public static class HtmlFormatter
public static System.Int32 MaxProperties { get; set;}
public static ITypeFormatter GetPreferredFormatterFor(System.Type type)
public static ITypeFormatter GetPreferredFormatterFor<T>()
public static System.Void RequireDefaultStyles()
Expand Down Expand Up @@ -167,7 +165,6 @@ Microsoft.DotNet.Interactive.Formatting
public static ITypeFormatter GetPreferredFormatterFor(System.Type type)
public class PocketView : System.Dynamic.DynamicObject, Microsoft.AspNetCore.Html.IHtmlContent, System.Dynamic.IDynamicMetaObjectProvider
public static System.Object Transform(System.Action<HtmlTag,System.Object> transform)
public static System.Object Transform(System.Action<HtmlTag,System.Object,FormatContext> transform)
.ctor(PocketView nested = null)
.ctor(System.String tagName, PocketView nested = null)
public HtmlAttributes HtmlAttributes { get;}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
// Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System.Text.Json;
using FluentAssertions;
using Microsoft.DotNet.Interactive.Formatting.Tests.Utility;
using Xunit;
using Xunit.Abstractions;
using static Microsoft.DotNet.Interactive.Formatting.Tests.Tags;

namespace Microsoft.DotNet.Interactive.Formatting.Tests;

public partial class HtmlFormatterTests : FormatterTestBase
{
public class Json : FormatterTestBase
{
private readonly ITestOutputHelper _output;

public Json(ITestOutputHelper output)
{
_output = output;
}

[Fact]
public void JsonDocument_and_JsonDocument_RootElement_output_the_same_HTML()
{
var jsonString = JsonSerializer.Serialize(new { Name = "cherry", Deliciousness = 9000 });

var jsonDocument = JsonDocument.Parse(jsonString);
var jsonElement = JsonDocument.Parse(jsonString).RootElement;

var jsonDocumentHtml = jsonDocument.ToDisplayString(HtmlFormatter.MimeType);
var jsonElementHtml = jsonElement.ToDisplayString(HtmlFormatter.MimeType);

jsonDocumentHtml.Should().Be(jsonElementHtml);
}

[Fact]
public void JSON_object_output_contains_a_text_summary()
{
var jsonString = JsonSerializer.Serialize(new { Name = "cherry", Deliciousness = 9000 });

var jsonDocument = JsonDocument.Parse(jsonString);

var html = jsonDocument.ToDisplayString(HtmlFormatter.MimeType);

html.Should().ContainAll(
"<code>",
jsonString.HtmlEncode().ToString(),
"</code>");
}

[Fact]
public void JSON_object_output_contains_table_of_properties_within_details_tag()
{
var jsonString = JsonSerializer.Serialize(new { Name = "cherry", Deliciousness = 9000 });

var jsonDocument = JsonDocument.Parse(jsonString);

var html = jsonDocument.ToDisplayString(HtmlFormatter.MimeType);

html.Should().ContainAll(
"<details",
"<td>Name</td>",
"<td><span>&quot;cherry&quot;</span></td>",
"</details>");
}

[Fact]
public void JSON_array_output_contains_a_text_summary()
{
var jsonString = JsonSerializer.Serialize(new object[] { "apple", "banana", "cherry" });

var jsonDocument = JsonDocument.Parse(jsonString);

var html = jsonDocument.ToDisplayString(HtmlFormatter.MimeType);

html.Should().ContainAll(
"<code>",
jsonString.HtmlEncode().ToString(),
"</code>");
}

[Fact]
public void JSON_array_output_contains_table_of_items_within_details_tag()
{
var jsonString = JsonSerializer.Serialize(new object[] { "apple", "banana", "cherry" });

var jsonDocument = JsonDocument.Parse(jsonString);

var html = jsonDocument.ToDisplayString(HtmlFormatter.MimeType).RemoveStyleElement();

html.Should().Contain(
"<tr><td><span>&quot;apple&quot;</span></td></tr><tr><td><span>&quot;banana&quot;</span></td></tr><tr><td><span>&quot;cherry&quot;</span></td></tr>");
}

[Fact]
public void Strings_with_escaped_sequences_are_encoded()
{
var value = "hola! \n \t \" \" ' ' the joy of escapes! ==> & white space ";

var text = value.ToDisplayString("text/html").RemoveStyleElement();

text.Should().Be($"{PlainTextBegin}hola! \n \t &quot; &quot; &#39; &#39; the joy of escapes! ==&gt; &amp; white space {PlainTextEnd}");
}

[Fact]
public void Strings_with_unicode_sequences_are_encoded()
{
var value = "hola! ʰ˽˵ΘϱϪԘÓŴ𝓌🦁♿🌪🍒☝🏿";

var text = value.ToDisplayString("text/html").RemoveStyleElement();

text.Should().Be($"{PlainTextBegin}{value.HtmlEncode()}{PlainTextEnd}");
}
}
}
Loading

0 comments on commit 276ca26

Please sign in to comment.