Skip to content

Commit

Permalink
Add support for display: contents style (#1726)
Browse files Browse the repository at this point in the history
Summary:
X-link: facebook/react-native#47035

This PR adds support for `display: contents` style by effectively skipping nodes with `display: contents` set during layout.

This required changes in the logic related to children traversal - before this PR a node would be always laid out in the context of its direct parent. After this PR that assumption is no longer true - `display: contents` allows nodes to be skipped, i.e.:

```html
<div id="node1">
  <div id="node2" style="display: contents;">
    <div id="node3" />
  </div>
</div>
```

`node3` will be laid out as if it were a child of `node1`.

Because of this, iterating over direct children of a node is no longer correct to achieve the correct layout. This PR introduces `LayoutableChildren::Iterator` which can traverse the subtree of a given node in a way that nodes with `display: contents` are replaced with their concrete children.

A tree like this:
```mermaid
flowchart TD
    A((A))
    B((B))
    C((C))
    D((D))
    E((E))
    F((F))
    G((G))
    H((H))
    I((I))
    J((J))

    A --> B
    A --> C
    B --> D
    B --> E
    C --> F
    D --> G
    F --> H
    G --> I
    H --> J

    style B fill:#50
    style C fill:#50
    style D fill:#50
    style H fill:#50
    style I fill:#50
```

would be laid out as if the green nodes (ones with `display: contents`) did not exist. It also changes the logic where children were accessed by index to use the iterator instead as random access would be non-trivial to implement and it's not really necessary - the iteration was always sequential and indices were only used as boundaries.

There's one place where knowledge of layoutable children is required to calculate the gap. An optimization for this is for a node to keep a counter of how many `display: contents` nodes are its children. If there are none, a short path of just returning the size of the children vector can be taken, otherwise it needs to iterate over layoutable children and count them, since the structure may be complex.

One more major change this PR introduces is `cleanupContentsNodesRecursively`. Since nodes with `display: contents` would be entirely skipped during the layout pass, they would keep previous metrics, would be kept as dirty, and, in the case of nested `contents` nodes, would not be cloned, breaking `doesOwn` relation. All of this is handled in the new method which clones `contents` nodes recursively, sets empty layout, and marks them as clean and having a new layout so that it can be used on the React Native side.

Relies on #1725

Changelog: [Internal]

Pull Request resolved: #1726

Test Plan: Added tests for `display: contents` based on existing tests for `display: none` and ensured that all the tests were passing.

Reviewed By: joevilches

Differential Revision: D64404340

Pulled By: NickGerleman

fbshipit-source-id: f6f6e9a6fad82873f18c8a0ead58aad897df5d09
  • Loading branch information
j-piasecki authored and facebook-github-bot committed Oct 19, 2024
1 parent 5687182 commit 68bb234
Show file tree
Hide file tree
Showing 26 changed files with 2,590 additions and 44 deletions.
2 changes: 1 addition & 1 deletion enums.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"SpaceEvenly",
],
"PositionType": ["Static", "Relative", "Absolute"],
"Display": ["Flex", "None"],
"Display": ["Flex", "None", "Contents"],
"Wrap": ["NoWrap", "Wrap", "WrapReverse"],
"BoxSizing": ["BorderBox", "ContentBox"],
"MeasureMode": ["Undefined", "Exactly", "AtMost"],
Expand Down
58 changes: 58 additions & 0 deletions gentest/fixtures/YGDisplayTest.html
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,61 @@
<div id="display_none_with_position_absolute" style="width: 100px; height: 100px;">
<div style="display:none; position: absolute; width: 100px; height: 100px"></div>
</div>

<div id="display_contents" style="width: 100px; height: 100px; flex-direction: row;">
<div style="display: contents;">
<div style="flex: 1; height: 10px;"></div>
<div style="flex: 1; height: 20px;"></div>
</div>
</div>

<div id="display_contents_fixed_size" style="width: 100px; height: 100px; flex-direction: row;">
<div style="display: contents; width: 50px; height: 50px;">
<div style="flex: 1; height: 10px;"></div>
<div style="flex: 1; height: 20px;"></div>
</div>
</div>

<div id="display_contents_with_margin" style="width: 100px; height: 100px; flex-direction: row;">
<div style="width: 20px; height: 20px; display: contents; margin: 10px;"></div>
<div style="flex-grow: 1;"></div>
</div>

