Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add HtmlTextWriter and required support #397

Closed
wants to merge 4 commits into from
Closed

Conversation

twsouthwick
Copy link
Member

@twsouthwick twsouthwick commented Sep 1, 2023

This is a port of HtmlTextWriter with the following changes:

  • HtmlTextWriter.[Enter|Exit]Style(...) methods (4 total) are not supported. The Style class brings in a lot of additional stuff we probably don't want to support
  • HashTable usage has been switched to Dictionary<,>
  • Manual array management has been switched to lists
  • Helper structs have been made into readonly records to simplify definition and usage
  • Annotated for nullability

TODO:

  • Add some tests

Fixes #378

@twsouthwick twsouthwick force-pushed the htmltextwriter branch 3 times, most recently from 95bc740 to a59ce12 Compare September 1, 2023 02:46
This is a port of HtmlTextWriter with the following changes:

- `HtmlTextWriter.[Enter|Exit]Style(...)` methods (4 total) are not supported. The `Style` class brings in a lot of additional stuff we probably don't want to support
- HashTable usage has been switched to Dictionary<,>
- Manual array management has been switched to lists
- Helper structs have been made into readonly records to simplify definition and usage
- Annotated for nullability
@Clounea
Copy link
Contributor

Clounea commented Oct 18, 2023

Inspired by the tests in this repo https://github.com/mfeingol/htmltextwriter, I think you could add the test like below. The render code is from https://learn.microsoft.com/en-us/dotnet/api/system.web.ui.htmltextwriter.addattribute?view=netframework-4.8.1, and the expected string is result I get from .NetFramework project with same render code.

[Fact]
public void RenderCustomControl()
{
    using MemoryStream mem = new MemoryStream();
    using StreamWriter sw = new StreamWriter(mem);
    using HtmlTextWriter writer = new HtmlTextWriter(sw);

    // Set attributes and values along with attributes and styles  
    // attribute defined for a <span> element.
    writer.AddAttribute(HtmlTextWriterAttribute.Onclick, "alert('Hello');");
    writer.AddAttribute("CustomAttribute", "CustomAttributeValue");
    writer.AddStyleAttribute(HtmlTextWriterStyle.Color, "Red");
    writer.AddStyleAttribute("Customstyle", "CustomStyleValue");
    writer.RenderBeginTag(HtmlTextWriterTag.Span);
    // Create a space and indent the markup inside the 
    // <span> element.
    writer.WriteLine();
    writer.Indent++;
    writer.Write("Hello");
    writer.WriteLine();

    // Controls the encoding of markup attributes
    // for an <img> element. Simple known values 
    // do not need encoding.
    writer.AddAttribute(HtmlTextWriterAttribute.Alt,
        "Encoding, \"Required\"",
        true);
    writer.AddAttribute("myattribute",
        "No &quot;encoding &quot; required",
        false);
    writer.RenderBeginTag(HtmlTextWriterTag.Img);
    writer.RenderEndTag();
    writer.WriteLine();

    // Create a non-standard markup element.
    writer.RenderBeginTag("MyTag");
    writer.Write("Contents of MyTag");
    writer.RenderEndTag();
    writer.WriteLine();

    // Create a manually rendered <img> element
    // that contains an alt attribute.
    writer.WriteBeginTag("img");
    writer.WriteAttribute("alt", "A custom image.");
    writer.Write(HtmlTextWriter.TagRightChar);
    writer.WriteEndTag("img");
    writer.WriteLine();

    writer.Indent--;
    writer.RenderEndTag();

    writer.Flush();
    mem.Position = 0;
    string html = Encoding.UTF8.GetString(mem.GetBuffer());

    const string expected = "<span onclick=\"alert(&#39;Hello&#39;);\" CustomAttribute=\"CustomAttributeValue\" style=\"color:Red;Customstyle:CustomStyleValue;\">\r\n\tHello\r\n\t<img alt=\"Encoding, &quot;Required&quot;\" myattribute=\"No &quot;encoding &quot; required\" />\r\n\t<MyTag>\r\n\t\tContents of MyTag\r\n\t</MyTag>\r\n\t<img alt=\"A custom image.\"></img>\r\n</span>";
    Assert.Equal(expected, html);
}

@twsouthwick
Copy link
Member Author

@Clounea you interested in adding that test? The expected variable could use a raw string, but other than that, it's probably a good start

@Clounea
Copy link
Contributor

Clounea commented Oct 24, 2023

@Clounea you interested in adding that test? The expected variable could use a raw string, but other than that, it's probably a good start

Yes, but how? I tried pushing changes to this branch but I don't have access. Do you know how to add the test?

@twsouthwick
Copy link
Member Author

Oh! I'll go ahead and add it - must be a permissions issue

@douglaseccker
Copy link

@twsouthwick Is there any plan to merge this PR?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Ask About HtmlTextWriter
3 participants