if (!isTargetElementEmpty) {
targetElement.setInnerHTML("");
}
Element contentElement = Elements.createSpanElement();
contentElement.getStyle().setDisplay(CSSStyleDeclaration.Display.INLINE_BLOCK);
for (int indexInLine = 0, lineSize = line.getText().length();
indexInLine < lineSize && ensureAllRenderersHaveARenderedNextChunk();) {
int chunkSize = currentLineRendererTargets.get(0).remainingCount;
if (chunkSize == 0) {
// Bad news, revert to naive rendering and log
setTextContentSafely(targetElement, line.getText());
Log.error(getClass(), "Line renderers do not have remaining chunks");
return;
}
renderChunk(line.getText(), indexInLine, chunkSize, contentElement);
markChunkRendered(chunkSize);
indexInLine += chunkSize;
}
targetElement.appendChild(contentElement);
if (line.getText().endsWith("\n")) {
Element lastChunk = (Element) contentElement.getLastChild();
Preconditions.checkState(lastChunk != null, "This line has no chunks!");
if (!StringUtils.isNullOrWhitespace(lastChunk.getClassName())) {
contentElement.getStyle().setProperty("float", "left");
Element newlineCharacterElement = createLastChunkElement(targetElement);
// Created on demand only because it is rarely used.
Element remainingSpaceElement = null;
for (int i = 0, n = currentLineRendererTargets.size(); i < n; i++) {
LineRendererTarget target = currentLineRendererTargets.get(i);
if (target.styleName != null) {
if (!target.lineRenderer.shouldLastChunkFillToRight()) {
newlineCharacterElement.addClassName(target.styleName);
} else {
if (remainingSpaceElement == null) {
newlineCharacterElement.getStyle().setProperty("float", "left");
remainingSpaceElement = createLastChunkElement(targetElement);
remainingSpaceElement.getStyle().setWidth("100%");
}
// Also apply to last chunk element so that there's no gap.
newlineCharacterElement.addClassName(target.styleName);
remainingSpaceElement.addClassName(target.styleName);
}
}
}
}
}