Skip to content

Commit

Permalink
#429 [refactor] replace setElement() and setStyle() by constructo…
Browse files Browse the repository at this point in the history
…r parameters

(as much as possible)
  • Loading branch information
asolntsev committed Oct 30, 2024
1 parent 99761e0 commit 3223974
Show file tree
Hide file tree
Showing 10 changed files with 92 additions and 112 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
import static java.lang.Integer.parseInt;
import static java.util.Collections.emptyList;
import static java.util.Collections.singletonList;
import static org.xhtmlrenderer.css.constants.IdentValue.INLINE;
import static org.xhtmlrenderer.css.newmatch.CascadedStyle.createLayoutPropertyDeclaration;
import static org.xhtmlrenderer.css.parser.PropertyValue.Type.VALUE_TYPE_FUNCTION;
import static org.xhtmlrenderer.layout.BoxBuilder.MarginDirection.HORIZONTAL;
Expand Down Expand Up @@ -101,15 +102,9 @@ public static BlockBox createRootBox(LayoutContext c, Document document) {

CalculatedStyle style = c.getSharedContext().getStyle(root);

BlockBox result;
if (style.isTable() || style.isInlineTable()) {
result = new TableBox();
} else {
result = new BlockBox();
}

result.setStyle(style);
result.setElement(root);
BlockBox result = style.isTable() || style.isInlineTable() ?
new TableBox(root, style) :
new BlockBox(root, style);

c.resolveCounters(style);

Expand Down Expand Up @@ -176,17 +171,13 @@ public static TableBox createMarginTable(
true,
Origin.USER)
));
TableBox result = (TableBox)createBlockBox(tableStyle, info, false);
TableBox result = (TableBox)createBlockBox(source, tableStyle, info, false);
result.setMarginAreaRoot(true);
result.setStyle(tableStyle);
result.setElement(source);
result.setAnonymous(true);
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);
TableSectionBox section = (TableSectionBox)createBlockBox(source, tableSectionStyle, info, false);
section.setAnonymous(true);
section.setChildrenContentType(ContentType.BLOCK);

