Skip to content

Commit

Permalink
Fix possible collision of minemark tag causing issues with parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
DeDiamondPro committed Aug 11, 2024
1 parent d8081dc commit 8f90383
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 5 deletions.
15 changes: 14 additions & 1 deletion src/main/java/dev/dediamondpro/minemark/MineMarkCore.java
Original file line number Diff line number Diff line change
Expand Up @@ -140,12 +140,15 @@ private MineMarkElement<S, R> parseDocument(@NotNull S style, Node document, @No
String html = htmlRenderer.render(document);
// Remove the markdown activation part
html = ACTIVATION_PATTERN.matcher(html).replaceFirst("");
// Get the wrapper to wrap the content with, make sure the html does not include it
String wrapper = getMineMarkWrapper(html);
// Prepare the HTML for parsing
html = "<minemark>\n" + html + "</minemark>";
html = "<" + wrapper + ">" + html + "</" + wrapper + ">";
// Acquire the lock to make sure this thread is the only one using the parser
parsingLock.lock();
try (InputStream stream = new ByteArrayInputStream(html.getBytes(charSet))) {
htmlParser.setStyle(style, new LayoutStyle(style));
htmlParser.setWrapper(wrapper);
InputSource source = new InputSource(stream);
source.setEncoding(charSet.name());
xmlParser.parse(source);
Expand All @@ -156,6 +159,16 @@ private MineMarkElement<S, R> parseDocument(@NotNull S style, Node document, @No
}
}

private String getMineMarkWrapper(String html) {
String wrapper = "minemark";
int num = 0;
while (html.contains(wrapper)) {
wrapper = "minemark-" + num;
num++;
}
return wrapper;
}

/**
* @param <S> The {@link Style} this core and all associated elements will use
* @param <R> The class passed to the rendering implementation at render time
Expand Down
16 changes: 12 additions & 4 deletions src/main/java/dev/dediamondpro/minemark/MineMarkHtmlParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public class MineMarkHtmlParser<S extends Style, R> extends DefaultHandler {
private MineMarkElement<S, R> markDown;
private Element<S, R> currentElement;
private LayoutStyle layoutStyle;
private String wrapper = "minemark";
private S style;
private StringBuilder textBuilder = new StringBuilder();
private boolean isPreFormatted = false;
Expand All @@ -50,16 +51,17 @@ protected MineMarkHtmlParser(TextElementCreator<S, R> textElementCreator, List<E
@Override
public void startElement(String uri, String localName, String qName, Attributes attributes) {
switch (qName) {
case "minemark":
currentElement = markDown = new MineMarkElement<>(style, layoutStyle, attributes);
return;
case "br":
textBuilder.append("\n");
return;
case "pre":
isPreFormatted = true;
break;
}
if (qName.equals(wrapper)) {
currentElement = markDown = new MineMarkElement<>(style, layoutStyle, attributes);
return;
}
addText();

LayoutStyle originalLayoutStyle = currentElement.getLayoutStyle();
Expand Down Expand Up @@ -89,7 +91,6 @@ public void startElement(String uri, String localName, String qName, Attributes
@Override
public void endElement(String uri, String localName, String qName) {
switch (qName) {
case "minemark":
case "br":
return;
case "pre":
Expand All @@ -98,6 +99,9 @@ public void endElement(String uri, String localName, String qName) {
}
addText();
currentElement.complete();
if (qName.equals(wrapper)) {
return; // We are done parsing
}
currentElement = currentElement.getParent();
}

Expand Down Expand Up @@ -162,6 +166,10 @@ protected void setStyle(S style, LayoutStyle layoutStyle) {
this.layoutStyle = layoutStyle;
}

protected void setWrapper(String wrapper) {
this.wrapper = wrapper;
}

protected MineMarkElement<S, R> getParsedResult() {
return markDown;
}
Expand Down

0 comments on commit 8f90383

Please sign in to comment.