Introduce HtmlProcessor
This commit is contained in:
parent
0627ff5f87
commit
febb7448dd
4 changed files with 57 additions and 47 deletions
|
@ -10,14 +10,9 @@ import android.content.Context;
|
||||||
import android.support.annotation.Nullable;
|
import android.support.annotation.Nullable;
|
||||||
import android.support.annotation.VisibleForTesting;
|
import android.support.annotation.VisibleForTesting;
|
||||||
import android.support.annotation.WorkerThread;
|
import android.support.annotation.WorkerThread;
|
||||||
import timber.log.Timber;
|
|
||||||
|
|
||||||
import com.fsck.k9.Globals;
|
import com.fsck.k9.Globals;
|
||||||
import com.fsck.k9.K9;
|
|
||||||
import com.fsck.k9.R;
|
import com.fsck.k9.R;
|
||||||
import com.fsck.k9.mail.internet.MimeUtility;
|
|
||||||
import com.fsck.k9.message.html.HtmlConverter;
|
|
||||||
import com.fsck.k9.message.html.HtmlSanitizer;
|
|
||||||
import com.fsck.k9.mail.Address;
|
import com.fsck.k9.mail.Address;
|
||||||
import com.fsck.k9.mail.Flag;
|
import com.fsck.k9.mail.Flag;
|
||||||
import com.fsck.k9.mail.Message;
|
import com.fsck.k9.mail.Message;
|
||||||
|
@ -28,9 +23,12 @@ import com.fsck.k9.mail.internet.Viewable;
|
||||||
import com.fsck.k9.mail.internet.Viewable.Flowed;
|
import com.fsck.k9.mail.internet.Viewable.Flowed;
|
||||||
import com.fsck.k9.mailstore.util.FlowedMessageUtils;
|
import com.fsck.k9.mailstore.util.FlowedMessageUtils;
|
||||||
import com.fsck.k9.message.extractors.AttachmentInfoExtractor;
|
import com.fsck.k9.message.extractors.AttachmentInfoExtractor;
|
||||||
|
import com.fsck.k9.message.html.HtmlConverter;
|
||||||
|
import com.fsck.k9.message.html.HtmlProcessor;
|
||||||
import com.fsck.k9.ui.crypto.MessageCryptoAnnotations;
|
import com.fsck.k9.ui.crypto.MessageCryptoAnnotations;
|
||||||
import com.fsck.k9.ui.crypto.MessageCryptoSplitter;
|
import com.fsck.k9.ui.crypto.MessageCryptoSplitter;
|
||||||
import com.fsck.k9.ui.crypto.MessageCryptoSplitter.CryptoMessageParts;
|
import com.fsck.k9.ui.crypto.MessageCryptoSplitter.CryptoMessageParts;
|
||||||
|
import timber.log.Timber;
|
||||||
|
|
||||||
import static com.fsck.k9.mail.internet.MimeUtility.getHeaderParameter;
|
import static com.fsck.k9.mail.internet.MimeUtility.getHeaderParameter;
|
||||||
import static com.fsck.k9.mail.internet.Viewable.Alternative;
|
import static com.fsck.k9.mail.internet.Viewable.Alternative;
|
||||||
|
@ -51,22 +49,22 @@ public class MessageViewInfoExtractor {
|
||||||
|
|
||||||
private final Context context;
|
private final Context context;
|
||||||
private final AttachmentInfoExtractor attachmentInfoExtractor;
|
private final AttachmentInfoExtractor attachmentInfoExtractor;
|
||||||
private final HtmlSanitizer htmlSanitizer;
|
private final HtmlProcessor htmlProcessor;
|
||||||
|
|
||||||
|
|
||||||
public static MessageViewInfoExtractor getInstance() {
|
public static MessageViewInfoExtractor getInstance() {
|
||||||
Context context = Globals.getContext();
|
Context context = Globals.getContext();
|
||||||
AttachmentInfoExtractor attachmentInfoExtractor = AttachmentInfoExtractor.getInstance();
|
AttachmentInfoExtractor attachmentInfoExtractor = AttachmentInfoExtractor.getInstance();
|
||||||
HtmlSanitizer htmlSanitizer = HtmlSanitizer.getInstance();
|
HtmlProcessor htmlProcessor = HtmlProcessor.newInstance();
|
||||||
return new MessageViewInfoExtractor(context, attachmentInfoExtractor, htmlSanitizer);
|
return new MessageViewInfoExtractor(context, attachmentInfoExtractor, htmlProcessor);
|
||||||
}
|
}
|
||||||
|
|
||||||
@VisibleForTesting
|
@VisibleForTesting
|
||||||
MessageViewInfoExtractor(Context context, AttachmentInfoExtractor attachmentInfoExtractor,
|
MessageViewInfoExtractor(Context context, AttachmentInfoExtractor attachmentInfoExtractor,
|
||||||
HtmlSanitizer htmlSanitizer) {
|
HtmlProcessor htmlProcessor) {
|
||||||
this.context = context;
|
this.context = context;
|
||||||
this.attachmentInfoExtractor = attachmentInfoExtractor;
|
this.attachmentInfoExtractor = attachmentInfoExtractor;
|
||||||
this.htmlSanitizer = htmlSanitizer;
|
this.htmlProcessor = htmlProcessor;
|
||||||
}
|
}
|
||||||
|
|
||||||
@WorkerThread
|
@WorkerThread
|
||||||
|
@ -199,8 +197,7 @@ public class MessageViewInfoExtractor {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
String content = HtmlConverter.wrapMessageContent(html);
|
String sanitizedHtml = htmlProcessor.processForDisplay(html.toString());
|
||||||
String sanitizedHtml = htmlSanitizer.sanitize(content);
|
|
||||||
|
|
||||||
return new ViewableExtractedText(text.toString(), sanitizedHtml);
|
return new ViewableExtractedText(text.toString(), sanitizedHtml);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
|
|
|
@ -0,0 +1,21 @@
|
||||||
|
package com.fsck.k9.message.html;
|
||||||
|
|
||||||
|
|
||||||
|
public class HtmlProcessor {
|
||||||
|
private final HtmlSanitizer htmlSanitizer;
|
||||||
|
|
||||||
|
|
||||||
|
public static HtmlProcessor newInstance() {
|
||||||
|
HtmlSanitizer htmlSanitizer = HtmlSanitizer.getInstance();
|
||||||
|
return new HtmlProcessor(htmlSanitizer);
|
||||||
|
}
|
||||||
|
|
||||||
|
private HtmlProcessor(HtmlSanitizer htmlSanitizer) {
|
||||||
|
this.htmlSanitizer = htmlSanitizer;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String processForDisplay(String html) {
|
||||||
|
String wrappedHtml = HtmlConverter.wrapMessageContent(html);
|
||||||
|
return htmlSanitizer.sanitize(wrappedHtml);
|
||||||
|
}
|
||||||
|
}
|
|
@ -12,8 +12,6 @@ import android.app.Application;
|
||||||
|
|
||||||
import com.fsck.k9.GlobalsHelper;
|
import com.fsck.k9.GlobalsHelper;
|
||||||
import com.fsck.k9.K9RobolectricTestRunner;
|
import com.fsck.k9.K9RobolectricTestRunner;
|
||||||
import com.fsck.k9.message.html.HtmlSanitizer;
|
|
||||||
import com.fsck.k9.message.html.HtmlSanitizerHelper;
|
|
||||||
import com.fsck.k9.mail.Address;
|
import com.fsck.k9.mail.Address;
|
||||||
import com.fsck.k9.mail.Message.RecipientType;
|
import com.fsck.k9.mail.Message.RecipientType;
|
||||||
import com.fsck.k9.mail.MessagingException;
|
import com.fsck.k9.mail.MessagingException;
|
||||||
|
@ -28,14 +26,17 @@ import com.fsck.k9.mail.internet.TextBody;
|
||||||
import com.fsck.k9.mail.internet.Viewable;
|
import com.fsck.k9.mail.internet.Viewable;
|
||||||
import com.fsck.k9.mail.internet.Viewable.MessageHeader;
|
import com.fsck.k9.mail.internet.Viewable.MessageHeader;
|
||||||
import com.fsck.k9.mailstore.MessageViewInfoExtractor.ViewableExtractedText;
|
import com.fsck.k9.mailstore.MessageViewInfoExtractor.ViewableExtractedText;
|
||||||
|
import com.fsck.k9.message.html.HtmlProcessor;
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
|
import org.mockito.invocation.InvocationOnMock;
|
||||||
|
import org.mockito.stubbing.Answer;
|
||||||
import org.robolectric.RuntimeEnvironment;
|
import org.robolectric.RuntimeEnvironment;
|
||||||
|
|
||||||
import static junit.framework.Assert.assertEquals;
|
import static junit.framework.Assert.assertEquals;
|
||||||
import static junit.framework.Assert.assertSame;
|
import static junit.framework.Assert.assertSame;
|
||||||
import static org.mockito.Matchers.any;
|
import static org.mockito.Matchers.anyString;
|
||||||
import static org.mockito.Mockito.mock;
|
import static org.mockito.Mockito.mock;
|
||||||
import static org.mockito.Mockito.when;
|
import static org.mockito.Mockito.when;
|
||||||
|
|
||||||
|
@ -57,10 +58,8 @@ public class MessageViewInfoExtractorTest {
|
||||||
|
|
||||||
GlobalsHelper.setContext(context);
|
GlobalsHelper.setContext(context);
|
||||||
|
|
||||||
HtmlSanitizer dummyHtmlSanitizer = HtmlSanitizerHelper.getDummyHtmlSanitizer();
|
HtmlProcessor htmlProcessor = createFakeHtmlProcessor();
|
||||||
|
messageViewInfoExtractor = new MessageViewInfoExtractor(context,null, htmlProcessor);
|
||||||
messageViewInfoExtractor = new MessageViewInfoExtractor(context,
|
|
||||||
null, dummyHtmlSanitizer);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -74,11 +73,11 @@ public class MessageViewInfoExtractorTest {
|
||||||
message.setHeader(MimeHeader.HEADER_CONTENT_TYPE, "text/plain; format=flowed");
|
message.setHeader(MimeHeader.HEADER_CONTENT_TYPE, "text/plain; format=flowed");
|
||||||
|
|
||||||
// Prepare fixture
|
// Prepare fixture
|
||||||
HtmlSanitizer htmlSanitizer = mock(HtmlSanitizer.class);
|
HtmlProcessor htmlProcessor = mock(HtmlProcessor.class);
|
||||||
MessageViewInfoExtractor messageViewInfoExtractor =
|
MessageViewInfoExtractor messageViewInfoExtractor =
|
||||||
new MessageViewInfoExtractor(context, null, htmlSanitizer);
|
new MessageViewInfoExtractor(context, null, htmlProcessor);
|
||||||
String value = "--sanitized html--";
|
String value = "--sanitized html--";
|
||||||
when(htmlSanitizer.sanitize(any(String.class))).thenReturn(value);
|
when(htmlProcessor.processForDisplay(anyString())).thenReturn(value);
|
||||||
|
|
||||||
// Extract text
|
// Extract text
|
||||||
List<Part> outputNonViewableParts = new ArrayList<>();
|
List<Part> outputNonViewableParts = new ArrayList<>();
|
||||||
|
@ -113,7 +112,7 @@ public class MessageViewInfoExtractorTest {
|
||||||
"</pre>";
|
"</pre>";
|
||||||
|
|
||||||
assertEquals(expectedText, container.text);
|
assertEquals(expectedText, container.text);
|
||||||
assertEquals(expectedHtml, getHtmlBodyText(container.html));
|
assertEquals(expectedHtml, container.html);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -140,7 +139,7 @@ public class MessageViewInfoExtractorTest {
|
||||||
"</pre>";
|
"</pre>";
|
||||||
|
|
||||||
assertEquals(expectedText, container.text);
|
assertEquals(expectedText, container.text);
|
||||||
assertEquals(expectedHtml, getHtmlBodyText(container.html));
|
assertEquals(expectedHtml, container.html);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -166,7 +165,7 @@ public class MessageViewInfoExtractorTest {
|
||||||
bodyText;
|
bodyText;
|
||||||
|
|
||||||
assertEquals(expectedText, container.text);
|
assertEquals(expectedText, container.text);
|
||||||
assertEquals(expectedHtml, getHtmlBodyText(container.html));
|
assertEquals(expectedHtml, container.html);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -211,7 +210,7 @@ public class MessageViewInfoExtractorTest {
|
||||||
|
|
||||||
|
|
||||||
assertEquals(expectedText, container.text);
|
assertEquals(expectedText, container.text);
|
||||||
assertEquals(expectedHtml, getHtmlBodyText(container.html));
|
assertEquals(expectedHtml, container.html);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -229,7 +228,7 @@ public class MessageViewInfoExtractorTest {
|
||||||
|
|
||||||
// Create message/rfc822 body
|
// Create message/rfc822 body
|
||||||
MimeMessage innerMessage = new MimeMessage();
|
MimeMessage innerMessage = new MimeMessage();
|
||||||
innerMessage.addSentDate(new Date(112, 02, 17), false);
|
innerMessage.addSentDate(new Date(112, 2, 17), false);
|
||||||
innerMessage.setRecipients(RecipientType.TO, new Address[] { new Address("to@example.com") });
|
innerMessage.setRecipients(RecipientType.TO, new Address[] { new Address("to@example.com") });
|
||||||
innerMessage.setSubject("Subject");
|
innerMessage.setSubject("Subject");
|
||||||
innerMessage.setFrom(new Address("from@example.com"));
|
innerMessage.setFrom(new Address("from@example.com"));
|
||||||
|
@ -290,7 +289,7 @@ public class MessageViewInfoExtractorTest {
|
||||||
"</pre>";
|
"</pre>";
|
||||||
|
|
||||||
assertEquals(expectedText, container.text);
|
assertEquals(expectedText, container.text);
|
||||||
assertEquals(expectedHtml, getHtmlBodyText(container.html));
|
assertEquals(expectedHtml, container.html);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -355,13 +354,19 @@ public class MessageViewInfoExtractorTest {
|
||||||
ViewableExtractedText firstMessageExtractedText =
|
ViewableExtractedText firstMessageExtractedText =
|
||||||
messageViewInfoExtractor.extractTextFromViewables(outputViewableParts);
|
messageViewInfoExtractor.extractTextFromViewables(outputViewableParts);
|
||||||
assertEquals(expectedExtractedText, firstMessageExtractedText.text);
|
assertEquals(expectedExtractedText, firstMessageExtractedText.text);
|
||||||
assertEquals(expectedHtmlText, getHtmlBodyText(firstMessageExtractedText.html));
|
assertEquals(expectedHtmlText, firstMessageExtractedText.html);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static String getHtmlBodyText(String htmlText) {
|
HtmlProcessor createFakeHtmlProcessor() {
|
||||||
htmlText = htmlText.substring(htmlText.indexOf("<body>") +6);
|
HtmlProcessor htmlProcessor = mock(HtmlProcessor.class);
|
||||||
htmlText = htmlText.substring(0, htmlText.indexOf("</body>"));
|
|
||||||
return htmlText;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
when(htmlProcessor.processForDisplay(anyString())).thenAnswer(new Answer<String>() {
|
||||||
|
@Override
|
||||||
|
public String answer(InvocationOnMock invocation) throws Throwable {
|
||||||
|
return (String) invocation.getArguments()[0];
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
return htmlProcessor;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,13 +0,0 @@
|
||||||
package com.fsck.k9.message.html;
|
|
||||||
|
|
||||||
|
|
||||||
public class HtmlSanitizerHelper {
|
|
||||||
public static HtmlSanitizer getDummyHtmlSanitizer() {
|
|
||||||
return new HtmlSanitizer() {
|
|
||||||
@Override
|
|
||||||
public String sanitize(String html) {
|
|
||||||
return html;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
Reference in a new issue