<div id="display_contents_with_padding" style="width: 100px; height: 100px; flex-direction: row;">
<div style="display: contents; padding: 10px;">
<div style="flex: 1; height: 10px;"></div>
<div style="flex: 1; height: 20px;"></div>
</div>
</div>

<div id="display_contents_with_position" style="width: 100px; height: 100px; flex-direction: row;">
<div style="display: contents; top: 10px;">
<div style="flex: 1; height: 10px;"></div>
<div style="flex: 1; height: 20px;"></div>
</div>
</div>

<div id="display_contents_with_position_absolute" style="width: 100px; height: 100px; flex-direction: row;">
<div style="display: contents; position: absolute; width: 50px; height: 50px;">
<div style="flex: 1; height: 10px;"></div>
<div style="flex: 1; height: 20px;"></div>
</div>
</div>

<div id="display_contents_nested" style="width: 100px; height: 100px; flex-direction: row;">
<div style="display: contents;">
<div style="display: contents;">
<div style="flex: 1; height: 10px;"></div>
<div style="flex: 1; height: 20px;"></div>
</div>
</div>
</div>

<div id="display_contents_with_siblings" style="width: 100px; height: 100px; flex-direction: row;">
<div style="flex: 1; height: 30px;"></div>
<div style="display: contents;">
<div style="flex: 1; height: 10px;"></div>
<div style="flex: 1; height: 20px;"></div>
</div>
<div style="flex: 1; height: 30px;"></div>
</div>
1 change: 1 addition & 0 deletions gentest/gentest-cpp.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ CPPEmitter.prototype = Object.create(Emitter.prototype, {

YGDisplayFlex: {value: 'YGDisplayFlex'},
YGDisplayNone: {value: 'YGDisplayNone'},
YGDisplayContents: {value: 'YGDisplayContents'},
YGAuto: {value: 'YGAuto'},

YGNodeCalculateLayout: {
Expand Down
1 change: 1 addition & 0 deletions gentest/gentest-java.js
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@ JavaEmitter.prototype = Object.create(Emitter.prototype, {

YGDisplayFlex: {value: 'YogaDisplay.FLEX'},
YGDisplayNone: {value: 'YogaDisplay.NONE'},
YGDisplayContents: {value: 'YogaDisplay.CONTENTS'},
YGAuto: {value: 'YogaConstants.AUTO'},

YGWrapNoWrap: {value: 'YogaWrap.NO_WRAP'},
Expand Down
1 change: 1 addition & 0 deletions gentest/gentest-javascript.js
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ JavascriptEmitter.prototype = Object.create(Emitter.prototype, {

YGDisplayFlex: {value: 'Display.Flex'},
YGDisplayNone: {value: 'Display.None'},
YGDisplayContents: {value: 'Display.Contents'},

YGBoxSizingBorderBox: {value: 'BoxSizing.BorderBox'},
YGBoxSizingContentBox: {value: 'BoxSizing.ContentBox'},
Expand Down
2 changes: 2 additions & 0 deletions gentest/gentest.js
Original file line number Diff line number Diff line change
Expand Up @@ -665,6 +665,8 @@ function displayValue(e, value) {
return e.YGDisplayFlex;
case 'none':
return e.YGDisplayNone;
case 'contents':
return e.YGDisplayContents;
}
}

Expand Down
4 changes: 3 additions & 1 deletion java/com/facebook/yoga/YogaDisplay.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@

public enum YogaDisplay {
FLEX(0),
NONE(1);
NONE(1),
CONTENTS(2);

private final int mIntValue;

Expand All @@ -27,6 +28,7 @@ public static YogaDisplay fromInt(int value) {
switch (value) {
case 0: return FLEX;
case 1: return NONE;
case 2: return CONTENTS;
default: throw new IllegalArgumentException("Unknown enum value: " + value);
}
}
Expand Down
107 changes: 107 additions & 0 deletions java/tests/generated/com/facebook/yoga/YGDisplayContentsTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @generated SignedSource<<be0d102e74e15f15050520f21afcaee1>>
* generated by gentest/gentest-driver.ts from gentest/fixtures/YGDisplayContentsTest.html
*/

package com.facebook.yoga;

import static org.junit.Assert.assertEquals;

import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import com.facebook.yoga.utils.TestUtils;

@RunWith(Parameterized.class)
public class YGDisplayContentsTest {
@Parameterized.Parameters(name = "{0}")
public static Iterable<TestParametrization.NodeFactory> nodeFactories() {
return TestParametrization.nodeFactories();
}

@Parameterized.Parameter public TestParametrization.NodeFactory mNodeFactory;

@Test
public void test_test1() {
YogaConfig config = YogaConfigFactory.create();

final YogaNode root = createNode(config);
root.setFlexDirection(YogaFlexDirection.ROW);
root.setPositionType(YogaPositionType.ABSOLUTE);
root.setWidth(100f);
root.setHeight(100f);

final YogaNode root_child0 = createNode(config);
root_child0.setDisplay(YogaDisplay.CONTENTS);
root.addChildAt(root_child0, 0);

final YogaNode root_child0_child0 = createNode(config);
root_child0_child0.setFlexGrow(1f);
root_child0_child0.setFlexShrink(1f);
root_child0_child0.setFlexBasisPercent(0f);
root_child0_child0.setHeight(10f);
root_child0.addChildAt(root_child0_child0, 0);

final YogaNode root_child0_child1 = createNode(config);
root_child0_child1.setFlexGrow(1f);
root_child0_child1.setFlexShrink(1f);
root_child0_child1.setFlexBasisPercent(0f);
root_child0_child1.setHeight(20f);
root_child0.addChildAt(root_child0_child1, 1);
root.setDirection(YogaDirection.LTR);
root.calculateLayout(YogaConstants.UNDEFINED, YogaConstants.UNDEFINED);

assertEquals(0f, root.getLayoutX(), 0.0f);
assertEquals(0f, root.getLayoutY(), 0.0f);
assertEquals(100f, root.getLayoutWidth(), 0.0f);
assertEquals(100f, root.getLayoutHeight(), 0.0f);

assertEquals(0f, root_child0.getLayoutX(), 0.0f);
assertEquals(0f, root_child0.getLayoutY(), 0.0f);
assertEquals(0f, root_child0.getLayoutWidth(), 0.0f);
assertEquals(0f, root_child0.getLayoutHeight(), 0.0f);

assertEquals(0f, root_child0_child0.getLayoutX(), 0.0f);
assertEquals(0f, root_child0_child0.getLayoutY(), 0.0f);
assertEquals(50f, root_child0_child0.getLayoutWidth(), 0.0f);
assertEquals(10f, root_child0_child0.getLayoutHeight(), 0.0f);

assertEquals(50f, root_child0_child1.getLayoutX(), 0.0f);
assertEquals(0f, root_child0_child1.getLayoutY(), 0.0f);
assertEquals(50f, root_child0_child1.getLayoutWidth(), 0.0f);
assertEquals(20f, root_child0_child1.getLayoutHeight(), 0.0f);

root.setDirection(YogaDirection.RTL);
root.calculateLayout(YogaConstants.UNDEFINED, YogaConstants.UNDEFINED);

assertEquals(0f, root.getLayoutX(), 0.0f);
assertEquals(0f, root.getLayoutY(), 0.0f);
assertEquals(100f, root.getLayoutWidth(), 0.0f);
assertEquals(100f, root.getLayoutHeight(), 0.0f);

assertEquals(0f, root_child0.getLayoutX(), 0.0f);
assertEquals(0f, root_child0.getLayoutY(), 0.0f);
assertEquals(0f, root_child0.getLayoutWidth(), 0.0f);
assertEquals(0f, root_child0.getLayoutHeight(), 0.0f);

assertEquals(50f, root_child0_child0.getLayoutX(), 0.0f);
assertEquals(0f, root_child0_child0.getLayoutY(), 0.0f);
assertEquals(50f, root_child0_child0.getLayoutWidth(), 0.0f);
assertEquals(10f, root_child0_child0.getLayoutHeight(), 0.0f);

assertEquals(0f, root_child0_child1.getLayoutX(), 0.0f);
assertEquals(0f, root_child0_child1.getLayoutY(), 0.0f);
assertEquals(50f, root_child0_child1.getLayoutWidth(), 0.0f);
assertEquals(20f, root_child0_child1.getLayoutHeight(), 0.0f);
}

private YogaNode createNode(YogaConfig config) {
return mNodeFactory.create(config);
}
}
Loading

0 comments on commit 68bb234

Please sign in to comment.