Skip to content

Commit 9156513

Browse files
authored
android - fix html output, setting up paragraph spans, parser (#68)
1 parent 3082b48 commit 9156513

3 files changed

Lines changed: 13 additions & 9 deletions

File tree

android/src/main/java/com/swmansion/reactnativerichtexteditor/ReactNativeRichTextEditorView.kt

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -163,9 +163,7 @@ class ReactNativeRichTextEditorView : AppCompatEditText {
163163
if (value == null) return
164164
isSettingValue = true
165165

166-
val isHtmlTagRecognized = value.startsWith("<html>") && value.endsWith("</html>")
167-
val isPTagRecognized = value.startsWith("<p>") && value.endsWith("</p>")
168-
val isHtml = isHtmlTagRecognized || isPTagRecognized
166+
val isHtml = value.startsWith("<html>") && value.endsWith("</html>")
169167
if (isHtml) {
170168
val parsed = EditorParser.fromHtml(value, EditorParser.FROM_HTML_MODE_COMPACT, richTextStyle, null, null)
171169
val withoutLastNewLine = parsed.trimEnd('\n')

android/src/main/java/com/swmansion/reactnativerichtexteditor/utils/EditorParser.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -869,9 +869,9 @@ private static void endLi(Editable text, RichTextStyle style) {
869869
List l = getLast(text, List.class);
870870
if (l != null) {
871871
if (l.mType.equals("ol")) {
872-
setListSpanFromMark(text, l, new EditorOrderedListSpan(l.mIndex, style));
872+
setParagraphSpanFromMark(text, l, new EditorOrderedListSpan(l.mIndex, style));
873873
} else {
874-
setListSpanFromMark(text, l, new EditorUnorderedListSpan(style));
874+
setParagraphSpanFromMark(text, l, new EditorUnorderedListSpan(style));
875875
}
876876
}
877877

@@ -885,18 +885,19 @@ private void startBlockquote(Editable text, Attributes attributes) {
885885

886886
private static void endBlockquote(Editable text, RichTextStyle style) {
887887
endBlockElement(text);
888-
end(text, Blockquote.class, new EditorBlockQuoteSpan(style));
888+
Blockquote last = getLast(text, Blockquote.class);
889+
setParagraphSpanFromMark(text, last, new EditorBlockQuoteSpan(style));
889890
}
890891

891892
private void startCodeBlock(Editable text, Attributes attributes) {
892893
startBlockElement(text, attributes, getMarginBlockquote());
893894
start(text, new CodeBlock());
894-
;
895895
}
896896

897897
private static void endCodeBlock(Editable text, RichTextStyle style) {
898898
endBlockElement(text);
899-
end(text, CodeBlock.class, new EditorCodeBlockSpan(style));
899+
CodeBlock last = getLast(text, CodeBlock.class);
900+
setParagraphSpanFromMark(text, last, new EditorCodeBlockSpan(style));
900901
}
901902

902903
private void startHeading(Editable text, Attributes attributes, int level) {
@@ -939,7 +940,7 @@ private static void setSpanFromMark(Spannable text, Object mark, Object... spans
939940
}
940941
}
941942

942-
private static void setListSpanFromMark(Spannable text, Object mark, Object... spans) {
943+
private static void setParagraphSpanFromMark(Spannable text, Object mark, Object... spans) {
943944
int where = text.getSpanStart(mark);
944945
text.removeSpan(mark);
945946
int len = text.length();

android/src/main/java/com/swmansion/reactnativerichtexteditor/utils/EditorSelection.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,11 @@ class EditorSelection(private val editorView: ReactNativeRichTextEditorView) {
104104
endPosition++
105105
}
106106

107+
if (startPosition >= endPosition) {
108+
// If the start position is equal or greater than the end position, return the same position
109+
startPosition = endPosition
110+
}
111+
107112
return Pair(startPosition, endPosition)
108113
}
109114

0 commit comments

Comments
 (0)