am 438275b2
: Merge branch \'readonly-p4-donut\' into donut
Merge commit '438275b283224a9f830e42d4e645981d1f73589b' * commit '438275b283224a9f830e42d4e645981d1f73589b': AI 149564: Escape special characters such as '<>&' in text/plain for display in WebView.
This commit is contained in:
commit
98475b1d7c
@ -94,9 +94,13 @@ public class MessageView extends Activity
|
|||||||
};
|
};
|
||||||
private static final int METHODS_STATUS_COLUMN = 1;
|
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+");
|
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 mSubjectView;
|
||||||
private TextView mFromView;
|
private TextView mFromView;
|
||||||
private TextView mDateView;
|
private TextView mDateView;
|
||||||
@ -899,6 +903,10 @@ public class MessageView extends Activity
|
|||||||
text = EmailHtmlUtil.resolveInlineImage(
|
text = EmailHtmlUtil.resolveInlineImage(
|
||||||
getContentResolver(), mAccount, text, mMessage, 0);
|
getContentResolver(), mAccount, text, mMessage, 0);
|
||||||
} else {
|
} 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
|
* Linkify the plain text and convert it to HTML by replacing
|
||||||
* \r?\n with <br> and adding a html/body wrapper.
|
* \r?\n with <br> and adding a html/body wrapper.
|
||||||
@ -923,7 +931,7 @@ public class MessageView extends Activity
|
|||||||
m.appendTail(sb);
|
m.appendTail(sb);
|
||||||
}
|
}
|
||||||
sb.append("</body></html>");
|
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 " ".
|
||||||
|
for (int i = 1, n = end - start; i < n; ++i) {
|
||||||
|
out.append(" ");
|
||||||
|
}
|
||||||
|
out.append(' ');
|
||||||
|
} else if (c == '\r' || c == '\n') {
|
||||||
|
out.append("<br>");
|
||||||
|
} else if (c == '<') {
|
||||||
|
out.append("<");
|
||||||
|
} else if (c == '>') {
|
||||||
|
out.append(">");
|
||||||
|
} else if (c == '&') {
|
||||||
|
out.append("&");
|
||||||
|
}
|
||||||
|
} while (match.find());
|
||||||
|
out.append(text.substring(end));
|
||||||
|
text = out.toString();
|
||||||
|
}
|
||||||
|
return text;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -65,7 +65,7 @@ public class MimeHeader {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void addHeader(String name, String value) throws MessagingException {
|
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 {
|
public void setHeader(String name, String value) throws MessagingException {
|
||||||
|
@ -74,6 +74,10 @@ public class MessageViewTests
|
|||||||
private WebView mMessageContentView;
|
private WebView mMessageContentView;
|
||||||
private Context mContext;
|
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() {
|
public MessageViewTests() {
|
||||||
super("com.android.email", MessageView.class);
|
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", "<b>Plain</b> &", plainTags);
|
||||||
|
|
||||||
|
// Successive spaces will be escaped as " "
|
||||||
|
String plainSpaces = MessageView.escapeCharacterToDisplay(textSpaces);
|
||||||
|
assertEquals("plain spaces", "3 spaces end.", plainSpaces);
|
||||||
|
|
||||||
|
// Newlines will be escaped as "<br>"
|
||||||
|
String plainNewlines = MessageView.escapeCharacterToDisplay(textNewlines);
|
||||||
|
assertEquals("plain spaces", "ab <br> <br> <br><br>", plainNewlines);
|
||||||
|
|
||||||
|
// All combinations.
|
||||||
|
String textAll = textTags + "\n" + textSpaces + "\n" + textNewlines;
|
||||||
|
String plainAll = MessageView.escapeCharacterToDisplay(textAll);
|
||||||
|
assertEquals("plain all",
|
||||||
|
"<b>Plain</b> &<br>" +
|
||||||
|
"3 spaces end.<br>" +
|
||||||
|
"ab <br> <br> <br><br>",
|
||||||
|
plainAll);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user