Skip to content

Commit 6996dfd

Browse files
fix: handle style conflicts during automatic links detection (#257)
# Summary Fixes: #256 Previously, links that were automatically detected from typed text could still receive link styles even in areas where link styling should be restricted (e.g. inside a code block). This PR fixes that, making the behavior consistent with manual link application: - Adds a new helper function `detectLinkConflicts` that checks whether the selected range contains blocking or conflicting styles. - Updates `afterTextChangedLinks` to skip applying auto-detected links if any conflicts are detected. ## Test Plan 1. Go to Example app 2. Create codeblock 3. Type example.com inside codeblock ## Screenshots / Videos https://github.com/user-attachments/assets/d1e66dec-527e-4bd5-adce-e1db2f2867ba ## Compatibility | OS | Implemented | | ------- | :---------: | | iOS | ❌ | | Android | ✅ |
1 parent 2f7feef commit 6996dfd

1 file changed

Lines changed: 18 additions & 2 deletions

File tree

android/src/main/java/com/swmansion/enriched/styles/ParametrizedStyles.kt

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,15 +104,31 @@ class ParametrizedStyles(private val view: EnrichedTextInputView) {
104104
return Triple(result, start, end)
105105
}
106106

107+
private fun canLinkBeApplied(): Boolean {
108+
val mergingConfig = EnrichedSpans.getMergingConfigForStyle(EnrichedSpans.LINK, view.htmlStyle)?: return true
109+
val conflictingStyles = mergingConfig.conflictingStyles
110+
val blockingStyles = mergingConfig.blockingStyles
111+
112+
for (style in blockingStyles) {
113+
if (view.spanState?.getStart(style) != null) return false
114+
}
115+
116+
for (style in conflictingStyles) {
117+
if (view.spanState?.getStart(style) != null) return false
118+
}
119+
120+
return true
121+
}
122+
107123
private fun afterTextChangedLinks(result: Triple<String, Int, Int>) {
108124
// Do not detect link if it's applied manually
109-
if (isSettingLinkSpan) return
125+
if (isSettingLinkSpan || !canLinkBeApplied()) return
126+
110127
val spannable = view.text as Spannable
111128
val (word, start, end) = result
112129

113130
// TODO: Consider using more reliable regex, this one matches almost anything
114131
val urlPattern = android.util.Patterns.WEB_URL.matcher(word)
115-
116132
val spans = spannable.getSpans(start, end, EnrichedLinkSpan::class.java)
117133
for (span in spans) {
118134
spannable.removeSpan(span)

0 commit comments

Comments
 (0)