@@ -114,6 +114,26 @@ public void lostOwnership(Clipboard clipboard, Transferable contents) {
114
114
" has been copied to the clipboard." );
115
115
}
116
116
117
+ /**
118
+ * Append a char to a stringbuffer while escaping for proper display in HTML.
119
+ * @param c input char to escape
120
+ * @param buffer StringBuffer to append html-safe version of c to.
121
+ */
122
+ private void appendToHTML (char c , StringBuffer buffer ) {
123
+ if (!html ) {
124
+ buffer .append (c );
125
+ } else if (c == '<' ) {
126
+ buffer .append ("<" );
127
+ } else if (c == '>' ) {
128
+ buffer .append (">" );
129
+ } else if (c == '&' ) {
130
+ buffer .append ("&" );
131
+ } else if (c > 127 ) {
132
+ buffer .append ("&#" + ((int ) c ) + ";" ); // use unicode entity
133
+ } else {
134
+ buffer .append (c ); // normal character
135
+ }
136
+ }
117
137
118
138
// A terrible headache...
119
139
public void appendFormattedLine (StringBuffer cf , int line ) {
@@ -138,7 +158,7 @@ public void appendFormattedLine(StringBuffer cf, int line) {
138
158
if (tokenMarker == null ) {
139
159
for (int j = 0 ; j < segmentCount ; j ++) {
140
160
char c = segmentArray [j + segmentOffset ];
141
- cf = cf . append ( c );
161
+ appendToHTML ( c , cf );
142
162
// int charWidth;
143
163
// if (c == '\t') {
144
164
// charWidth = (int) painter.nextTabStop(width, j) - width;
@@ -171,7 +191,7 @@ public void appendFormattedLine(StringBuffer cf, int line) {
171
191
if (id == Token .END ) {
172
192
char c = segmentArray [segmentOffset + offset ];
173
193
if (segmentOffset + offset < limit ) {
174
- cf . append ( c );
194
+ appendToHTML ( c , cf );
175
195
} else {
176
196
cf .append ('\n' );
177
197
}
@@ -203,7 +223,7 @@ public void appendFormattedLine(StringBuffer cf, int line) {
203
223
// cf.append(' ');
204
224
// }
205
225
} else {
206
- cf . append ( c );
226
+ appendToHTML ( c , cf );
207
227
}
208
228
// Place close tags [/]
209
229
if (j == (length - 1 ) && id != Token .NULL && styles [id ].isBold ())
@@ -225,4 +245,4 @@ public void appendFormattedLine(StringBuffer cf, int line) {
225
245
}
226
246
}
227
247
}
228
- }
248
+ }
0 commit comments