Merge pull request #7501 from SpiritCroc/duplicated-pill-fixes

Fix duplicated pills when pills contain other spans
This commit is contained in:
Benoit Marty 2022-11-07 15:33:34 +01:00 committed by GitHub
commit 7a24e16092
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 15 additions and 0 deletions

1
changelog.d/7501.bugfix Normal file
View file

@ -0,0 +1 @@
Fix duplicated mention pills in some cases

View file

@ -83,6 +83,20 @@ class PillsPostProcessor @AssistedInject constructor(
val pillSpan = linkSpan.createPillSpan(roomId) ?: return@forEach
val startSpan = renderedText.getSpanStart(linkSpan)
val endSpan = renderedText.getSpanEnd(linkSpan)
// GlideImagesPlugin causes duplicated pills if we have a nested spans in the pill span,
// such as images or italic text.
// Accordingly, it's better to remove all spans that are contained in this span before rendering.
renderedText.getSpans(startSpan, endSpan, Any::class.java).forEach remove@{
if (it !is LinkSpan) {
// Make sure to only remove spans that are contained in this link, and not are bigger than this link, e.g. like reply-blocks
val start = renderedText.getSpanStart(it)
if (start < startSpan) return@remove
val end = renderedText.getSpanEnd(it)
if (end > endSpan) return@remove
renderedText.removeSpan(it)
}
}
addPillSpan(renderedText, pillSpan, startSpan, endSpan)
}
}