From a2a1ed9ba280cc0cb1edf4f7dba03e4e624086ff Mon Sep 17 00:00:00 2001 From: Andrei Solntsev Date: Tue, 6 Aug 2024 21:47:30 +0300 Subject: [PATCH] #359 add tests for HTMLOutline --- .../org/xhtmlrenderer/pdf/HTMLOutline.java | 40 +++++++++---------- .../xhtmlrenderer/pdf/HTMLOutlineTest.java | 39 ++++++++++++++++++ 2 files changed, 59 insertions(+), 20 deletions(-) create mode 100644 flying-saucer-pdf/src/test/java/org/xhtmlrenderer/pdf/HTMLOutlineTest.java diff --git a/flying-saucer-pdf/src/main/java/org/xhtmlrenderer/pdf/HTMLOutline.java b/flying-saucer-pdf/src/main/java/org/xhtmlrenderer/pdf/HTMLOutline.java index 4c87601bb..687ce6d2b 100644 --- a/flying-saucer-pdf/src/main/java/org/xhtmlrenderer/pdf/HTMLOutline.java +++ b/flying-saucer-pdf/src/main/java/org/xhtmlrenderer/pdf/HTMLOutline.java @@ -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); /** sectioning roots */ - 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() { @@ -160,7 +157,7 @@ public static List generate(Element context, Box box) { if (level < 1) { continue; // Illegal value } - } catch (NumberFormatException e) { + } catch (NumberFormatException ignore) { continue; // Invalid value } @@ -174,7 +171,7 @@ public static List generate(Element context, Box box) { } initBoxRefs(map, box); return root.bookmark.getChildren(); - } // generate(Element, Box) : List + } private static void initBoxRefs(Map map, Box box) { Bookmark bookmark = map.get(box.getElement()); @@ -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; } diff --git a/flying-saucer-pdf/src/test/java/org/xhtmlrenderer/pdf/HTMLOutlineTest.java b/flying-saucer-pdf/src/test/java/org/xhtmlrenderer/pdf/HTMLOutlineTest.java new file mode 100644 index 000000000..23fb9a888 --- /dev/null +++ b/flying-saucer-pdf/src/test/java/org/xhtmlrenderer/pdf/HTMLOutlineTest.java @@ -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"); + } +}