Skip to content

Commit 0c026d5

Browse files
remove changes to visitor
1 parent 7a9558b commit 0c026d5

3 files changed

Lines changed: 106 additions & 86 deletions

File tree

commonmark/src/main/java/org/commonmark/node/AbstractVisitor.java

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,6 @@
77
* call {@link #visitChildren}.
88
*/
99
public abstract class AbstractVisitor implements Visitor {
10-
private final int maxDepth;
11-
private int currentDepth;
12-
13-
public AbstractVisitor() {
14-
this(Integer.MAX_VALUE);
15-
}
16-
17-
protected AbstractVisitor(int maxDepth) {
18-
if (maxDepth < 0) {
19-
throw new IllegalArgumentException("maxDepth must be >= 0");
20-
}
21-
this.maxDepth = maxDepth;
22-
}
2310

2411
@Override
2512
public void visit(BlockQuote blockQuote) {
@@ -142,20 +129,12 @@ public void visit(CustomNode customNode) {
142129
* @param parent the parent node whose children should be visited
143130
*/
144131
protected void visitChildren(Node parent) {
145-
if (currentDepth >= maxDepth) {
146-
return;
147-
}
148132
Node node = parent.getFirstChild();
149133
while (node != null) {
150134
// A subclass of this visitor might modify the node, resulting in getNext returning a different node or no
151135
// node after visiting it. So get the next node before visiting.
152136
Node next = node.getNext();
153-
currentDepth++;
154-
try {
155-
node.accept(this);
156-
} finally {
157-
currentDepth--;
158-
}
137+
node.accept(this);
159138
node = next;
160139
}
161140
}

commonmark/src/test/java/org/commonmark/test/AbstractVisitorTest.java

Lines changed: 0 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -3,38 +3,9 @@
33
import org.commonmark.node.*;
44
import org.junit.jupiter.api.Test;
55

6-
import java.util.ArrayList;
7-
import java.util.List;
8-
96
import static org.assertj.core.api.Assertions.assertThat;
10-
import static org.assertj.core.api.Assertions.assertThatThrownBy;
117

128
public class AbstractVisitorTest {
13-
@Test
14-
public void maxDepthMustBeZeroOrGreater() {
15-
assertThatThrownBy(() -> new RecordingVisitor(-1))
16-
.isInstanceOf(IllegalArgumentException.class);
17-
}
18-
19-
@Test
20-
public void maxDepthZeroVisitsOnlyRoot() {
21-
var paragraph = paragraphTree();
22-
var visitor = new RecordingVisitor(0);
23-
24-
paragraph.accept(visitor);
25-
26-
assertThat(visitor.visited).containsExactly("paragraph");
27-
}
28-
29-
@Test
30-
public void maxDepthOneVisitsDirectChildrenButNotGrandchildren() {
31-
var paragraph = paragraphTree();
32-
var visitor = new RecordingVisitor(1);
33-
34-
paragraph.accept(visitor);
35-
36-
assertThat(visitor.visited).containsExactly("paragraph", "emphasis", "text:tail");
37-
}
389

3910
@Test
4011
public void replacingNodeInVisitorShouldNotDestroyVisitOrder() {
@@ -63,39 +34,4 @@ private static void assertCode(String expectedLiteral, Node node) {
6334
Code code = (Code) node;
6435
assertThat(code.getLiteral()).isEqualTo(expectedLiteral);
6536
}
66-
67-
private static Paragraph paragraphTree() {
68-
var paragraph = new Paragraph();
69-
var emphasis = new Emphasis();
70-
emphasis.appendChild(new Text("nested"));
71-
paragraph.appendChild(emphasis);
72-
paragraph.appendChild(new Text("tail"));
73-
return paragraph;
74-
}
75-
76-
private static final class RecordingVisitor extends AbstractVisitor {
77-
private final List<String> visited = new ArrayList<>();
78-
79-
private RecordingVisitor(int maxDepth) {
80-
super(maxDepth);
81-
}
82-
83-
@Override
84-
public void visit(Paragraph paragraph) {
85-
visited.add("paragraph");
86-
super.visit(paragraph);
87-
}
88-
89-
@Override
90-
public void visit(Emphasis emphasis) {
91-
visited.add("emphasis");
92-
super.visit(emphasis);
93-
}
94-
95-
@Override
96-
public void visit(Text text) {
97-
visited.add("text:" + text.getLiteral());
98-
super.visit(text);
99-
}
100-
}
10137
}

commonmark/src/test/java/org/commonmark/test/ParserTest.java

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,55 @@ public void maxOpenBlockParsersAlsoLimitsMixedListAndBlockQuoteNesting() {
206206
assertThat(deepestParagraph.getNext()).isNull();
207207
}
208208

209+
@Test
210+
public void maxOpenBlockParsersAlignsWithVisitorDepthAtDocumentRoot() {
211+
var parser = Parser.builder().maxOpenBlockParsers(5).build();
212+
213+
var document = parser.parse(String.join("\n",
214+
"- level1",
215+
" > level2",
216+
" > > level3",
217+
" > > > level4"));
218+
var listBlock = document.getFirstChild();
219+
var listItem = listBlock.getFirstChild();
220+
var blockQuote1 = listItem.getLastChild();
221+
var blockQuote2 = blockQuote1.getLastChild();
222+
var deepestParagraph = blockQuote2.getLastChild();
223+
var depthFiveVisitor = new RecordingVisitor(5);
224+
var unlimitedVisitor = new RecordingVisitor();
225+
226+
assertThat(depth(deepestParagraph.getFirstChild())).isEqualTo(6);
227+
assertThat(depth(deepestParagraph.getLastChild())).isEqualTo(6);
228+
229+
document.accept(depthFiveVisitor);
230+
document.accept(unlimitedVisitor);
231+
232+
assertThat(depthFiveVisitor.visited).containsExactly(
233+
"document",
234+
"bulletList",
235+
"listItem",
236+
"paragraph",
237+
"text:level1",
238+
"blockQuote",
239+
"paragraph",
240+
"text:level2",
241+
"blockQuote",
242+
"paragraph");
243+
assertThat(unlimitedVisitor.visited).containsExactly(
244+
"document",
245+
"bulletList",
246+
"listItem",
247+
"paragraph",
248+
"text:level1",
249+
"blockQuote",
250+
"paragraph",
251+
"text:level2",
252+
"blockQuote",
253+
"paragraph",
254+
"text:level3",
255+
"text:> level4");
256+
}
257+
209258
private String firstText(Node n) {
210259
while (!(n instanceof Text)) {
211260
assertThat(n).isNotNull();
@@ -244,4 +293,60 @@ private String alternatingNestedList(int levels) {
244293
}
245294
return String.join("\n", lines);
246295
}
296+
297+
private int depth(Node node) {
298+
int depth = 0;
299+
while (node.getParent() != null) {
300+
node = node.getParent();
301+
depth++;
302+
}
303+
return depth;
304+
}
305+
306+
private static final class RecordingVisitor extends AbstractVisitor {
307+
private final List<String> visited = new ArrayList<>();
308+
309+
private RecordingVisitor() {
310+
}
311+
312+
private RecordingVisitor(int maxDepth) {
313+
super(maxDepth);
314+
}
315+
316+
@Override
317+
public void visit(Document document) {
318+
visited.add("document");
319+
super.visit(document);
320+
}
321+
322+
@Override
323+
public void visit(BulletList bulletList) {
324+
visited.add("bulletList");
325+
super.visit(bulletList);
326+
}
327+
328+
@Override
329+
public void visit(ListItem listItem) {
330+
visited.add("listItem");
331+
super.visit(listItem);
332+
}
333+
334+
@Override
335+
public void visit(BlockQuote blockQuote) {
336+
visited.add("blockQuote");
337+
super.visit(blockQuote);
338+
}
339+
340+
@Override
341+
public void visit(Paragraph paragraph) {
342+
visited.add("paragraph");
343+
super.visit(paragraph);
344+
}
345+
346+
@Override
347+
public void visit(Text text) {
348+
visited.add("text:" + text.getLiteral());
349+
super.visit(text);
350+
}
351+
}
247352
}

0 commit comments

Comments
 (0)