Skip to content

Commit

Permalink
#359 add tests for HTMLOutline
Browse files Browse the repository at this point in the history
  • Loading branch information
asolntsev committed Aug 6, 2024
1 parent 83347c2 commit a2a1ed9
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,24 +35,21 @@
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static java.util.regex.Pattern.CASE_INSENSITIVE;

class HTMLOutline {

private static final Pattern HEADING =
Pattern.compile("h(\\d+)", Pattern.CASE_INSENSITIVE);
private static final Pattern HEADING = Pattern.compile("h(\\d+)", CASE_INSENSITIVE);

/** <a href="https://www.w3.org/TR/html51/sections.html#sectioning-roots">sectioning roots</a> */
private static final Pattern ROOT =
Pattern.compile("blockquote|details|fieldset|figure|td",
Pattern.CASE_INSENSITIVE);
private static final Pattern ROOT = Pattern.compile("blockquote|details|fieldset|figure|td", CASE_INSENSITIVE);

private static final Pattern WS = Pattern.compile("\\s+");

private static final int MAX_NAME_LENGTH = 200;

private final HTMLOutline parent;

private final int level;

private final Bookmark bookmark;

private HTMLOutline() {
Expand Down Expand Up @@ -160,7 +157,7 @@ public static List<Bookmark> generate(Element context, Box box) {
if (level < 1) {
continue; // Illegal value
}
} catch (NumberFormatException e) {
} catch (NumberFormatException ignore) {
continue; // Invalid value
}

Expand All @@ -174,7 +171,7 @@ public static List<Bookmark> generate(Element context, Box box) {
}
initBoxRefs(map, box);
return root.bookmark.getChildren();
} // generate(Element, Box) : List<Bookmark>
}

private static void initBoxRefs(Map<Element,Bookmark> map, Box box) {
Bookmark bookmark = map.get(box.getElement());
Expand All @@ -198,19 +195,22 @@ private static String getBookmarkName(Element element) {
return name;
}

static String getOutlineLevel(Element element) {
private static String getOutlineLevel(Element element) {
String bookmark = element.getAttribute("data-pdf-bookmark").trim();
if (bookmark.isEmpty()) {
Matcher heading = HEADING.matcher(element.getTagName());
if (heading.matches()) {
bookmark = heading.group(1);
} else if (ROOT.matcher(element.getTagName()).matches()) {
bookmark = "exclude";
} else {
bookmark = "none";
}
return bookmark.isEmpty() ?
getOutlineLevelFromTagName(element.getTagName()) :
bookmark;
}

static String getOutlineLevelFromTagName(String tagName) {
Matcher heading = HEADING.matcher(tagName);
if (heading.matches()) {
return heading.group(1);
} else if (ROOT.matcher(tagName).matches()) {
return "exclude";
} else {
return "none";
}
return bookmark;
}


Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package org.xhtmlrenderer.pdf;

import org.junit.jupiter.api.Test;

import static org.assertj.core.api.Assertions.assertThat;
import static org.xhtmlrenderer.pdf.HTMLOutline.getOutlineLevelFromTagName;

class HTMLOutlineTest {
@Test
void getsOutlineLevelFromTagName_header() {
assertThat(getOutlineLevelFromTagName("h1")).isEqualTo("1");
assertThat(getOutlineLevelFromTagName("H2")).isEqualTo("2");
assertThat(getOutlineLevelFromTagName("h3")).isEqualTo("3");
assertThat(getOutlineLevelFromTagName("H6")).isEqualTo("6");
assertThat(getOutlineLevelFromTagName("h7")).isEqualTo("7");
assertThat(getOutlineLevelFromTagName("h10")).isEqualTo("10");
assertThat(getOutlineLevelFromTagName("h16")).isEqualTo("16");
assertThat(getOutlineLevelFromTagName("h99")).isEqualTo("99");
}

@Test
void getsOutlineLevelFromTagName_exclude() {
assertThat(getOutlineLevelFromTagName("blockquote")).isEqualTo("exclude");
assertThat(getOutlineLevelFromTagName("BLOCKQUOTE")).isEqualTo("exclude");
assertThat(getOutlineLevelFromTagName("details")).isEqualTo("exclude");
assertThat(getOutlineLevelFromTagName("fieldset")).isEqualTo("exclude");
assertThat(getOutlineLevelFromTagName("figure")).isEqualTo("exclude");
assertThat(getOutlineLevelFromTagName("td")).isEqualTo("exclude");
assertThat(getOutlineLevelFromTagName("TD")).isEqualTo("exclude");
}

@Test
void getsOutlineLevelFromTagName_none() {
assertThat(getOutlineLevelFromTagName("div")).isEqualTo("none");
assertThat(getOutlineLevelFromTagName("table")).isEqualTo("none");
assertThat(getOutlineLevelFromTagName("span")).isEqualTo("none");
assertThat(getOutlineLevelFromTagName("SPAN")).isEqualTo("none");
}
}

0 comments on commit a2a1ed9

Please sign in to comment.