Skip to content

Commit b664b51

Browse files
authoredNov 25, 2021
Merge pull request #945 from wordpress-mobile/fix/preserve-link-span-order
Preserve link, code span instances when styling
2 parents 01da97c + a07af30 commit b664b51

File tree

4 files changed

+65
-31
lines changed

4 files changed

+65
-31
lines changed
 

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

+2-16
Original file line numberDiff line numberDiff line change
@@ -1481,22 +1481,8 @@ open class AztecText : AppCompatEditText, TextWatcher, UnknownHtmlSpan.OnUnknown
14811481
private fun switchToAztecStyle(editable: Editable, start: Int, end: Int) {
14821482
editable.getSpans(start, end, IAztecBlockSpan::class.java).forEach { blockFormatter.setBlockStyle(it) }
14831483
editable.getSpans(start, end, EndOfParagraphMarker::class.java).forEach { it.verticalPadding = verticalParagraphMargin }
1484-
1485-
val urlSpans = editable.getSpans(start, end, AztecURLSpan::class.java)
1486-
for (span in urlSpans) {
1487-
val spanStart = editable.getSpanStart(span)
1488-
val spanEnd = editable.getSpanEnd(span)
1489-
editable.removeSpan(span)
1490-
editable.setSpan(linkFormatter.makeUrlSpan(span.url, span.attributes), spanStart, spanEnd, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
1491-
}
1492-
1493-
val codeSpans = editable.getSpans(start, end, AztecCodeSpan::class.java)
1494-
codeSpans.forEach {
1495-
val spanStart = editable.getSpanStart(it)
1496-
val spanEnd = editable.getSpanEnd(it)
1497-
editable.removeSpan(it)
1498-
editable.setSpan(inlineFormatter.makeInlineSpan(it.javaClass, it.attributes), spanStart, spanEnd, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
1499-
}
1484+
editable.getSpans(start, end, AztecURLSpan::class.java).forEach { it.linkStyle = linkFormatter.linkStyle }
1485+
editable.getSpans(start, end, AztecCodeSpan::class.java).forEach { it.codeStyle = inlineFormatter.codeStyle }
15001486

15011487
val imageSpans = editable.getSpans(start, end, AztecImageSpan::class.java)
15021488
imageSpans.forEach {

‎aztec/src/main/kotlin/org/wordpress/aztec/spans/AztecCodeSpan.kt

+9-9
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,10 @@ import org.wordpress.aztec.formatting.InlineFormatter
2727
class AztecCodeSpan(override var attributes: AztecAttributes = AztecAttributes()) : MetricAffectingSpan(), IAztecInlineSpan {
2828
override val TAG = "code"
2929

30-
private var codeBackground: Int = 0
31-
private var codeBackgroundAlpha: Float = 0.0f
32-
private var codeColor: Int = 0
30+
var codeStyle = InlineFormatter.CodeStyle(0, 0.0f, 0)
3331

3432
constructor(codeStyle: InlineFormatter.CodeStyle, attributes: AztecAttributes = AztecAttributes()) : this(attributes) {
35-
this.codeBackground = codeStyle.codeBackground
36-
this.codeBackgroundAlpha = codeStyle.codeBackgroundAlpha
37-
this.codeColor = codeStyle.codeColor
33+
this.codeStyle = codeStyle
3834
}
3935

4036
override fun updateDrawState(tp: TextPaint?) {
@@ -46,9 +42,13 @@ class AztecCodeSpan(override var attributes: AztecAttributes = AztecAttributes()
4642
}
4743

4844
private fun configureTextPaint(tp: TextPaint?) {
49-
val alpha: Int = (codeBackgroundAlpha * 255).toInt()
45+
val alpha: Int = (codeStyle.codeBackgroundAlpha * 255).toInt()
5046
tp?.typeface = Typeface.MONOSPACE
51-
tp?.bgColor = Color.argb(alpha, Color.red(codeBackground), Color.green(codeBackground), Color.blue(codeBackground))
52-
tp?.color = codeColor
47+
tp?.bgColor = Color.argb(
48+
alpha,
49+
Color.red(codeStyle.codeBackground),
50+
Color.green(codeStyle.codeBackground),
51+
Color.blue(codeStyle.codeBackground))
52+
tp?.color = codeStyle.codeColor
5353
}
5454
}

‎aztec/src/main/kotlin/org/wordpress/aztec/spans/AztecURLSpan.kt

+4-6
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,7 @@ import org.wordpress.aztec.formatting.LinkFormatter
2525
class AztecURLSpan : URLSpan, IAztecInlineSpan {
2626
override val TAG = "a"
2727

28-
private var linkColor = 0
29-
private var linkUnderline = true
28+
var linkStyle = LinkFormatter.LinkStyle(0, true)
3029

3130
override var attributes: AztecAttributes = AztecAttributes()
3231

@@ -39,12 +38,11 @@ class AztecURLSpan : URLSpan, IAztecInlineSpan {
3938
}
4039

4140
constructor(url: String, linkStyle: LinkFormatter.LinkStyle, attributes: AztecAttributes = AztecAttributes()) : this(url, attributes) {
42-
this.linkColor = linkStyle.linkColor
43-
this.linkUnderline = linkStyle.linkUnderline
41+
this.linkStyle = linkStyle
4442
}
4543

4644
override fun updateDrawState(ds: TextPaint) {
47-
ds.color = if (linkColor != 0) linkColor else ds.linkColor
48-
ds.isUnderlineText = linkUnderline
45+
ds.color = if (linkStyle.linkColor != 0) linkStyle.linkColor else ds.linkColor
46+
ds.isUnderlineText = linkStyle.linkUnderline
4947
}
5048
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package org.wordpress.aztec
2+
3+
import android.app.Activity
4+
import org.junit.Assert
5+
import org.junit.Before
6+
import org.junit.Test
7+
import org.junit.runner.RunWith
8+
import org.robolectric.Robolectric
9+
import org.robolectric.RobolectricTestRunner
10+
import org.robolectric.annotation.Config
11+
import org.wordpress.aztec.spans.AztecCodeSpan
12+
import org.wordpress.aztec.spans.AztecStyleStrongSpan
13+
import org.wordpress.aztec.spans.AztecURLSpan
14+
import org.wordpress.aztec.spans.IAztecInlineSpan
15+
16+
@RunWith(RobolectricTestRunner::class)
17+
@Config(sdk = intArrayOf(23))
18+
class InlineElementsTest {
19+
20+
lateinit var editText: AztecText
21+
22+
/**
23+
* Initialize variables.
24+
*/
25+
@Before
26+
fun init() {
27+
val activity = Robolectric.buildActivity(Activity::class.java).create().visible().get()
28+
editText = AztecText(activity)
29+
editText.setCalypsoMode(false)
30+
activity.setContentView(editText)
31+
}
32+
33+
@Test
34+
@Throws(Exception::class)
35+
fun preserveLinkTagOrder() {
36+
editText.fromHtml("<p>This is a <a href=\"http://wordpress.com\"><strong>link</strong></a></p>")
37+
val spans = editText.text.getSpans(0, editText.length(), IAztecInlineSpan::class.java)
38+
Assert.assertTrue(spans[0] is AztecURLSpan)
39+
Assert.assertTrue(spans[1] is AztecStyleStrongSpan)
40+
}
41+
42+
@Test
43+
@Throws(Exception::class)
44+
fun preserveCodeTagOrder() {
45+
editText.fromHtml("<p>This is a <code><strong>some code</strong></code></p>")
46+
val spans = editText.text.getSpans(0, editText.length(), IAztecInlineSpan::class.java)
47+
Assert.assertTrue(spans[0] is AztecCodeSpan)
48+
Assert.assertTrue(spans[1] is AztecStyleStrongSpan)
49+
}
50+
}

0 commit comments

Comments
 (0)
Please sign in to comment.