Skip to content

Commit

Permalink
Merge pull request #262 from commonmark/error-on-invalid-block-types
Browse files Browse the repository at this point in the history
Check arg of enabledBlockTypes when building parser instead of NPE later
  • Loading branch information
robinst authored Jun 2, 2022
2 parents 472812b + 9f31262 commit cd600b6
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 4 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ with the exception that 0.x versions can break between minor versions.
### Added
- YAML front matter extension: Limited support for single and double
quoted string values (#260)
### Changed
- Check argument of `enabledBlockTypes` when building parser instead of NPEing later

## [0.18.2] - 2022-02-24
### Changed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,14 @@ public static List<BlockParserFactory> calculateBlockParserFactories(List<BlockP
return list;
}

public static void checkEnabledBlockTypes(Set<Class<? extends Block>> enabledBlockTypes) {
for (Class<? extends Block> enabledBlockType : enabledBlockTypes) {
if (!NODES_TO_CORE_FACTORIES.containsKey(enabledBlockType)) {
throw new IllegalArgumentException("Can't enable block type " + enabledBlockType + ", possible options are: " + NODES_TO_CORE_FACTORIES.keySet());
}
}
}

/**
* The main parsing function. Returns a parsed document AST.
*/
Expand Down
1 change: 1 addition & 0 deletions commonmark/src/main/java/org/commonmark/parser/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ public Builder enabledBlockTypes(Set<Class<? extends Block>> enabledBlockTypes)
if (enabledBlockTypes == null) {
throw new NullPointerException("enabledBlockTypes must not be null");
}
DocumentParser.checkEnabledBlockTypes(enabledBlockTypes);
this.enabledBlockTypes = enabledBlockTypes;
return this;
}
Expand Down
11 changes: 7 additions & 4 deletions commonmark/src/test/java/org/commonmark/test/ParserTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,7 @@
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.*;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
Expand Down Expand Up @@ -75,6 +72,12 @@ public void enabledBlockTypes() {
assertThat(document.getFirstChild(), not(instanceOf(Heading.class)));
}

@Test(expected = IllegalArgumentException.class)
public void enabledBlockTypesThrowsWhenGivenUnknownClass() {
// BulletList can't be enabled separately at the moment, only all ListBlock types
Parser.builder().enabledBlockTypes(new HashSet<>(Arrays.asList(Heading.class, BulletList.class))).build();
}

@Test
public void indentation() {
String given = " - 1 space\n - 3 spaces\n - 5 spaces\n\t - tab + space";
Expand Down

0 comments on commit cd600b6

Please sign in to comment.