Skip to content

Commit

Permalink
Change gentest to only write position relative to parent
Browse files Browse the repository at this point in the history
Summary:
`child.offsetLeft/Top` calculates the offset from child to its nearest positioned ancestor, not its direct parent. These are often the same and have not mattered in the past since we have not supported position static. Since are are in the process of supporting that, we would like our tests to be usable so this adjusts the gentest methodology to only speak the same language as Yoga - that is left/top are always relative to direct parents.

It works by using `getBoundingClientRect().left/top` instead. Then we pass that down to children and subtract it from the childs `getBoundingClientRect()` to get the position relative to the parent. Note we have to round the final result as `child.offsetLeft/Top` is rounded.

Reviewed By: NickGerleman

Differential Revision: D51053629

fbshipit-source-id: 8809588d12953565228ae50fdf38197213c46182
  • Loading branch information
joevilches authored and facebook-github-bot committed Nov 7, 2023
1 parent 301ed20 commit 484118c
Show file tree
Hide file tree
Showing 4 changed files with 155 additions and 154 deletions.
15 changes: 8 additions & 7 deletions gentest/gentest.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,9 @@ function printTest(e, ext, LTRContainer, RTLContainer, genericContainer) {
]);
e.emitPrologue();

const LTRLayoutTree = calculateTree(LTRContainer);
const RTLLayoutTree = calculateTree(RTLContainer);
const genericLayoutTree = calculateTree(genericContainer);
const LTRLayoutTree = calculateTree(LTRContainer, 0, 0);
const RTLLayoutTree = calculateTree(RTLContainer, 0, 0);
const genericLayoutTree = calculateTree(genericContainer, 0, 0);

for (let i = 0; i < genericLayoutTree.length; i++) {
e.emitTestPrologue(
Expand Down Expand Up @@ -679,18 +679,19 @@ function getRoundedSize(node) {
};
}

function calculateTree(root) {
function calculateTree(root, parentOffsetLeft, parentOffsetTop) {
const rootLayout = [];

for (let i = 0; i < root.children.length; i++) {
const child = root.children[i];
const boundingRect = child.getBoundingClientRect();
const layout = {
name: child.id !== '' ? child.id : 'INSERT_NAME_HERE',
left: child.offsetLeft + child.parentNode.clientLeft,
top: child.offsetTop + child.parentNode.clientTop,
left: Math.round(boundingRect.left - parentOffsetLeft),
top: Math.round(boundingRect.top - parentOffsetTop),
width: child.offsetWidth,
height: child.offsetHeight,
children: calculateTree(child),
children: calculateTree(child, boundingRect.left, boundingRect.top),
style: getYogaStyle(child),
declaredStyle: child.style,
rawStyle: child.getAttribute('style'),
Expand Down
Loading

0 comments on commit 484118c

Please sign in to comment.