Skip to content

Commit

Permalink
Merge pull request #31 from 2sic/develop
Browse files Browse the repository at this point in the history
04.04.01 - minor fix on Attribute generation (spaces paddings)
  • Loading branch information
iJungleboy authored Sep 6, 2023
2 parents fe87c55 + b55e3df commit 240daaf
Show file tree
Hide file tree
Showing 12 changed files with 74 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ public partial class HtmlTagsServiceImplementation

/// <inheritdoc />
public Attribute Attr(string name, string value, AttributeOptions options = null)
=> new Attribute(name, value, options);
=> new Attribute(name, value, options ?? AttributeOptions.StandaloneOptions);

/// <inheritdoc />
public Attribute Attr(string name, object value = null, AttributeOptions options = null)
=> new Attribute(name, value, options);
=> new Attribute(name, value, options ?? AttributeOptions.StandaloneOptions);

/// <inheritdoc />
public TagCustom Custom(string name, params object[] content)
Expand Down
10 changes: 6 additions & 4 deletions Razor.Blade/Markup/Attribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ private string Build()
var currentOptions = AttributeOptions.UseOrCreate(Options);

if (Value == null && currentOptions.DropValueIfNull)
return Name;
return currentOptions.AddPaddings ? $" {Name} " : Name;

var val = Internals.Html.Encode(ValueStringOrSerialized(Value)) ?? "";

Expand All @@ -65,9 +65,11 @@ private string Build()
val = val.Replace(Internals.Html.Encode(safeQuote), safeQuote);
}

return currentOptions.KeepEmpty || !string.IsNullOrEmpty(val)
? $"{Name}={currentOptions.Quote}{val}{currentOptions.Quote}"
: "";
if (!currentOptions.KeepEmpty && string.IsNullOrEmpty(val))
return "";

var result = $"{Name}={currentOptions.Quote}{val}{currentOptions.Quote}";
return currentOptions.AddPaddings ? $" {result} " : result;
}
/// <summary>
/// An sequence already prepared, so no more building would be necessary
Expand Down
7 changes: 7 additions & 0 deletions Razor.Blade/Markup/AttributeOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ public class AttributeOptions
/// </summary>
public const bool DefaultDropValueIfNull = true;

public const bool DefaultAddSpacePaddings = false;

#endregion

#region Constructor to enforce fluid API
Expand Down Expand Up @@ -68,10 +70,15 @@ public AttributeOptions(AttributeOptions original = null, string quote = null, b
/// </summary>
public bool DropValueIfNull { get; }

public bool AddPaddings { get; private set; }

[PrivateApi]
internal static AttributeOptions UseOrCreate(AttributeOptions original) => original ?? DefaultOptions;

[PrivateApi]
internal static AttributeOptions DefaultOptions = new AttributeOptions();

[PrivateApi]
internal static AttributeOptions StandaloneOptions = new AttributeOptions() { AddPaddings = true };
}
}
2 changes: 1 addition & 1 deletion Razor.Blade/Razor.Blade.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<PropertyGroup>
<TargetFrameworks>netstandard2.0;net472;net6.0;net7.0</TargetFrameworks>
<RootNamespace>ToSic.Razor</RootNamespace>
<Version>04.04.00</Version>
<Version>04.04.01</Version>
<AssemblyName>ToSic.Razor</AssemblyName>
<PackageId>ToSic.Razor</PackageId>
<Authors>ToSic.Razor</Authors>
Expand Down
3 changes: 2 additions & 1 deletion ToSic.Razor.Dnn/BuildScripts/MSBuild.Community.Tasks.Targets
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
<PropertyGroup>
<MSBuildCommunityTasksPath Condition="'$(MSBuildCommunityTasksPath)' == ''">$(MSBuildProjectDirectory)\BuildScripts</MSBuildCommunityTasksPath>
<MSBuildDnnBinPath Condition="'$(MSBuildDnnBinPath)' == ''">$(MSBuildProjectDirectory)\..\..\bin</MSBuildDnnBinPath>
<MSBuildCommunityTasksLib>$(SolutionDir)\packages\MSBuildTasks.1.5.0.235\tools\MSBuild.Community.Tasks.dll</MSBuildCommunityTasksLib>
<!-- 2dm 2023-09-05 disabled this, as it caused issues on a clean PC -->
<!--<MSBuildCommunityTasksLib>$(SolutionDir)\packages\MSBuildTasks.1.5.0.235\tools\MSBuild.Community.Tasks.dll</MSBuildCommunityTasksLib>-->
</PropertyGroup>
<UsingTask AssemblyFile="$(MSBuildCommunityTasksLib)" TaskName="MSBuild.Community.Tasks.AspNet.InstallAspNet" />
<UsingTask AssemblyFile="$(MSBuildCommunityTasksLib)" TaskName="MSBuild.Community.Tasks.AssemblyInfo" />
Expand Down
4 changes: 2 additions & 2 deletions ToSic.Razor.Dnn/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,5 @@
//
// You can specify all the values or you can default the Revision and Build Numbers
// by using the '*' as shown below:
[assembly: AssemblyVersion("04.04.00.00")]
[assembly: AssemblyFileVersion("04.04.00.00")]
[assembly: AssemblyVersion("04.04.01.00")]
[assembly: AssemblyFileVersion("04.04.01.00")]
2 changes: 1 addition & 1 deletion ToSic.Razor.Dnn/Razor.Blade.Dnn.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="2.1.1">
<ExcludeAssets>runtime</ExcludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="2.1.1" >
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="2.1.1">
<ExcludeAssets>runtime</ExcludeAssets>
</PackageReference>
</ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion ToSic.Razor.Dnn/ToSic_Razor_Blade_Dnn.dnn
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<dotnetnuke type="Package" version="5.0">
<packages>
<package name="ToSic.RazorBlade" type="Library" version="04.04.00">
<package name="ToSic.RazorBlade" type="Library" version="04.04.01">
<friendlyName>2sic RazorBlade</friendlyName>
<description>2sic RazorBlade</description>
<iconFile>icon.png</iconFile>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,11 @@ public void H1Test()
Is("<h1></h1>", HtmlTagsService.H1());
Is("<h1>content</h1>", HtmlTagsService.H1("content"));
}

