Failing editor tests

This commit is contained in:
Dimitry Ivanov 2021-02-01 17:09:13 +03:00
parent 3069432bc2
commit 646e708c82
5 changed files with 247 additions and 137 deletions

View File

@ -1,4 +1,16 @@
[
{
"javaClassName": "io.noties.markwon.app.samples.html.InspectHtmlTextSample",
"id": "20210201140501",
"title": "Inspect text",
"description": "Inspect text content of a `HTML` node",
"artifacts": [
"HTML"
],
"tags": [
"HTML"
]
},
{
"javaClassName": "io.noties.markwon.app.samples.image.HugeImageSample",
"id": "20210118165230",

View File

@ -0,0 +1,57 @@
package io.noties.markwon.app.samples.html
import android.text.style.URLSpan
import io.noties.markwon.Markwon
import io.noties.markwon.MarkwonVisitor
import io.noties.markwon.app.sample.Tags
import io.noties.markwon.app.sample.ui.MarkwonTextViewSample
import io.noties.markwon.html.HtmlPlugin
import io.noties.markwon.html.HtmlTag
import io.noties.markwon.html.MarkwonHtmlRenderer
import io.noties.markwon.html.TagHandler
import io.noties.markwon.sample.annotations.MarkwonArtifact
import io.noties.markwon.sample.annotations.MarkwonSampleInfo
@MarkwonSampleInfo(
id = "20210201140501",
title = "Inspect text",
description = "Inspect text content of a `HTML` node",
artifacts = [MarkwonArtifact.HTML],
tags = [Tags.html]
)
class InspectHtmlTextSample : MarkwonTextViewSample() {
override fun render() {
val md = """
<p>lorem ipsum</p>
<div class="custom-youtube-player">https://www.youtube.com/watch?v=abcdefgh</div>
""".trimIndent()
val markwon = Markwon.builder(context)
.usePlugin(HtmlPlugin.create {
it.addHandler(DivHandler())
})
.build()
markwon.setMarkdown(textView, md)
}
class DivHandler : TagHandler() {
override fun handle(visitor: MarkwonVisitor, renderer: MarkwonHtmlRenderer, tag: HtmlTag) {
val attr = tag.attributes()["class"] ?: return
if (attr.contains(CUSTOM_CLASS)) {
val text = visitor.builder().substring(tag.start(), tag.end())
visitor.builder().setSpan(
URLSpan(text),
tag.start(),
tag.end()
)
}
}
override fun supportedTags(): Collection<String> = setOf("div")
companion object {
const val CUSTOM_CLASS = "custom-youtube-player"
}
}
}

View File

@ -94,6 +94,28 @@ public class MarkwonEditorUtilsTest {
assertMatched(input, strike, "~~", 3, 11);
}
@Test
public void delimited_triple_asterisks() {
final String input = "***italic bold bold***";
final Match bold = findDelimited(input, 0, "**", "__");
final Match em = findDelimited(input, 0, "*", "_");
assertMatched(input, bold, "**", 0, input.length());
assertMatched(input, em, "*", 0, input.length());
}
@Test
public void delimited_triple_asterisks_2() {
final String input = "***italic bold* bold**";
final Match bold = findDelimited(input, 0, "**", "__");
final Match em = findDelimited(input, 0, "*", "_");
assertMatched(input, bold, "**", 0, input.length());
assertMatched(input, em, "*", 0, 15);
}
private static void assertMatched(
@NonNull String input,
@Nullable Match match,

View File

@ -12,6 +12,7 @@ import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.VisibleForTesting;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ExecutorService;
@ -77,12 +78,6 @@ class AsyncDrawableLoaderImpl extends AsyncDrawableLoader {
@NonNull
private Future<?> execute(@NonNull final AsyncDrawable asyncDrawable) {
// todo: more efficient DefaultMediaDecoder... BitmapFactory.decodeStream is a bit not optimal
// for big images for sure. We _could_ introduce internal Drawable that will check for
// image bounds (but we will need to cache inputStream in order to inspect and optimize
// input image...)
return executorService.submit(new Runnable() {
@Override
public void run() {
@ -113,6 +108,8 @@ class AsyncDrawableLoaderImpl extends AsyncDrawableLoader {
final ImageItem.WithDecodingNeeded withDecodingNeeded = imageItem.getAsWithDecodingNeeded();
// @since $SNAPSHOT; close input stream
try {
MediaDecoder mediaDecoder = mediaDecoders.get(withDecodingNeeded.contentType());
if (mediaDecoder == null) {
@ -125,6 +122,13 @@ class AsyncDrawableLoaderImpl extends AsyncDrawableLoader {
// throw that no media decoder is found
throw new IllegalStateException("No media-decoder is found: " + destination);
}
} finally {
try {
withDecodingNeeded.inputStream().close();
} catch (IOException e) {
Log.e("MARKWON-IMAGE", "Error closing inputStream", e);
}
}
} else {
drawable = imageItem.getAsWithResult().result();
}

View File

@ -71,14 +71,13 @@ public class DefaultDownScalingMediaDecoder extends MediaDecoder {
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory
.decodeStream(readFile(file), null, options);
// initial result when obtaining bounds is discarded
decode(file, options);
options.inSampleSize = calculateInSampleSize(options, maxWidth, maxHeight);
options.inJustDecodeBounds = false;
final Bitmap bitmap = BitmapFactory
.decodeStream(readFile(file), null, options);
final Bitmap bitmap = decode(file, options);
return new BitmapDrawable(resources, bitmap);
} finally {
@ -123,6 +122,22 @@ public class DefaultDownScalingMediaDecoder extends MediaDecoder {
return file;
}
@Nullable
private static Bitmap decode(@NonNull File file, @NonNull BitmapFactory.Options options) {
final InputStream is = readFile(file);
// not yet, still min SDK is 16
try {
return BitmapFactory.decodeStream(is, null, options);
} finally {
try {
is.close();
} catch (IOException e) {
// ignored
}
}
}
@NonNull
private static InputStream readFile(@NonNull File file) {
try {