From 06d3d8514363824b5766e26d03d154911783095f Mon Sep 17 00:00:00 2001 From: "David A. Mellis" Date: Tue, 16 Nov 2010 22:51:24 -0500 Subject: [PATCH] Handling < > & and unicode in copy to html. http://code.google.com/p/arduino/issues/detail?id=29 --- .../processing/app/tools/DiscourseFormat.java | 28 ++++++++++++++++--- 1 file changed, 24 insertions(+), 4 deletions(-) diff --git a/app/src/processing/app/tools/DiscourseFormat.java b/app/src/processing/app/tools/DiscourseFormat.java index a9a40d299..097d7ee2c 100644 --- a/app/src/processing/app/tools/DiscourseFormat.java +++ b/app/src/processing/app/tools/DiscourseFormat.java @@ -114,6 +114,26 @@ public class DiscourseFormat { " has been copied to the clipboard."); } + /** + * Append a char to a stringbuffer while escaping for proper display in HTML. + * @param c input char to escape + * @param buffer StringBuffer to append html-safe version of c to. + */ + private void appendToHTML(char c, StringBuffer buffer) { + if (!html) { + buffer.append(c); + } else if (c == '<') { + buffer.append("<"); + } else if (c == '>') { + buffer.append(">"); + } else if (c == '&') { + buffer.append("&"); + } else if (c > 127) { + buffer.append("&#" + ((int) c) + ";"); // use unicode entity + } else { + buffer.append(c); // normal character + } + } // A terrible headache... public void appendFormattedLine(StringBuffer cf, int line) { @@ -138,7 +158,7 @@ public class DiscourseFormat { if (tokenMarker == null) { for (int j = 0; j < segmentCount; j++) { char c = segmentArray[j + segmentOffset]; - cf = cf.append(c); + appendToHTML(c, cf); // int charWidth; // if (c == '\t') { // charWidth = (int) painter.nextTabStop(width, j) - width; @@ -171,7 +191,7 @@ public class DiscourseFormat { if (id == Token.END) { char c = segmentArray[segmentOffset + offset]; if (segmentOffset + offset < limit) { - cf.append(c); + appendToHTML(c, cf); } else { cf.append('\n'); } @@ -203,7 +223,7 @@ public class DiscourseFormat { // cf.append(' '); // } } else { - cf.append(c); + appendToHTML(c, cf); } // Place close tags [/] if (j == (length - 1) && id != Token.NULL && styles[id].isBold()) @@ -225,4 +245,4 @@ public class DiscourseFormat { } } } -} \ No newline at end of file +}