Skip to content

Commit

Permalink
Merge pull request #93 from HtmlTags/data-annotations
Browse files Browse the repository at this point in the history
Full support for model metadata
  • Loading branch information
jbogard authored Mar 26, 2018
2 parents 76cb13e + 0d13b7b commit 90e2dee
Show file tree
Hide file tree
Showing 25 changed files with 872 additions and 102 deletions.
4 changes: 2 additions & 2 deletions Build.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ exec { & dotnet build -c Release --version-suffix=$buildSuffix -v q /nologo }
Push-Location -Path .\test\HtmlTags.Testing

try {
exec { & dotnet xunit -configuration Release -nobuild }
exec { & dotnet xunit -configuration Release -nobuild -fxversion 2.0.0 }
}
finally {
Pop-Location
Expand All @@ -47,7 +47,7 @@ finally {
Push-Location -Path .\test\HtmlTags.AspNetCore.Testing

try {
exec { & dotnet xunit -configuration Release -nobuild }
exec { & dotnet xunit -configuration Release -nobuild -fxversion 2.0.0 }
}
finally {
Pop-Location
Expand Down
17 changes: 17 additions & 0 deletions Directory.Build.props
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<Project>

<PropertyGroup>
<Description>Easy generation of html with a jquery inspired object model</Description>
<Copyright>Copyright Jeremy D. Miller, Josh Arnold, Joshua Flanagan, Jimmy Bogard, et al. All rights reserved.</Copyright>
<VersionPrefix>7.0.0</VersionPrefix>
<Authors>Jeremy D. Miller;Joshua Flanagan;Josh Arnold;Jimmy Bogard</Authors>
<LangVersion>latest</LangVersion>
<WarningsAsErrors>true</WarningsAsErrors>
<NoWarn>$(NoWarn);1701;1702;1591</NoWarn>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<PackageIconUrl>https://raw.githubusercontent.com/HtmlTags/htmltags/master/logo/FubuHtml_256.png</PackageIconUrl>
<PackageProjectUrl>https://github.com/HtmlTags/htmltags</PackageProjectUrl>
<PackageLicenseUrl>https://github.com/HtmlTags/htmltags/raw/master/license.txt</PackageLicenseUrl>
</PropertyGroup>

</Project>
2 changes: 1 addition & 1 deletion HtmlTags.sln
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
.gitignore = .gitignore
appveyor.yml = appveyor.yml
Build.ps1 = Build.ps1
NuGet.config = NuGet.config
Directory.Build.props = Directory.Build.props
..\readme.md = ..\readme.md
EndProjectSection
EndProject
Expand Down
6 changes: 6 additions & 0 deletions NuGet.Config
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<packageSources>
<add key="nuget.org" value="https://api.nuget.org/v3/index.json" protocolVersion="3" />
</packageSources>
</configuration>
9 changes: 0 additions & 9 deletions NuGet.config

This file was deleted.

Binary file removed UpgradeLog.htm
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
Expand All @@ -19,8 +20,12 @@ public HomeIndexModel()
}
public class BlargModel
{
[Required]
[MinLength(10)]
public string Blorg { get; set; }
}
[Required]
[MaxLength(20)]
public string Value { get; set; }
public Blarg Blarg { get; set; }

Expand Down
22 changes: 17 additions & 5 deletions src/HtmlTags.AspNetCore.TestSite/Views/Home/Index.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
<a class="right carousel-control" href="#myCarousel" role="button" data-slide="next">
<span class="glyphicon glyphicon-chevron-right" aria-hidden="true"></span>
<span class="sr-only">Next</span>

</a>
</div>

Expand All @@ -86,26 +87,37 @@

@Html.Lookup(nameof(Model.Value)).RemoveClass("readonly")

@(Html.ButtonGroup(Html.PrimaryButton("Save"),
Html.LinkButton("Cancel", nameof(HomeController.Index))))

@(Html.ButtonGroup(Html.PrimaryButton("Save"),
Html.LinkButton("Cancel", nameof(HomeController.Index))))

Input Tag value:
<input-tag for="Value" class="foo"></input-tag>
Editor for value:
@Html.EditorFor(m => m.Value)
Input tag blarg:
<input-tag for="Blarg"></input-tag>
Editor for blarg:
@Html.EditorFor(m => m.Blarg)
Input tag blorg.blorg:
<input-tag for="Blorg.Blorg"></input-tag>
Editor for blorg.blorg:
@Html.EditorFor(m => m.Blorg.Blorg)
Input tag Blorgs[0].Blarg:
<input-tag for="Blorgs[0].Blorg"></input-tag>
Editor for Blorgs[0].Blarg:
@Html.EditorFor(m => m.Blorgs[0].Blorg)
@Html.Input(m => m.Value).AddClass("foo")
Display tag for Value:
<display-tag for="Value"></display-tag>
Display for Vlaue:
@Html.Display(m => m.Value)
Label tag for Value:
<label-tag for="Value"></label-tag>
Label for Value:
@Html.Label(m => m.Value)

@{
var tag = new HtmlTag("canvas");
var tag = new HtmlTag("canvas");
}
@tag

Expand Down
8 changes: 3 additions & 5 deletions src/HtmlTags.AspNetCore.TestSite/web.config
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
<?xml version="1.0" encoding="utf-8"?>
<configuration>

<!--
Configure your application settings in appsettings.json. Learn more at http://go.microsoft.com/fwlink/?LinkId=786380
-->

<system.webServer>
<handlers>
<add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModule" resourceType="Unspecified"/>
<add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModule" resourceType="Unspecified" />
</handlers>
<aspNetCore processPath="%LAUNCHER_PATH%" arguments="%LAUNCHER_ARGS%" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" forwardWindowsAuthToken="false"/>
<aspNetCore processPath="%LAUNCHER_PATH%" arguments="%LAUNCHER_ARGS%" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" forwardWindowsAuthToken="false" startupTimeLimit="3600" requestTimeout="23:00:00" />
</system.webServer>
</configuration>
</configuration>
9 changes: 9 additions & 0 deletions src/HtmlTags.AspNetCore/ElementName.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace HtmlTags
{
public class ElementName
{
public ElementName(string value) => Value = value;

public string Value { get; }
}
}
41 changes: 29 additions & 12 deletions src/HtmlTags.AspNetCore/HtmlHelperExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
namespace HtmlTags
using System.Linq;
using HtmlTags.Reflection;
using Microsoft.AspNetCore.Mvc.ViewFeatures;
using Microsoft.AspNetCore.Mvc.ViewFeatures.Internal;

namespace HtmlTags
{
using System;
using System.Linq.Expressions;
Expand All @@ -9,39 +14,51 @@

public static class HtmlHelperExtensions
{
public static HtmlTag Input<T>(this IHtmlHelper<T> helper, Expression<Func<T, object>> expression)
private static readonly DotNotationElementNamingConvention NamingConvention = new DotNotationElementNamingConvention();

public static HtmlTag Input<T, TResult>(this IHtmlHelper<T> helper, Expression<Func<T, TResult>> expression)
where T : class
{
var generator = GetGenerator(helper);
var generator = GetGenerator(helper, expression);
return generator.InputFor(expression);
}

public static HtmlTag Label<T>(this IHtmlHelper<T> helper, Expression<Func<T, object>> expression)
public static HtmlTag Label<T, TResult>(this IHtmlHelper<T> helper, Expression<Func<T, TResult>> expression)
where T : class
{
var generator = GetGenerator(helper);
var generator = GetGenerator(helper, expression);
return generator.LabelFor(expression);
}

public static HtmlTag Display<T>(this IHtmlHelper<T> helper, Expression<Func<T, object>> expression)
public static HtmlTag Display<T, TResult>(this IHtmlHelper<T> helper, Expression<Func<T, TResult>> expression)
where T : class
{
var generator = GetGenerator(helper);
var generator = GetGenerator(helper, expression);
return generator.DisplayFor(expression);
}

public static HtmlTag Tag<T>(this IHtmlHelper<T> helper, Expression<Func<T, object>> expression, string category)
public static HtmlTag Tag<T, TResult>(this IHtmlHelper<T> helper, Expression<Func<T, TResult>> expression, string category)
where T : class
{
var generator = GetGenerator(helper);
var generator = GetGenerator(helper, expression);
return generator.TagFor(expression, category);
}

public static IElementGenerator<T> GetGenerator<T>(IHtmlHelper<T> helper) where T : class
public static IElementGenerator<T> GetGenerator<T, TResult>(IHtmlHelper<T> helper, Expression<Func<T, TResult>> expression) where T : class
{
var library = helper.ViewContext.HttpContext.RequestServices.GetService<HtmlConventionLibrary>();
return ElementGenerator<T>.For(library, t => helper.ViewContext.HttpContext.RequestServices.GetService(t), helper.ViewData.Model);
var modelExplorer =
ExpressionMetadataProvider.FromLambdaExpression(expression, helper.ViewData, helper.MetadataProvider);

var elementName = new ElementName(NamingConvention.GetName(typeof(T), expression.ToAccessor()));

return GetGenerator(helper, modelExplorer, helper.ViewContext, elementName);
}

public static IElementGenerator<T> GetGenerator<T>(IHtmlHelper<T> helper, params object[] additionalServices) where T : class
{
var library = helper.ViewContext.HttpContext.RequestServices.GetService<HtmlConventionLibrary>();
object ServiceLocator(Type t) => additionalServices.FirstOrDefault(t.IsInstanceOfType) ?? helper.ViewContext.HttpContext.RequestServices.GetService(t);
return ElementGenerator<T>.For(library, ServiceLocator, helper.ViewData.Model);
}
}
}
12 changes: 11 additions & 1 deletion src/HtmlTags.AspNetCore/HtmlTagTagHelper.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Linq;

namespace HtmlTags
{
Expand Down Expand Up @@ -34,7 +35,16 @@ public override void Process(TagHelperContext context, TagHelperOutput output)

var library = ViewContext.HttpContext.RequestServices.GetService<HtmlConventionLibrary>();

var tagGenerator = new TagGenerator(library.TagLibrary, new ActiveProfile(), t => ViewContext.HttpContext.RequestServices.GetService(t));
var additionalServices = new object[]
{
For.ModelExplorer,
ViewContext,
new ElementName(For.Name)
};

object ServiceLocator(Type t) => additionalServices.FirstOrDefault(t.IsInstanceOfType) ?? ViewContext.HttpContext.RequestServices.GetService(t);

var tagGenerator = new TagGenerator(library.TagLibrary, new ActiveProfile(), ServiceLocator);

var tag = tagGenerator.Build(request, Category);

Expand Down
12 changes: 2 additions & 10 deletions src/HtmlTags.AspNetCore/HtmlTags.AspNetCore.csproj
Original file line number Diff line number Diff line change
@@ -1,18 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<Description>Easy generation of html with a jquery inspired object model</Description>
<Copyright>Copyright 2008-2016 Jeremy D. Miller, Josh Arnold, Joshua Flanagan, et al. All rights reserved.</Copyright>
<VersionPrefix>6.0.0</VersionPrefix>
<Authors>Jeremy D. Miller;Joshua Flanagan;Josh Arnold</Authors>
<TargetFrameworks>netstandard2.0</TargetFrameworks>
<DefineConstants>$(DefineConstants);ASPNETCORE</DefineConstants>
<AssemblyName>HtmlTags.AspNetCore</AssemblyName>
<RootNamespace>HtmlTags</RootNamespace>
<PackageId>HtmlTags.AspNetCore</PackageId>
<PackageTags>html;ASP.NET MVC</PackageTags>
<PackageIconUrl>https://raw.githubusercontent.com/HtmlTags/htmltags/master/logo/FubuHtml_256.png</PackageIconUrl>
<PackageProjectUrl>https://github.com/HtmlTags/htmltags</PackageProjectUrl>
<PackageLicenseUrl>https://github.com/HtmlTags/htmltags/raw/master/license.txt</PackageLicenseUrl>
</PropertyGroup>

<ItemGroup>
Expand All @@ -25,10 +19,8 @@
<PackageReference Include="Microsoft.AspNetCore.Mvc.ViewFeatures" Version="2.0.0" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Razor" Version="2.0.0" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.TagHelpers" Version="2.0.0" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.DataAnnotations" Version="2.0.0" />
<PackageReference Include="System.Diagnostics.Process" Version="4.3.0" />
</ItemGroup>

<ItemGroup>
<Folder Include="Properties\" />
</ItemGroup>
</Project>
Loading

0 comments on commit 90e2dee

Please sign in to comment.