-
Notifications
You must be signed in to change notification settings - Fork 1
Add Xml.Escape helper method #85
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
392c16d
Add Xml.Escape helper method with tests
Copilot f3b0f4d
Add null check and fix test structure in Xml.Escape
Copilot ccaa030
Add isolated surrogate tests for Xml.Escape
Copilot 723e281
Refactor Xml tests into unified act/assert style
Copilot 44378b7
Use FluentAssertions throw assertion in Xml test
Copilot 9e24b04
Use Assert.Throws in XmlTests null assertion
Copilot edb982c
Update XmlTests.cs
Tyrrrz 2d8f6a7
Update Xml.cs
Tyrrrz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| using System; | ||
| using FluentAssertions; | ||
| using Xunit; | ||
|
|
||
| namespace PowerKit.Tests; | ||
|
|
||
| public class XmlTests | ||
| { | ||
| [Fact] | ||
| public void Escape_Test() | ||
| { | ||
| // Act & assert | ||
| Xml.Escape("hello world").Should().Be("hello world"); | ||
| Xml.Escape("foo & bar").Should().Be("foo & bar"); | ||
| Xml.Escape("1 < 2").Should().Be("1 < 2"); | ||
| Xml.Escape("2 > 1").Should().Be("2 > 1"); | ||
| Xml.Escape("say \"hello\"").Should().Be("say "hello""); | ||
| Xml.Escape("it's").Should().Be("it's"); | ||
| Xml.Escape("& < > \" '").Should().Be("& < > " '"); | ||
| Xml.Escape("foo\u0001bar").Should().Be("foobar"); | ||
| Xml.Escape("foo\t\n\rbar").Should().Be("foo\t\n\rbar"); | ||
| Xml.Escape("\U0001F600").Should().Be("\U0001F600"); | ||
| Xml.Escape("foo\uD800bar").Should().Be("foobar"); | ||
| Xml.Escape("foo\uDC00bar").Should().Be("foobar"); | ||
| Xml.Escape("").Should().BeEmpty(); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,94 @@ | ||
| using System; | ||
| using System.Text; | ||
|
|
||
| namespace PowerKit; | ||
|
|
||
| /// <summary> | ||
| /// Helper methods for working with XML. | ||
| /// </summary> | ||
| public static class Xml | ||
| { | ||
| private static bool IsValidXmlChar(char ch) => | ||
| ch == '\t' | ||
| || ch == '\n' | ||
| || ch == '\r' | ||
| || (ch >= '\x20' && ch <= '\xD7FF') | ||
| || (ch >= '\xE000' && ch <= '\xFFFD'); | ||
|
|
||
| /// <summary> | ||
| /// Escapes invalid XML characters in the specified string, returning a valid XML string. | ||
| /// Characters that cannot be represented in XML (such as most control characters) are removed. | ||
| /// Special XML characters (<c>&</c>, <c><</c>, <c>></c>, <c>"</c>, <c>'</c>) | ||
| /// are replaced with their corresponding XML entities. | ||
| /// </summary> | ||
| public static string Escape(string str) | ||
| { | ||
| var builder = default(StringBuilder); | ||
|
|
||
| var i = 0; | ||
| while (i < str.Length) | ||
| { | ||
| var ch = str[i]; | ||
|
|
||
| string? replacement; | ||
| if (ch == '&') | ||
| { | ||
| replacement = "&"; | ||
| } | ||
| else if (ch == '<') | ||
| { | ||
| replacement = "<"; | ||
| } | ||
| else if (ch == '>') | ||
| { | ||
| replacement = ">"; | ||
| } | ||
| else if (ch == '"') | ||
| { | ||
| replacement = """; | ||
| } | ||
| else if (ch == '\'') | ||
| { | ||
| replacement = "'"; | ||
| } | ||
| else if ( | ||
| char.IsHighSurrogate(ch) | ||
| && i + 1 < str.Length | ||
| && char.IsLowSurrogate(str[i + 1]) | ||
| ) | ||
| { | ||
| // Valid surrogate pair — represents a supplementary character (U+10000..U+10FFFF), | ||
| // which is valid in XML. Append both chars and advance past the pair. | ||
| builder?.Append(ch); | ||
| builder?.Append(str[i + 1]); | ||
| i += 2; | ||
| continue; | ||
| } | ||
| else if (IsValidXmlChar(ch)) | ||
| { | ||
| replacement = null; | ||
| } | ||
| else | ||
| { | ||
| // Truly invalid XML character — skip it. | ||
| builder ??= new StringBuilder(str, 0, i, str.Length); | ||
| i++; | ||
| continue; | ||
| } | ||
|
|
||
| if (replacement is not null) | ||
| { | ||
| builder ??= new StringBuilder(str, 0, i, str.Length); | ||
| builder.Append(replacement); | ||
| } | ||
| else | ||
| { | ||
| builder?.Append(ch); | ||
| } | ||
|
|
||
| i++; | ||
| } | ||
|
|
||
| return builder?.ToString() ?? str; | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.