-
Notifications
You must be signed in to change notification settings - Fork 901
Fixing numbered lists always starting from 1 #6473
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
d904ebb
overriding the default list handler with an implementation that takes…
ouchadam ff2fc3e
adding changelog entry
ouchadam a0f86d2
removing extra line
ouchadam cfb1e09
adding tests around the event html rendering
ouchadam ead8cec
adding test case for showing html entities are processed
ouchadam File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Fixes numbered lists always starting from 1 |
91 changes: 91 additions & 0 deletions
91
vector/src/androidTest/java/im/vector/app/core/utils/TestSpan.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,91 @@ | ||
| /* | ||
| * Copyright (c) 2022 New Vector Ltd | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package im.vector.app.core.utils | ||
|
|
||
| import android.graphics.Canvas | ||
| import android.graphics.Paint | ||
| import android.text.Layout | ||
| import android.text.Spannable | ||
| import androidx.core.text.getSpans | ||
| import im.vector.app.features.html.HtmlCodeSpan | ||
| import io.mockk.justRun | ||
| import io.mockk.mockk | ||
| import io.mockk.slot | ||
| import io.mockk.verify | ||
| import io.noties.markwon.core.spans.EmphasisSpan | ||
| import io.noties.markwon.core.spans.OrderedListItemSpan | ||
| import io.noties.markwon.core.spans.StrongEmphasisSpan | ||
|
|
||
| fun Spannable.toTestSpan(): String { | ||
| var output = toString() | ||
| readSpansWithContent().forEach { | ||
| val tags = it.span.readTags() | ||
| val remappedContent = it.span.remapContent(source = this, originalContent = it.content) | ||
| output = output.replace(it.content, "${tags.open}$remappedContent${tags.close}") | ||
| } | ||
| return output | ||
| } | ||
|
|
||
| private fun Spannable.readSpansWithContent() = getSpans<Any>().map { span -> | ||
| val start = getSpanStart(span) | ||
| val end = getSpanEnd(span) | ||
| SpanWithContent( | ||
| content = substring(start, end), | ||
| span = span | ||
| ) | ||
| }.reversed() | ||
|
|
||
| private fun Any.readTags(): SpanTags { | ||
| return when (this::class) { | ||
| OrderedListItemSpan::class -> SpanTags("[list item]", "[/list item]") | ||
| HtmlCodeSpan::class -> SpanTags("[code]", "[/code]") | ||
| StrongEmphasisSpan::class -> SpanTags("[bold]", "[/bold]") | ||
| EmphasisSpan::class -> SpanTags("[italic]", "[/italic]") | ||
| else -> throw IllegalArgumentException("Unknown ${this::class}") | ||
| } | ||
| } | ||
|
|
||
| private fun Any.remapContent(source: CharSequence, originalContent: String): String { | ||
| return when (this::class) { | ||
| OrderedListItemSpan::class -> { | ||
| val prefix = (this as OrderedListItemSpan).collectNumber(source) | ||
| "$prefix$originalContent" | ||
| } | ||
| else -> originalContent | ||
| } | ||
| } | ||
|
|
||
| private fun OrderedListItemSpan.collectNumber(text: CharSequence): String { | ||
| val fakeCanvas = mockk<Canvas>() | ||
| val fakeLayout = mockk<Layout>() | ||
| justRun { fakeCanvas.drawText(any(), any(), any(), any()) } | ||
| val paint = Paint() | ||
| drawLeadingMargin(fakeCanvas, paint, 0, 0, 0, 0, 0, text, 0, text.length - 1, true, fakeLayout) | ||
| val slot = slot<String>() | ||
| verify { fakeCanvas.drawText(capture(slot), any(), any(), any()) } | ||
| return slot.captured | ||
| } | ||
|
|
||
| private data class SpanTags( | ||
| val open: String, | ||
| val close: String, | ||
| ) | ||
|
|
||
| private data class SpanWithContent( | ||
| val content: String, | ||
| val span: Any | ||
| ) |
82 changes: 82 additions & 0 deletions
82
vector/src/androidTest/java/im/vector/app/features/html/EventHtmlRendererTest.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| /* | ||
| * Copyright (c) 2022 New Vector Ltd | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package im.vector.app.features.html | ||
|
|
||
| import androidx.core.text.toSpannable | ||
| import androidx.test.platform.app.InstrumentationRegistry | ||
| import im.vector.app.core.resources.ColorProvider | ||
| import im.vector.app.core.utils.toTestSpan | ||
| import im.vector.app.features.settings.VectorPreferences | ||
| import io.mockk.every | ||
| import io.mockk.mockk | ||
| import org.amshove.kluent.shouldBeEqualTo | ||
| import org.junit.Test | ||
| import org.junit.runner.RunWith | ||
| import org.junit.runners.JUnit4 | ||
| import kotlin.text.Typography.nbsp | ||
|
|
||
| @RunWith(JUnit4::class) | ||
| class EventHtmlRendererTest { | ||
|
|
||
| private val context = InstrumentationRegistry.getInstrumentation().targetContext | ||
| private val fakeVectorPreferences = mockk<VectorPreferences>().also { | ||
| every { it.latexMathsIsEnabled() } returns false | ||
| } | ||
|
|
||
| private val renderer = EventHtmlRenderer( | ||
| MatrixHtmlPluginConfigure(ColorProvider(context), context.resources), | ||
| context, | ||
| fakeVectorPreferences | ||
| ) | ||
|
|
||
| @Test | ||
| fun takesInitialListPositionIntoAccount() { | ||
| val result = """<ol start="5"><li>first entry<li></ol>""".renderAsTestSpan() | ||
|
|
||
| result shouldBeEqualTo "[list item]5.${nbsp}first entry[/list item]\n" | ||
| } | ||
|
|
||
| @Test | ||
| fun doesNotProcessMarkdownWithinCodeBlocks() { | ||
| val result = """<code>__italic__ **bold**</code>""".renderAsTestSpan() | ||
|
|
||
| result shouldBeEqualTo "[code]__italic__ **bold**[/code]" | ||
| } | ||
|
|
||
| @Test | ||
| fun doesNotProcessMarkdownBoldAndItalic() { | ||
| val result = """__italic__ **bold**""".renderAsTestSpan() | ||
|
|
||
| result shouldBeEqualTo "__italic__ **bold**" | ||
| } | ||
|
|
||
| @Test | ||
| fun processesHtmlWithinCodeBlocks() { | ||
| val result = """<code><i>italic</i> <b>bold</b></code>""".renderAsTestSpan() | ||
|
|
||
| result shouldBeEqualTo "[code][italic]italic[/italic] [bold]bold[/bold][/code]" | ||
| } | ||
|
|
||
| @Test | ||
| fun processesHtmlEntities() { | ||
| val result = """& < > ' """".renderAsTestSpan() | ||
|
|
||
| result shouldBeEqualTo """& < > ' """" | ||
| } | ||
|
|
||
| private fun String.renderAsTestSpan() = renderer.render(this).toSpannable().toTestSpan() | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
111 changes: 111 additions & 0 deletions
111
vector/src/main/java/im/vector/app/features/html/ListHandlerWithInitialStart.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,111 @@ | ||
| /* | ||
| * Copyright (c) 2022 New Vector Ltd | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package im.vector.app.features.html; | ||
|
|
||
| import androidx.annotation.NonNull; | ||
|
|
||
| import org.commonmark.node.ListItem; | ||
|
|
||
| import java.util.Arrays; | ||
| import java.util.Collection; | ||
|
|
||
| import io.noties.markwon.MarkwonConfiguration; | ||
| import io.noties.markwon.MarkwonVisitor; | ||
| import io.noties.markwon.RenderProps; | ||
| import io.noties.markwon.SpanFactory; | ||
| import io.noties.markwon.SpannableBuilder; | ||
| import io.noties.markwon.core.CoreProps; | ||
| import io.noties.markwon.html.HtmlTag; | ||
| import io.noties.markwon.html.MarkwonHtmlRenderer; | ||
| import io.noties.markwon.html.TagHandler; | ||
|
|
||
| /** | ||
| * Copied from https://github.com/noties/Markwon/blob/master/markwon-html/src/main/java/io/noties/markwon/html/tag/ListHandler.java#L44 | ||
| * With a modification on the starting list position | ||
| */ | ||
| public class ListHandlerWithInitialStart extends TagHandler { | ||
|
|
||
| private static final String START_KEY = "start"; | ||
|
|
||
| @Override | ||
| public void handle( | ||
| @NonNull MarkwonVisitor visitor, | ||
| @NonNull MarkwonHtmlRenderer renderer, | ||
| @NonNull HtmlTag tag) { | ||
|
|
||
| if (!tag.isBlock()) { | ||
| return; | ||
| } | ||
|
|
||
| final HtmlTag.Block block = tag.getAsBlock(); | ||
| final boolean ol = "ol".equals(block.name()); | ||
| final boolean ul = "ul".equals(block.name()); | ||
|
|
||
| if (!ol && !ul) { | ||
| return; | ||
| } | ||
|
|
||
| final MarkwonConfiguration configuration = visitor.configuration(); | ||
| final RenderProps renderProps = visitor.renderProps(); | ||
| final SpanFactory spanFactory = configuration.spansFactory().get(ListItem.class); | ||
|
|
||
| // Modified line | ||
| int number = Integer.parseInt(block.attributes().containsKey(START_KEY) ? block.attributes().get(START_KEY) : "1"); | ||
|
|
||
| final int bulletLevel = currentBulletListLevel(block); | ||
|
|
||
| for (HtmlTag.Block child : block.children()) { | ||
|
|
||
| visitChildren(visitor, renderer, child); | ||
|
|
||
| if (spanFactory != null && "li".equals(child.name())) { | ||
|
|
||
| // insert list item here | ||
| if (ol) { | ||
| CoreProps.LIST_ITEM_TYPE.set(renderProps, CoreProps.ListItemType.ORDERED); | ||
| CoreProps.ORDERED_LIST_ITEM_NUMBER.set(renderProps, number++); | ||
| } else { | ||
| CoreProps.LIST_ITEM_TYPE.set(renderProps, CoreProps.ListItemType.BULLET); | ||
| CoreProps.BULLET_LIST_ITEM_LEVEL.set(renderProps, bulletLevel); | ||
| } | ||
|
|
||
| SpannableBuilder.setSpans( | ||
| visitor.builder(), | ||
| spanFactory.getSpans(configuration, renderProps), | ||
| child.start(), | ||
| child.end()); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| @NonNull | ||
| @Override | ||
| public Collection<String> supportedTags() { | ||
| return Arrays.asList("ol", "ul"); | ||
| } | ||
|
|
||
| private static int currentBulletListLevel(@NonNull HtmlTag.Block block) { | ||
| int level = 0; | ||
| while ((block = block.parent()) != null) { | ||
| if ("ul".equals(block.name()) | ||
| || "ol".equals(block.name())) { | ||
| level += 1; | ||
| } | ||
| } | ||
| return level; | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this line is the fix for taking into account the starting position