Skip to content

Commit

Permalink
Merge pull request #127 from liias/master
Browse files Browse the repository at this point in the history
Fix infinite loop when using float and word-wrap:word
  • Loading branch information
pbrant committed Sep 10, 2017
2 parents b889902 + 5d9907b commit 25805ee
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 2 deletions.
Expand Up @@ -45,13 +45,17 @@
import org.xhtmlrenderer.render.MarkerData;
import org.xhtmlrenderer.render.StrutMetrics;
import org.xhtmlrenderer.render.TextDecoration;
import org.xhtmlrenderer.util.XRRuntimeException;

/**
* This class is responsible for flowing inline content into lines. Block
* content which participates in an inline formatting context is also handled
* here as well as floating and absolutely positioned content.
*/
public class InlineBoxing {

private static final int MAX_ITERATION_COUNT = 100000;

private InlineBoxing() {
}

Expand Down Expand Up @@ -161,7 +165,12 @@ public static void layoutContent(LayoutContext c, BlockBox box, int initialY, in
lbContext.setMaster(iB.getContentFunction().getLayoutReplacementText());
}

int q = 0;
do {
if (q++ > MAX_ITERATION_COUNT) {
throw new XRRuntimeException("Too many iterations (" + q + ") in InlineBoxing, giving up.");
}

lbContext.reset();

int fit = 0;
Expand Down
Expand Up @@ -179,14 +179,14 @@ private static void doBreakText(LayoutContext c,
}

context.setNeedsNewLine(true);
if (right < 0 && style.getWordWrap() == IdentValue.BREAK_WORD) {
if (right <= 0 && style.getWordWrap() == IdentValue.BREAK_WORD) {
if (!tryToBreakAnywhere) {
doBreakText(c, context, avail, style, true);
return;
}
}

if (right >= 0) { // found a place to wrap
if (right > 0) { // found a place to wrap
context.setEnd(context.getStart() + right);
context.setWidth(getWidth(c, f, context.getMaster().substring(context.getStart(), context.getStart() + right)));
return;
Expand Down
@@ -0,0 +1,19 @@
package org.xhtmlrenderer.pdf.bug;

import org.junit.Test;
import org.xhtmlrenderer.pdf.ITextRenderer;

import java.io.File;
import java.net.URL;

public class EndlessLoopTest {

@Test(timeout = 3000L)
public void testWordwrap() throws Exception {
URL htmlUrl = getClass().getResource("EndlessLoopTest_wordwrap.html");
File htmlFile = new File(htmlUrl.toURI());
ITextRenderer renderer = new ITextRenderer();
renderer.setDocument(htmlFile);
renderer.layout();
}
}
@@ -0,0 +1,4 @@
<body>
<div style="width: 100px; float: left; background-color: darkorange">floated</div>
<div style="width: 101px; word-wrap: break-word; background-color: lavender">word wrapped</div>
</body>

0 comments on commit 25805ee

Please sign in to comment.