Skip to content

Commit 991f7bf

Browse files
authored
Merge pull request #292 from wordpress-mobile/issue/288-quote-gets-removed
Quote gets removed
2 parents 24d9ef7 + e394a96 commit 991f7bf

File tree

7 files changed

+78
-42
lines changed

7 files changed

+78
-42
lines changed

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

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -99,23 +99,27 @@ class AztecParser {
9999
fun addVisualNewlinesToBlockElements(spanned: Editable) {
100100
// add visual newlines at starts
101101
spanned.getSpans(0, spanned.length, AztecSurroundedWithNewlines::class.java).forEach {
102+
val parent = AztecNestable.getParent(spanned, SpanWrapper(spanned, it))
103+
104+
// a list item "repels" a child list so the list will appear in the next line
105+
val repelling = (parent?.span is AztecListItemSpan) && (it is AztecListSpan)
106+
102107
val spanStart = spanned.getSpanStart(it)
103108

104-
// no need for newline if at text start
105-
if (spanStart < 1) {
109+
// no need for newline if at text start, unless repelling needs to happen
110+
if (!repelling && spanStart < 1) {
106111
return@forEach
107112
}
108113

109-
val parentStart = AztecNestable.getParent(spanned, SpanWrapper(spanned, it))?.start ?: 0
114+
val parentStart = parent?.start ?: 0
110115

111-
// no need for newline if we're a childBlock at the start of our parent
112-
if (spanStart == parentStart && (it is AztecChildBlockSpan || it is AztecSurroundedWithNewlines)) {
116+
// no need for newline if we're at the start of our parent, unless repelling needs to happen
117+
if (!repelling && spanStart == parentStart) {
113118
return@forEach
114119
}
115120

116-
// no need for newline if there's already one, unless we're at the start of our parent
117-
// and this is a block span
118-
if (spanStart != parentStart && spanned[spanStart - 1] == '\n' && (it is AztecBlockSpan || spanStart == 1)) {
121+
// no need for newline if there's already one, unless repelling needs to happen
122+
if (!repelling && spanned[spanStart - 1] == '\n') {
119123
return@forEach
120124
}
121125

@@ -192,16 +196,21 @@ class AztecParser {
192196
}
193197

194198
spanned.getSpans(0, spanned.length, AztecSurroundedWithNewlines::class.java).forEach {
199+
val parent = AztecNestable.getParent(spanned, SpanWrapper(spanned, it))
200+
201+
// a list item "repels" a child list so the list will appear in the next line
202+
val repelling = (parent?.span is AztecListItemSpan) && (it is AztecListSpan)
203+
195204
val spanStart = spanned.getSpanStart(it)
196205

197-
if (spanStart < 1) {
198-
// no visual newline if at text start so, return
206+
if (!repelling && spanStart < 1) {
207+
// no visual newline if at text start and not repelling so, return
199208
return@forEach
200209
}
201210

202-
if (spanStart < 2) {
203-
// no visual newline can exist unless there are at least 2 chars before the block (one will be the newline
204-
// and the other will be the leading content) so, return
211+
if (!repelling && spanStart < 2) {
212+
// if not repelling, no visual newline can exist unless there are at least 2 chars before the block
213+
// (one will be the newline and the other will be the leading content) so, return
205214
return@forEach
206215
}
207216

@@ -210,14 +219,14 @@ class AztecParser {
210219
return@forEach
211220
}
212221

213-
if (spanned.getSpans(spanStart, spanStart, AztecSurroundedWithNewlines::class.java).any {
214-
spanned.getSpanEnd(it) == spanStart }) {
222+
if (spanned.getSpans(spanStart, spanStart, AztecSurroundedWithNewlines::class.java).any { before ->
223+
spanned.getSpanEnd(before) == spanStart }) {
215224
// the newline before us is the end of a previous block element so, return
216225
return@forEach
217226
}
218227

219-
if (spanned[spanStart - 2] == '\n' && it is AztecBlockSpan) {
220-
// there's another newline before so, the adjacent one is not a visual one so, return
228+
if (!repelling && spanned[spanStart - 2] == '\n' && it is AztecBlockSpan) {
229+
// there's another newline before and we're not repelling a parent so, the adjacent one is not a visual one so, return
221230
return@forEach
222231
}
223232

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

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -338,10 +338,16 @@ class BlockFormatter(editor: AztecText, val listStyle: ListStyle, val quoteStyle
338338
if (startOfLine != 0) {
339339
val spansOnPreviousLine = editableText.getSpans(startOfLine - 1, startOfLine - 1, spanToApply.javaClass)
340340
.firstOrNull()
341-
// if same span type is found (also check for heading style equality if a heading) extend the start
342-
if (spansOnPreviousLine != null
343-
&& (spansOnPreviousLine !is AztecHeadingSpan
344-
|| spansOnPreviousLine.heading == (spanToApply as AztecHeadingSpan).heading)) {
341+
342+
if (spansOnPreviousLine == null) {
343+
// no similar blocks before us so, don't expand
344+
} else if (spansOnPreviousLine.nestingLevel != nestingLevel) {
345+
// other block is at a different nesting level so, don't expand
346+
} else if (spansOnPreviousLine is AztecHeadingSpan
347+
&& spansOnPreviousLine.heading != (spanToApply as AztecHeadingSpan).heading) {
348+
// Heading span is of different style so, don't expand
349+
} else {
350+
// expand the start
345351
startOfBlock = editableText.getSpanStart(spansOnPreviousLine)
346352
liftBlock(blockElementType, startOfBlock, endOfBlock)
347353
}
@@ -350,10 +356,16 @@ class BlockFormatter(editor: AztecText, val listStyle: ListStyle, val quoteStyle
350356
if (endOfLine != editableText.length) {
351357
val spanOnNextLine = editableText.getSpans(endOfLine + 1, endOfLine + 1, spanToApply.javaClass)
352358
.firstOrNull()
353-
// if same span type is found (also check for heading style equality if a heading) extend the end
354-
if (spanOnNextLine != null
355-
&& (spanOnNextLine !is AztecHeadingSpan
356-
|| spanOnNextLine.heading == (spanToApply as AztecHeadingSpan).heading)) {
359+
360+
if (spanOnNextLine == null) {
361+
// no similar blocks after us so, don't expand
362+
} else if (spanOnNextLine.nestingLevel != nestingLevel) {
363+
// other block is at a different nesting level so, don't expand
364+
} else if (spanOnNextLine is AztecHeadingSpan
365+
&& spanOnNextLine.heading != (spanToApply as AztecHeadingSpan).heading) {
366+
// Heading span is of different style so, don't expand
367+
} else {
368+
// expand the end
357369
endOfBlock = editableText.getSpanEnd(spanOnNextLine)
358370
liftBlock(blockElementType, startOfBlock, endOfBlock)
359371
}
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
package org.wordpress.aztec.spans
22

3-
interface AztecBlockSpan : AztecLineBlockSpan, AztecParagraphStyle, AztecSurroundedWithNewlines, AztecSpan, ParagraphFlagged
3+
4+
interface AztecBlockSpan : AztecParagraphStyle, AztecSurroundedWithNewlines, AztecSpan, ParagraphFlagged

aztec/src/main/kotlin/org/wordpress/aztec/spans/AztecChildBlockSpan.kt

Lines changed: 0 additions & 4 deletions
This file was deleted.

aztec/src/main/kotlin/org/wordpress/aztec/spans/AztecLineBlockSpan.kt

Lines changed: 0 additions & 10 deletions
This file was deleted.

aztec/src/main/kotlin/org/wordpress/aztec/spans/AztecListItemSpan.kt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@ package org.wordpress.aztec.spans
22

33
import android.text.TextUtils
44

5-
class AztecListItemSpan(override var nestingLevel: Int, override var attributes: String = "") : AztecBlockSpan,
6-
AztecChildBlockSpan {
5+
class AztecListItemSpan(override var nestingLevel: Int, override var attributes: String = "") : AztecBlockSpan {
76

87
private val TAG = "li"
98

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

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -766,4 +766,33 @@ class ListTest(listTextFormat: TextFormat, listHtmlTag: String) {
766766
Assert.assertEquals(EndOfBufferMarkerAdder.ensureEndOfTextMarker(""), editText.text.toString())
767767
Assert.assertEquals("", editText.toHtml())
768768
}
769+
770+
@Test
771+
@Throws(Exception::class)
772+
fun nestListWithSimilarNeighboringList_issue288() {
773+
val preQuote = "<$listTag><li>Unordered1</li><li></li></$listTag><blockquote>"
774+
val aftQuote = "</blockquote><$listTag><li>Unordered2</li><li></li></$listTag>"
775+
editText.fromHtml(preQuote + "Quote" + aftQuote)
776+
777+
editText.setSelection(editText.text.indexOf("Quote"))
778+
779+
editText.toggleFormatting(listType)
780+
781+
Assert.assertEquals(preQuote + "<$listTag><li>Quote</li></$listTag>" + aftQuote, editText.toHtml())
782+
}
783+
784+
@Test
785+
@Throws(Exception::class)
786+
fun nestedListsHaveVisualNewline() {
787+
val html = "outpre<blockquote><$listTag><li><$listTag><li>nested</li></$listTag></li></$listTag></blockquote>outaft"
788+
editText.fromHtml(html)
789+
790+
val nestedPosition = editText.text.indexOf("nested")
791+
792+
// there should be a (visual) newline between the nested list and the parent list item
793+
Assert.assertEquals(editText.text[nestedPosition - 1], '\n')
794+
795+
// but not in the html
796+
Assert.assertEquals(html, editText.toHtml())
797+
}
769798
}

0 commit comments

Comments
 (0)