Skip to content

Commit 06d3d85

Browse files
committed
Handling < > & and unicode in copy to html.
http://code.google.com/p/arduino/issues/detail?id=29
1 parent 6cbb866 commit 06d3d85

File tree

1 file changed

+24
-4
lines changed

1 file changed

+24
-4
lines changed

app/src/processing/app/tools/DiscourseFormat.java

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,26 @@ public void lostOwnership(Clipboard clipboard, Transferable contents) {
114114
" has been copied to the clipboard.");
115115
}
116116

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("&lt;");
127+
} else if (c == '>') {
128+
buffer.append("&gt;");
129+
} else if (c == '&') {
130+
buffer.append("&amp;");
131+
} else if (c > 127) {
132+
buffer.append("&#" + ((int) c) + ";"); // use unicode entity
133+
} else {
134+
buffer.append(c); // normal character
135+
}
136+
}
117137

118138
// A terrible headache...
119139
public void appendFormattedLine(StringBuffer cf, int line) {
@@ -138,7 +158,7 @@ public void appendFormattedLine(StringBuffer cf, int line) {
138158
if (tokenMarker == null) {
139159
for (int j = 0; j < segmentCount; j++) {
140160
char c = segmentArray[j + segmentOffset];
141-
cf = cf.append(c);
161+
appendToHTML(c, cf);
142162
// int charWidth;
143163
// if (c == '\t') {
144164
// charWidth = (int) painter.nextTabStop(width, j) - width;
@@ -171,7 +191,7 @@ public void appendFormattedLine(StringBuffer cf, int line) {
171191
if (id == Token.END) {
172192
char c = segmentArray[segmentOffset + offset];
173193
if (segmentOffset + offset < limit) {
174-
cf.append(c);
194+
appendToHTML(c, cf);
175195
} else {
176196
cf.append('\n');
177197
}
@@ -203,7 +223,7 @@ public void appendFormattedLine(StringBuffer cf, int line) {
203223
// cf.append(' ');
204224
// }
205225
} else {
206-
cf.append(c);
226+
appendToHTML(c, cf);
207227
}
208228
// Place close tags [/]
209229
if (j == (length - 1) && id != Token.NULL && styles[id].isBold())
@@ -225,4 +245,4 @@ public void appendFormattedLine(StringBuffer cf, int line) {
225245
}
226246
}
227247
}
228-
}
248+
}

0 commit comments

Comments
 (0)