Expand All @@ -195,9 +186,7 @@ public static TableBox createMarginTable(
TableRowBox row = null;
if (direction == HORIZONTAL) {
CalculatedStyle tableRowStyle = pageStyle.createAnonymousStyle(IdentValue.TABLE_ROW);
row = (TableRowBox)createBlockBox(tableRowStyle, info, false);
row.setStyle(tableRowStyle);
row.setElement(source);
row = (TableRowBox)createBlockBox(source, tableRowStyle, info, false);
row.setAnonymous(true);
row.setChildrenContentType(ContentType.BLOCK);

Expand All @@ -216,9 +205,7 @@ public static TableBox createMarginTable(
if (cell != null) {
if (direction == VERTICAL) {
CalculatedStyle tableRowStyle = pageStyle.createAnonymousStyle(IdentValue.TABLE_ROW);
row = (TableRowBox) createBlockBox(tableRowStyle, info, false);
row.setStyle(tableRowStyle);
row.setElement(source);
row = (TableRowBox) createBlockBox(source, tableRowStyle, info, false);
row.setAnonymous(true);
row.setChildrenContentType(ContentType.BLOCK);

Expand Down Expand Up @@ -279,10 +266,9 @@ private static TableCellBox createMarginBox(
ChildBoxInfo info = new ChildBoxInfo(true);
info.setContainsTableContent();

TableCellBox result = new TableCellBox();
Element element = c.getRootLayer().getMaster().getElement(); // XXX Doesn't make sense, but we need something here
TableCellBox result = new TableCellBox(element, style);
result.setAnonymous(true);
result.setStyle(style);
result.setElement(c.getRootLayer().getMaster().getElement()); // XXX Doesn't make sense, but we need something here

if (hasContent && ! style.isDisplayNone()) {
children.addAll(createGeneratedMarginBoxContent(
Expand Down Expand Up @@ -514,11 +500,9 @@ private static void createAnonymousTableContent(LayoutContext c, BlockBox source
anonDisplay = next;
}
CalculatedStyle anonStyle = source.getStyle().createAnonymousStyle(anonDisplay);
BlockBox anonBox = createBlockBox(anonStyle, nested, false);
anonBox.setStyle(anonStyle);
Element element = source.getElement(); // XXX Doesn't really make sense, but what to do?
BlockBox anonBox = createBlockBox(element, anonStyle, nested, false);
anonBox.setAnonymous(true);
// XXX Doesn't really make sense, but what to do?
anonBox.setElement(source.getElement());
resolveTableContent(c, anonBox, childrenForAnonymous, nested);

if (next == IdentValue.TABLE) {
Expand Down Expand Up @@ -586,11 +570,9 @@ private static BlockBox reorderTableContent(LayoutContext c, TableBox table) {
anonStyle = table.getStyle().createAnonymousStyle(IdentValue.BLOCK);
}

BlockBox anonBox = new BlockBox();
anonBox.setStyle(anonStyle);
BlockBox anonBox = new BlockBox(table.getElement(), anonStyle);
anonBox.setAnonymous(true);
anonBox.setFromCaptionedTable(true);
anonBox.setElement(table.getElement());

anonBox.setChildrenContentType(ContentType.BLOCK);
anonBox.addAllChildren(topCaptions);
Expand Down Expand Up @@ -945,18 +927,16 @@ private static List<Styleable> createGeneratedContent(
}
return inlineBoxes;
} else {
CalculatedStyle anon = style.createAnonymousStyle(IdentValue.INLINE);
CalculatedStyle anon = style.createAnonymousStyle(INLINE);
for (Styleable inlineBox : inlineBoxes) {
InlineBox iB = (InlineBox) inlineBox;
iB.setStyle(anon);
iB.applyTextTransform();
iB.setElement(null);
}

BlockBox result = createBlockBox(style, info, true);
result.setStyle(style);
BlockBox result = createBlockBox(element, style, info, true);
result.setInlineContent(inlineBoxes);
result.setElement(element);
result.setChildrenContentType(ContentType.INLINE);
result.setPseudoElementOrClass(peName);

Expand All @@ -974,7 +954,7 @@ private static List<Styleable> createGeneratedMarginBoxContent(
List<Styleable> result = createGeneratedContentList(
c, element, property, null, style, CONTENT_LIST_MARGIN_BOX, info);

CalculatedStyle anon = style.createAnonymousStyle(IdentValue.INLINE);
CalculatedStyle anon = style.createAnonymousStyle(INLINE);
for (Styleable s : result) {
if (s instanceof InlineBox iB) {
iB.setElement(null);
Expand All @@ -987,34 +967,31 @@ private static List<Styleable> createGeneratedMarginBoxContent(
}

private static BlockBox createBlockBox(
CalculatedStyle style, ChildBoxInfo info, boolean generated) {
Element source, CalculatedStyle style, ChildBoxInfo info, boolean generated) {
if (style.isFloated() && !(style.isAbsolute() || style.isFixed())) {
BlockBox result;
if (style.isTable() || style.isInlineTable()) {
result = new TableBox();
} else {
result = new BlockBox();
}
BlockBox result = style.isTable() || style.isInlineTable() ?
new TableBox(source, style) :
new BlockBox(source, style);
result.setFloatedBoxData(new FloatedBoxData());
return result;
} else if (style.isSpecifiedAsBlock()) {
return new BlockBox();
return new BlockBox(source, style);
} else if (! generated && (style.isTable() || style.isInlineTable())) {
return new TableBox();
return new TableBox(source, style);
} else if (style.isTableCell()) {
info.setContainsTableContent();
return new TableCellBox();
return new TableCellBox(source, style);
} else if (! generated && style.isTableRow()) {
info.setContainsTableContent();
return new TableRowBox();
return new TableRowBox(source, style);
} else if (! generated && style.isTableSection()) {
info.setContainsTableContent();
return new TableSectionBox();
return new TableSectionBox(source, style);
} else if (style.isTableCaption()) {
info.setContainsTableContent();
return new BlockBox();
return new BlockBox(source, style);
} else {
return new BlockBox();
return new BlockBox(source, style);
}
}

Expand Down Expand Up @@ -1053,15 +1030,10 @@ private static void addColumnOrColumnGroup(

private static InlineBox createInlineBox(
String text, Element parent, CalculatedStyle parentStyle, @Nullable Text node) {
InlineBox result = new InlineBox(text, node);

if (parentStyle.isInline() && ! (parent.getParentNode() instanceof Document)) {
result.setStyle(parentStyle);
result.setElement(parent);
} else {
result.setStyle(parentStyle.createAnonymousStyle(IdentValue.INLINE));
}

InlineBox result = parentStyle.isInline() && !(parent.getParentNode() instanceof Document) ?
new InlineBox(text, node, null, null, parent, null, parentStyle) :
new InlineBox(text, node, null, null, null, null, parentStyle.createAnonymousStyle(INLINE));
result.applyTextTransform();

return result;
Expand Down Expand Up @@ -1122,9 +1094,7 @@ private static void createChildren(
needEndText = true;
}
} else {
child = createBlockBox(style, info, false);
child.setStyle(style);
child.setElement(element);
child = createBlockBox(element, style, info, false);
if (style.isListItem()) {
BlockBox block = (BlockBox) child;
block.setListCounter(c.getCounterContext(style).getCurrentCounterValue("list-item"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import com.google.errorprone.annotations.CheckReturnValue;
import org.jspecify.annotations.Nullable;
import org.w3c.dom.Element;
import org.xhtmlrenderer.css.constants.CSSName;
import org.xhtmlrenderer.css.constants.IdentValue;
import org.xhtmlrenderer.css.style.CalculatedStyle;
Expand Down Expand Up @@ -66,11 +67,16 @@ public class TableBox extends BlockBox {

private boolean _marginAreaRoot;

@Nullable
private ContentLimitContainer _contentLimitContainer;

private int _extraSpaceTop;
private int _extraSpaceBottom;

public TableBox(@Nullable Element element, @Nullable CalculatedStyle style) {
super(element, style);
}

@Override
public boolean isMarginAreaRoot() {
return _marginAreaRoot;
Expand All @@ -82,11 +88,7 @@ public void setMarginAreaRoot(boolean marginAreaRoot) {

@Override
public BlockBox copyOf() {
TableBox result = new TableBox();
result.setStyle(getStyle());
result.setElement(getElement());

return result;
return new TableBox(getElement(), getStyle());
}

public void addStyleColumn(TableColumn col) {
Expand Down Expand Up @@ -147,7 +149,7 @@ public void appendColumn(int span) {
}

@Override
public void setStyle(CalculatedStyle style) {
public final void setStyle(CalculatedStyle style) {
super.setStyle(style);

if (isMarginAreaRoot()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
package org.xhtmlrenderer.newtable;

import com.google.errorprone.annotations.CheckReturnValue;
import org.jspecify.annotations.Nullable;
import org.w3c.dom.Element;
import org.xhtmlrenderer.css.constants.CSSName;
import org.xhtmlrenderer.css.constants.IdentValue;
import org.xhtmlrenderer.css.style.CalculatedStyle;
Expand All @@ -42,12 +44,14 @@
import java.util.Set;

public class TableCellBox extends BlockBox {
public static final TableCellBox SPANNING_CELL = new TableCellBox();
public static final TableCellBox SPANNING_CELL = new TableCellBox(null, null);

private int _row;
private int _col;

@Nullable
private TableBox _table;
@Nullable
private TableSectionBox _section;

private BorderPropertySet _collapsedLayoutBorder;
Expand Down Expand Up @@ -78,16 +82,13 @@ public class TableCellBox extends BlockBox {
private static final int BCOL = 7;
private static final int BTABLE = 6;

public TableCellBox() {
public TableCellBox(@Nullable Element source, @Nullable CalculatedStyle style) {
super(source, style);
}

@Override
public BlockBox copyOf() {
TableCellBox result = new TableCellBox();
result.setStyle(getStyle());
result.setElement(getElement());

return result;
return new TableCellBox(getElement(), getStyle());
}

@Override
Expand Down Expand Up @@ -138,6 +139,7 @@ public void layout(LayoutContext c) {
super.layout(c);
}

@Nullable
public TableBox getTable() {
// cell -> row -> section -> table
if (_table == null) {
Expand All @@ -146,6 +148,7 @@ public TableBox getTable() {
return _table;
}

@Nullable
protected TableSectionBox getSection() {
if (_section == null) {
_section = (TableSectionBox)getParent().getParent();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@

import com.google.errorprone.annotations.CheckReturnValue;
import org.jspecify.annotations.Nullable;
import org.w3c.dom.Element;
import org.xhtmlrenderer.css.constants.CSSName;
import org.xhtmlrenderer.css.constants.IdentValue;
import org.xhtmlrenderer.css.style.CalculatedStyle;
import org.xhtmlrenderer.css.style.CssContext;
import org.xhtmlrenderer.css.style.derived.BorderPropertySet;
import org.xhtmlrenderer.css.style.derived.RectPropertySet;
Expand Down Expand Up @@ -50,16 +52,13 @@ public class TableRowBox extends BlockBox {
private int _extraSpaceTop;
private int _extraSpaceBottom;

public TableRowBox() {
public TableRowBox(@Nullable Element element, @Nullable CalculatedStyle style) {
super(element, style);
}

@Override
public BlockBox copyOf() {
TableRowBox result = new TableRowBox();
result.setStyle(getStyle());
result.setElement(getElement());

return result;
return new TableRowBox(getElement(), getStyle());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
*/
package org.xhtmlrenderer.newtable;

import org.jspecify.annotations.Nullable;
import org.w3c.dom.Element;
import org.xhtmlrenderer.css.style.CalculatedStyle;
import org.xhtmlrenderer.layout.LayoutContext;
import org.xhtmlrenderer.render.BlockBox;
import org.xhtmlrenderer.render.Box;
Expand All @@ -40,16 +43,13 @@ public class TableSectionBox extends BlockBox {
private boolean _capturedOriginalAbsY;
private int _originalAbsY;

public TableSectionBox() {
public TableSectionBox(@Nullable Element element, @Nullable CalculatedStyle style) {
super(element, style);
}

@Override
public BlockBox copyOf() {
TableSectionBox result = new TableSectionBox();
result.setStyle(getStyle());
result.setElement(getElement());

return result;
return new TableSectionBox(getElement(), getStyle());
}

public List<RowData> getGrid() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,7 @@ public final class AnonymousBlockBox extends BlockBox {

public AnonymousBlockBox(Element element, CalculatedStyle style, List<InlineBox> savedParents,
List<Styleable> inlineContent) {
setElement(element);
setStyle(style);
super(element, style);
setAnonymous(true);
_openInlineBoxes = savedParents;
setChildrenContentType(ContentType.INLINE);
Expand Down
Loading

0 comments on commit 3223974

Please sign in to comment.