Skip to content

Commit cf91ed1

Browse files
committed
Preserving spaces and newlines in code tag.
1 parent d6d6982 commit cf91ed1

File tree

2 files changed

+30
-18
lines changed

2 files changed

+30
-18
lines changed

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

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,11 @@ public interface ImageGetter {
9090

9191
interface Callbacks {
9292
void onUseDefaultImage();
93+
9394
void onImageFailed();
95+
9496
void onImageLoaded(Drawable drawable);
97+
9598
void onImageLoading(Drawable drawable);
9699
}
97100
}
@@ -106,7 +109,7 @@ public interface TagHandler {
106109
* a tag that it does not know how to interpret.
107110
*/
108111
boolean handleTag(boolean opening, String tag, Editable output, OnMediaTappedListener onMediaTappedListener,
109-
Context context, Attributes attributes, int nestingLevel);
112+
Context context, Attributes attributes, int nestingLevel);
110113
}
111114

112115
private Html() {
@@ -183,6 +186,7 @@ class HtmlToSpannedConverter implements ContentHandler, LexicalHandler {
183186
public int unknownTagLevel = 0;
184187
public Unknown unknown;
185188
private boolean insidePreTag = false;
189+
private boolean insideCodeTag = false;
186190

187191
private String mSource;
188192
private UnknownHtmlSpan.OnUnknownHtmlClickListener onUnknownHtmlClickListener;
@@ -291,12 +295,14 @@ private void handleStartTag(String tag, Attributes attributes, int nestingLevel)
291295
} else if (tag.equalsIgnoreCase("sub")) {
292296
start(spannableStringBuilder, TextFormat.FORMAT_SUBSCRIPT, attributes);
293297
} else if (tag.equalsIgnoreCase("code")) {
298+
insideCodeTag = true;
294299
start(spannableStringBuilder, TextFormat.FORMAT_CODE, attributes);
295300
} else {
296301
if (tagHandler != null) {
297-
if(tag.equalsIgnoreCase("pre")){
302+
if (tag.equalsIgnoreCase("pre")) {
298303
insidePreTag = true;
299304
}
305+
300306
boolean tagHandled = tagHandler.handleTag(true, tag, spannableStringBuilder, onMediaTappedListener,
301307
context, attributes, nestingLevel);
302308
if (tagHandled) {
@@ -369,9 +375,10 @@ private void handleEndTag(String tag, int nestingLevel) {
369375
} else if (tag.equalsIgnoreCase("sub")) {
370376
end(spannableStringBuilder, TextFormat.FORMAT_SUBSCRIPT);
371377
} else if (tag.equalsIgnoreCase("code")) {
378+
insideCodeTag = false;
372379
end(spannableStringBuilder, TextFormat.FORMAT_CODE);
373380
} else if (tagHandler != null) {
374-
if(tag.equalsIgnoreCase("pre")){
381+
if (tag.equalsIgnoreCase("pre")) {
375382
insidePreTag = false;
376383
}
377384
tagHandler.handleTag(false, tag, spannableStringBuilder, onMediaTappedListener, context, new AztecAttributes(),
@@ -611,7 +618,7 @@ public void characters(char ch[], int start, int length) throws SAXException {
611618
for (int i = 0; i < length; i++) {
612619
char c = ch[i + start];
613620

614-
if (!insidePreTag && c == ' ' || c == '\n') {
621+
if (!insidePreTag && !insideCodeTag && c == ' ' || c == '\n') {
615622
char pred;
616623
int len = sb.length();
617624

aztec/src/main/kotlin/org/wordpress/aztec/watchers/EndOfParagraphMarkerAdder.kt

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,7 @@ import android.text.Editable
44
import android.text.Spanned
55
import android.text.TextWatcher
66
import org.wordpress.aztec.AztecText
7-
import org.wordpress.aztec.spans.AztecHeadingSpan
8-
import org.wordpress.aztec.spans.AztecListItemSpan
9-
import org.wordpress.aztec.spans.AztecPreformatSpan
10-
import org.wordpress.aztec.spans.EndOfParagraphMarker
7+
import org.wordpress.aztec.spans.*
118
import java.lang.ref.WeakReference
129

1310

@@ -36,22 +33,30 @@ class EndOfParagraphMarkerAdder(aztecText: AztecText, val verticalParagraphMargi
3633
val inputStart = textChangedEventDetails.inputStart
3734
val inputEnd = textChangedEventDetails.inputEnd
3835

39-
val isInsideList = aztecText.text.getSpans(inputStart, inputEnd, AztecListItemSpan::class.java).isNotEmpty()
40-
val isInsidePre = aztecText.text.getSpans(inputStart, inputEnd, AztecPreformatSpan::class.java).isNotEmpty()
41-
var insideHeading = aztecText.text.getSpans(inputStart, inputEnd, AztecHeadingSpan::class.java).isNotEmpty()
42-
43-
if (insideHeading && (aztecText.text.length > inputEnd
44-
&& aztecText.text[inputEnd] == '\n')) {
45-
insideHeading = false
46-
}
47-
48-
if (!isInsideList && !insideHeading && !isInsidePre) {
36+
if (paragraphMarkerCanBeApplied(aztecText.text)) {
4937
aztecText.text.setSpan(EndOfParagraphMarker(verticalParagraphMargin), inputStart, inputEnd,
5038
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
5139
}
5240
}
5341
}
5442

43+
fun paragraphMarkerCanBeApplied(text: Editable): Boolean {
44+
val inputStart = textChangedEventDetails.inputStart
45+
val inputEnd = textChangedEventDetails.inputEnd
46+
47+
val isInsideList = text.getSpans(inputStart, inputEnd, AztecListItemSpan::class.java).isNotEmpty()
48+
val isInsidePre = text.getSpans(inputStart, inputEnd, AztecPreformatSpan::class.java).isNotEmpty()
49+
val isInsideCode = text.getSpans(inputStart, inputEnd, AztecCodeSpan::class.java).isNotEmpty()
50+
var insideHeading = text.getSpans(inputStart, inputEnd, AztecHeadingSpan::class.java).isNotEmpty()
51+
52+
if (insideHeading && (text.length > inputEnd
53+
&& text[inputEnd] == '\n')) {
54+
insideHeading = false
55+
}
56+
57+
return !isInsideList && !insideHeading && !isInsidePre && !isInsideCode
58+
}
59+
5560
override fun afterTextChanged(text: Editable) {
5661
text.getSpans(0, text.length, EndOfParagraphMarker::class.java).forEach {
5762
text.setSpan(it, text.getSpanStart(it), text.getSpanEnd(it), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)

0 commit comments

Comments
 (0)