Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gobble spaces after backslash-hardline-break #1

Merged
merged 2 commits into from
Aug 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,77 @@ public void blockquotesWithCrLfLineBreaks() {
assertFalse(it.hasNext());
}

@Test
public void assertSpacesHardLineBreak() {

//---------------------- - -1----------
//--------------01234567 8 901234567890
String given = "line1 \r\n line2";
Parser parser = Parser.builder().build();
Document document = parser.parse(given);

assertThat(document.getFirstChild(), instanceOf(Paragraph.class));
ReversiblePeekingIterator<Node> it = document.getFirstChild().getChildIterator();

assertTrue(it.hasNext());
Node node = it.next();
assertThat(node, instanceOf(Text.class));
assertEquals("line1", node.getChars().toString());
assertEquals(0, node.getStartOffset());
assertEquals(5, node.getEndOffset());

assertTrue(it.hasNext());
node = it.next();
assertThat(node, instanceOf(HardLineBreak.class));
assertEquals(5, node.getStartOffset());
assertEquals(9, node.getEndOffset());

assertTrue(it.hasNext());
node = it.next();
assertThat(node, instanceOf(Text.class));
assertEquals("line2", node.getChars().toString());
assertEquals(14, node.getStartOffset());
assertEquals(19, node.getEndOffset());

assertFalse(it.hasNext());
}

@Test
public void assertBackslashHardLineBreak() {

//-------------------- - - --1----------
//--------------012345 6 7 8901234567890
String given = "line1\\\r\n line2";
Parser parser = Parser.builder().build();
Document document = parser.parse(given);

assertThat(document.getFirstChild(), instanceOf(Paragraph.class));
ReversiblePeekingIterator<Node> it = document.getFirstChild().getChildIterator();

assertTrue(it.hasNext());
Node node = it.next();
assertThat(node, instanceOf(Text.class));
assertEquals("line1", node.getChars().toString());
assertEquals(0, node.getStartOffset());
assertEquals(5, node.getEndOffset());

assertTrue(it.hasNext());
node = it.next();
assertThat(node, instanceOf(HardLineBreak.class));
assertEquals(5, node.getStartOffset());
assertEquals(8, node.getEndOffset());

assertTrue(it.hasNext());
node = it.next();
assertThat(node, instanceOf(Text.class));
assertEquals("line2", node.getChars().toString());
assertEquals(13, node.getStartOffset());
assertEquals(18, node.getEndOffset());

assertFalse(it.hasNext());
}


String escape(String input, Parser parser) {
BasedSequence baseSeq = BasedSequence.of(input);
List<SpecialLeadInHandler> handlers = Parser.SPECIAL_LEAD_IN_HANDLERS.get(parser.getOptions());
Expand Down
2 changes: 1 addition & 1 deletion flexmark-core-test/src/test/resources/ast_spec.md
Original file line number Diff line number Diff line change
Expand Up @@ -12815,7 +12815,7 @@ Document[0, 14]
Paragraph[0, 14]
Text[0, 3] chars:[0, 3, "foo"]
HardLineBreak[3, 5]
Text[10, 13] chars:[10, 13, " bar"]
Text[10, 13] chars:[10, 13, "bar"]
````````````````````````````````


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -524,9 +524,8 @@ public boolean parseNewline() {
}

// gobble leading spaces in next line
while (peek() == ' ') {
index++;
}
gobbleLeadingSpaces();

return true;
}

Expand All @@ -542,6 +541,7 @@ protected boolean parseBackslash() {
int charsMatched = peek(1) == '\n' ? 2 : 1;
appendNode(new HardLineBreak(input.subSequence(index - 1, index + charsMatched)));
index += charsMatched;
gobbleLeadingSpaces();
} else if (index < input.length() && myParsing.ESCAPABLE.matcher(input.subSequence(index, index + 1)).matches()) {
appendText(input, index - 1, index + 1);
index++;
Expand All @@ -550,6 +550,12 @@ protected boolean parseBackslash() {
}
return true;
}

private void gobbleLeadingSpaces() {
while (peek() == ' ') {
index++;
}
}

/**
* Attempt to parse backticks, adding either a backtick code span or a literal sequence of backticks.
Expand Down
Loading