JavaScriptUtils.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. #region License
  2. // Copyright (c) 2007 James Newton-King
  3. //
  4. // Permission is hereby granted, free of charge, to any person
  5. // obtaining a copy of this software and associated documentation
  6. // files (the "Software"), to deal in the Software without
  7. // restriction, including without limitation the rights to use,
  8. // copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the
  10. // Software is furnished to do so, subject to the following
  11. // conditions:
  12. //
  13. // The above copyright notice and this permission notice shall be
  14. // included in all copies or substantial portions of the Software.
  15. //
  16. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  17. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  18. // OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  19. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  20. // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  21. // WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  22. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  23. // OTHER DEALINGS IN THE SOFTWARE.
  24. #endregion
  25. using System;
  26. using System.Collections;
  27. using System.Globalization;
  28. using System.IO;
  29. using System.Text;
  30. using System.Text.RegularExpressions;
  31. using System.Web;
  32. using System.Collections.Generic;
  33. using System.Drawing;
  34. using System.Web.UI.WebControls;
  35. namespace Newtonsoft.Json
  36. {
  37. internal static class JavaScriptUtils
  38. {
  39. public static void WriteEscapedJavaScriptString (string value, TextWriter writer) {
  40. WriteEscapedJavaScriptString (value, '"', true, writer);
  41. }
  42. public static void WriteEscapedJavaScriptString (string value, char delimiter, bool appendDelimiters, TextWriter writer) {
  43. // leading delimiter
  44. if (appendDelimiters)
  45. writer.Write (delimiter);
  46. if (!string.IsNullOrEmpty (value))
  47. for (int i = 0; i < value.Length; i++)
  48. WriteJavaScriptChar (value [i], delimiter, writer);
  49. // trailing delimiter
  50. if (appendDelimiters)
  51. writer.Write (delimiter);
  52. }
  53. public static void WriteEscapedJavaScriptChar (char value, char delimiter, bool appendDelimiters, TextWriter writer) {
  54. // leading delimiter
  55. if (appendDelimiters)
  56. writer.Write (delimiter);
  57. WriteJavaScriptChar (value, delimiter, writer);
  58. // trailing delimiter
  59. if (appendDelimiters)
  60. writer.Write (delimiter);
  61. }
  62. public static void WriteJavaScriptChar (char value, char delimiter, TextWriter writer) {
  63. switch (value) {
  64. case '\t':
  65. writer.Write (@"\t");
  66. break;
  67. case '\n':
  68. writer.Write (@"\n");
  69. break;
  70. case '\r':
  71. writer.Write (@"\r");
  72. break;
  73. case '\f':
  74. writer.Write (@"\f");
  75. break;
  76. case '\b':
  77. writer.Write (@"\b");
  78. break;
  79. case '<':
  80. writer.Write (@"\u003c");
  81. break;
  82. case '>':
  83. writer.Write (@"\u003e");
  84. break;
  85. case '"':
  86. // only escape if this charater is being used as the delimiter
  87. if (delimiter == '"')
  88. writer.Write (@"\""");
  89. else
  90. writer.Write (value);
  91. break;
  92. case '\'':
  93. writer.Write (@"\u0027");
  94. break;
  95. case '\\':
  96. writer.Write (@"\\");
  97. break;
  98. default:
  99. if (value > '\u001f')
  100. writer.Write (value);
  101. else {
  102. writer.Write("\\u00");
  103. int intVal = (int) value;
  104. writer.Write ((char) ('0' + (intVal >> 4)));
  105. intVal &= 0xf;
  106. writer.Write ((char) (intVal < 10 ? '0' + intVal : 'a' + (intVal - 10)));
  107. }
  108. break;
  109. }
  110. }
  111. }
  112. }