From 7b014c35290787c66a981b083bf27a0d62d64ae7 Mon Sep 17 00:00:00 2001 From: Sergey Kokorin Date: Sat, 13 Jul 2024 11:33:22 +0200 Subject: [PATCH] Add explit name parameter (#44) --- Sources/DotNetGraph.Tests/Core/DotNodeTests.cs | 13 +++++++++++++ Sources/DotNetGraph/Core/DotIdentifier.cs | 7 +++++-- Sources/DotNetGraph/Extensions/DotNodeExtensions.cs | 5 +++-- 3 files changed, 21 insertions(+), 4 deletions(-) diff --git a/Sources/DotNetGraph.Tests/Core/DotNodeTests.cs b/Sources/DotNetGraph.Tests/Core/DotNodeTests.cs index fe5f5c3..0c524be 100644 --- a/Sources/DotNetGraph.Tests/Core/DotNodeTests.cs +++ b/Sources/DotNetGraph.Tests/Core/DotNodeTests.cs @@ -39,4 +39,17 @@ public async Task CompileNodeWithColor() var result = writer.GetStringBuilder().ToString(); result.Should().Be("Test [\n\t\"color\"=\"#FF0000\"\n]\n"); } + + [TestMethod] + public async Task CompileNodesProperties() + { + var node = new DotNode() + .WithIdentifier("node", quoteReservedWords: false).WithStyle(DotNodeStyle.Filled); + await using var writer = new StringWriter(); + var context = new CompilationContext(writer, new CompilationOptions()); + await node.CompileAsync(context); + + var result = writer.GetStringBuilder().ToString(); + result.Should().Be("node [\n\t\"style\"=\"filled\"\n]\n"); + } } \ No newline at end of file diff --git a/Sources/DotNetGraph/Core/DotIdentifier.cs b/Sources/DotNetGraph/Core/DotIdentifier.cs index 8547b89..dbaebe6 100644 --- a/Sources/DotNetGraph/Core/DotIdentifier.cs +++ b/Sources/DotNetGraph/Core/DotIdentifier.cs @@ -9,6 +9,8 @@ namespace DotNetGraph.Core { public class DotIdentifier : IDotElement, IEquatable { + private readonly bool _quoteReservedWords; + private static readonly Regex NoQuotesRequiredRegex = new Regex("^([a-zA-Z\\200-\\377_][a-zA-Z\\200-\\3770-9_]*|[-]?(.[0-9]+|[0-9]+(.[0-9]+)?))$"); @@ -26,8 +28,9 @@ private static readonly Regex NoQuotesRequiredRegex public bool IsHtml { get; set; } = false; - public DotIdentifier(string value, bool isHtml = false) + public DotIdentifier(string value, bool isHtml = false, bool quoteReservedWords = true) { + _quoteReservedWords = quoteReservedWords; Value = value; IsHtml = isHtml; } @@ -41,7 +44,7 @@ public async Task CompileAsync(CompilationContext context) } var value = context.Options.AutomaticEscapedCharactersFormat ? Value.FormatGraphvizEscapedCharacters() : Value; - if (RequiresDoubleQuotes(value)) + if (RequiresDoubleQuotes(value) && _quoteReservedWords) await context.TextWriter.WriteAsync($"\"{value}\""); else await context.TextWriter.WriteAsync($"{value}"); diff --git a/Sources/DotNetGraph/Extensions/DotNodeExtensions.cs b/Sources/DotNetGraph/Extensions/DotNodeExtensions.cs index dbc9e64..c3e3c0a 100644 --- a/Sources/DotNetGraph/Extensions/DotNodeExtensions.cs +++ b/Sources/DotNetGraph/Extensions/DotNodeExtensions.cs @@ -5,9 +5,10 @@ namespace DotNetGraph.Extensions { public static class DotNodeExtensions { - public static DotNode WithIdentifier(this DotNode node, string identifier, bool isHtml = false) + public static DotNode WithIdentifier(this DotNode node, string identifier, bool isHtml = false, + bool quoteReservedWords = true) { - node.Identifier = new DotIdentifier(identifier, isHtml); + node.Identifier = new DotIdentifier(identifier, isHtml, quoteReservedWords); return node; }