[TestMethod()]
public void AttributesHaveSpaces()
{
Is(" name='value' ", HtmlTagsService.Attr("name", "value"));
}
}
}
50 changes: 32 additions & 18 deletions ToSic.RazorBladeTests/TagBuilderTests/AttributeBuilderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,40 +6,54 @@ namespace ToSic.RazorBladeTests.TagBuilderTests
[TestClass]
public class AttributeBuilderTests
{
/// <summary>
/// Test accessor to reduce use-count
/// </summary>
public Attribute NewAttribute(string name, object? value = default, AttributeOptions? options = default)
=> new Attribute(name, value, options);

[TestMethod]
public void BasicAttributes()
{
Assert.AreEqual("name='value'", new Attribute("name", "value").ToString());
Assert.AreEqual("something='other'", new Attribute("something", "other").ToString());
Assert.AreEqual("name='value'", NewAttribute("name", "value").ToString());
Assert.AreEqual("something='other'", NewAttribute("something", "other").ToString());
}

[TestMethod] public void BasicAttributesStandalone()
{
var o = AttributeOptions.StandaloneOptions;
Assert.AreEqual(" name='value' ", NewAttribute("name", "value", o).ToString());
Assert.AreEqual(" something='other' ", NewAttribute("something", "other", o).ToString());

}

[TestMethod]
public void BasicAttributesQuote()
{
var options = new AttributeOptions(quote: "\"");
Assert.AreEqual("name=\"value\"",
new Attribute("name", "value", options).ToString());
NewAttribute("name", "value", options).ToString());
Assert.AreEqual("something=\"other\"",
new Attribute("something", "other", options).ToString());
NewAttribute("something", "other", options).ToString());
}

[TestMethod]
public void UnEncodeQuote()
{
var options = new AttributeOptions(encodeQuotes: true);
Assert.AreEqual("name='{\"name\":\"daniel\"}'",
new Attribute("name", "{\"name\":\"daniel\"}").ToString());
NewAttribute("name", "{\"name\":\"daniel\"}").ToString());
Assert.AreEqual("name='{&quot;name&quot;:&quot;daniel&quot;}'",
new Attribute("name", "{\"name\":\"daniel\"}", options).ToString());
NewAttribute("name", "{\"name\":\"daniel\"}", options).ToString());

options = new AttributeOptions(quote: "\"");
Assert.AreEqual("name=\"{&quot;name&quot;:&quot;daniel&quot;}\"",
new Attribute("name", "{\"name\":\"daniel\"}", options).ToString(),
NewAttribute("name", "{\"name\":\"daniel\"}", options).ToString(),
"with a different quote and encodeQuotes false");

