From b335255d61120fedaabda8c4dabf63f7de3f3d2a Mon Sep 17 00:00:00 2001 From: e550443 Date: Fri, 2 Aug 2024 16:20:02 +0200 Subject: [PATCH] fix: link converting table columns width to 'auto' with 'Fit to page' property Refs: #122 --- .../pdf/exporter/util/HtmlProcessor.java | 16 +++++++++------- .../pdf/exporter/util/HtmlProcessorTest.java | 6 +++--- 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/src/main/java/ch/sbb/polarion/extension/pdf/exporter/util/HtmlProcessor.java b/src/main/java/ch/sbb/polarion/extension/pdf/exporter/util/HtmlProcessor.java index a91abf2..c1242c0 100644 --- a/src/main/java/ch/sbb/polarion/extension/pdf/exporter/util/HtmlProcessor.java +++ b/src/main/java/ch/sbb/polarion/extension/pdf/exporter/util/HtmlProcessor.java @@ -171,7 +171,7 @@ public String processHtmlForPDF(@NotNull String html, @NotNull ExportParams expo html = cutEmptyChapters(html); } - html = adjustCellWidth(html); + html = adjustCellWidth(html, exportParams); html = html.replace(">\n ", "> "); html = html.replace("\n or elements of regular tables which width in styles specified in pixels ("px"). - // or element till "width:" in styles matched into first unnamed group and width value - into second unnamed group. - // Then we replace matched content by first group content plus "auto" instead of value in pixels. - html = RegexMatcher.get("( regexEngine.group(1) + "auto"); + String adjustCellWidth(@NotNull String html, @NotNull ExportParams exportParams) { + if (exportParams.isFitToPage()) { + // This regexp searches for or elements of regular tables which width in styles specified in pixels ("px"). + // or element till "width:" in styles matched into first unnamed group and width value - into second unnamed group. + // Then we replace matched content by first group content plus "auto" instead of value in pixels. + html = RegexMatcher.get("( regexEngine.group(1) + "auto"); + } // Next step we look for tables which represent WorkItem attributes and force them to take 100% of available width html = RegexMatcher.get("(class=\"polarion-dle-workitem-fields-end-table\")") diff --git a/src/test/java/ch/sbb/polarion/extension/pdf/exporter/util/HtmlProcessorTest.java b/src/test/java/ch/sbb/polarion/extension/pdf/exporter/util/HtmlProcessorTest.java index 5d65165..19cdcf1 100644 --- a/src/test/java/ch/sbb/polarion/extension/pdf/exporter/util/HtmlProcessorTest.java +++ b/src/test/java/ch/sbb/polarion/extension/pdf/exporter/util/HtmlProcessorTest.java @@ -169,7 +169,7 @@ void adjustCellWidthTest() { String invalidHtml = new String(isInvalidHtml.readAllBytes(), StandardCharsets.UTF_8); // Spaces and new lines are removed to exclude difference in space characters - String fixedHtml = processor.adjustCellWidth(invalidHtml).replaceAll(" ", ""); + String fixedHtml = processor.adjustCellWidth(invalidHtml, new ExportParams()).replaceAll(" ", ""); String validHtml = new String(isValidHtml.readAllBytes(), StandardCharsets.UTF_8).replaceAll(" ", ""); assertEquals(TestStringUtils.removeLineEndings(validHtml), TestStringUtils.removeLineEndings(fixedHtml)); } @@ -300,9 +300,9 @@ void processHtmlForPDFTestCutEmptyWorkItemAttributesDisabled() { String html = new String(isHtml.readAllBytes(), StandardCharsets.UTF_8); HtmlProcessor spyHtmlProcessor = spy(processor); - // to avoid changing input html and check with regular equals - when(spyHtmlProcessor.adjustCellWidth(html)).thenReturn(html); ExportParams exportParams = getExportParams(); + // to avoid changing input html and check with regular equals + when(spyHtmlProcessor.adjustCellWidth(html, exportParams)).thenReturn(html); exportParams.setCutEmptyChapters(false); // Spaces, new lines & nbsp symbols are removed to exclude difference in space characters