render mix of block and inline content in one block element

Signed-off-by: Florian Thienel <florian@thienel.org>
diff --git a/org.eclipse.vex.core/src/org/eclipse/vex/core/internal/visualization/CssBasedBoxModelBuilder.java b/org.eclipse.vex.core/src/org/eclipse/vex/core/internal/visualization/CssBasedBoxModelBuilder.java
index 6744928..ea52d49 100644
--- a/org.eclipse.vex.core/src/org/eclipse/vex/core/internal/visualization/CssBasedBoxModelBuilder.java
+++ b/org.eclipse.vex.core/src/org/eclipse/vex/core/internal/visualization/CssBasedBoxModelBuilder.java
@@ -27,6 +27,7 @@
 
 import java.util.Collection;
 import java.util.Collections;
+import java.util.LinkedList;
 import java.util.List;
 import java.util.Set;
 
@@ -79,13 +80,6 @@
 		return asStructuralBox(visualize(node));
 	}
 
-	private <P extends IParentBox<IStructuralBox>> P visualizeChildrenAsStructure(final Iterable<VisualizeResult> childrenResults, final P parentBox) {
-		for (final VisualizeResult visualizeResult : childrenResults) {
-			parentBox.appendChild(asStructuralBox(visualizeResult));
-		}
-		return parentBox;
-	}
-
 	private IStructuralBox asStructuralBox(final VisualizeResult visualizeResult) {
 		if (visualizeResult.inline) {
 			return visualizeAsBlock(visualizeResult.node, visualizeResult.childrenResults);
@@ -119,17 +113,19 @@
 	 */
 
 	private VisualizeResult visualize(final INode node) {
+		final Styles styles = styleSheet.getStyles(node);
 		return node.accept(new CollectingNodeTraversal<VisualizeResult>() {
 			@Override
 			public VisualizeResult visit(final IDocument document) {
 				final Collection<VisualizeResult> childrenResults = traverseChildren(document);
-				return new VisualizeResult(document, childrenResults, nodeReference(document, visualizeChildrenAsStructure(childrenResults, verticalBlock())));
+				return new VisualizeResult(document, childrenResults, nodeReference(document, visualizeChildrenAsStructure(document, styles, childrenResults, verticalBlock())));
 			}
 
 			@Override
 			public VisualizeResult visit(final IDocumentFragment documentFragment) {
 				final Collection<VisualizeResult> childrenResults = traverseChildren(documentFragment);
-				return new VisualizeResult(documentFragment, childrenResults, nodeReference(documentFragment, visualizeChildrenAsStructure(childrenResults, verticalBlock())));
+				return new VisualizeResult(documentFragment, childrenResults,
+						nodeReference(documentFragment, visualizeChildrenAsStructure(documentFragment, styles, childrenResults, verticalBlock())));
 			}
 
 			@Override
@@ -206,11 +202,7 @@
 				if (isEmptyElement(element)) {
 					content = visualizeEmptyElement(styles, element);
 				} else {
-					if (mayContainText || containsInlineContent) {
-						content = visualizeInlineNodeContent(element, styles, childrenResults, paragraph(styles));
-					} else {
-						content = visualizeChildrenAsStructure(childrenResults, verticalBlock());
-					}
+					content = visualizeChildrenAsStructure(element, styles, childrenResults, verticalBlock());
 				}
 
 				if (mayContainText) {
@@ -252,6 +244,25 @@
 		return paragraph(styles, nodeTag(element, styles));
 	}
 
+	private <P extends IParentBox<IStructuralBox>> P visualizeChildrenAsStructure(final INode node, final Styles styles, final Iterable<VisualizeResult> childrenResults, final P parentBox) {
+		final LinkedList<VisualizeResult> pendingInline = new LinkedList<VisualizeResult>();
+		for (final VisualizeResult visualizeResult : childrenResults) {
+			if (visualizeResult.inline) {
+				pendingInline.add(visualizeResult);
+			} else {
+				if (!pendingInline.isEmpty()) {
+					parentBox.appendChild(visualizeInlineNodeContent(node, styles, pendingInline, paragraph(styles)));
+				}
+				pendingInline.clear();
+				parentBox.appendChild(asStructuralBox(visualizeResult));
+			}
+		}
+		if (!pendingInline.isEmpty()) {
+			parentBox.appendChild(visualizeInlineNodeContent(node, styles, pendingInline, paragraph(styles)));
+		}
+		return parentBox;
+	}
+
 	private static IStructuralBox surroundWithPseudoElements(final IStructuralBox content, final INode node, final Styles styles) {
 		final IStructuralBox pseudoElementBefore = visualizePseudoElementAsBlock(styles, node, PseudoElement.BEFORE);
 		final IStructuralBox pseudoElementAfter = visualizePseudoElementAsBlock(styles, node, PseudoElement.AFTER);