options = new AttributeOptions(encodeQuotes: true, quote: "\"");
Assert.AreEqual("name=\"{&quot;name&quot;:&quot;daniel&quot;}\"",
new Attribute("name", "{\"name\":\"daniel\"}", options).ToString(),
NewAttribute("name", "{\"name\":\"daniel\"}", options).ToString(),
"with a different quote and encodeQuotes = true");

}
Expand All @@ -49,17 +63,17 @@ public void UnEncodeApostropheInValue()
{
var options = new AttributeOptions(quote: "\"");
Assert.AreEqual("name=\"isn't it ironic\"",
new Attribute("name", "isn't it ironic", options).ToString(),
NewAttribute("name", "isn't it ironic", options).ToString(),
"apostrophe with a different quote and encodeQuotes = false");

options = new AttributeOptions(encodeQuotes: true);
Assert.AreEqual("name='isn&apos;t it ironic'",
new Attribute("name", "isn't it ironic", options).ToString(),
NewAttribute("name", "isn't it ironic", options).ToString(),
"apostrophe with a different quote and encodeQuotes = true");

options = new AttributeOptions(encodeQuotes: true, quote: "\"");
Assert.AreEqual("name=\"isn&apos;t it ironic\"",
new Attribute("name", "isn't it ironic", options).ToString(),
NewAttribute("name", "isn't it ironic", options).ToString(),
"apostrophe with a different quote and encodeQuotes = true");

}
Expand All @@ -68,28 +82,28 @@ public void UnEncodeApostropheInValue()
public void BasicAttributesEmpty()
{
Assert.AreEqual("name=''",
new Attribute("name", "").ToString());
NewAttribute("name", "").ToString());
Assert.AreEqual("name",
new Attribute("name", null).ToString());
NewAttribute("name", null).ToString());
Assert.AreEqual("name=''",
new Attribute("name", null,
NewAttribute("name", null,
new AttributeOptions(dropValueIfNull: false)).ToString());

var options = new AttributeOptions(keepEmpty: false);
Assert.AreEqual("",
new Attribute("name", "", options).ToString());
NewAttribute("name", "", options).ToString());
}

[TestMethod]
public void ObjectValues()
{
//var options = new AttributeOptions { KeepEmpty = false };
Assert.AreEqual("name='54'",
new Attribute("name", 54).ToString());
NewAttribute("name", 54).ToString());
Assert.AreEqual("name='{\"Name\":\"Daniel\"}'",
new Attribute("name", new { Name = "Daniel" }).ToString());
NewAttribute("name", new { Name = "Daniel" }).ToString());
Assert.AreEqual("name='Daniel'",
new Attribute("name", "Daniel" as object).ToString());
NewAttribute("name", "Daniel" as object).ToString());
}

}
Expand Down
9 changes: 8 additions & 1 deletion ToSic.RazorBladeTests/TagTests/TagAttributeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,18 @@ public void TagAttributeContainingPTag()
}

[TestMethod]
public void TagAttributeStandalone()
public void TagAttributeWithoutValue()
{
Is("<div data-fancybox>", TestDiv().Attr("data-fancybox").TagStart);
}

[TestMethod]
public void TagAttributeOnly()
{
Is(" name='value' ", Tag.Attr("name", "value"));
}


[TestMethod]
public void TagAttributeMultiple()
{
Expand Down
9 changes: 6 additions & 3 deletions docs/manifest.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"homepages": [],
"source_base_path": "A:/razor-blade/DocFx Generator",
"source_base_path": "C:/Projects/razor-blades/DocFx Generator",
"xrefmap": "xrefmap.yml",
"files": [
{
Expand Down Expand Up @@ -141,6 +141,9 @@
"version": ""
},
{
"log_codes": [
"UidNotFound"
],
"type": "Conceptual",
"source_relative_path": "index.md",
"output": {
Expand Down Expand Up @@ -1777,11 +1780,11 @@
{
"status": {
"can_incremental": false,
"details": "Disable incremental build by force rebuild option.",
"details": "Cannot build incrementally because last build info is missing.",
"incrementalPhase": "build",
"total_file_count": 0,
"skipped_file_count": 0,
"full_build_reason_code": "ForceRebuild"
"full_build_reason_code": "NoAvailableBuildCache"
},
"processors": {
"ConceptualDocumentProcessor": {
Expand Down

0 comments on commit 240daaf

Please sign in to comment.