Skip to content

Commit ea13f66

Browse files
committed
Ctrl^e+f for layout
1 parent 28972bb commit ea13f66

File tree

1 file changed

+94
-99
lines changed

1 file changed

+94
-99
lines changed

test/ProgressOnderwijsUtils.Tests/HtmlDslGenerator.cs

Lines changed: 94 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,7 @@ static string[] splitList(string list)
7575
=> list.Split(';').Select(s => s.Trim().TrimEnd('*')).Where(s => s != "").ToArray();
7676

7777
var elements = tableOfElements.QuerySelectorAll("tbody tr")
78-
.SelectMany(
79-
tableRow => {
78+
.SelectMany(tableRow => {
8079
var tds = tableRow.QuerySelectorAll("th,td");
8180
var elementNames = tds[elementNameColumnIndex].TextContent.Split(',').Select(n => n.Trim());
8281
var elementAnchor = tds[elementNameColumnIndex].QuerySelector("a")?.GetAttribute("href");
@@ -87,8 +86,7 @@ static string[] splitList(string list)
8786
var elementLink = new Uri(specUri, elementAnchor).ToString();
8887
var attributes = splitList(tds[attributesColumnIndex].TextContent)
8988
.Where(s => s != "any" && s != "globals" && !s.Contains(" ")).ToArray();
90-
return elementNames.Select(
91-
elementName => new {
89+
return elementNames.Select(elementName => new {
9290
elementName,
9391
elementLink,
9492
description,
@@ -102,102 +100,97 @@ static string[] splitList(string list)
102100
)
103101
.Where(el => !el.elementName.Contains(" "))
104102
.GroupBy(el => el.elementName)
105-
.Select(
106-
elGroup =>
107-
new {
108-
elementName = elGroup.Key,
109-
csName = toClassName(elGroup.Key),
110-
csUpperName = toClassName(elGroup.Key).ToUpperInvariant(),
111-
elementMetaData =
112-
new XElement(
113-
"summary",
114-
elGroup.SelectMany(
115-
el =>
116-
new object[] {
117-
el.description + ". See: ",
118-
new XElement("a", new XAttribute("href", el.elementLink), el.elementLink),
119-
new XElement("br")
120-
}
121-
)
122-
),
123-
attributes = elGroup.SelectMany(el => el.attributes).ToDistinctArray(),
124-
categories = elGroup.SelectMany(el => el.categories).ToDistinctArray(),
125-
parents = elGroup.SelectMany(el => el.parents).ToDistinctArray(),
126-
children = elGroup.SelectMany(el => el.children).ToDistinctArray(),
127-
}
103+
.Select(elGroup =>
104+
new {
105+
elementName = elGroup.Key,
106+
csName = toClassName(elGroup.Key),
107+
csUpperName = toClassName(elGroup.Key).ToUpperInvariant(),
108+
elementMetaData =
109+
new XElement(
110+
"summary",
111+
elGroup.SelectMany(el =>
112+
new object[] {
113+
el.description + ". See: ",
114+
new XElement("a", new XAttribute("href", el.elementLink), el.elementLink),
115+
new XElement("br")
116+
}
117+
)
118+
),
119+
attributes = elGroup.SelectMany(el => el.attributes).ToDistinctArray(),
120+
categories = elGroup.SelectMany(el => el.categories).ToDistinctArray(),
121+
parents = elGroup.SelectMany(el => el.parents).ToDistinctArray(),
122+
children = elGroup.SelectMany(el => el.children).ToDistinctArray(),
123+
}
128124
).ToArray();
129125

130126
var specificAttributes = elements.SelectMany(el => el.attributes).ToDistinctArray();
131127

132128
var elAttrInterfaces = specificAttributes
133-
.Select(
134-
attrName => $$"""
135-
public interface IHasAttr_{{toClassName(attrName)}} { }
129+
.Select(attrName => $$"""
130+
public interface IHasAttr_{{toClassName(attrName)}} { }
136131
137-
"""
132+
"""
138133
);
139134
var elAttrExtensionMethods = globalAttributes.Concat(specificAttributes)
140135
.Select(AttrHelper).JoinStrings("");
141136

142137
var elTagNameClasses = elements
143-
.Select(
144-
el =>
145-
voidElements.Contains(el.elementName)
146-
? $$"""
147-
148-
public struct {{el.csUpperName}} : IHtmlElement<{{el.csUpperName}}>{{el.attributes.Select(attrName => $", IHasAttr_{toClassName(attrName)}").JoinStrings("")}}
149-
{
150-
public string TagName => "{{el.elementName}}";
151-
string IHtmlElement.TagStart => "<{{el.elementName}}";
152-
string IHtmlElement.EndTag => "";
153-
ReadOnlySpan<byte> IHtmlElement.TagStartUtf8 => "<{{el.elementName}}"u8;
154-
ReadOnlySpan<byte> IHtmlElement.EndTagUtf8 => ""u8;
155-
public bool ContainsUnescapedText => {{(el.elementName is "script" or "style" ? "true" : "false")}};
156-
HtmlAttributes attrs;
157-
{{el.csUpperName}} IHtmlElement<{{el.csUpperName}}>.ReplaceAttributesWith(HtmlAttributes replacementAttributes) => new {{el.csUpperName}} { attrs = replacementAttributes };
158-
HtmlAttributes IHtmlElement.Attributes => attrs;
159-
IHtmlElement IHtmlElement.ApplyAlteration<THtmlTagAlteration>(THtmlTagAlteration change) => change.AlterEmptyElement(this);
160-
[Pure] public HtmlFragment AsFragment() => HtmlFragment.Element(this);
161-
public static implicit operator HtmlFragment({{el.csUpperName}} tag) => tag.AsFragment();
162-
public static HtmlFragment operator +({{el.csUpperName}} unary) => unary;
163-
public static HtmlFragment operator +({{el.csUpperName}} head, HtmlFragment tail) => HtmlFragment.Fragment(HtmlFragment.Element(head), tail);
164-
public static HtmlFragment operator +(string head, {{el.csUpperName}} tail) => HtmlFragment.Fragment(head, HtmlFragment.Element(tail));
165-
}
166-
"""
167-
: $$"""
168-
169-
public struct {{el.csUpperName}} : IHtmlElementAllowingContent<{{el.csUpperName}}>{{el.attributes.Select(attrName => $", IHasAttr_{toClassName(attrName)}").JoinStrings("")}}
170-
{
171-
public string TagName => "{{el.elementName}}";
172-
string IHtmlElement.TagStart => "<{{el.elementName}}";
173-
string IHtmlElement.EndTag => "</{{el.elementName}}>";
174-
ReadOnlySpan<byte> IHtmlElement.TagStartUtf8 => "<{{el.elementName}}"u8;
175-
ReadOnlySpan<byte> IHtmlElement.EndTagUtf8 => "</{{el.elementName}}>"u8;
176-
public bool ContainsUnescapedText => {{(el.elementName is "script" or "style" ? "true" : "false")}};
177-
HtmlAttributes attrs;
178-
{{el.csUpperName}} IHtmlElement<{{el.csUpperName}}>.ReplaceAttributesWith(HtmlAttributes replacementAttributes) => new {{el.csUpperName}} { attrs = replacementAttributes, children = children };
179-
HtmlAttributes IHtmlElement.Attributes => attrs;
180-
HtmlFragment children;
181-
{{el.csUpperName}} IHtmlElementAllowingContent<{{el.csUpperName}}>.ReplaceContentWith(HtmlFragment replacementContents) => new {{el.csUpperName}} { attrs = attrs, children = replacementContents };
182-
HtmlFragment IHtmlElementAllowingContent.GetContent() => children;
183-
IHtmlElement IHtmlElement.ApplyAlteration<THtmlTagAlteration>(THtmlTagAlteration change) => change.AlterElementAllowingContent(this);
184-
[Pure] public HtmlFragment AsFragment() => HtmlFragment.Element(this);
185-
public static implicit operator HtmlFragment({{el.csUpperName}} tag) => tag.AsFragment();
186-
public static HtmlFragment operator +({{el.csUpperName}} unary) => unary;
187-
public static HtmlFragment operator +({{el.csUpperName}} head, HtmlFragment tail) => HtmlFragment.Fragment(HtmlFragment.Element(head), tail);
188-
public static HtmlFragment operator +(string head, {{el.csUpperName}} tail) => HtmlFragment.Fragment(head, HtmlFragment.Element(tail));
189-
}
190-
"""
138+
.Select(el =>
139+
voidElements.Contains(el.elementName)
140+
? $$"""
141+
142+
public struct {{el.csUpperName}} : IHtmlElement<{{el.csUpperName}}>{{el.attributes.Select(attrName => $", IHasAttr_{toClassName(attrName)}").JoinStrings("")}}
143+
{
144+
public string TagName => "{{el.elementName}}";
145+
string IHtmlElement.TagStart => "<{{el.elementName}}";
146+
string IHtmlElement.EndTag => "";
147+
ReadOnlySpan<byte> IHtmlElement.TagStartUtf8 => "<{{el.elementName}}"u8;
148+
ReadOnlySpan<byte> IHtmlElement.EndTagUtf8 => ""u8;
149+
public bool ContainsUnescapedText => {{(el.elementName is "script" or "style" ? "true" : "false")}};
150+
HtmlAttributes attrs;
151+
{{el.csUpperName}} IHtmlElement<{{el.csUpperName}}>.ReplaceAttributesWith(HtmlAttributes replacementAttributes) => new {{el.csUpperName}} { attrs = replacementAttributes };
152+
HtmlAttributes IHtmlElement.Attributes => attrs;
153+
IHtmlElement IHtmlElement.ApplyAlteration<THtmlTagAlteration>(THtmlTagAlteration change) => change.AlterEmptyElement(this);
154+
[Pure] public HtmlFragment AsFragment() => HtmlFragment.Element(this);
155+
public static implicit operator HtmlFragment({{el.csUpperName}} tag) => tag.AsFragment();
156+
public static HtmlFragment operator +({{el.csUpperName}} unary) => unary;
157+
public static HtmlFragment operator +({{el.csUpperName}} head, HtmlFragment tail) => HtmlFragment.Fragment(HtmlFragment.Element(head), tail);
158+
public static HtmlFragment operator +(string head, {{el.csUpperName}} tail) => HtmlFragment.Fragment(head, HtmlFragment.Element(tail));
159+
}
160+
"""
161+
: $$"""
162+
163+
public struct {{el.csUpperName}} : IHtmlElementAllowingContent<{{el.csUpperName}}>{{el.attributes.Select(attrName => $", IHasAttr_{toClassName(attrName)}").JoinStrings("")}}
164+
{
165+
public string TagName => "{{el.elementName}}";
166+
string IHtmlElement.TagStart => "<{{el.elementName}}";
167+
string IHtmlElement.EndTag => "</{{el.elementName}}>";
168+
ReadOnlySpan<byte> IHtmlElement.TagStartUtf8 => "<{{el.elementName}}"u8;
169+
ReadOnlySpan<byte> IHtmlElement.EndTagUtf8 => "</{{el.elementName}}>"u8;
170+
public bool ContainsUnescapedText => {{(el.elementName is "script" or "style" ? "true" : "false")}};
171+
HtmlAttributes attrs;
172+
{{el.csUpperName}} IHtmlElement<{{el.csUpperName}}>.ReplaceAttributesWith(HtmlAttributes replacementAttributes) => new {{el.csUpperName}} { attrs = replacementAttributes, children = children };
173+
HtmlAttributes IHtmlElement.Attributes => attrs;
174+
HtmlFragment children;
175+
{{el.csUpperName}} IHtmlElementAllowingContent<{{el.csUpperName}}>.ReplaceContentWith(HtmlFragment replacementContents) => new {{el.csUpperName}} { attrs = attrs, children = replacementContents };
176+
HtmlFragment IHtmlElementAllowingContent.GetContent() => children;
177+
IHtmlElement IHtmlElement.ApplyAlteration<THtmlTagAlteration>(THtmlTagAlteration change) => change.AlterElementAllowingContent(this);
178+
[Pure] public HtmlFragment AsFragment() => HtmlFragment.Element(this);
179+
public static implicit operator HtmlFragment({{el.csUpperName}} tag) => tag.AsFragment();
180+
public static HtmlFragment operator +({{el.csUpperName}} unary) => unary;
181+
public static HtmlFragment operator +({{el.csUpperName}} head, HtmlFragment tail) => HtmlFragment.Fragment(HtmlFragment.Element(head), tail);
182+
public static HtmlFragment operator +(string head, {{el.csUpperName}} tail) => HtmlFragment.Fragment(head, HtmlFragment.Element(tail));
183+
}
184+
"""
191185
);
192186

193187
var elFields = elements
194-
.Select(
195-
el => $"""
188+
.Select(el => $"""
196189
197-
{Regex.Replace(el.elementMetaData.ToString(SaveOptions.None), @"^|(?<=\n)", " ///")}
198-
public static readonly HtmlTagKinds.{el.csUpperName} _{el.csName} = new HtmlTagKinds.{el.csUpperName}();
190+
{Regex.Replace(el.elementMetaData.ToString(SaveOptions.None), @"^|(?<=\n)", " ///")}
191+
public static readonly HtmlTagKinds.{el.csUpperName} _{el.csName} = new HtmlTagKinds.{el.csUpperName}();
199192
200-
"""
193+
"""
201194
);
202195

203196
var diff = new[] {
@@ -275,22 +268,25 @@ string GenerateAttributeLookupTable(
275268
{
276269
// Generate lookup entries for each element
277270
var elementLookups = elements.Select(el => {
278-
var elementAttributes = globalAttributes.Concat(el.attributes).ToDistinctArray();
279-
var attributeEntries = elementAttributes.Select(attrName => {
280-
var methodName = $"_{toClassName(attrName)}";
281-
return $"[\"{attrName}\"] = \"{methodName}\"";
282-
}).JoinStrings(",\n ");
283-
284-
return $"[\"" + el.elementName + "\"] = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase) {\n" +
285-
$" {attributeEntries}\n" +
286-
" },";
287-
}).JoinStrings("\n ");
271+
var elementAttributes = globalAttributes.Concat(el.attributes).ToDistinctArray();
272+
var attributeEntries = elementAttributes.Select(attrName => {
273+
var methodName = $"_{toClassName(attrName)}";
274+
return $"[\"{attrName}\"] = \"{methodName}\"";
275+
}
276+
).JoinStrings(",\n ");
277+
278+
return "[\"" + el.elementName + "\"] = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase) {\n" +
279+
$" {attributeEntries}\n" +
280+
" },";
281+
}
282+
).JoinStrings("\n ");
288283

289284
// Generate default attributes for unknown elements (global attributes only)
290285
var defaultAttributeEntries = globalAttributes.Select(attrName => {
291-
var methodName = $"_{toClassName(attrName)}";
292-
return $"[\"{attrName}\"] = \"{methodName}\"";
293-
}).JoinStrings(",\n ");
286+
var methodName = $"_{toClassName(attrName)}";
287+
return $"[\"{attrName}\"] = \"{methodName}\"";
288+
}
289+
).JoinStrings(",\n ");
294290

295291
return $$"""
296292
#nullable enable
@@ -336,8 +332,7 @@ static string AttrExtensionMethod(string attrName, string applicabilityTypeContr
336332
where THtmlTag : struct{{applicabilityTypeContraint}}, IHtmlElement<THtmlTag>
337333
=> htmlTagExpr.Attribute("{{attrName}}"{{attrValueExpr}});
338334
""";
339-
}
340-
335+
}
341336

342337
static string? AssertFileExistsAndApproveContent(Uri GeneratedOutputFilePath, string generatedCSharpContent)
343338
{

0 commit comments

Comments
 (0)