Skip to content

Commit 86008eb

Browse files
authored
Merge pull request #278 from wordpress-mobile/experiment/paragraph-spans
Paragraph spans
2 parents 6983cea + f898186 commit 86008eb

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

66 files changed

+2584
-1982
lines changed

aztec/src/main/java/org/wordpress/aztec/Html.java

Lines changed: 118 additions & 189 deletions
Large diffs are not rendered by default.

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

Lines changed: 232 additions & 295 deletions
Large diffs are not rendered by default.

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

Lines changed: 13 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -34,24 +34,17 @@ class AztecTagHandler : Html.TagHandler {
3434
private var order = 0
3535

3636
override fun handleTag(opening: Boolean, tag: String, output: Editable,
37-
onMediaTappedListener: AztecText.OnMediaTappedListener?, context: Context, attributes: Attributes?): Boolean {
37+
onMediaTappedListener: AztecText.OnMediaTappedListener?, context: Context, attributes: Attributes?,
38+
nestingLevel: Int): Boolean {
3839
val attributeString = Html.stringifyAttributes(attributes).toString()
3940

4041
when (tag.toLowerCase()) {
4142
LIST_LI -> {
42-
if (opening) {
43-
start(output, AztecListItemSpan(attributeString))
44-
} else {
45-
endList(output)
46-
}
43+
handleElement(output, opening, AztecListItemSpan(nestingLevel, attributeString))
4744
return true
4845
}
4946
STRIKETHROUGH_S, STRIKETHROUGH_STRIKE, STRIKETHROUGH_DEL -> {
50-
if (opening) {
51-
start(output, AztecStrikethroughSpan(tag, attributeString))
52-
} else {
53-
end(output, AztecStrikethroughSpan::class.java)
54-
}
47+
handleElement(output, opening, AztecStrikethroughSpan(tag, attributeString))
5548
return true
5649
}
5750
DIV, SPAN -> {
@@ -63,15 +56,15 @@ class AztecTagHandler : Html.TagHandler {
6356
return true
6457
}
6558
LIST_UL -> {
66-
handleBlockElement(output, opening, AztecUnorderedListSpan(attributeString))
59+
handleElement(output, opening, AztecUnorderedListSpan(nestingLevel, attributeString))
6760
return true
6861
}
6962
LIST_OL -> {
70-
handleBlockElement(output, opening, AztecOrderedListSpan(attributeString))
63+
handleElement(output, opening, AztecOrderedListSpan(nestingLevel, attributeString))
7164
return true
7265
}
7366
BLOCKQUOTE -> {
74-
handleBlockElement(output, opening, AztecQuoteSpan(attributeString))
67+
handleElement(output, opening, AztecQuoteSpan(nestingLevel, attributeString))
7568
return true
7669
}
7770
IMAGE -> {
@@ -87,12 +80,12 @@ class AztecTagHandler : Html.TagHandler {
8780
return true
8881
}
8982
PARAGRAPH -> {
90-
handleBlockElement(output, opening, ParagraphSpan(attributeString))
83+
handleElement(output, opening, ParagraphSpan(nestingLevel, attributeString))
9184
return true
9285
}
9386
else -> {
9487
if (tag.length == 2 && Character.toLowerCase(tag[0]) == 'h' && tag[1] >= '1' && tag[1] <= '6') {
95-
handleBlockElement(output, opening, AztecHeadingSpan(tag, attributeString))
88+
handleElement(output, opening, AztecHeadingSpan(nestingLevel, tag, attributeString))
9689
return true
9790
}
9891
}
@@ -109,35 +102,14 @@ class AztecTagHandler : Html.TagHandler {
109102
return AztecMediaSpan(context, loadingDrawable, attributes, onMediaTappedListener)
110103
}
111104

112-
private fun handleBlockElement(output: Editable, opening: Boolean, span: Any) {
113-
if (output.isNotBlank()) {
114-
val nestedInBlockElement = isNestedInBlockElement(output, opening)
115-
116-
val followingBlockElement = opening && output.last() == '\n' &&
117-
output.getSpans(output.lastIndex, output.lastIndex, AztecLineBlockSpan::class.java).isNotEmpty()
118-
119-
if (!followingBlockElement && !nestedInBlockElement && (output.last() != '\n' || opening)) {
120-
output.append("\n")
121-
} else if (span !is AztecListSpan && !opening && nestedInBlockElement) {
122-
output.append("\n")
123-
}
124-
}
125-
105+
private fun handleElement(output: Editable, opening: Boolean, span: Any) {
126106
if (opening) {
127107
start(output, span)
128108
} else {
129109
end(output, span.javaClass)
130110
}
131111
}
132112

133-
fun isNestedInBlockElement(output: Editable, opening: Boolean): Boolean {
134-
val spanLookupIndex = if (opening) output.length else output.length - 1
135-
val minNumberOfSpans = if (opening) 0 else 1
136-
137-
return output.getSpans(spanLookupIndex, spanLookupIndex, AztecLineBlockSpan::class.java).size > minNumberOfSpans
138-
}
139-
140-
141113
private fun start(output: Editable, mark: Any) {
142114
output.setSpan(mark, output.length, output.length, Spanned.SPAN_MARK_MARK)
143115
}
@@ -157,31 +129,17 @@ class AztecTagHandler : Html.TagHandler {
157129
}
158130
}
159131

160-
private fun endList(output: Editable) {
161-
val last = getLast(output, AztecListItemSpan::class.java) as AztecListItemSpan
162-
if (output.isEmpty() || output.last() != '\n' ||
163-
output.getSpans(output.length, output.length, AztecListItemSpan::class.java).isNotEmpty()) {
164-
output.append("\n")
165-
}
166-
val end = output.length
167-
output.setSpan(last, end - 1, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
168-
169-
val list = output.getSpans(0, output.length, AztecListSpan::class.java).last()
170-
list.lastItem = last
171-
}
172-
173132
private fun end(output: Editable, kind: Class<*>) {
174133
val last = getLast(output, kind)
175134
val start = output.getSpanStart(last)
176135
val end = output.length
177136

178-
output.removeSpan(last) // important to keep the correct order of spans!
179137
if (start != end) {
180138
output.setSpan(last, start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
181139
}
182-
//if block element is empty add newline to it and extend span
183140
else if (start == end && AztecBlockSpan::class.java.isAssignableFrom(kind)) {
184-
output.append("\n")
141+
//if block element is empty add a ZWJ to make it non empty and extend span
142+
output.append(Constants.ZWJ_CHAR)
185143
output.setSpan(last, start, output.length, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
186144
}
187145
}
@@ -223,4 +181,4 @@ class AztecTagHandler : Html.TagHandler {
223181
}
224182
}
225183
}
226-
}
184+
}

0 commit comments

Comments
 (0)