From 85bb516783c9cc99051d492a5929cc9dedaf29cd Mon Sep 17 00:00:00 2001 From: Andrei Solntsev Date: Sat, 12 Oct 2024 00:51:22 +0300 Subject: [PATCH] [refactor] convert constants `BlockBox.CONTENT_*` to enum `ContentType` --- .../org/xhtmlrenderer/layout/BoxBuilder.java | 21 ++-- .../xhtmlrenderer/newtable/TableCellBox.java | 2 +- .../xhtmlrenderer/newtable/TableRowBox.java | 2 +- .../render/AnonymousBlockBox.java | 2 +- .../org/xhtmlrenderer/render/BlockBox.java | 97 +++++++++---------- 5 files changed, 62 insertions(+), 62 deletions(-) diff --git a/flying-saucer-core/src/main/java/org/xhtmlrenderer/layout/BoxBuilder.java b/flying-saucer-core/src/main/java/org/xhtmlrenderer/layout/BoxBuilder.java index 54368da1e..7b8f81e31 100644 --- a/flying-saucer-core/src/main/java/org/xhtmlrenderer/layout/BoxBuilder.java +++ b/flying-saucer-core/src/main/java/org/xhtmlrenderer/layout/BoxBuilder.java @@ -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; @@ -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); @@ -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); @@ -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); @@ -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); } } @@ -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); @@ -961,7 +962,7 @@ private static List 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()) { diff --git a/flying-saucer-core/src/main/java/org/xhtmlrenderer/newtable/TableCellBox.java b/flying-saucer-core/src/main/java/org/xhtmlrenderer/newtable/TableCellBox.java index 72103ded8..b36aca99a 100644 --- a/flying-saucer-core/src/main/java/org/xhtmlrenderer/newtable/TableCellBox.java +++ b/flying-saucer-core/src/main/java/org/xhtmlrenderer/newtable/TableCellBox.java @@ -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; } diff --git a/flying-saucer-core/src/main/java/org/xhtmlrenderer/newtable/TableRowBox.java b/flying-saucer-core/src/main/java/org/xhtmlrenderer/newtable/TableRowBox.java index 91c4144d1..104ea52a4 100644 --- a/flying-saucer-core/src/main/java/org/xhtmlrenderer/newtable/TableRowBox.java +++ b/flying-saucer-core/src/main/java/org/xhtmlrenderer/newtable/TableRowBox.java @@ -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; diff --git a/flying-saucer-core/src/main/java/org/xhtmlrenderer/render/AnonymousBlockBox.java b/flying-saucer-core/src/main/java/org/xhtmlrenderer/render/AnonymousBlockBox.java index ced75af21..8da813a70 100755 --- a/flying-saucer-core/src/main/java/org/xhtmlrenderer/render/AnonymousBlockBox.java +++ b/flying-saucer-core/src/main/java/org/xhtmlrenderer/render/AnonymousBlockBox.java @@ -46,7 +46,7 @@ public AnonymousBlockBox(Element element, CalculatedStyle style, List setStyle(style); setAnonymous(true); _openInlineBoxes = savedParents; - setChildrenContentType(BlockBox.CONTENT_INLINE); + setChildrenContentType(ContentType.INLINE); setInlineContent(inlineContent); } diff --git a/flying-saucer-core/src/main/java/org/xhtmlrenderer/render/BlockBox.java b/flying-saucer-core/src/main/java/org/xhtmlrenderer/render/BlockBox.java index b8a5e49c1..cf89c2936 100755 --- a/flying-saucer-core/src/main/java/org/xhtmlrenderer/render/BlockBox.java +++ b/flying-saucer-core/src/main/java/org/xhtmlrenderer/render/BlockBox.java @@ -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; @@ -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 @@ -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; @@ -97,7 +101,7 @@ public enum Position { @Nullable private ReplacedElement _replacedElement; - private int _childrenContentType; + private ContentType _childrenContentType = UNKNOWN; @Nullable private List _inlineContent; @@ -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()); @@ -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 { @@ -559,7 +563,7 @@ public void reset(LayoutContext c) { getReplacedElement().detach(c); setReplacedElement(null); } - if (getChildrenContentType() == CONTENT_INLINE) { + if (getChildrenContentType() == ContentType.INLINE) { removeAllChildren(); } @@ -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); } } @@ -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) { @@ -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; } @@ -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); @@ -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); @@ -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); @@ -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)) { @@ -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) { @@ -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 styles = new LinkedList<>(); styles.add(style); for (Styleable child : _inlineContent) { @@ -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) { @@ -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) { @@ -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) { @@ -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) { @@ -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)) { @@ -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"); }; }