Skip to content
Merged
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
15 changes: 6 additions & 9 deletions AnkiDroid/src/main/java/com/ichi2/anki/ViewerResourceHandler.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,9 @@ import timber.log.Timber
import java.io.ByteArrayInputStream
import java.io.File
import java.io.FileInputStream
import java.nio.file.Paths
import kotlin.io.path.pathString

private const val RANGE_HEADER = "Range"
private const val MATHJAX_PATH_PREFIX = "/_anki/js/vendor/mathjax"
private const val MATHJAX_PATH_PREFIX = "/_anki/js/vendor/mathjax/"

class ViewerResourceHandler(
context: Context,
Expand All @@ -37,13 +35,12 @@ class ViewerResourceHandler(
}

try {
if (path == "/_anki/js/mathjax.js") {
return WebResourceResponse(guessMimeType(path), null, assetManager.open("backend/js/mathjax.js"))
}
if (path.startsWith(MATHJAX_PATH_PREFIX)) {
val mathjaxAssetPath =
Paths
.get(
"backend/js/vendor/mathjax",
path.removePrefix(MATHJAX_PATH_PREFIX),
).pathString
// Asset names use forward slashes, including when tests run on Windows.
val mathjaxAssetPath = "backend/js/vendor/mathjax/${path.removePrefix(MATHJAX_PATH_PREFIX)}"
Comment on lines +38 to +43

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

mapping /_anki/ to backend/ would be simpler and avoid churn in the future, I guess (no PC to test it)

It's less specific, only accepting mathjax calls could be safer.

Implementer choice

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I'd rather special case for now. Let's revisit it if the list grows

val inputStream = assetManager.open(mathjaxAssetPath)
return WebResourceResponse(guessMimeType(path), null, inputStream)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,28 @@ class ViewerResourceHandlerTest : RobolectricTest() {
assertNull(response, "a request escaping the media directory must not be served")
}

@Test
fun `MathJax configuration is available at the backend URL`() {
val response = ViewerResourceHandler(targetContext).shouldInterceptRequest(request("/_anki/js/mathjax.js"))

val expected = readAssetText("backend/js/mathjax.js")
assertEquals(expected, assertNotNull(response).data.bufferedReader().use { it.readText() })
}

@Test
fun `MathJax implementation is available at the backend URL`() {
val response = ViewerResourceHandler(targetContext).shouldInterceptRequest(request("/_anki/js/vendor/mathjax/tex-chtml-full.js"))

val expected = readAssetText("backend/js/vendor/mathjax/tex-chtml-full.js")
assertEquals(expected, assertNotNull(response).data.bufferedReader().use { it.readText() })
}

private fun readAssetText(path: String): String =
targetContext.assets
.open(path)
.bufferedReader()
.use { it.readText() }

private fun request(path: String): WebResourceRequest =
mock {
on { url } doReturn "http://127.0.0.1$path".toUri()
Expand Down
Loading