Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 53016390 authored by Philip Whitehouse's avatar Philip Whitehouse
Browse files

Handle invalid HTML better

parent e238ee54
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -27,7 +27,7 @@ dependencies {
    compile "com.squareup.okio:okio:${okioVersion}"
    compile 'commons-io:commons-io:2.4'
    compile "com.android.support:support-v4:${androidSupportLibraryVersion}"
    compile 'net.sourceforge.htmlcleaner:htmlcleaner:2.16'
    compile 'net.sourceforge.htmlcleaner:htmlcleaner:2.18'
    compile 'de.cketti.library.changelog:ckchangelog:1.2.1'
    compile 'com.github.bumptech.glide:glide:3.6.1'
    compile 'com.splitwise:tokenautocomplete:2.0.7'
+1 −0
Original line number Diff line number Diff line
@@ -47,6 +47,7 @@ public class HtmlSanitizer {
        properties.setOmitDoctypeDeclaration(false);
        properties.setTranslateSpecialEntities(false);
        properties.setRecognizeUnicodeChars(false);
        properties.setIgnoreQuestAndExclam(false);

        return properties;
    }
+34 −0
Original line number Diff line number Diff line
@@ -99,4 +99,38 @@ public class HtmlSanitizerTest {
        assertEquals("<html><head><meta http-equiv=\"content-type\" content=\"text/html; charset=UTF-8\" /></head>" +
                "<body>Message</body></html>", htmlSanitizer.sanitize(html));
    }

    @Test
    public void shouldProduceValidHtmlFromHtmlWithXmlDeclaration() {
        String html = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
                "<html><head></head><body></body></html>";

        String result = htmlSanitizer.sanitize(html);

        assertEquals("<html><head></head><body></body></html>", result);
    }

    @Test
    public void shouldNormalizeTables() {
        String html = "<html><head></head><body><table><tr><td></td><td></td></tr></table></body></html>";

        String result = htmlSanitizer.sanitize(html);

        assertEquals("<html><head></head><body><table><tbody>" +
                "<tr><td></td><td></td></tr>" +
                "</tbody></table></body></html>", result);
    }

    @Test
    public void shouldHtmlEncodeXmlDirectives() {
        String html = "<html><head></head><body><table>" +
                "<tr><td><!==><!==>Hmailserver service shutdown:</td><td><!==><!==>Ok</td></tr>" +
                "</table></body></html>";

        String result = htmlSanitizer.sanitize(html);

        assertEquals("<html><head></head><body><table><tbody>" +
                "<tr><td>&lt;!==&gt;&lt;!==&gt;Hmailserver service shutdown:</td><td>&lt;!==&gt;&lt;!==&gt;Ok</td></tr>" +
                "</tbody></table></body></html>", result);
    }
}