resolved conflicts for merge of 7403a54738 to donut

This commit is contained in:
Mihai Preda 2009-06-08 20:52:12 +02:00
commit a9576bdad6
5 changed files with 79 additions and 84 deletions

View File

@ -1151,9 +1151,9 @@ public class MessageCompose extends Activity implements OnClickListener, OnFocus
if (!plainTextFlag) {
text = EmailHtmlUtil.resolveInlineImage(
getContentResolver(), mAccount, text, message, 0);
} else {
text = EmailHtmlUtil.escapeCharacterToDisplay(text);
}
text = EmailHtmlUtil.escapeCharacterToDisplay(
text, plainTextFlag);
mQuotedTextBar.setVisibility(View.VISIBLE);
mQuotedText.setVisibility(View.VISIBLE);
mQuotedText.loadDataWithBaseURL("email://", text, "text/html",
@ -1192,9 +1192,9 @@ public class MessageCompose extends Activity implements OnClickListener, OnFocus
if (!plainTextFlag) {
text = EmailHtmlUtil.resolveInlineImage(
getContentResolver(), mAccount, text, message, 0);
} else {
text = EmailHtmlUtil.escapeCharacterToDisplay(text);
}
text = EmailHtmlUtil.escapeCharacterToDisplay(
text, plainTextFlag);
mQuotedTextBar.setVisibility(View.VISIBLE);
mQuotedText.setVisibility(View.VISIBLE);
mQuotedText.loadDataWithBaseURL("email://", text, "text/html",

View File

@ -99,10 +99,6 @@ public class MessageView extends Activity
private static final Pattern IMG_TAG_START_REGEX = Pattern.compile("<(?i)img\\s+");
// Regex that matches Web URL protocol part as case insensitive.
private static final Pattern WEB_URL_PROTOCOL = Pattern.compile("(?i)http|https://");
// 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;
@ -925,7 +921,7 @@ public class MessageView extends Activity
} else {
// And also escape special character, such as "<>&",
// to HTML escape sequence.
text = escapeCharacterToDisplay(text);
text = EmailHtmlUtil.escapeCharacterToDisplay(text);
/*
* Linkify the plain text and convert it to HTML by replacing
@ -1157,44 +1153,4 @@ 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

@ -26,8 +26,15 @@ import com.android.email.provider.AttachmentProvider;
import android.content.ContentResolver;
import android.net.Uri;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class EmailHtmlUtil {
// Regex that matches characters that have special meaning in HTML. '<', '>', '&' and
// multiple continuous spaces.
private static final Pattern PLAIN_TEXT_TO_ESCAPE = Pattern.compile("[<>&]| {2,}|\r?\n");
/**
* Resolve content-id reference in src attribute of img tag to AttachmentProvider's
* content uri. This method calls itself recursively at most the number of
@ -69,10 +76,44 @@ public class EmailHtmlUtil {
return text;
}
public static String escapeCharacterToDisplay(String text, boolean plainText) {
// TODO: implement html escaping as in CL 145919, 148437 to fix bug 1785319
/**
* Escape some special character as HTML escape sequence.
*
* @param text Text to be displayed using WebView.
* @return Text correctly escaped.
*/
public 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

@ -55,8 +55,7 @@ public class MessageViewTests
private static final String EXTRA_FOLDER = "com.android.email.MessageView_folder";
private static final String EXTRA_MESSAGE = "com.android.email.MessageView_message";
private static final String EXTRA_FOLDER_UIDS = "com.android.email.MessageView_folderUids";
private static final String EXTRA_NEXT = "com.android.email.MessageView_next";
// used by the mock controller
private static final String FOLDER_NAME = "folder";
private static final String MESSAGE_UID = "message_uid";
@ -67,10 +66,6 @@ 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);
}
@ -188,30 +183,4 @@ public class MessageViewTests
super(application);
}
}
/**
* 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);
}
}

View File

@ -36,6 +36,10 @@ import java.io.IOException;
public class EmailHtmlUtilTest extends AndroidTestCase {
private Account mAccount;
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";
@Override
protected void setUp() throws Exception {
super.setUp();
@ -141,4 +145,29 @@ public class EmailHtmlUtilTest extends AndroidTestCase {
mAccount, null, msg4, 0);
assertNull(actual5);
}
/**
* Test for escapeCharacterToDisplay in plain text mode.
*/
public void testEscapeCharacterToDisplayPlainText() {
String plainTags = EmailHtmlUtil.escapeCharacterToDisplay(textTags);
assertEquals("plain tag", "&lt;b&gt;Plain&lt;/b&gt; &amp;", plainTags);
// Successive spaces will be escaped as "&nbsp;"
String plainSpaces = EmailHtmlUtil.escapeCharacterToDisplay(textSpaces);
assertEquals("plain spaces", "3 spaces&nbsp;&nbsp; end.", plainSpaces);
// Newlines will be escaped as "<br>"
String plainNewlines = EmailHtmlUtil.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 = EmailHtmlUtil.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);
}
}