3
0
Fork 0
forked from mirrors/nixpkgs

lib: add function escapeXML

Given a string, this function returns a string that can be inserted
verbatim in an XML document.
This commit is contained in:
Robert Helgesson 2021-10-03 11:28:03 +02:00
parent a7eb89bdba
commit e75f346aa3
No known key found for this signature in database
GPG key ID: 36BDAA14C2797E89
3 changed files with 19 additions and 1 deletions

View file

@ -91,7 +91,7 @@ let
concatImapStringsSep makeSearchPath makeSearchPathOutput
makeLibraryPath makeBinPath optionalString
hasInfix hasPrefix hasSuffix stringToCharacters stringAsChars escape
escapeShellArg escapeShellArgs escapeRegex replaceChars lowerChars
escapeShellArg escapeShellArgs escapeRegex escapeXML replaceChars lowerChars
upperChars toLower toUpper addContextFrom splitString
removePrefix removeSuffix versionOlder versionAtLeast
getName getVersion

View file

@ -362,6 +362,19 @@ rec {
if match "[a-zA-Z_][a-zA-Z0-9_'-]*" s != null
then s else escapeNixString s;
/* Escapes a string such that it is safe to include verbatim in an XML
document.
Type: string -> string
Example:
escapeXML ''"test" 'test' < & >''
=> "\\[\\^a-z]\\*"
*/
escapeXML = builtins.replaceStrings
["\"" "'" "<" ">" "&"]
["&quot;" "&apos;" "&lt;" "&gt;" "&amp;"];
# Obsolete - use replaceStrings instead.
replaceChars = builtins.replaceStrings or (
del: new: s:

View file

@ -246,6 +246,11 @@ runTests {
};
};
testEscapeXML = {
expr = escapeXML ''"test" 'test' < & >'';
expected = "&quot;test&quot; &apos;test&apos; &lt; &amp; &gt;";
};
# LISTS
testFilter = {