JavaScriptUtils.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. #region License
  2. // Copyright 2006 James Newton-King
  3. // http://www.newtonsoft.com
  4. //
  5. // This work is licensed under the Creative Commons Attribution 2.5 License
  6. // http://creativecommons.org/licenses/by/2.5/
  7. //
  8. // You are free:
  9. // * to copy, distribute, display, and perform the work
  10. // * to make derivative works
  11. // * to make commercial use of the work
  12. //
  13. // Under the following conditions:
  14. // * For any reuse or distribution, you must make clear to others the license terms of this work.
  15. // * Any of these conditions can be waived if you get permission from the copyright holder.
  16. #endregion
  17. using System;
  18. using System.Collections;
  19. using System.Globalization;
  20. using System.IO;
  21. using System.Text;
  22. using System.Text.RegularExpressions;
  23. using System.Web;
  24. using System.Collections.Generic;
  25. using System.Drawing;
  26. using System.Web.UI.WebControls;
  27. namespace Newtonsoft.Json
  28. {
  29. internal static class JavaScriptUtils
  30. {
  31. public static string EscapeJavaScriptString(string value)
  32. {
  33. return EscapeJavaScriptString(value, '"', true);
  34. }
  35. public static string EscapeJavaScriptString(string value, char delimiter, bool appendDelimiters)
  36. {
  37. if (string.IsNullOrEmpty(value))
  38. {
  39. if (appendDelimiters)
  40. return new string(delimiter, 2);
  41. else
  42. return string.Empty;
  43. }
  44. StringBuilder sb = null;
  45. int lastWritePosition = 0;
  46. int skipped = 0;
  47. // leading delimiter
  48. if (appendDelimiters)
  49. {
  50. sb = new StringBuilder(value.Length + 5);
  51. sb.Append(delimiter);
  52. }
  53. for (int i = 0; i < value.Length; i++)
  54. {
  55. char currentChar = value[i];
  56. string escapedValue;
  57. switch (currentChar)
  58. {
  59. case '\t':
  60. escapedValue = @"\t";
  61. break;
  62. case '\n':
  63. escapedValue = @"\n";
  64. break;
  65. case '\r':
  66. escapedValue = @"\r";
  67. break;
  68. case '\f':
  69. escapedValue = @"\f";
  70. break;
  71. case '\b':
  72. escapedValue = @"\b";
  73. break;
  74. case '"':
  75. // only escape if this charater is being used as the delimiter
  76. escapedValue = (delimiter == '"') ? "\\\"" : null;
  77. break;
  78. case '\'':
  79. // only escape if this charater is being used as the delimiter
  80. escapedValue = (delimiter == '\'') ? @"\'" : null;
  81. break;
  82. case '\\':
  83. escapedValue = @"\\";
  84. break;
  85. default:
  86. escapedValue = null;
  87. break;
  88. }
  89. // test if the char needs to be escaped or whether it can be skipped
  90. if (escapedValue != null)
  91. {
  92. if (sb == null)
  93. sb = new StringBuilder(value.Length + 5);
  94. // write skipped text
  95. if (skipped > 0)
  96. {
  97. sb.Append(value, lastWritePosition, skipped);
  98. skipped = 0;
  99. }
  100. // write escaped value and note position
  101. sb.Append(escapedValue);
  102. lastWritePosition = i + 1;
  103. }
  104. else
  105. {
  106. skipped++;
  107. }
  108. }
  109. // nothing was escaped. return initial string
  110. if (sb == null)
  111. return value;
  112. // write any remaining skipped text
  113. if (skipped > 0)
  114. sb.Append(value, lastWritePosition, skipped);
  115. // trailing delimiter
  116. if (appendDelimiters)
  117. sb.Append(delimiter);
  118. return sb.ToString();
  119. }
  120. }
  121. }