Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #3432: [A11y] Content description for rich text based images #3433

Merged
merged 7 commits into from
Jul 12, 2021
Merged
Show file tree
Hide file tree
Changes from 4 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,6 +113,8 @@ class CustomHtmlContentHandler private constructor(
customTagHandlers.getValue(tag).handleClosingTag(output)
customTagHandlers.getValue(tag)
.handleTag(attributes, openTagIndex, output.length, output, imageRetriever)
customTagHandlers.getValue(tag)
.handleContentDescription(attributes, openTagIndex, output.length, output)
}
}
}
Expand Down Expand Up @@ -163,6 +165,21 @@ class CustomHtmlContentHandler private constructor(
* @param output the destination [Editable] to which spans can be added
*/
fun handleClosingTag(output: Editable) {}

/**
* Called when a custom tag is encountered. This is always called after the closing tag.
*
* @param attributes the tag's attributes
* @param openIndex the index in the output [Editable] at which this tag begins
* @param closeIndex the index in the output [Editable] at which this tag ends
* @param output the destination [Editable] to which spans can be added
*/
fun handleContentDescription(
attributes: Attributes,
openIndex: Int,
closeIndex: Int,
output: Editable
) {}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ package org.oppia.android.util.parser.html

import android.text.Editable
import android.text.Spannable
import android.text.SpannableStringBuilder
import android.text.style.ImageSpan
import org.oppia.android.util.logging.ConsoleLogger
import org.xml.sax.Attributes

/** The custom tag corresponding to [ImageTagHandler]. */
const val CUSTOM_IMG_TAG = "oppia-noninteractive-image"
private const val CUSTOM_IMG_FILE_PATH_ATTRIBUTE = "filepath-with-value"
private const val CUSTOM_IMG_ALT_TEXT_ATTRIBUTE = "alt-with-value"

/**
* A custom tag handler for supporting custom Oppia images parsed with [CustomHtmlContentHandler].
Expand Down Expand Up @@ -43,6 +45,25 @@ class ImageTagHandler(
endIndex,
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE
)
} else consoleLogger.e("ImageTagHandler", "Failed to parse image tag")
} else consoleLogger.e("ImageTagHandler", "Failed to parse $CUSTOM_IMG_FILE_PATH_ATTRIBUTE")
}

override fun handleContentDescription(
attributes: Attributes,
openIndex: Int,
closeIndex: Int,
output: Editable
) {
val contentDescription = attributes.getJsonStringValue(CUSTOM_IMG_ALT_TEXT_ATTRIBUTE)
if (contentDescription != null) {
val spannableBuilder = SpannableStringBuilder(contentDescription)
spannableBuilder.setSpan(
contentDescription,
/* start= */0,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit fix

Suggested change
/* start= */0,
/* start= */ 0,

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

/* end= */ contentDescription.length,
Spannable.SPAN_INCLUSIVE_EXCLUSIVE
)
output.replace(openIndex, closeIndex, spannableBuilder)
} else consoleLogger.e("ImageTagHandler", "Failed to parse $CUSTOM_IMG_ALT_TEXT_ATTRIBUTE")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ private const val IMAGE_TAG_WITHOUT_FILEPATH_MARKUP =
"<oppia-noninteractive-image alt-with-value=\"&amp;quot;alt text 2&amp;quot;\" " +
"caption-with-value=\"&amp;quot;&amp;quot;\"></oppia-noninteractive-image>"

private const val IMAGE_TAG_WITHOUT_ALT_VALUE_MARKUP =
"<oppia-noninteractive-image caption-with-value=\"&amp;quot;&amp;quot;\" " +
"filepath-with-value=\"&amp;quot;test_image1.png&amp;quot;\"></oppia-noninteractive-image>"

/** Tests for [ImageTagHandler]. */
@RunWith(AndroidJUnit4::class)
@LooperMode(LooperMode.Mode.PAUSED)
Expand Down Expand Up @@ -109,7 +113,7 @@ class ImageTagHandlerTest {
fun testParseHtml_withImageCardMarkup_hasNoReadableText() {
val parsedHtml =
CustomHtmlContentHandler.fromHtml(
html = IMAGE_TAG_MARKUP_1,
html = IMAGE_TAG_WITHOUT_ALT_VALUE_MARKUP,
imageRetriever = mockImageRetriever,
customTagHandlers = tagHandlersWithImageTagSupport
)
Expand All @@ -120,6 +124,22 @@ class ImageTagHandlerTest {
assertThat(parsedHtmlStr.first().isObjectReplacementCharacter()).isTrue()
}

@Test
fun testParseHtml_withImageCardMarkup_missingAltValue_hasReadableText() {
val parsedHtml =
CustomHtmlContentHandler.fromHtml(
html = IMAGE_TAG_MARKUP_1,
imageRetriever = mockImageRetriever,
customTagHandlers = tagHandlersWithImageTagSupport
)

// The image only adds a control character, so there aren't any human-readable characters.
val parsedHtmlStr = parsedHtml.toString()
val parsedContentDescription = "alt text 1"
assertThat(parsedHtmlStr).hasLength(parsedContentDescription.length)
assertThat(parsedHtmlStr.first().isObjectReplacementCharacter()).isFalse()
}

@Test
fun testParseHtml_withImageCardMarkup_missingFilename_doesNotIncludeImageSpan() {
val parsedHtml =
Expand Down