Skip to content

Commit

Permalink
[refactor] convert constants BlockBox.CONTENT_* to enum ContentType
Browse files Browse the repository at this point in the history
  • Loading branch information
asolntsev committed Oct 11, 2024
1 parent 6f85152 commit 85bb516
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 62 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
import org.xhtmlrenderer.newtable.TableSectionBox;
import org.xhtmlrenderer.render.AnonymousBlockBox;
import org.xhtmlrenderer.render.BlockBox;
import org.xhtmlrenderer.render.BlockBox.ContentType;
import org.xhtmlrenderer.render.Box;
import org.xhtmlrenderer.render.FloatedBoxData;
import org.xhtmlrenderer.render.InlineBox;
Expand Down Expand Up @@ -179,14 +180,14 @@ public static TableBox createMarginTable(
result.setStyle(tableStyle);
result.setElement(source);
result.setAnonymous(true);
result.setChildrenContentType(BlockBox.CONTENT_BLOCK);
result.setChildrenContentType(ContentType.BLOCK);

CalculatedStyle tableSectionStyle = pageStyle.createAnonymousStyle(IdentValue.TABLE_ROW_GROUP);
TableSectionBox section = (TableSectionBox)createBlockBox(tableSectionStyle, info, false);
section.setStyle(tableSectionStyle);
section.setElement(source);
section.setAnonymous(true);
section.setChildrenContentType(BlockBox.CONTENT_BLOCK);
section.setChildrenContentType(ContentType.BLOCK);

result.addChild(section);

Expand All @@ -197,7 +198,7 @@ public static TableBox createMarginTable(
row.setStyle(tableRowStyle);
row.setElement(source);
row.setAnonymous(true);
row.setChildrenContentType(BlockBox.CONTENT_BLOCK);
row.setChildrenContentType(ContentType.BLOCK);

row.setHeightOverride(height);

Expand All @@ -218,7 +219,7 @@ public static TableBox createMarginTable(
row.setStyle(tableRowStyle);
row.setElement(source);
row.setAnonymous(true);
row.setChildrenContentType(BlockBox.CONTENT_BLOCK);
row.setChildrenContentType(ContentType.BLOCK);

row.setHeightOverride(height);

Expand Down Expand Up @@ -308,18 +309,18 @@ private static void resolveChildren(
if (info.isContainsBlockLevelContent()) {
insertAnonymousBlocks(
c.getSharedContext(), owner, children, info.isLayoutRunningBlocks());
owner.setChildrenContentType(BlockBox.CONTENT_BLOCK);
owner.setChildrenContentType(ContentType.BLOCK);
} else {
WhitespaceStripper.stripInlineContent(children);
if (!children.isEmpty()) {
owner.setInlineContent(children);
owner.setChildrenContentType(BlockBox.CONTENT_INLINE);
owner.setChildrenContentType(ContentType.INLINE);
} else {
owner.setChildrenContentType(BlockBox.CONTENT_EMPTY);
owner.setChildrenContentType(ContentType.EMPTY);
}
}
} else {
owner.setChildrenContentType(BlockBox.CONTENT_EMPTY);
owner.setChildrenContentType(ContentType.EMPTY);
}
}

Expand Down Expand Up @@ -592,7 +593,7 @@ private static BlockBox reorderTableContent(LayoutContext c, TableBox table) {
anonBox.setFromCaptionedTable(true);
anonBox.setElement(table.getElement());

anonBox.setChildrenContentType(BlockBox.CONTENT_BLOCK);
anonBox.setChildrenContentType(ContentType.BLOCK);
anonBox.addAllChildren(topCaptions);
anonBox.addChild(table);
anonBox.addAllChildren(bottomCaptions);
Expand Down Expand Up @@ -961,7 +962,7 @@ private static List<Styleable> createGeneratedContent(
result.setStyle(style);
result.setInlineContent(inlineBoxes);
result.setElement(element);
result.setChildrenContentType(BlockBox.CONTENT_INLINE);
result.setChildrenContentType(ContentType.INLINE);
result.setPseudoElementOrClass(peName);

if (! style.isLaidOutInInlineContext()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ private boolean isPaintBackgroundsAndBorders() {
// XXX Not quite right, but good enough for now
// (e.g. absolute boxes will be counted as content here when the spec
// says the cell should be treated as empty).
return showEmpty || getChildrenContentType() != BlockBox.CONTENT_EMPTY;
return showEmpty || getChildrenContentType() != ContentType.EMPTY;

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ protected void layoutChildren(LayoutContext c, int contentStart) {
section.setNeedCellWidthCalc(false);
}

if (getChildrenContentType() != CONTENT_EMPTY) {
if (getChildrenContentType() != ContentType.EMPTY) {
for (Box box : getChildren()) {
TableCellBox cell = (TableCellBox) box;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public AnonymousBlockBox(Element element, CalculatedStyle style, List<InlineBox>
setStyle(style);
setAnonymous(true);
_openInlineBoxes = savedParents;
setChildrenContentType(BlockBox.CONTENT_INLINE);
setChildrenContentType(ContentType.INLINE);
setInlineContent(inlineContent);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
package org.xhtmlrenderer.render;

import com.google.errorprone.annotations.CheckReturnValue;
import org.jspecify.annotations.NonNull;
import org.jspecify.annotations.Nullable;
import org.xhtmlrenderer.css.constants.CSSName;
import org.xhtmlrenderer.css.constants.IdentValue;
Expand Down Expand Up @@ -61,6 +62,7 @@
import static org.xhtmlrenderer.css.constants.IdentValue.DISC;
import static org.xhtmlrenderer.css.constants.IdentValue.NONE;
import static org.xhtmlrenderer.css.constants.IdentValue.SQUARE;
import static org.xhtmlrenderer.render.BlockBox.ContentType.UNKNOWN;

/**
* A block box as defined in the CSS spec. It also provides a base class for
Expand All @@ -74,10 +76,12 @@ public enum Position {
BOTH
}

public static final int CONTENT_UNKNOWN = 0;
public static final int CONTENT_INLINE = 1;
public static final int CONTENT_BLOCK = 2;
public static final int CONTENT_EMPTY = 4;
public enum ContentType {
UNKNOWN,
INLINE,
BLOCK,
EMPTY
}

protected static final int NO_BASELINE = Integer.MIN_VALUE;

Expand All @@ -97,7 +101,7 @@ public enum Position {
@Nullable
private ReplacedElement _replacedElement;

private int _childrenContentType;
private ContentType _childrenContentType = UNKNOWN;

@Nullable
private List<Styleable> _inlineContent;
Expand Down Expand Up @@ -172,10 +176,10 @@ public String toString() {
}

result.append(switch (getChildrenContentType()) {
case CONTENT_BLOCK -> "(B) ";
case CONTENT_INLINE -> "(I) ";
case CONTENT_EMPTY -> "(E) ";
default -> "";
case BLOCK -> "(B) ";
case INLINE -> "(I) ";
case EMPTY -> "(E) ";
case UNKNOWN -> "";
});

result.append(getExtraBoxDescription());
Expand Down Expand Up @@ -216,15 +220,15 @@ public String dump(LayoutContext c, String indent, int which) {
result.append(" styleMargin=[").append(styleMargin.top()).append(", ").append(styleMargin.right())
.append(", ").append(styleMargin.bottom()).append(", ").append(styleMargin.right()).append("] ");

if (getChildrenContentType() != CONTENT_EMPTY) {
if (getChildrenContentType() != ContentType.EMPTY) {
result.append('\n');
}

switch (getChildrenContentType()) {
case CONTENT_BLOCK:
case BLOCK:
dumpBoxes(c, indent, getChildren(), which, result);
break;
case CONTENT_INLINE:
case INLINE:
if (which == Box.DUMP_RENDER) {
dumpBoxes(c, indent, getChildren(), which, result);
} else {
Expand Down Expand Up @@ -559,7 +563,7 @@ public void reset(LayoutContext c) {
getReplacedElement().detach(c);
setReplacedElement(null);
}
if (getChildrenContentType() == CONTENT_INLINE) {
if (getChildrenContentType() == ContentType.INLINE) {
removeAllChildren();
}

Expand Down Expand Up @@ -956,7 +960,7 @@ private void applyCSSMinMaxHeight(CssContext c) {
}

public void ensureChildren(LayoutContext c) {
if (getChildrenContentType() == CONTENT_UNKNOWN) {
if (getChildrenContentType() == UNKNOWN) {
BoxBuilder.createChildren(c, this);
}
}
Expand All @@ -973,12 +977,12 @@ protected void layoutChildren(LayoutContext c, int contentStart) {
}

switch (getChildrenContentType()) {
case CONTENT_INLINE:
case INLINE -> {
layoutInlineChildren(c, contentStart, calcInitialBreakAtLine(c), true);
break;
case CONTENT_BLOCK:
}
case BLOCK -> {
BlockBoxing.layoutContent(c, this, contentStart);
break;
}
}

if (getFirstLetterStyle() != null) {
Expand Down Expand Up @@ -1076,11 +1080,13 @@ private void satisfyWidowsAndOrphans(LayoutContext c, int contentStart, boolean
}
}

public int getChildrenContentType() {
@NonNull
@CheckReturnValue
public ContentType getChildrenContentType() {
return _childrenContentType;
}

public void setChildrenContentType(int contentType) {
public void setChildrenContentType(ContentType contentType) {
_childrenContentType = contentType;
}

Expand Down Expand Up @@ -1190,7 +1196,7 @@ private void collapseTopMargin(

if (isMayCollapseMarginsWithChildren() && isNoTopPaddingOrBorder(c)) {
ensureChildren(c);
if (getChildrenContentType() == CONTENT_BLOCK) {
if (getChildrenContentType() == ContentType.BLOCK) {
for (Box box : getChildren()) {
BlockBox child = (BlockBox) box;
child.collapseTopMargin(c, false, result);
Expand Down Expand Up @@ -1228,7 +1234,7 @@ private void collapseBottomMargin(
if (isMayCollapseMarginsWithChildren() &&
! getStyle().isTable() && isNoBottomPaddingOrBorder(c)) {
ensureChildren(c);
if (getChildrenContentType() == CONTENT_BLOCK) {
if (getChildrenContentType() == ContentType.BLOCK) {
for (int i = getChildCount() - 1; i >= 0; i--) {
BlockBox child = (BlockBox) getChild(i);

Expand Down Expand Up @@ -1273,7 +1279,7 @@ private void collapseEmptySubtreeMargins(LayoutContext c, MarginCollapseResult r
setBottomMarginCalculated(true);

ensureChildren(c);
if (getChildrenContentType() == CONTENT_BLOCK) {
if (getChildrenContentType() == ContentType.BLOCK) {
for (Box box : getChildren()) {
BlockBox child = (BlockBox) box;
child.collapseEmptySubtreeMargins(c, result);
Expand All @@ -1296,9 +1302,9 @@ private boolean isVerticalMarginsAdjoin(LayoutContext c) {
}

ensureChildren(c);
if (getChildrenContentType() == CONTENT_INLINE) {
if (getChildrenContentType() == ContentType.INLINE) {
return false;
} else if (getChildrenContentType() == CONTENT_BLOCK) {
} else if (getChildrenContentType() == ContentType.BLOCK) {
for (Box box : getChildren()) {
BlockBox child = (BlockBox) box;
if (child.isSkipWhenCollapsingMargins() || !child.isVerticalMarginsAdjoin(c)) {
Expand Down Expand Up @@ -1541,16 +1547,9 @@ public void calcMinMaxWidth(LayoutContext c) {

ensureChildren(c);

if (getChildrenContentType() == CONTENT_BLOCK ||
getChildrenContentType() == CONTENT_INLINE) {
switch (getChildrenContentType()) {
case CONTENT_BLOCK:
calcMinMaxWidthBlockChildren(c);
break;
case CONTENT_INLINE:
calcMinMaxWidthInlineChildren(c);
break;
}
switch (getChildrenContentType()) {
case BLOCK -> calcMinMaxWidthBlockChildren(c);
case INLINE -> calcMinMaxWidthInlineChildren(c);
}

if (minimumMaxWidth > _maxWidth) {
Expand Down Expand Up @@ -1763,7 +1762,7 @@ public void styleText(LayoutContext c) {

// FIXME Should be expanded into generic restyle facility
public void styleText(LayoutContext c, CalculatedStyle style) {
if (getChildrenContentType() == CONTENT_INLINE) {
if (getChildrenContentType() == ContentType.INLINE) {
Deque<CalculatedStyle> styles = new LinkedList<>();
styles.add(style);
for (Styleable child : _inlineContent) {
Expand Down Expand Up @@ -1946,10 +1945,10 @@ private LastLineBoxContext(int i) {
}

private void findLastLineBox(LastLineBoxContext context) {
int type = getChildrenContentType();
ContentType type = getChildrenContentType();
int count = getChildCount();
if (count > 0) {
if (type == CONTENT_INLINE) {
if (type == ContentType.INLINE) {
for (int i = count - 1; i >= 0; i--) {
LineBox child = (LineBox) getChild(i);
if (child.getHeight() > 0) {
Expand All @@ -1959,7 +1958,7 @@ private void findLastLineBox(LastLineBoxContext context) {
}
}
}
} else if (type == CONTENT_BLOCK) {
} else if (type == ContentType.BLOCK) {
for (int i = count - 1; i >= 0; i--) {
((BlockBox) getChild(i)).findLastLineBox(context);
if (context.current == 0) {
Expand All @@ -1971,17 +1970,17 @@ private void findLastLineBox(LastLineBoxContext context) {
}

private LineBox findLastLineBox() {
int type = getChildrenContentType();
ContentType type = getChildrenContentType();
int count = getChildCount();
if (count > 0) {
if (type == CONTENT_INLINE) {
if (type == ContentType.INLINE) {
for (int i = count - 1; i >= 0; i--) {
LineBox result = (LineBox) getChild(i);
if (result.getHeight() > 0) {
return result;
}
}
} else if (type == CONTENT_BLOCK) {
} else if (type == ContentType.BLOCK) {
for (int i = count - 1; i >= 0; i--) {
LineBox result = ((BlockBox) getChild(i)).findLastLineBox();
if (result != null) {
Expand All @@ -1995,17 +1994,17 @@ private LineBox findLastLineBox() {
}

private LineBox findFirstLineBox() {
int type = getChildrenContentType();
ContentType type = getChildrenContentType();
int count = getChildCount();
if (count > 0) {
if (type == CONTENT_INLINE) {
if (type == ContentType.INLINE) {
for (int i = 0; i < count; i++) {
LineBox result = (LineBox) getChild(i);
if (result.getHeight() > 0) {
return result;
}
}
} else if (type == CONTENT_BLOCK) {
} else if (type == ContentType.BLOCK) {
for (int i = 0; i < count; i++) {
LineBox result = ((BlockBox) getChild(i)).findFirstLineBox();
if (result != null) {
Expand Down Expand Up @@ -2076,9 +2075,9 @@ public boolean isInMainFlow() {
public boolean isContainsInlineContent(LayoutContext c) {
ensureChildren(c);
return switch (getChildrenContentType()) {
case CONTENT_INLINE -> true;
case CONTENT_EMPTY -> false;
case CONTENT_BLOCK -> {
case INLINE -> true;
case EMPTY -> false;
case BLOCK -> {
for (Box value : getChildren()) {
BlockBox box = (BlockBox) value;
if (box.isContainsInlineContent(c)) {
Expand All @@ -2087,7 +2086,7 @@ public boolean isContainsInlineContent(LayoutContext c) {
}
yield false;
}
default -> throw new RuntimeException("internal error: no children");
case UNKNOWN -> throw new RuntimeException("internal error: no children");
};

}
Expand Down

0 comments on commit 85bb516

Please sign in to comment.