Merge branch 'readonly-p4-donut' into donut

This commit is contained in:
Mihai Preda 2009-06-04 09:42:51 -07:00 committed by The Android Open Source Project
commit 438275b283
3 changed files with 80 additions and 4 deletions

View File

@ -94,9 +94,13 @@ public class MessageView extends Activity
};
private static final int METHODS_STATUS_COLUMN = 1;
// regex that matches start of img tag. '<(?i)img\s+'.
// Regex that matches start of img tag. '<(?i)img\s+'.
private static final Pattern IMG_TAG_START_REGEX = Pattern.compile("<(?i)img\\s+");
// Regex that matches characters that has special meaning in HTML. '<', '>', '&' and
// continuous spaces at least two.
private static final Pattern PLAIN_TEXT_TO_ESCAPE = Pattern.compile("[<>&]| {2,}|\r?\n");
private TextView mSubjectView;
private TextView mFromView;
private TextView mDateView;
@ -899,6 +903,10 @@ public class MessageView extends Activity
text = EmailHtmlUtil.resolveInlineImage(
getContentResolver(), mAccount, text, mMessage, 0);
} else {
// And also escape special character, such as "<>&",
// to HTML escape sequence.
text = escapeCharacterToDisplay(text);
/*
* Linkify the plain text and convert it to HTML by replacing
* \r?\n with <br> and adding a html/body wrapper.
@ -923,7 +931,7 @@ public class MessageView extends Activity
m.appendTail(sb);
}
sb.append("</body></html>");
text = sb.toString().replaceAll("\r?\n", "<br>");
text = sb.toString();
}
/*
@ -1115,5 +1123,44 @@ public class MessageView extends Activity
}
}
}
/**
* Escape some special character as HTML escape sequence.
*
* @param text Text to be displayed using WebView.
* @return Text correctly escaped.
*/
/* package */ static String escapeCharacterToDisplay(String text) {
Pattern pattern = PLAIN_TEXT_TO_ESCAPE;
Matcher match = pattern.matcher(text);
if (match.find()) {
StringBuilder out = new StringBuilder();
int end = 0;
do {
int start = match.start();
out.append(text.substring(end, start));
end = match.end();
int c = text.codePointAt(start);
if (c == ' ') {
// Escape successive spaces into series of "&nbsp;".
for (int i = 1, n = end - start; i < n; ++i) {
out.append("&nbsp;");
}
out.append(' ');
} else if (c == '\r' || c == '\n') {
out.append("<br>");
} else if (c == '<') {
out.append("&lt;");
} else if (c == '>') {
out.append("&gt;");
} else if (c == '&') {
out.append("&amp;");
}
} while (match.find());
out.append(text.substring(end));
text = out.toString();
}
return text;
}
}

View File

@ -65,7 +65,7 @@ public class MimeHeader {
}
public void addHeader(String name, String value) throws MessagingException {
mFields.add(new Field(name, MimeUtility.foldAndEncode(value)));
mFields.add(new Field(name, value));
}
public void setHeader(String name, String value) throws MessagingException {

View File

@ -74,6 +74,10 @@ public class MessageViewTests
private WebView mMessageContentView;
private Context mContext;
private static final String textTags = "<b>Plain</b> &";
private static final String textSpaces = "3 spaces end.";
private static final String textNewlines = "ab \r\n \n \n\r\n";
public MessageViewTests() {
super("com.android.email", MessageView.class);
}
@ -168,4 +172,29 @@ public class MessageViewTests
}
}
/**
* Test for escapeCharacterToDisplay in plain text mode.
*/
public void testEscapeCharacterToDisplayPlainText() {
// HTML tag will be escaped.
String plainTags = MessageView.escapeCharacterToDisplay(textTags);
assertEquals("plain tag", "&lt;b&gt;Plain&lt;/b&gt; &amp;", plainTags);
// Successive spaces will be escaped as "&nbsp;"
String plainSpaces = MessageView.escapeCharacterToDisplay(textSpaces);
assertEquals("plain spaces", "3 spaces&nbsp;&nbsp; end.", plainSpaces);
// Newlines will be escaped as "<br>"
String plainNewlines = MessageView.escapeCharacterToDisplay(textNewlines);
assertEquals("plain spaces", "ab <br>&nbsp; <br>&nbsp;&nbsp; <br><br>", plainNewlines);
// All combinations.
String textAll = textTags + "\n" + textSpaces + "\n" + textNewlines;
String plainAll = MessageView.escapeCharacterToDisplay(textAll);
assertEquals("plain all",
"&lt;b&gt;Plain&lt;/b&gt; &amp;<br>" +
"3 spaces&nbsp;&nbsp; end.<br>" +
"ab <br>&nbsp; <br>&nbsp;&nbsp; <br><br>",
plainAll);
}
}