Skip to content

Commit

Permalink
#447 fix parsing CSS head rules
Browse files Browse the repository at this point in the history
the probles was in line CSSParser.java:553 - the `margins` map was overriden by the new value. Previous map content was lost.
  • Loading branch information
asolntsev committed Nov 22, 2024
1 parent 576cd4d commit cda7687
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
import java.util.Map;
import java.util.Set;

import static java.util.Collections.emptyMap;
import static java.util.Locale.ROOT;
import static org.w3c.dom.css.CSSPrimitiveValue.CSS_NUMBER;
import static org.w3c.dom.css.CSSPrimitiveValue.CSS_PERCENTAGE;
Expand Down Expand Up @@ -550,7 +551,7 @@ private void page(Stylesheet stylesheet) throws IOException {
skip_whitespace();
break;
} else if (t == Token.TK_AT_RULE) {
margins = margin(stylesheet);
margins.putAll(margin(stylesheet));
} else {
declaration_list(ruleset, false, true, false);
}
Expand All @@ -576,21 +577,19 @@ private void page(Stylesheet stylesheet) throws IOException {
// margin_sym S* '{' declaration [ ';' S* declaration? ]* '}' S*
// ;
private Map<MarginBoxName, List<PropertyDeclaration>> margin(Stylesheet stylesheet) throws IOException {
Map<MarginBoxName, List<PropertyDeclaration>> margins = new HashMap<>();

Token t = next();
if (t != Token.TK_AT_RULE) {
error(new CSSParseException(t, Token.TK_AT_RULE, getCurrentLine()), "at rule", true);
recover(true, false);
return margins;
return emptyMap();
}

String name = getTokenValue(t);
MarginBoxName marginBoxName = MarginBoxName.valueOf(name);
if (marginBoxName == null) {
error(new CSSParseException(name + " is not a valid margin box name", getCurrentLine()), "at rule", true);
recover(true, false);
return margins;
return emptyMap();
}

skip_whitespace();
Expand All @@ -605,7 +604,7 @@ private Map<MarginBoxName, List<PropertyDeclaration>> margin(Stylesheet styleshe
push(t);
throw new CSSParseException(t, Token.TK_RBRACE, getCurrentLine());
}
margins.put(marginBoxName, ruleset.getPropertyDeclarations());
return Map.of(marginBoxName, ruleset.getPropertyDeclarations());
} else {
push(t);
throw new CSSParseException(t, Token.TK_LBRACE, getCurrentLine());
Expand All @@ -614,7 +613,7 @@ private Map<MarginBoxName, List<PropertyDeclaration>> margin(Stylesheet styleshe
error(e, "margin box", true);
recover(false, false);
}
return margins;
return emptyMap();
}


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

import com.codeborne.pdftest.PDF;
import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;

import static com.codeborne.pdftest.assertj.Assertions.assertThat;
import static org.xhtmlrenderer.pdf.TestUtils.printFile;

public class HeaderTest {
private static final Logger log = LoggerFactory.getLogger(HeaderTest.class);

@Test
void pageWithHeader() throws IOException {
String fileName = "page-with-header.html";
byte[] bytes = Html2Pdf.fromClasspathResource(fileName);

PDF pdf = printFile(log, bytes, "page-with-header.pdf");
assertThat(pdf).containsText("Header", "Body", "Footer");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package org.xhtmlrenderer.pdf;

import com.codeborne.pdftest.PDF;
import org.slf4j.Logger;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

public class TestUtils {
public static PDF printFile(Logger log, byte[] pdf, String filename) throws IOException {
File file = new File("target", filename);
try (FileOutputStream o = new FileOutputStream(file)) {
o.write(pdf);
}
log.info("Generated PDF: {}", file.getAbsolutePath());
return new PDF(pdf);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,18 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import static com.codeborne.pdftest.assertj.Assertions.assertThat;
import static org.xhtmlrenderer.pdf.TestUtils.printFile;

public class WordBreakTest {
private static final Logger log = LoggerFactory.getLogger(WordBreakTest.class);

@Test
void breakAll() throws IOException {

byte[] pdf = Html2Pdf.fromClasspathResource("org/xhtmlrenderer/pdf/break-all.html");
printFile(pdf, "break-all.pdf");
assertThat(new PDF(pdf)).containsExactText("HelloWorld1\nHelloWorld2\nHelloWorld3\nHelloWorld4\nHelloWorld5\n");
}

private static void printFile(byte[] pdf, String filename) throws IOException {
File file = new File("target", filename);
try (FileOutputStream o = new FileOutputStream(file)) {
o.write(pdf);
}
log.info("Generated PDF: {}", file.getAbsolutePath());
byte[] result = Html2Pdf.fromClasspathResource("org/xhtmlrenderer/pdf/break-all.html");
PDF pdf = printFile(log, result, "break-all.pdf");
assertThat(pdf).containsExactText("HelloWorld1\nHelloWorld2\nHelloWorld3\nHelloWorld4\nHelloWorld5\n");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,10 @@
import org.xhtmlrenderer.pdf.ITextRenderer;
import org.xhtmlrenderer.resource.XMLResource;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import static com.codeborne.pdftest.assertj.Assertions.assertThat;
import static org.xhtmlrenderer.pdf.TestUtils.printFile;

/**
* Reproducible example for <a href="https://github.com/flyingsaucerproject/flyingsaucer/issues/276">issue 276</a>
Expand All @@ -37,29 +36,19 @@ void tableWithFloatedCell() throws IOException {

ITextRenderer renderer = new ITextRenderer();
byte[] result = renderer.createPDF(XMLResource.load(page).getDocument());
printFile(result, "table-with-floated-cell.pdf");
PDF pdf = new PDF(result);
PDF pdf = printFile(log, result, "table-with-floated-cell.pdf");
assertThat(pdf).containsText("first cell", "second cell");
}

@Test
void tableCell() throws IOException {
String page = """
<table><tr><td style="float:left;">first cell {float:left;}</td></tr></table>""";

ITextRenderer renderer = new ITextRenderer();
byte[] result = renderer.createPDF(XMLResource.load(page).getDocument());
printFile(result, "table-cell.pdf");

PDF pdf = new PDF(result);

PDF pdf = printFile(log, result, "table-cell.pdf");
assertThat(pdf).containsText("first cell");
}

private static void printFile(byte[] pdf, String filename) throws IOException {
File file = new File("target", filename);
try (FileOutputStream o = new FileOutputStream(file)) {
o.write(pdf);
}
log.info("Generated PDF: {}", file.getAbsolutePath());
}
}
32 changes: 32 additions & 0 deletions flying-saucer-pdf/src/test/resources/page-with-header.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<html xmlns="http://www.w3.org/1999/xhtml" lang="en">

<head>
<style>
@page {
size: 200px 200px;
@top-left {
content: element(header);
}
@bottom-left {
content: element(footer);
}
}

#header {
position: running(header);
}

#footer {
position: running(footer);
}
</style>
<title></title>
</head>

<body>
<div id="header">Header</div>
<div id="body">Body</div>
<div id="footer">Footer</div>
</body>

</html>

0 comments on commit cda7687

Please sign in to comment.