Skip to content

Commit 46fd4c2

Browse files
authored
feat: android - improve ol parsing (#46)
1 parent 15d2266 commit 46fd4c2

1 file changed

Lines changed: 12 additions & 18 deletions

File tree

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

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -377,13 +377,7 @@ private static void withinBlockIndividual(StringBuilder out, Spanned text, int s
377377
String tagType = isList ? "li" : "p";
378378
out.append("<");
379379

380-
if (isUlListItem) {
381-
out.append("li");
382-
} else if (isOlListItem) {
383-
out.append("li index=").append(index);
384-
} else {
385-
out.append("p");
386-
}
380+
out.append(tagType);
387381

388382
out.append(">");
389383
withinParagraph(out, text, i, next);
@@ -571,6 +565,9 @@ class HtmlToSpannedConverter implements ContentHandler {
571565
*/
572566
private static final Map<String, Integer> sColorMap;
573567

568+
private static Integer currentOrderedListItemIndex = 0;
569+
private static Boolean isInOrderedList = false;
570+
574571
static {
575572
sColorMap = new HashMap<>();
576573
sColorMap.put("darkgray", 0xFFA9A9A9);
@@ -666,6 +663,11 @@ private void handleStartTag(String tag, Attributes attributes) {
666663
startBlockElement(mSpannableStringBuilder, attributes, getMarginParagraph());
667664
startCssStyle(mSpannableStringBuilder, attributes);
668665
} else if (tag.equalsIgnoreCase("ul")) {
666+
isInOrderedList = false;
667+
startBlockElement(mSpannableStringBuilder, attributes, getMarginList());
668+
} else if (tag.equalsIgnoreCase("ol")) {
669+
isInOrderedList = true;
670+
currentOrderedListItemIndex = 0;
669671
startBlockElement(mSpannableStringBuilder, attributes, getMarginList());
670672
} else if (tag.equalsIgnoreCase("li")) {
671673
startLi(mSpannableStringBuilder, attributes);
@@ -844,17 +846,9 @@ private static void handleBr(Editable text) {
844846
private void startLi(Editable text, Attributes attributes) {
845847
startBlockElement(text, attributes, getMarginListItem());
846848

847-
// assumption, ordered list includes index as their index attribute
848-
// unordered lists does not define index
849-
String value = attributes.getValue("", "index");
850-
851-
if (value != null) {
852-
try {
853-
int index = Integer.parseInt(value);
854-
start(text, new List("ol", index));
855-
} catch (NumberFormatException e) {
856-
start(text, new List("ul", 0));
857-
}
849+
if (isInOrderedList) {
850+
currentOrderedListItemIndex++;
851+
start(text, new List("ol", currentOrderedListItemIndex));
858852
} else {
859853
start(text, new List("ul", 0));
860854
}

0 commit comments

Comments
 (0)