Skip to content

Commit 23434cf

Browse files
authored
Merge pull request #697 from wordpress-mobile/issue/688-nested-paragraphs
Issue/688 nested paragraphs
2 parents 7f475a8 + 918fb03 commit 23434cf

File tree

2 files changed

+34
-3
lines changed

2 files changed

+34
-3
lines changed

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

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -174,9 +174,7 @@ class AztecParser @JvmOverloads constructor(val plugins: List<IAztecPlugin> = li
174174
spanned.insert(spanStart, "\n")
175175

176176
// expand all same-start parents to include the new newline
177-
SpanWrapper.getSpans<IAztecNestable>(spanned, spanStart + 1, spanStart + 2)
178-
.filter { subParent -> subParent.span.nestingLevel < it.nestingLevel && subParent.start == spanStart + 1 }
179-
.forEach { subParent -> subParent.start-- }
177+
expandSurroundingSpansAtStart(spanned, it, spanStart + 1, 1)
180178

181179
markBlockElementLineBreak(spanned, spanStart)
182180
}
@@ -196,6 +194,9 @@ class AztecParser @JvmOverloads constructor(val plugins: List<IAztecPlugin> = li
196194
// but still, expand the span to include the newline for block spans, because they are paragraphs
197195
if (it is IAztecParagraphStyle) {
198196
spanned.setSpan(it, spanned.getSpanStart(it), spanEnd + 1, spanned.getSpanFlags(it))
197+
198+
// expand all same-end parents to include the new newline
199+
expandSurroundingSpansAtEnd(spanned, it, spanEnd, 1)
199200
}
200201

201202
return@forEach
@@ -207,12 +208,33 @@ class AztecParser @JvmOverloads constructor(val plugins: List<IAztecPlugin> = li
207208
// expand the span to include the new newline for block spans, because they are paragraphs
208209
if (it is IAztecParagraphStyle) {
209210
spanned.setSpan(it, spanned.getSpanStart(it), spanEnd + 1, spanned.getSpanFlags(it))
211+
212+
// expand all same-end parents to include the new newline
213+
expandSurroundingSpansAtEnd(spanned, it, spanEnd, 1)
210214
}
211215

212216
markBlockElementLineBreak(spanned, spanEnd)
213217
}
214218
}
215219

220+
private fun expandSurroundingSpansAtStart(spanned: Editable,
221+
paragraph: IAztecSurroundedWithNewlines,
222+
spanStart: Int,
223+
extra: Int) {
224+
SpanWrapper.getSpans<IAztecNestable>(spanned, spanStart, spanned.getSpanEnd(paragraph))
225+
.filter { parent -> parent.start == spanStart && parent.span.nestingLevel < paragraph.nestingLevel }
226+
.forEach { parent -> parent.start -= extra }
227+
}
228+
229+
private fun expandSurroundingSpansAtEnd(spanned: Editable,
230+
paragraph: IAztecSurroundedWithNewlines,
231+
spanEnd: Int,
232+
extra: Int) {
233+
SpanWrapper.getSpans<IAztecNestable>(spanned, spanned.getSpanStart(paragraph), spanEnd)
234+
.filter { parent -> parent.end == spanEnd && parent.span.nestingLevel < paragraph.nestingLevel }
235+
.forEach { parent -> parent.end += extra }
236+
}
237+
216238
// Always try to put a visual newline before block elements and only put one after if needed
217239
fun syncVisualNewlinesOfBlockElements(spanned: Editable) {
218240
// clear any visual newline marking. We'll mark them with a fresh set of passes

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1219,4 +1219,13 @@ class AztecParserTest : AndroidTestCase() {
12191219
val output = mParser.toHtml(span)
12201220
Assert.assertEquals(output, inputAfterParser)
12211221
}
1222+
1223+
@Test
1224+
@Throws(Exception::class)
1225+
fun parseHtmlToSpanToHtmlParagraphInsideHiddenSpan_isEqual() {
1226+
val input = "<p>a</p><div><p>b</p></div><p>c</p>"
1227+
val span = SpannableString(mParser.fromHtml(input, RuntimeEnvironment.application.applicationContext))
1228+
val output = mParser.toHtml(span)
1229+
Assert.assertEquals(input, output)
1230+
}
12221231
}

0 commit comments

Comments
 (0)