Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

floor alignment offset #549

Closed
wants to merge 2 commits into from
Closed

Conversation

robtfm
Copy link
Contributor

@robtfm robtfm commented Oct 4, 2023

Objective

correct space-xxx behaviour for cramped containers.

Context

i noticed a difference in layout between chrome and firefox vs taffy (via bevy) when the computed alignment for non-first items returns a negative value. it seems to be corrected by flooring the offset at zero.

i don't know for sure if the floor should be with the gap or without, i assumed with.

screenshots below for containers with justify-content set to each space-xxx mode
note the "modified" screenshots still show differences in the last two columns where the height is a negative number of px - i am not sure if there's a good spec for that, but i can work around it by just supplying Auto if the input is negative.

space-between original
space-between original
modified
space-between modified

space-evenly original
space=-evenly original
modified
space-evenly modified

space-around original
space-around original
modified
space-around modified

@nicoburns nicoburns self-requested a review October 4, 2023 12:24
Copy link
Collaborator

@nicoburns nicoburns left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi, and thanks for the PR!

Good catch - that value definitely shouldn't ever be negative! My intuition is that it is free_space that ought to be floored at zero, as the concept of "free space" is such that it should always be non-negative. I suspect gap may also need to be floored at zero. But that should happen elsewhere (when the layout algorithm first accesses the gap style) as gap is set by the user and a negative value is never valid. I think it is fine for this code to assume that gap is positive.

Having said that, we usually try not to rely on intuition but on generated tests that assert that our output is identical to Chrome (unless we determine that Chrome's output is incorrect). As such I would like to request the following changes:

  • Change the code to floor free_space, either at the top of this function, or in the callers (this function is used by both the flexbox and grid algorithms). I'd be tempted to put the floor where the free_space is first being computed.

  • Add a line to the changelog (RELEASES.md) under "Fixes"

  • Would it be possible for you to turn the HTML you have been testing Chrome/Firefox's output with into Taffy "generated tests". This would involve:

    • Adding HTML file(s) to test_fixtures/flex (duplicate one of the existing ones as a template, and modify the "test root" with your HTML).
    • Running cargo gentest (you will need Chrome and chromedriver installed) to generate the tests
    • You can then actually run the new tests with cargo test

    Failing that, could you post a sample of the HTML that triggers this issue as a comment on this issue so that we can we create the tests ourselves?

@robtfm
Copy link
Contributor Author

robtfm commented Oct 4, 2023

  • Change the code to floor free_space, either at the top of this function, or in the callers (this function is used by both the flexbox and grid algorithms). I'd be tempted to put the floor where the free_space is first being computed.

i think this would be incorrect - chrome only floors non-first items (see the way the lines go above the boxes in the evenly and around screenshots in cols 6-7.

also just to point out - i made this patch to the 0.3.x branch, not sure if that's what you want.

the code i used is below (can provide the bevy code too but i guess you don't need that?), just changing the justify-content for each case. i'm not really keen to add the tests, i think it will take me much longer than someone with more familiarity ...

css

.screen {
  display: flex;
  flex-direction: row;
  width: 1280px;
  height: 100px;
  background-color: red;
  position: absolute;
  top: 10%;
}

.col {
  display: flex;
  flex-direction: column;
  height: 100%;
  width: 10%;
  border: 1px solid blue;
  background-color: #404040;
}

.box {
  display: flex;
  flex-direction: column;
  justify-content: space-evenly;
  border: 1px solid red;
  color: white;
  font-family: 'Fira Mono', monospace;
  font-size: 7pt;
}

html

<!DOCTYPE html>
<html>

<head>
    <link rel="stylesheet" href="styles2.css">
</head>

<body>
    <div class="screen">
        <div class="col">
            <div class="box" style="height: 75px;">
                <div>text line one</div>
                <div>text line two</div>
            </div>
        </div>
        <div class="col">
            <div class="box" style="height: 65px;">
                <div>text line one</div>
                <div>text line two</div>
            </div>
        </div>
        <div class="col">
            <div class="box" style="height: 55px;">
                <div>text line one</div>
                <div>text line two</div>
            </div>
        </div>
        <div class="col">
            <div class="box" style="height: 45px;">
                <div>text line one</div>
                <div>text line two</div>
            </div>
        </div>
        <div class="col">
            <div class="box" style="height: 35px;">
                <div>text line one</div>
                <div>text line two</div>
            </div>
        </div>
        <div class="col">
            <div class="box" style="height: 25px;">
                <div>text line one</div>
                <div>text line two</div>
            </div>
        </div>
        <div class="col">
            <div class="box" style="height: 15px;">
                <div>text line one</div>
                <div>text line two</div>
            </div>
        </div>
        <div class="col">
            <div class="box" style="height: 5px;">
                <div>text line one</div>
                <div>text line two</div>
            </div>
        </div>
        <div class="col">
            <div class="box" style="height: -5px;">
                <div>text line one</div>
                <div>text line two</div>
            </div>
        </div>
        <div class="col">
            <div class="box" style="height: -15px;">
                <div>text line one</div>
                <div>text line two</div>
            </div>
        </div>
    </div>
</body>

</html>

@nicoburns
Copy link
Collaborator

the code i used is below (can provide the bevy code too but i guess you don't need that?), just changing the justify-content for each case. i'm not really keen to add the tests, i think it will take me much longer than someone with more familiarity ...

Yeah, no worries. I'll take a look later. The HTML you provided is really helpful.

also just to point out - i made this patch to the 0.3.x branch, not sure if that's what you want.

We normally try to land fixes on main first and then backport to release branches. So against would be preferable. For such a small fix it's easy for us to rebase though.

i think this would be incorrect - chrome only floors non-first items (see the way the lines go above the boxes in the evenly and around screenshots in cols 6-7.

Ah ok. That's good to know. I'll play around with the tests and try to pin down the behaviour.

@nicoburns
Copy link
Collaborator

We probably ought to implement the "safe" alignment variants too. With justify-content: safe space-evenly you get this (Chrome):

Screenshot 2023-10-04 at 15 26 43

@nicoburns
Copy link
Collaborator

@robtfm Taffy v0.3.15 has been released with a fix for this issue :)

@robtfm
Copy link
Contributor Author

robtfm commented Oct 5, 2023

awesome, thank you :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants