Skip to content

Commit 9069cf5

Browse files
authored
Merge pull request #272 from wordpress-mobile/issue/265-fixing-link-issue
Issue/265 fixing link issue
2 parents c9b86df + ce5f30a commit 9069cf5

File tree

6 files changed

+63
-7
lines changed

6 files changed

+63
-7
lines changed

aztec/src/main/kotlin/org/wordpress/aztec/AztecText.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -428,7 +428,8 @@ class AztecText : EditText, TextWatcher, UnknownHtmlSpan.OnUnknownHtmlClickListe
428428
}
429429

430430
fun getSelectedText(): String {
431-
if (selectionStart == -1 || selectionEnd == -1) return ""
431+
if (selectionStart == -1 || selectionEnd == -1
432+
|| editableText.length < selectionEnd || editableText.length < selectionStart) return ""
432433
return editableText.substring(selectionStart, selectionEnd)
433434
}
434435

aztec/src/main/kotlin/org/wordpress/aztec/formatting/BlockFormatter.kt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,14 @@ class BlockFormatter(editor: AztecText, listStyle: ListStyle, quoteStyle: QuoteS
279279
val spanStart = editableText.getSpanStart(it)
280280
var spanEnd = editableText.getSpanEnd(it)
281281

282+
//when removing empty span don't forget to delete ZWJ
283+
if(spanEnd - spanStart == 1 && editableText[spanStart] == Constants.ZWJ_CHAR){
284+
editableText.removeSpan(it)
285+
editor.disableTextChangedListener()
286+
editableText.delete(spanStart, spanStart + 1)
287+
return@forEach
288+
}
289+
282290
//if splitting block set a range that would be excluded from it
283291
val boundsOfSelectedText = if (ignoreLineBounds) IntRange(start, end) else getSelectedTextBounds(editableText, start, end)
284292

aztec/src/main/kotlin/org/wordpress/aztec/formatting/LinkFormatter.kt

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import android.text.Spanned
55
import android.text.TextUtils
66
import android.util.Patterns
77
import org.wordpress.aztec.AztecText
8+
import org.wordpress.aztec.Constants
9+
import org.wordpress.aztec.spans.AztecBlockSpan
810
import org.wordpress.aztec.spans.AztecURLSpan
911

1012

@@ -88,23 +90,39 @@ class LinkFormatter(editor: AztecText, linkStyle: LinkStyle):AztecFormatter(edit
8890

8991
fun addLink(link: String, anchor: String, start: Int, end: Int) {
9092
val cleanLink = link.trim()
91-
val newEnd: Int
9293

9394
val actualAnchor = if (TextUtils.isEmpty(anchor)) cleanLink else anchor
9495

96+
var realStart = start
97+
var realEnd = end
98+
99+
95100
if (start == end) {
101+
val insertingIntoEmptyBlockElement = editableText.getSpans(realStart,realStart,AztecBlockSpan::class.java).any {
102+
editableText.getSpanEnd(it) - editableText.getSpanStart(it) == 1 &&
103+
editableText[editableText.getSpanStart(it)] == Constants.ZWJ_CHAR
104+
}
105+
96106
//insert anchor
97-
editableText.insert(start, actualAnchor)
98-
newEnd = start + actualAnchor.length
107+
editableText.insert(realStart, actualAnchor)
108+
realEnd = realStart + actualAnchor.length
109+
110+
//when anchor is inserted into empty block element, the Constants.ZWJ_CHAR at the beginning of it
111+
// will be consumed, so we need to adjust index by 1
112+
if(insertingIntoEmptyBlockElement){
113+
realStart--
114+
realEnd--
115+
116+
}
99117
} else {
100118
//apply span to text
101119
if (editor.getSelectedText() != anchor) {
102-
editableText.replace(start, end, actualAnchor)
120+
editableText.replace(realStart, realEnd, actualAnchor)
103121
}
104-
newEnd = start + actualAnchor.length
122+
realEnd = realStart + actualAnchor.length
105123
}
106124

107-
linkValid(link, start, newEnd)
125+
linkValid(link, realStart, realEnd)
108126
}
109127

110128
fun editLink(link: String, anchor: String?, start: Int = selectionStart, end: Int = selectionEnd) {

aztec/src/test/kotlin/org/wordpress/aztec/LinkTest.kt

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,25 @@ class LinkTest() {
6363
Assert.assertEquals("<a href=\"http://wordpress.com\">WordPress</a>", editText.toHtml())
6464
}
6565

66+
@Test
67+
@Throws(Exception::class)
68+
fun insertLinkIntoEmptyQuote() {
69+
editText.toggleFormatting(TextFormat.FORMAT_QUOTE)
70+
Assert.assertEquals("<blockquote></blockquote>", editText.toHtml())
71+
editText.setSelection(editText.length())
72+
editText.link("http://wordpress.com", "WordPress")
73+
Assert.assertEquals("<blockquote><a href=\"http://wordpress.com\">WordPress</a></blockquote>", editText.toHtml())
74+
}
75+
76+
@Test
77+
@Throws(Exception::class)
78+
fun insertLinkIntoEmptyList() {
79+
editText.toggleFormatting(TextFormat.FORMAT_ORDERED_LIST)
80+
Assert.assertEquals("<ol><li></li></ol>", editText.toHtml())
81+
editText.setSelection(editText.length())
82+
editText.link("http://wordpress.com", "WordPress")
83+
Assert.assertEquals("<ol><li><a href=\"http://wordpress.com\">WordPress</a></li></ol>", editText.toHtml())
84+
}
6685

6786
@Test
6887
@Throws(Exception::class)

aztec/src/test/kotlin/org/wordpress/aztec/ListTest.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,11 @@ class ListTest(listTextFormat: TextFormat, listHtmlTag: String) {
110110
fun emptyList() {
111111
editText.toggleFormatting(listType)
112112
Assert.assertEquals("<$listTag><li></li></$listTag>", editText.toHtml())
113+
114+
//remove list
115+
editText.toggleFormatting(listType)
116+
Assert.assertEquals(0, editText.length())
117+
Assert.assertEquals("", editText.toHtml())
113118
}
114119

115120
@Test

aztec/src/test/kotlin/org/wordpress/aztec/QuoteTest.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,11 @@ class QuoteTest() {
9494
fun emptyQuote() {
9595
editText.toggleFormatting(formattingType)
9696
Assert.assertEquals("<$quoteTag></$quoteTag>", editText.toHtml())
97+
98+
//remove quote
99+
editText.toggleFormatting(formattingType)
100+
Assert.assertEquals(0, editText.length())
101+
Assert.assertEquals("", editText.toHtml())
97102
}
98103

99104
@Test

0 commit comments

Comments
 (0)