Skip to content

Commit 002ff88

Browse files
authored
Merge pull request #632 from wordpress-mobile/issue/629-IOOB-HeadingHandler-handleNewlineInBody
"Fix" IOOB handling newline in Headings
2 parents 2724e88 + 105202d commit 002ff88

File tree

4 files changed

+33
-11
lines changed

4 files changed

+33
-11
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class AztecExceptionHandler(private val logHelper: ExceptionHandlerHelper?, priv
3333
AppLog.e(AppLog.T.EDITOR, "HTML Content of Aztec Editor before the crash " + visualEditor.toPlainHtml(false))
3434
} catch (e: Throwable) {
3535
AppLog.e(AppLog.T.EDITOR, "HTML Content of Aztec Editor before the crash is unavailable, log the details instead")
36-
AztecLog.logEditorContentDetails(visualEditor)
36+
AztecLog.logContentDetails(visualEditor)
3737
}
3838
}
3939

aztec/src/main/kotlin/org/wordpress/aztec/handlers/BlockHandler.kt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@ package org.wordpress.aztec.handlers
22

33
import android.text.Spannable
44
import android.text.Spanned
5+
import org.wordpress.android.util.AppLog
56
import org.wordpress.aztec.Constants
67
import org.wordpress.aztec.spans.IAztecBlockSpan
78
import org.wordpress.aztec.spans.IAztecNestable
9+
import org.wordpress.aztec.util.AztecLog
810
import org.wordpress.aztec.util.SpanWrapper
911
import org.wordpress.aztec.watchers.BlockElementWatcher.TextChangeHandler
1012

@@ -115,6 +117,13 @@ abstract class BlockHandler<SpanType : IAztecBlockSpan>(val clazz: Class<SpanTyp
115117

116118
companion object {
117119
fun set(text: Spannable, block: IAztecBlockSpan, start: Int, end: Int) {
120+
if (start > end) {
121+
AppLog.w(AppLog.T.EDITOR, "BlockHandler.set static method called with start > end. Start: " + start + " End: " + end)
122+
AppLog.w(AppLog.T.EDITOR, "Invoked with block type of " + block.javaClass.canonicalName)
123+
AztecLog.logContentDetails(text)
124+
return
125+
}
126+
118127
val flags = Spanned.SPAN_PARAGRAPH
119128
if (SpanWrapper.isInvalidParagraph(text, start, end, flags)) return
120129

aztec/src/main/kotlin/org/wordpress/aztec/handlers/HeadingHandler.kt

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package org.wordpress.aztec.handlers
22

33
import android.text.Spannable
4+
import org.wordpress.aztec.Constants
45
import org.wordpress.aztec.spans.AztecHeadingSpan
56
import org.wordpress.aztec.watchers.TextDeleter
67

@@ -31,7 +32,14 @@ class HeadingHandler : BlockHandler<AztecHeadingSpan>(AztecHeadingSpan::class.ja
3132
// for the end-of-text marker event in order to attach the new list item to it when that happens.
3233

3334
override fun handleNewlineInBody() {
34-
if (newlineIndex == block.end - 2) {
35+
// if the new newline is the second-last character of the block and the last one is a newline (which
36+
// is a visual newline) or the end-of-buffer marker, or it's the last character of the text then it's the last
37+
// actual character of the block
38+
val atEndOfBlock = (newlineIndex == block.end - 2 &&
39+
(text[block.end - 1] == Constants.NEWLINE || text[block.end - 1] == Constants.END_OF_BUFFER_MARKER)) ||
40+
newlineIndex == text.length - 1
41+
42+
if (atEndOfBlock) {
3543
// newline added at the end of the block (right before its visual newline) so, just end the block and
3644
// not add a new block after it
3745
} else {

aztec/src/main/kotlin/org/wordpress/aztec/util/Azteclog.kt

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package org.wordpress.aztec.util
22

3+
import android.text.Spannable
34
import org.json.JSONArray
45
import org.json.JSONException
56
import org.json.JSONObject
@@ -14,23 +15,27 @@ class AztecLog {
1415
}
1516

1617
companion object {
17-
fun logEditorContentDetails(aztecText: AztecText) {
18+
fun logContentDetails(aztecText: AztecText) {
19+
AppLog.d(AppLog.T.EDITOR, "Below are the details of the content in the editor:")
20+
logContentDetails(aztecText.text)
21+
}
22+
23+
fun logContentDetails(text: Spannable) {
1824
try {
1925
val logContentJSON = JSONObject()
20-
logContentJSON.put("content", aztecText.text.toString())
21-
logContentJSON.put("length", aztecText.text.length)
26+
logContentJSON.put("content", text.toString())
27+
logContentJSON.put("length", text.length)
2228
val spansJSON = JSONArray()
23-
val spans = aztecText.text.getSpans(0, aztecText.text.length, Any::class.java)
29+
val spans = text.getSpans(0, text.length, Any::class.java)
2430
spans.forEach {
2531
val currenSpanJSON = JSONObject()
26-
currenSpanJSON.put("clasz", it.javaClass.name)
27-
currenSpanJSON.put("start", aztecText.text.getSpanStart(it))
28-
currenSpanJSON.put("end", aztecText.text.getSpanEnd(it))
29-
currenSpanJSON.put("flags", aztecText.text.getSpanFlags(it))
32+
currenSpanJSON.put("clazz", it.javaClass.name)
33+
currenSpanJSON.put("start", text.getSpanStart(it))
34+
currenSpanJSON.put("end", text.getSpanEnd(it))
35+
currenSpanJSON.put("flags", text.getSpanFlags(it))
3036
spansJSON.put(currenSpanJSON)
3137
}
3238
logContentJSON.put("spans", spansJSON)
33-
AppLog.d(AppLog.T.EDITOR, "Below are the details of the content in the editor:")
3439
AppLog.d(AppLog.T.EDITOR, logContentJSON.toString())
3540
} catch (e: JSONException) {
3641
AppLog.e(AppLog.T.EDITOR, "Uhh ohh! There was an error logging the content of the Editor. This should" +

0 commit comments

Comments
 (0)