-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add new merge default classes tag helper
- Loading branch information
Showing
6 changed files
with
247 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
namespace Tailwind.Css.TagHelpers; | ||
|
||
using System.Text.Encodings.Web; | ||
|
||
using Microsoft.AspNetCore.Html; | ||
using Microsoft.AspNetCore.Mvc.TagHelpers; | ||
using Microsoft.AspNetCore.Razor.TagHelpers; | ||
using Microsoft.Extensions.Options; | ||
|
||
[HtmlTargetElement("*", Attributes = ForAttributeName)] | ||
public class MergeDefaultClassTagHelper : TagHelper | ||
{ | ||
protected const string ForAttributeName = "merge-classes"; | ||
protected const string DefaultClassAttributeName = "default-class"; | ||
|
||
private readonly TagOptions _settings; | ||
|
||
public MergeDefaultClassTagHelper(IOptions<TagOptions> settings) | ||
=> _settings = settings?.Value ?? throw new ArgumentNullException(nameof(settings)); | ||
|
||
/// <summary> | ||
/// The classes to merge into the main class list. | ||
/// </summary> | ||
[HtmlAttributeName(DefaultClassAttributeName)] | ||
public string? DefaultClass { get; set; } | ||
|
||
public override void Process(TagHelperContext context, TagHelperOutput output) | ||
{ | ||
ArgumentNullException.ThrowIfNull(output); | ||
|
||
var classList = Utilities.SplitClassList(DefaultClass); | ||
|
||
if (_settings.IncludeComments) | ||
{ | ||
output.PreElement.AppendHtmlLine("<!--"); | ||
|
||
output.PreElement.Append(" Base: "); | ||
output.PreElement.AppendLine(output.Attributes.GetValue("class")); | ||
|
||
output.PreElement.Append(" Default: "); | ||
output.PreElement.AppendLine(DefaultClass ?? ""); | ||
|
||
output.PreElement.AppendHtmlLine("-->"); | ||
} | ||
|
||
if (classList?.Length > 0) | ||
{ | ||
foreach (var className in classList) | ||
{ | ||
output.AddClass(className, HtmlEncoder.Default); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,149 @@ | ||
namespace Tailwind.Css.TagHelpers; | ||
|
||
using Microsoft.AspNetCore.Html; | ||
using Microsoft.AspNetCore.Razor.TagHelpers; | ||
using Microsoft.Extensions.Options; | ||
|
||
public class MergeDefaultClassTagHelperTests : TagHelperTestBase | ||
{ | ||
[Fact] | ||
public void Should_set_to_defaults_when_class_is_empty() | ||
{ | ||
// Given | ||
var context = MakeTagHelperContext( | ||
tagName: "div"); | ||
var output = MakeTagHelperOutput( | ||
tagName: "div"); | ||
|
||
var options = Options.Create(new TagOptions()); | ||
var helper = new MergeDefaultClassTagHelper(options) | ||
{ | ||
DefaultClass = "bg-white text-black", | ||
}; | ||
|
||
// When | ||
helper.Process(context, output); | ||
|
||
// Then | ||
output.Attributes["class"].ShouldNotBeNull(); | ||
|
||
var classList = output.Attributes["class"].Value as HtmlString; | ||
classList.ShouldNotBeNull(); | ||
classList.Value.ShouldBe("bg-white text-black"); | ||
} | ||
|
||
[Fact] | ||
public void Should_merge_defaults_into_existing_list() | ||
{ | ||
// Given | ||
var context = MakeTagHelperContext( | ||
tagName: "div", | ||
new TagHelperAttributeList | ||
{ | ||
{ "class", "flex flex-col" }, | ||
}); | ||
var output = MakeTagHelperOutput( | ||
tagName: "div", | ||
new TagHelperAttributeList | ||
{ | ||
{ "class", "flex flex-col" }, | ||
}); | ||
|
||
var options = Options.Create(new TagOptions()); | ||
var helper = new MergeDefaultClassTagHelper(options) | ||
{ | ||
DefaultClass = "bg-white text-black", | ||
}; | ||
|
||
// When | ||
helper.Process(context, output); | ||
|
||
// Then | ||
output.Attributes["class"].ShouldNotBeNull(); | ||
|
||
var classList = output.Attributes["class"].Value as HtmlString; | ||
classList.ShouldNotBeNull(); | ||
classList.Value.ShouldBe("flex flex-col bg-white text-black"); | ||
} | ||
|
||
[Theory] | ||
[InlineData(null)] | ||
[InlineData("flex flex-col")] | ||
public void Should_not_emit_comments_when_setting_is_disabled(string? defaultClasses) | ||
{ | ||
// Given | ||
var attributeList = new TagHelperAttributeList(); | ||
|
||
if (defaultClasses is not null) | ||
{ | ||
attributeList.Add("class", defaultClasses); | ||
} | ||
|
||
var context = MakeTagHelperContext( | ||
tagName: "div", | ||
attributeList); | ||
var output = MakeTagHelperOutput( | ||
tagName: "div", | ||
attributeList); | ||
|
||
var options = Options.Create( | ||
new TagOptions | ||
{ | ||
IncludeComments = false, | ||
}); | ||
var helper = new MergeDefaultClassTagHelper(options) | ||
{ | ||
DefaultClass = "bg-white underline", | ||
}; | ||
|
||
// When | ||
helper.Process(context, output); | ||
|
||
// Then | ||
output.PreElement.GetContent().ShouldBeEmpty(); | ||
} | ||
|
||
[Theory] | ||
[InlineData(null)] | ||
[InlineData("flex flex-col")] | ||
public void Should_emit_comments_when_setting_is_enabled(string? defaultClasses) | ||
{ | ||
// Given | ||
var attributeList = new TagHelperAttributeList(); | ||
|
||
if (defaultClasses is not null) | ||
{ | ||
attributeList.Add("class", defaultClasses); | ||
} | ||
|
||
var context = MakeTagHelperContext( | ||
tagName: "div", | ||
attributeList); | ||
var output = MakeTagHelperOutput( | ||
tagName: "div", | ||
attributeList); | ||
|
||
var options = Options.Create( | ||
new TagOptions | ||
{ | ||
IncludeComments = true, | ||
}); | ||
var helper = new MergeDefaultClassTagHelper(options) | ||
{ | ||
DefaultClass = "bg-white text-black", | ||
}; | ||
|
||
// When | ||
helper.Process(context, output); | ||
|
||
// Then | ||
output.PreElement.GetContent().ShouldBe( | ||
$""" | ||
<!-- | ||
Base: {defaultClasses} | ||
Default: bg-white text-black | ||
--> | ||
"""); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters