Code Coverage Statistics for Source File
Newtonsoft.Json\Utilities\JavaScriptUtils.cs
Symbol Coverage: 100.00% (57 of 57)
Branch Coverage: 100.00% (36 of 36)
Cyclomatic Complexity Avg: 9.33 Max:24
Code Lines: 55
Symbol Coverage Trend
View:
L | V | Source |
---|---|---|
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 |
||
26 |
using System; |
|
27 |
using System.Collections; |
|
28 |
using System.Globalization; |
|
29 |
using System.IO; |
|
30 |
using System.Text; |
|
31 |
using System.Text.RegularExpressions; |
|
32 |
using System.Collections.Generic; |
|
33 |
||
34 |
namespace Newtonsoft.Json.Utilities |
|
35 |
{ |
|
36 |
internal static class JavaScriptUtils |
|
37 |
{ |
|
38 |
public static void WriteEscapedJavaScriptString(TextWriter writer, string value, char delimiter, bool appendDelimiters) |
|
39 |
{ |
|
40 |
// leading delimiter |
|
41 |
144485 |
if (appendDelimiters)
|
42 |
144485 |
writer.Write(delimiter);
|
43 |
||
44 |
144485 |
if (value != null)
|
45 |
{ |
|
46 |
144485 |
int lastWritePosition = 0;
|
47 |
144485 |
int skipped = 0;
|
48 |
144485 |
char[] chars = null;
|
49 |
||
50 |
144485 |
for (int i = 0; i < value.Length; i++) |
51 |
{ |
|
52 |
1332138 |
char c = value[i];
|
53 |
string escapedValue; |
|
54 |
||
55 |
1332138 |
switch (c)
|
56 |
{ |
|
57 |
case '\t': |
|
58 |
8 |
escapedValue = @"\t";
|
59 |
8 |
break;
|
60 |
case '\n': |
|
61 |
23 |
escapedValue = @"\n";
|
62 |
23 |
break;
|
63 |
case '\r': |
|
64 |
23 |
escapedValue = @"\r";
|
65 |
23 |
break;
|
66 |
case '\f': |
|
67 |
7 |
escapedValue = @"\f";
|
68 |
7 |
break;
|
69 |
case '\b': |
|
70 |
7 |
escapedValue = @"\b";
|
71 |
7 |
break;
|
72 |
case '\\': |
|
73 |
20 |
escapedValue = @"\\";
|
74 |
20 |
break;
|
75 |
case '\u0085': // Next Line |
|
76 |
1 |
escapedValue = @"\u0085";
|
77 |
1 |
break;
|
78 |
case '\u2028': // Line Separator |
|
79 |
1 |
escapedValue = @"\u2028";
|
80 |
1 |
break;
|
81 |
case '\u2029': // Paragraph Separator |
|
82 |
1 |
escapedValue = @"\u2029";
|
83 |
1 |
break;
|
84 |
case '\'': |
|
85 |
// only escape if this charater is being used as the delimiter |
|
86 |
236 |
escapedValue = (delimiter == '\'') ? @"\'" : null;
|
87 |
236 |
break;
|
88 |
case '"': |
|
89 |
// only escape if this charater is being used as the delimiter |
|
90 |
37 |
escapedValue = (delimiter == '"') ? "\\\"" : null;
|
91 |
37 |
break;
|
92 |
default: |
|
93 |
1331774 |
escapedValue = (c <= '\u001f') ? StringUtils.ToCharAsUnicode(c) : null;
|
94 |
1331774 |
break;
|
95 |
} |
|
96 |
||
97 |
1332138 |
if (escapedValue != null)
|
98 |
{ |
|
99 |
5386 |
if (chars == null)
|
100 |
5242 |
chars = value.ToCharArray();
|
101 |
||
102 |
// write skipped text |
|
103 |
5386 |
if (skipped > 0)
|
104 |
{ |
|
105 |
74 |
writer.Write(chars, lastWritePosition, skipped);
|
106 |
74 |
skipped = 0;
|
107 |
} |
|
108 |
||
109 |
// write escaped value and note position |
|
110 |
5386 |
writer.Write(escapedValue);
|
111 |
5386 |
lastWritePosition = i + 1;
|
112 |
} |
|
113 |
else |
|
114 |
{ |
|
115 |
1326752 |
skipped++;
|
116 |
} |
|
117 |
} |
|
118 |
||
119 |
// write any remaining skipped text |
|
120 |
144485 |
if (skipped > 0)
|
121 |
{ |
|
122 |
144469 |
if (lastWritePosition == 0)
|
123 |
139237 |
writer.Write(value);
|
124 |
else |
|
125 |
5232 |
writer.Write(chars, lastWritePosition, skipped);
|
126 |
} |
|
127 |
} |
|
128 |
||
129 |
// trailing delimiter |
|
130 |
144485 |
if (appendDelimiters)
|
131 |
144485 |
writer.Write(delimiter);
|
132 |
144485 |
}
|
133 |
||
134 |
public static string ToEscapedJavaScriptString(string value) |
|
135 |
{ |
|
136 |
1 |
return ToEscapedJavaScriptString(value, '"', true);
|
137 |
1 |
}
|
138 |
||
139 |
public static string ToEscapedJavaScriptString(string value, char delimiter, bool appendDelimiters) |
|
140 |
{ |
|
141 |
30 |
using (StringWriter w = StringUtils.CreateStringWriter(StringUtils.GetLength(value) ?? 16))
|
142 |
{ |
|
143 |
30 |
WriteEscapedJavaScriptString(w, value, delimiter, appendDelimiters);
|
144 |
30 |
return w.ToString();
|
145 |
} |
|
146 |
30 |
}
|
147 |
} |
|
148 |
} |