Skip to content

Commit 6c66b41

Browse files
authored
Added support for scoped css attribute and .net preview 8 (#195)
* Added support for scoped css attribute * updated changelog
1 parent 5ca56c7 commit 6c66b41

File tree

10 files changed

+82
-16
lines changed

10 files changed

+82
-16
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ List of new features.
1313

1414
- Added `InvokeAsync(Func<Task>)` to `RenderedComponentInvokeAsyncExtensions`. By [@JeroenBos](https://github.com/JeroenBos) in [#151](https://github.com/egil/bUnit/pull/177).
1515
- Added `ITestRenderer Renderer { get ; }` to `IRenderedFragment` to make it possible to simplify the `IRenderedComponentBase<TComponent>` interface. By [@JeroenBos](https://github.com/JeroenBos) in [#151](https://github.com/egil/bUnit/pull/177).
16+
- Added support for scoped CSS to `MarkupMatches` and related comparer methods. By [@egil](https://github.com/egil) in [#195](https://github.com/egil/bUnit/pull/195).
1617

1718
### Changed
1819
List of changes in existing functionality.

src/Directory.Build.props

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,11 @@
3636
</ItemGroup>
3737

3838
<ItemGroup Condition="'$(TargetFramework)' == 'net5.0' AND $(MSBuildProjectName)!='bunit.template' AND $(MSBuildProjectName)!='bunit'">
39-
<PackageReference Include="Microsoft.AspNetCore.Components.Authorization" Version="5.0.0-preview.7.*" />
40-
<PackageReference Include="Microsoft.Extensions.Logging" Version="5.0.0-preview.7.*" />
41-
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="5.0.0-preview.7.*" />
42-
<PackageReference Include="Microsoft.AspNetCore.Components" Version="5.0.0-preview.7.*" />
43-
<PackageReference Include="Microsoft.AspNetCore.Components.Web" Version="5.0.0-preview.7.*" />
39+
<PackageReference Include="Microsoft.AspNetCore.Components.Authorization" Version="5.0.0-preview.8.*" />
40+
<PackageReference Include="Microsoft.Extensions.Logging" Version="5.0.0-preview.8.*" />
41+
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="5.0.0-preview.8.*" />
42+
<PackageReference Include="Microsoft.AspNetCore.Components" Version="5.0.0-preview.8.*" />
43+
<PackageReference Include="Microsoft.AspNetCore.Components.Web" Version="5.0.0-preview.8.*" />
4444
</ItemGroup>
4545

4646
<ItemGroup Condition="$(MSBuildProjectName)!='bunit.template' AND $(MSBuildProjectName)!='bunit'">

src/bunit.web/Rendering/Internal/Htmlizer.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,13 @@ internal class Htmlizer
2626
};
2727

2828
private const string BLAZOR_INTERNAL_ATTR_PREFIX = "__internal_";
29+
private const string BLAZOR_CSS_SCOPE_ATTR_PREFIX = "b-";
2930
public const string BLAZOR_ATTR_PREFIX = "blazor:";
3031
public const string ELEMENT_REFERENCE_ATTR_NAME = BLAZOR_ATTR_PREFIX + "elementreference";
3132

3233
public static bool IsBlazorAttribute(string attributeName)
33-
=> attributeName.StartsWith(BLAZOR_ATTR_PREFIX, StringComparison.Ordinal);
34+
=> attributeName.StartsWith(BLAZOR_ATTR_PREFIX, StringComparison.Ordinal) ||
35+
attributeName.StartsWith(BLAZOR_CSS_SCOPE_ATTR_PREFIX, StringComparison.Ordinal);
3436

3537
public static string ToBlazorAttribute(string attributeName)
3638
{

tests/Directory.Build.props

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@
1515
</ItemGroup>
1616

1717
<ItemGroup Condition="'$(TargetFramework)' == 'net5.0' AND $(MSBuildProjectName)!='bunit.testassets'">
18-
<PackageReference Include="Microsoft.Extensions.Logging" Version="5.0.0-preview.7.*" />
19-
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="5.0.0-preview.7.*" />
20-
<PackageReference Include="Microsoft.AspNetCore.Components" Version="5.0.0-preview.7.*" />
21-
<PackageReference Include="Microsoft.AspNetCore.Components.Web" Version="5.0.0-preview.7.*" />
18+
<PackageReference Include="Microsoft.Extensions.Logging" Version="5.0.0-preview.8.*" />
19+
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="5.0.0-preview.8.*" />
20+
<PackageReference Include="Microsoft.AspNetCore.Components" Version="5.0.0-preview.8.*" />
21+
<PackageReference Include="Microsoft.AspNetCore.Components.Web" Version="5.0.0-preview.8.*" />
2222
</ItemGroup>
2323

2424
<ItemGroup Condition="$(MSBuildProjectName)!='bunit.testassets'">
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<h1>Hello Pink World!</h1>
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
h1 {
2+
color: deeppink;
3+
}

tests/bunit.testassets/bunit.testassets.csproj

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
<PropertyGroup>
44
<TargetFrameworks>netstandard2.1;net5.0</TargetFrameworks>
5-
<RazorLangVersion>3.0</RazorLangVersion>
65
<RootNamespace>Bunit.TestAssets</RootNamespace>
76
<AssemblyName>Bunit.TestAssets</AssemblyName>
87
<IsPackable>false</IsPackable>
@@ -15,15 +14,19 @@
1514
<WarningsAsErrors>CS8602;CS8603;CS8625</WarningsAsErrors>
1615
</PropertyGroup>
1716

17+
<PropertyGroup Condition="'$(TargetFramework)' == 'netstandard2.1'">
18+
<RazorLangVersion>3.0</RazorLangVersion>
19+
</PropertyGroup>
20+
1821
<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.1'">
1922
<PackageReference Include="Microsoft.AspNetCore.Components" Version="3.1.1" />
2023
<PackageReference Include="Microsoft.AspNetCore.Components.Authorization" Version="3.1.1" />
2124
<PackageReference Include="Microsoft.AspNetCore.Components.Web" Version="3.1.1" />
2225
</ItemGroup>
2326
<ItemGroup Condition="'$(TargetFramework)' == 'net5.0'">
24-
<PackageReference Include="Microsoft.AspNetCore.Components" Version="5.0.0-preview.7.*" />
25-
<PackageReference Include="Microsoft.AspNetCore.Components.Authorization" Version="5.0.0-preview.7.*" />
26-
<PackageReference Include="Microsoft.AspNetCore.Components.Web" Version="5.0.0-preview.7.*" />
27+
<PackageReference Include="Microsoft.AspNetCore.Components" Version="5.0.0-preview.8.*" />
28+
<PackageReference Include="Microsoft.AspNetCore.Components.Authorization" Version="5.0.0-preview.8.*" />
29+
<PackageReference Include="Microsoft.AspNetCore.Components.Web" Version="5.0.0-preview.8.*" />
2730
</ItemGroup>
2831

2932
</Project>
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#if NET5_0
2+
3+
using System;
4+
using System.Collections.Generic;
5+
using System.Linq;
6+
using System.Text;
7+
using System.Threading.Tasks;
8+
9+
using Bunit.TestAssets.SampleComponents;
10+
11+
using Xunit;
12+
13+
namespace Bunit
14+
{
15+
public partial class MarkupMatchesAssertExtensionsTest : TestContext
16+
{
17+
[Fact(DisplayName = "MarkupMatches correctly ignores scoped css attributes")]
18+
public void Test_net5_001()
19+
{
20+
var cut = RenderComponent<ScopedCssElements>();
21+
22+
cut.MarkupMatches("<h1>Hello Pink World!</h1>");
23+
}
24+
}
25+
}
26+
27+
#endif

tests/bunit.web.tests/Rendering/Internal/HtmlizerTests.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
using System;
22
using Bunit.TestAssets.BlazorE2E;
3+
using Bunit.TestAssets.SampleComponents;
4+
35
using Microsoft.AspNetCore.Components;
46
using Microsoft.AspNetCore.Components.Rendering;
57
using Microsoft.AspNetCore.Components.Web;
@@ -8,9 +10,9 @@
810

911
namespace Bunit.Rendering.Internal
1012
{
11-
public class HtmlizerTests : TestContext
13+
public partial class HtmlizerTests : TestContext
1214
{
13-
[Theory(DisplayName = "The component contains correctly prefixed internal attributes.")]
15+
[Theory(DisplayName = "Htmlizer correctly prefixed stopPropagation and preventDefault attributes")]
1416
[InlineData(false, true)]
1517
[InlineData(true, false)]
1618
[InlineData(true, true)]
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#if NET5_0
2+
using System;
3+
using Bunit.TestAssets.BlazorE2E;
4+
using Bunit.TestAssets.SampleComponents;
5+
6+
using Microsoft.AspNetCore.Components;
7+
using Microsoft.AspNetCore.Components.Rendering;
8+
using Microsoft.AspNetCore.Components.Web;
9+
using Shouldly;
10+
using Xunit;
11+
12+
namespace Bunit.Rendering.Internal
13+
{
14+
public partial class HtmlizerTests : TestContext
15+
{
16+
[Theory(DisplayName = "IsBlazorAttribute correctly identifies Blazor attributes")]
17+
[InlineData("b-twl12ishk1=\"\"")]
18+
[InlineData("blazor:onclick=\"1\"")]
19+
[InlineData("blazor:__internal_stopPropagation_onclick=\"\"")]
20+
[InlineData("blazor:__internal_preventDefault_onclick=\"\"")]
21+
public void TestNET5_001(string blazorAttribute)
22+
{
23+
Htmlizer.IsBlazorAttribute(blazorAttribute).ShouldBeTrue();
24+
}
25+
}
26+
}
27+
#endif

0 commit comments

Comments
 (0)