Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -220,20 +220,21 @@ class HtmlParser private constructor(
* renders ordered lists as bullets rather than as numbers).
*/
private fun replaceListTags(html: String): String {
var adjustedHtml = html
if ("<li>" in adjustedHtml) {
adjustedHtml = adjustedHtml.replace("<li>", "<$CUSTOM_LIST_LI_TAG>")
.replace("</li>", "</$CUSTOM_LIST_LI_TAG>")
}
if ("<ul>" in adjustedHtml) {
adjustedHtml = adjustedHtml.replace("<ul>", "<$CUSTOM_LIST_UL_TAG>")
.replace("</ul>", "</$CUSTOM_LIST_UL_TAG>")
}
if ("<ol>" in adjustedHtml) {
adjustedHtml = adjustedHtml.replace("<ol>", "<$CUSTOM_LIST_OL_TAG>")
.replace("</ol>", "</$CUSTOM_LIST_OL_TAG>")
}
return adjustedHtml
return html
.replaceListTag(originalTag = "li", replacementTag = CUSTOM_LIST_LI_TAG)
.replaceListTag(originalTag = "ul", replacementTag = CUSTOM_LIST_UL_TAG)
.replaceListTag(originalTag = "ol", replacementTag = CUSTOM_LIST_OL_TAG)
}

/**
* Replaces opening and closing [originalTag] tags while preserving attributes such as the XHTML
* namespace included by Oppia's rich-text editor.
*/
private fun String.replaceListTag(originalTag: String, replacementTag: String): String {
return replace(
Regex("""<(/?)$originalTag(?=[\s>])""", RegexOption.IGNORE_CASE),
"<$1$replacementTag"
)
}

private fun trimSpannable(spannable: SpannableStringBuilder): SpannableStringBuilder {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import android.text.style.BulletSpan
import android.text.style.ClickableSpan
import android.text.style.ImageSpan
import android.text.style.LeadingMarginSpan
import android.text.style.URLSpan
import android.view.View
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
Expand All @@ -30,6 +31,7 @@ import com.google.common.truth.Truth.assertThat
import dagger.Component
import org.hamcrest.CoreMatchers
import org.hamcrest.Matchers.not
import org.json.JSONObject
import org.junit.After
import org.junit.Before
import org.junit.Rule
Expand Down Expand Up @@ -984,6 +986,68 @@ class HtmlParserTest {
)
}

@Test
fun testHtmlContent_withWorkedExampleNamespacedList_preservesBlockSpacing() {
val htmlParser = htmlParserFactory.create(
resourceBucketName,
entityType = "",
entityId = "",
imageCenterAlign = false,
displayLocale = appLanguageLocaleHandler.getDisplayLocale()
)
val textView = TextView(context)
val workedExampleMarkup = createWorkedExampleMarkup(
questionHtml = "Where should the content be separated?",
answerHtml =
"<ol xmlns=\"http://www.w3.org/1999/xhtml\"><li>Outer item one:<ul>" +
"<li>Inner item one.</li><li>Inner item two.</li></ul></li>" +
"<li>Outer item two.</li></ol><p>Following paragraph.</p>"
)

val htmlResult = htmlParser.parseOppiaHtml(
workedExampleMarkup,
textView,
workedExampleLabels = WORKED_EXAMPLE_LABELS
)

assertThat(htmlResult.getSpansFromWholeString(ListItemLeadingMarginSpan.OlSpan::class))
.hasLength(2)
assertThat(htmlResult.getSpansFromWholeString(ListItemLeadingMarginSpan.UlSpan::class))
.hasLength(2)
assertThat(htmlResult.toString()).contains("Inner item two.\nOuter item two.")
assertThat(htmlResult.toString()).contains("Outer item two.\n\nFollowing paragraph.")
}

@Test
fun testHtmlContent_withWorkedExampleNamespacedList_doesNotCreateFalseLinks() {
val htmlParser = htmlParserFactory.create(
resourceBucketName,
entityType = "",
entityId = "",
imageCenterAlign = false,
displayLocale = appLanguageLocaleHandler.getDisplayLocale()
)
val textView = TextView(context)
val workedExampleMarkup = createWorkedExampleMarkup(
questionHtml = "Add the decimals.",
answerHtml =
"<ul xmlns=\"http://www.w3.org/1999/xhtml\">" +
"<li>Add a decimal point before the tenths place.</li>" +
"<li>Now add the place value digits.</li>" +
"<li>The answer is 5.682.</li></ul>"
)

val htmlResult = htmlParser.parseOppiaHtml(
workedExampleMarkup,
textView,
workedExampleLabels = WORKED_EXAMPLE_LABELS
)

assertThat(htmlResult.toString()).contains("place.\nNow")
assertThat(htmlResult.toString()).contains("digits.\nThe")
assertThat(htmlResult.getSpansFromWholeString(URLSpan::class)).isEmpty()
}

@Test
fun testHtmlContent_withUrl_hasClickableSpanAndCorrectText() {
val htmlParser = htmlParserFactory.create(
Expand Down Expand Up @@ -1430,6 +1494,21 @@ class HtmlParserTest {
return DisplayLocaleImpl(context, formattingLocale, machineLocale, formatterFactory)
}

private fun createWorkedExampleMarkup(questionHtml: String, answerHtml: String): String {
return "<$CUSTOM_WORKED_EXAMPLE_TAG " +
"question-with-value=\"${questionHtml.encodeAsWorkedExampleAttribute()}\" " +
"answer-with-value=\"${answerHtml.encodeAsWorkedExampleAttribute()}\">" +
"</$CUSTOM_WORKED_EXAMPLE_TAG>"
}

private fun String.encodeAsWorkedExampleAttribute(): String {
return JSONObject.quote(this)
.replace("&", "&amp;amp;")
.replace("\"", "&amp;quot;")
.replace("<", "&amp;lt;")
.replace(">", "&amp;gt;")
}

private fun Spannable.getTextForSpan(span: Any): String =
subSequence(getSpanStart(span), getSpanEnd(span)).toString()

Expand Down
Loading