Skip to content

Commit

Permalink
fix indentation
Browse files Browse the repository at this point in the history
  • Loading branch information
Narve Sætre committed Oct 24, 2023
1 parent d7f56a7 commit eccf71d
Show file tree
Hide file tree
Showing 12 changed files with 64 additions and 20 deletions.
4 changes: 2 additions & 2 deletions DV8.Html.Tests/Helpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ public static string Canonical(this string s)
s = Regex.Replace(s, attrPattern, "='$1'");

// Replace indents:
var indentPattern = ">(\\s+)<";
s = Regex.Replace(s, indentPattern, "><");
var indentPattern = new Regex(@"(\s+)<");
s = indentPattern.Replace(s, "<");

// Replace spaces before end-of-element:
var endPattern = @" +\/\>";
Expand Down
14 changes: 7 additions & 7 deletions DV8.Html.Tests/HtmlElementTests/PrettyPrintTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ namespace DV8.Html.Tests.HtmlElementTests;

public class PrettyPrintTest
{
// [Test]
// public void ShouldIncludeLineBreaks()
// {
// var d = new Div(new P("text"));
// var s = d.ToHtml();
// Assert.AreEqual("<div>\r\n <p>text</p>\r\n</div>\r\n", s);
// }
[Test]
public void ShouldIncludeLineBreaks()
{
var d = new Div(new P("text"));
var s = d.ToHtml();
Assert.AreEqual("<div>\r\n <p>text</p>\r\n</div>", s);
}

}
9 changes: 7 additions & 2 deletions DV8.Html.Tests/HtmlElementTests/UnderscoreTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,13 @@ public void TestIt()

var exp = @"
<!DOCTYPE html><html>
<head><title>Hello, World!</title></head>
<body><h1>Hello, World!</h1><p>This is a paragraph with &lt;&gt;. <ul><li>Apple</li><li>Banana</li><li>Cherry</li></ul></p>
<head>
<title>Hello, World!</title>
</head>
<body>
<h1>Hello, World!</h1>
<p>This is a paragraph with &lt;&gt;.
<ul><li>Apple</li><li>Banana</li><li>Cherry</li></ul></p>
This will <em>not</em> be escaped
</body></html>";

Expand Down
2 changes: 1 addition & 1 deletion DV8.Html/DV8.Html.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<PackageVersion>2.18</PackageVersion>
<PackageVersion>2.19</PackageVersion>
<PackageDescription>C# package with a HTML DSL and support for generating HTML elements and serializing object graphs to HTML</PackageDescription>
<RootNamespace>DV8.Html</RootNamespace>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
Expand Down
1 change: 1 addition & 0 deletions DV8.Html/Elements/Em.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ namespace DV8.Html.Elements;

public class Em: HtmlElement
{
protected override bool IsInlineBlock => true;
public Em()
{
}
Expand Down
1 change: 1 addition & 0 deletions DV8.Html/Elements/H1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ namespace DV8.Html.Elements;

public class H1 : HtmlElement
{
protected override bool IsInlineBlock => true;
public H1()
{
}
Expand Down
17 changes: 10 additions & 7 deletions DV8.Html/Elements/Option.cs
Original file line number Diff line number Diff line change
@@ -1,34 +1,37 @@
using DV8.Html.Framework;

// ReSharper disable PropertyCanBeMadeInitOnly.Global
// ReSharper disable MemberCanBePrivate.Global

namespace DV8.Html.Elements;

public class Option : HtmlElement
{
public string? Value
protected override bool IsInlineBlock => true;

public string? Value
{
get => Get("value");
set => Set("value", value);
}
public bool Selected

public bool Selected
{
get => GetBool("selected");
set => SetBool("selected", value);
}
}

public bool Disabled
public bool Disabled
{
get => GetBool("disabled");
set => SetBool("disabled", value);
}
}

public Option()
{
}

public Option(string value, string? text = null): base(null, text) =>
public Option(string value, string? text = null) : base(null, text) =>
Value = value;

public Option WithSelected(bool selected)
Expand Down
10 changes: 10 additions & 0 deletions DV8.Html/Elements/P.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,21 @@ namespace DV8.Html.Elements;

public class P : HtmlElement
{
protected override bool IsInlineBlock => true;

public P()
{
}

public P(string s) : base(null, s)
{
}

// protected P(params IHtmlElement[] htmlElements) : base(htmlElements)
// {
// }
//
// public P(string text, params IHtmlElement[] htmlElements) : base(null, htmlElements)
// {
// }
}
3 changes: 3 additions & 0 deletions DV8.Html/Elements/Pre.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
using DV8.Html.Framework;

// ReSharper disable UnusedType.Global

namespace DV8.Html.Elements;

public class Pre : HtmlElement
{
protected override bool IsInlineBlock => true;

public Pre()
{
}
Expand Down
1 change: 1 addition & 0 deletions DV8.Html/Elements/Span.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ namespace DV8.Html.Elements;

public class Span : HtmlElement
{
protected override bool IsInlineBlock => true;
public Span()
{
}
Expand Down
1 change: 1 addition & 0 deletions DV8.Html/Elements/Textarea.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ namespace DV8.Html.Elements;

public class Textarea : InputElement, IFormElement
{
protected override bool IsInlineBlock => true;

}
21 changes: 20 additions & 1 deletion DV8.Html/Framework/HtmlElement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ public class HtmlElement : IHtmlElement
/// </summary>
protected virtual bool IsSelfClosing => false;

/// <summary>
/// Hints whether this is mostly a text-container (like span, p, h1) or a block (like div).
/// Blocks elements will use prefix to enable pretty printing.
/// </summary>
protected virtual bool IsInlineBlock => false;


public string? Id
{
Expand Down Expand Up @@ -183,11 +189,20 @@ public string GetDefaultTag()

public virtual void WriteHtml(HtmlWriter writer, string prefix = "")
{
// writer.WriteRaw(prefix);
// Assume we start at beginning of line
// if (!IsInlineBlock)
{
writer.WriteRaw(prefix);
}
writer.WriteStartOfElement(Tag);
WriteAttributes(writer);
writer.WriteEndOfElementTag();

if (!IsInlineBlock)
{
writer.WriteRaw("\r\n");
}

// bool hasTextContent = Children.Any(s => s is TextContent or UnsafeTextContent);
// if(!hasTextContent)
// writer.WriteRaw("\r\n");
Expand All @@ -198,6 +213,10 @@ public virtual void WriteHtml(HtmlWriter writer, string prefix = "")

if (!IsSelfClosing)
{
if (!IsInlineBlock)
{
writer.WriteRaw("\r\n");
}
// writer.WriteRaw(prefix);
writer.WriteEndElement(Tag);
}
Expand Down

0 comments on commit eccf71d

Please sign in to comment.