Skip to content

A community-supported supercharged document management system: scan, index and archive all your documents

add unittest for transform_inline_html

phaile384bd78c5ee
diff --git a/src/paperless_mail/parsers.py b/src/paperless_mail/parsers.py
index ed084618a..ccfdfe3a3 100644
--- a/src/paperless_mail/parsers.py
+++ b/src/paperless_mail/parsers.py
@@ -296,22 +296,26 @@ class MailDocumentParser(DocumentParser):
return response.content
- def transform_inline_html(self, orig_html, attachments):
+ @staticmethod
+ def transform_inline_html(html, attachments):
def clean_html_script(text: str):
- text = text.replace("<script", "<div hidden ")
- text = text.replace("</script", "</div")
+ compiled_open = re.compile(re.escape("<script"), re.IGNORECASE)
+ text = compiled_open.sub("<div hidden ", text)
+
+ compiled_close = re.compile(re.escape("</script"), re.IGNORECASE)
+ text = compiled_close.sub("</div", text)
return text
- orig_html = clean_html_script(orig_html)
+ html_clean = clean_html_script(html)
files = []
for a in attachments:
name_cid = "cid:" + a.content_id
name_clean = "".join(e for e in name_cid if e.isalnum())
files.append((name_clean, BytesIO(a.payload)))
- orig_html = orig_html.replace(name_cid, name_clean)
+ html_clean = html_clean.replace(name_cid, name_clean)
- files.append(("index.html", StringIO(orig_html)))
+ files.append(("index.html", StringIO(html_clean)))
return files
diff --git a/src/paperless_mail/tests/samples/sample.html b/src/paperless_mail/tests/samples/sample.html
new file mode 100644
index 000000000..3aa5f615d
--- /dev/null
+++ b/src/paperless_mail/tests/samples/sample.html
@@ -0,0 +1,15 @@
+ <html>
+ <head>
+ <meta http-equiv="content-type" content="text/html; charset=UTF-8">
+ </head>
+ <body>
+ <p>Some Text</p>
+ <p><img src="cid:[email protected]" alt=""></p>
+ <p>and an embedded image.<br>
+ </p>
+ <p id="changeme">Paragraph unchanged.</p>
+ <scRipt>
+ document.getElementById("changeme").innerHTML = "Paragraph changed via Java Script.";
+ </script>
+ </body>
+ </html>
diff --git a/src/paperless_mail/tests/samples/sample.png b/src/paperless_mail/tests/samples/sample.png
new file mode 100644
index 000000000..162aadad0
Binary files /dev/null and b/src/paperless_mail/tests/samples/sample.png differ
diff --git a/src/paperless_mail/tests/test_eml.py b/src/paperless_mail/tests/test_eml.py
index a0f4c0b77..cfbe31edb 100644
--- a/src/paperless_mail/tests/test_eml.py
+++ b/src/paperless_mail/tests/test_eml.py
@@ -282,3 +282,27 @@ class TestParser(TestCase):
# Check successful parsing
parsed = parser.tika_parse(html)
self.assertEqual(expected_text, parsed)
+
+ def test_transform_inline_html(self):
+ class MailAttachmentMock:
+ def __init__(self, payload, content_id):
+ self.payload = payload
+ self.content_id = content_id
+
+ parser = MailDocumentParser(None)
+
+ result = None
+
+ with open(os.path.join(self.SAMPLE_FILES, "sample.html")) as html_file:
+ with open(os.path.join(self.SAMPLE_FILES, "sample.png"), "rb") as png_file:
+ html = html_file.read()
+ png = png_file.read()
+ attachments = [
+ MailAttachmentMock(png, "[email protected]"),
+ ]
+ result = parser.transform_inline_html(html, attachments)
+
+ resulting_html = result[-1][1].read()
+ self.assertTrue(result[-1][0] == "index.html")
+ self.assertTrue(result[0][0] in resulting_html)
+ self.assertFalse("<script" in resulting_html.lower())