FriendToNetWebDevelopers.HtmlAttributeDictionary 1.2.2

There is a newer version of this package available.
See the version list below for details.
dotnet add package FriendToNetWebDevelopers.HtmlAttributeDictionary --version 1.2.2                
NuGet\Install-Package FriendToNetWebDevelopers.HtmlAttributeDictionary -Version 1.2.2                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="FriendToNetWebDevelopers.HtmlAttributeDictionary" Version="1.2.2" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add FriendToNetWebDevelopers.HtmlAttributeDictionary --version 1.2.2                
#r "nuget: FriendToNetWebDevelopers.HtmlAttributeDictionary, 1.2.2"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install FriendToNetWebDevelopers.HtmlAttributeDictionary as a Cake Addin
#addin nuget:?package=FriendToNetWebDevelopers.HtmlAttributeDictionary&version=1.2.2

// Install FriendToNetWebDevelopers.HtmlAttributeDictionary as a Cake Tool
#tool nuget:?package=FriendToNetWebDevelopers.HtmlAttributeDictionary&version=1.2.2                

HTML Attribute Dictionary

Summary

A dictionary which extends Dictionary<string,string> and provides html attribute and value safety features.

Some of these can change regularly. Please see the CHANGELOG.md file for more details.

Installation

Install-Package FriendToNetWebDevelopers.HtmlAttributeDictionary

Usage

How?

Add the following to your _ViewImports.cshtml

@using FriendToNetWebDevelopers.HtmlAttributeDictionary
@addTagHelper *, FriendToNetWebDevelopers.HtmlAttributeDictionary

This is the simplest use case I could come up with.

@{  //                                 Please note the multiple classes being brought in
    //                                          Along with your id ↓         ↓
    //                                                      ↓      ↓         ↓
    var dictionary = HtmlAttributeDictionaryFactory.WithId("foo", "some-hero bar");
    if(needsScrim)
    {
        dictionary.AddClassToClassAttribute("hero-scrim");
    }
}
<section>
    
    <div html-attributes="dictionary">
        
    </div>
</section>

-or just with classes-

@{
    var dictionary = HtmlAttributeDictionaryFactory.WithClasses("some-hero bar");
    if(needsScrim)
    {
        dictionary.AddClassToClassAttribute("hero-scrim");
    }
}
<section>
    
    
    <div @Html.Raw(dictionary)>
        
    </div>
</section>

But can be initialized in the following ways as well.

//Provides an empty dictionary
var dictionary = HtmlAttributeDictionaryFactory.Get();

//ALSO provides an empty dictionary
dictionary = new HtmlAttributeDictionary();

//Follows the safety procedures to attempt to bring in the attributes from a different type of dictionary
var someOtherDictionary = new Dictionary<string, string>();
dictionary = new HtmlAttributeDictionary(someOtherDictionary);

//Follows the safety procedures to attempt to bring in the attributes from a list of key-value-pairs
var list = new KeyValuePair<string,string>();
dictionary = new HtmlAttributeDictionary(list);

While I like using the factory when producing the attribute dictionary inline, that's just because of the shortcuts it provides. This can be produced entirely dynamically and still work.

Why?

This keeps things simple and safe. It even allows for data to come in from an unsafe source and be safe to display.

It even prevents invalid attributes from being added.

What if there's a problem

Please feel free to report any issues here on GitHub.

Include as many details as possible and one of us will check it out.

Thanks

A big thank you to Joshua Hess for his initial review and contribution of the Tag Helper feature.

Heartland Business Systems Logo

I would also like to extend my gratitude to my employer, Heartland Business Systems, for putting me in a position to help others by providing this helper library to others free of charge. They've supported me in creating this work and share it openly with .NET Web Developers everywhere.

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.2.6 106 7/17/2024
1.2.5 105 6/24/2024
1.2.4 93 6/24/2024
1.2.3 88 6/7/2024
1.2.2 104 6/7/2024
1.2.1 103 6/5/2024
1.2.0 125 5/29/2024
1.1.0 110 5/28/2024
1.0.1 112 5/27/2024

Added ability to use on openeing tag only