Skip to content

Commit 90e2dee

Browse files
authored
Merge pull request #93 from HtmlTags/data-annotations
Full support for model metadata
2 parents 76cb13e + 0d13b7b commit 90e2dee

25 files changed

+872
-102
lines changed

Build.ps1

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ exec { & dotnet build -c Release --version-suffix=$buildSuffix -v q /nologo }
3838
Push-Location -Path .\test\HtmlTags.Testing
3939

4040
try {
41-
exec { & dotnet xunit -configuration Release -nobuild }
41+
exec { & dotnet xunit -configuration Release -nobuild -fxversion 2.0.0 }
4242
}
4343
finally {
4444
Pop-Location
@@ -47,7 +47,7 @@ finally {
4747
Push-Location -Path .\test\HtmlTags.AspNetCore.Testing
4848

4949
try {
50-
exec { & dotnet xunit -configuration Release -nobuild }
50+
exec { & dotnet xunit -configuration Release -nobuild -fxversion 2.0.0 }
5151
}
5252
finally {
5353
Pop-Location

Directory.Build.props

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<Project>
2+
3+
<PropertyGroup>
4+
<Description>Easy generation of html with a jquery inspired object model</Description>
5+
<Copyright>Copyright Jeremy D. Miller, Josh Arnold, Joshua Flanagan, Jimmy Bogard, et al. All rights reserved.</Copyright>
6+
<VersionPrefix>7.0.0</VersionPrefix>
7+
<Authors>Jeremy D. Miller;Joshua Flanagan;Josh Arnold;Jimmy Bogard</Authors>
8+
<LangVersion>latest</LangVersion>
9+
<WarningsAsErrors>true</WarningsAsErrors>
10+
<NoWarn>$(NoWarn);1701;1702;1591</NoWarn>
11+
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
12+
<PackageIconUrl>https://raw.githubusercontent.com/HtmlTags/htmltags/master/logo/FubuHtml_256.png</PackageIconUrl>
13+
<PackageProjectUrl>https://github.com/HtmlTags/htmltags</PackageProjectUrl>
14+
<PackageLicenseUrl>https://github.com/HtmlTags/htmltags/raw/master/license.txt</PackageLicenseUrl>
15+
</PropertyGroup>
16+
17+
</Project>

HtmlTags.sln

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
99
.gitignore = .gitignore
1010
appveyor.yml = appveyor.yml
1111
Build.ps1 = Build.ps1
12-
NuGet.config = NuGet.config
12+
Directory.Build.props = Directory.Build.props
1313
..\readme.md = ..\readme.md
1414
EndProjectSection
1515
EndProject

NuGet.Config

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<configuration>
3+
<packageSources>
4+
<add key="nuget.org" value="https://api.nuget.org/v3/index.json" protocolVersion="3" />
5+
</packageSources>
6+
</configuration>

NuGet.config

Lines changed: 0 additions & 9 deletions
This file was deleted.

UpgradeLog.htm

-57.9 KB
Binary file not shown.

src/HtmlTags.AspNetCore.TestSite/Controllers/HomeController.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.ComponentModel.DataAnnotations;
34
using System.Linq;
45
using System.Threading.Tasks;
56
using Microsoft.AspNetCore.Mvc;
@@ -19,8 +20,12 @@ public HomeIndexModel()
1920
}
2021
public class BlargModel
2122
{
23+
[Required]
24+
[MinLength(10)]
2225
public string Blorg { get; set; }
2326
}
27+
[Required]
28+
[MaxLength(20)]
2429
public string Value { get; set; }
2530
public Blarg Blarg { get; set; }
2631

src/HtmlTags.AspNetCore.TestSite/Views/Home/Index.cshtml

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@
6767
<a class="right carousel-control" href="#myCarousel" role="button" data-slide="next">
6868
<span class="glyphicon glyphicon-chevron-right" aria-hidden="true"></span>
6969
<span class="sr-only">Next</span>
70+
7071
</a>
7172
</div>
7273

@@ -86,26 +87,37 @@
8687

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

89-
@(Html.ButtonGroup(Html.PrimaryButton("Save"),
90-
Html.LinkButton("Cancel", nameof(HomeController.Index))))
91-
90+
@(Html.ButtonGroup(Html.PrimaryButton("Save"),
91+
Html.LinkButton("Cancel", nameof(HomeController.Index))))
9292

93+
Input Tag value:
9394
<input-tag for="Value" class="foo"></input-tag>
95+
Editor for value:
9496
@Html.EditorFor(m => m.Value)
97+
Input tag blarg:
9598
<input-tag for="Blarg"></input-tag>
99+
Editor for blarg:
96100
@Html.EditorFor(m => m.Blarg)
101+
Input tag blorg.blorg:
97102
<input-tag for="Blorg.Blorg"></input-tag>
103+
Editor for blorg.blorg:
98104
@Html.EditorFor(m => m.Blorg.Blorg)
105+
Input tag Blorgs[0].Blarg:
99106
<input-tag for="Blorgs[0].Blorg"></input-tag>
107+
Editor for Blorgs[0].Blarg:
100108
@Html.EditorFor(m => m.Blorgs[0].Blorg)
101109
@Html.Input(m => m.Value).AddClass("foo")
110+
Display tag for Value:
102111
<display-tag for="Value"></display-tag>
112+
Display for Vlaue:
103113
@Html.Display(m => m.Value)
114+
Label tag for Value:
104115
<label-tag for="Value"></label-tag>
116+
Label for Value:
105117
@Html.Label(m => m.Value)
106-
118+
107119
@{
108-
var tag = new HtmlTag("canvas");
120+
var tag = new HtmlTag("canvas");
109121
}
110122
@tag
111123

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<configuration>
3-
43
<!--
54
Configure your application settings in appsettings.json. Learn more at http://go.microsoft.com/fwlink/?LinkId=786380
65
-->
7-
86
<system.webServer>
97
<handlers>
10-
<add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModule" resourceType="Unspecified"/>
8+
<add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModule" resourceType="Unspecified" />
119
</handlers>
12-
<aspNetCore processPath="%LAUNCHER_PATH%" arguments="%LAUNCHER_ARGS%" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" forwardWindowsAuthToken="false"/>
10+
<aspNetCore processPath="%LAUNCHER_PATH%" arguments="%LAUNCHER_ARGS%" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" forwardWindowsAuthToken="false" startupTimeLimit="3600" requestTimeout="23:00:00" />
1311
</system.webServer>
14-
</configuration>
12+
</configuration>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
namespace HtmlTags
2+
{
3+
public class ElementName
4+
{
5+
public ElementName(string value) => Value = value;
6+
7+
public string Value { get; }
8+
}
9+
}
Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
namespace HtmlTags
1+
using System.Linq;
2+
using HtmlTags.Reflection;
3+
using Microsoft.AspNetCore.Mvc.ViewFeatures;
4+
using Microsoft.AspNetCore.Mvc.ViewFeatures.Internal;
5+
6+
namespace HtmlTags
27
{
38
using System;
49
using System.Linq.Expressions;
@@ -9,39 +14,51 @@
914

1015
public static class HtmlHelperExtensions
1116
{
12-
public static HtmlTag Input<T>(this IHtmlHelper<T> helper, Expression<Func<T, object>> expression)
17+
private static readonly DotNotationElementNamingConvention NamingConvention = new DotNotationElementNamingConvention();
18+
19+
public static HtmlTag Input<T, TResult>(this IHtmlHelper<T> helper, Expression<Func<T, TResult>> expression)
1320
where T : class
1421
{
15-
var generator = GetGenerator(helper);
22+
var generator = GetGenerator(helper, expression);
1623
return generator.InputFor(expression);
1724
}
1825

19-
public static HtmlTag Label<T>(this IHtmlHelper<T> helper, Expression<Func<T, object>> expression)
26+
public static HtmlTag Label<T, TResult>(this IHtmlHelper<T> helper, Expression<Func<T, TResult>> expression)
2027
where T : class
2128
{
22-
var generator = GetGenerator(helper);
29+
var generator = GetGenerator(helper, expression);
2330
return generator.LabelFor(expression);
2431
}
2532

26-
public static HtmlTag Display<T>(this IHtmlHelper<T> helper, Expression<Func<T, object>> expression)
33+
public static HtmlTag Display<T, TResult>(this IHtmlHelper<T> helper, Expression<Func<T, TResult>> expression)
2734
where T : class
2835
{
29-
var generator = GetGenerator(helper);
36+
var generator = GetGenerator(helper, expression);
3037
return generator.DisplayFor(expression);
3138
}
3239

33-
public static HtmlTag Tag<T>(this IHtmlHelper<T> helper, Expression<Func<T, object>> expression, string category)
40+
public static HtmlTag Tag<T, TResult>(this IHtmlHelper<T> helper, Expression<Func<T, TResult>> expression, string category)
3441
where T : class
3542
{
36-
var generator = GetGenerator(helper);
43+
var generator = GetGenerator(helper, expression);
3744
return generator.TagFor(expression, category);
3845
}
3946

40-
public static IElementGenerator<T> GetGenerator<T>(IHtmlHelper<T> helper) where T : class
47+
public static IElementGenerator<T> GetGenerator<T, TResult>(IHtmlHelper<T> helper, Expression<Func<T, TResult>> expression) where T : class
4148
{
42-
var library = helper.ViewContext.HttpContext.RequestServices.GetService<HtmlConventionLibrary>();
43-
return ElementGenerator<T>.For(library, t => helper.ViewContext.HttpContext.RequestServices.GetService(t), helper.ViewData.Model);
49+
var modelExplorer =
50+
ExpressionMetadataProvider.FromLambdaExpression(expression, helper.ViewData, helper.MetadataProvider);
51+
52+
var elementName = new ElementName(NamingConvention.GetName(typeof(T), expression.ToAccessor()));
53+
54+
return GetGenerator(helper, modelExplorer, helper.ViewContext, elementName);
4455
}
4556

57+
public static IElementGenerator<T> GetGenerator<T>(IHtmlHelper<T> helper, params object[] additionalServices) where T : class
58+
{
59+
var library = helper.ViewContext.HttpContext.RequestServices.GetService<HtmlConventionLibrary>();
60+
object ServiceLocator(Type t) => additionalServices.FirstOrDefault(t.IsInstanceOfType) ?? helper.ViewContext.HttpContext.RequestServices.GetService(t);
61+
return ElementGenerator<T>.For(library, ServiceLocator, helper.ViewData.Model);
62+
}
4663
}
4764
}

src/HtmlTags.AspNetCore/HtmlTagTagHelper.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Linq;
23

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

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

37-
var tagGenerator = new TagGenerator(library.TagLibrary, new ActiveProfile(), t => ViewContext.HttpContext.RequestServices.GetService(t));
38+
var additionalServices = new object[]
39+
{
40+
For.ModelExplorer,
41+
ViewContext,
42+
new ElementName(For.Name)
43+
};
44+
45+
object ServiceLocator(Type t) => additionalServices.FirstOrDefault(t.IsInstanceOfType) ?? ViewContext.HttpContext.RequestServices.GetService(t);
46+
47+
var tagGenerator = new TagGenerator(library.TagLibrary, new ActiveProfile(), ServiceLocator);
3848

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

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,12 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

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

1812
<ItemGroup>
@@ -25,10 +19,8 @@
2519
<PackageReference Include="Microsoft.AspNetCore.Mvc.ViewFeatures" Version="2.0.0" />
2620
<PackageReference Include="Microsoft.AspNetCore.Mvc.Razor" Version="2.0.0" />
2721
<PackageReference Include="Microsoft.AspNetCore.Mvc.TagHelpers" Version="2.0.0" />
22+
<PackageReference Include="Microsoft.AspNetCore.Mvc.DataAnnotations" Version="2.0.0" />
2823
<PackageReference Include="System.Diagnostics.Process" Version="4.3.0" />
2924
</ItemGroup>
3025

31-
<ItemGroup>
32-
<Folder Include="Properties\" />
33-
</ItemGroup>
3426
</Project>

0 commit comments

Comments
 (0)