Skip to content

Servo's CSS parser converts absolute-unit lengths to a fixed-point representation even for specified values #15729

Closed

Description

@bzbarsky

Testcase:

<div style="width: 0.33px"></div>
<script>
  console.log(document.querySelector("div").style.width);
</script>

In servo this logs "0.31666666px". It should really preserve the original "0.33px" value.

In Gecko, specified values are stored as floating point plus unit, and not converted to 1/60 px integers until computed value time.

And even then, note that servo above is converting by truncation, whereas Gecko uses rounding. Specifically, Gecko does NSToCoordRoundWithClamp which checks for overflow issues, then rounds to nearest via NSToCoordRound.

Activity

  1. SimonSapin commented on Feb 24, 2017

    @SimonSapin
    Member

    Relevant code is in components/style/values/specified/length.rs. Changes to do:

    • Replace Au with f32 in specified value types. (They should already be in enum variants that indicate the unit. If/where they’re not, add newtypes that do.)
    • Move the unit conversion from parsing code to to_computed_value methods
    • In these conversions, do clamping and rounding. Something like:
    struct Px(pub f32);
    impl From<Px> for Au {
        fn from(px: Px) -> Au {
            const AU_PER_PX: f64 = 60.0;
            Au(
                ((px.0 as f64) * AU_PER_PX)
                .min(::std::i32::MAX as f64)
                .max(::std::i32::MIN as f64)
                .round()
                as i32
            )
        }
    }
  2. bzbarsky commented on Feb 24, 2017

    @bzbarsky
    ContributorAuthor
           .min(::std::i32::MAX as f64)
          .max(::std::i32::MIN as f64)
    

    Fwiw, we're going to need more clamping than that going into Gecko code, but presumably that will be somewhere Gecko-specific? For Gecko, the relevant clamping points are 1<<30 and -(1<<30), at least when not NS_COORD_IS_FLOAT... Please file a separate issue for that in the right place, whatever that is?

  3. SimonSapin commented on Feb 24, 2017

    @SimonSapin
    Member

    I didn’t know that Gecko clamps to a smaller range. Is it to allow some amount of arithmetic on the results without overflow? That sounds like a good idea, let’s do that instead of i32::MAX / i32::MIN for Servo too.

    What is NS_COORD_IS_FLOAT?

  4. bzbarsky commented on Feb 24, 2017

    @bzbarsky
    ContributorAuthor

    Is it to allow some amount of arithmetic on the results without overflow?

    I don't think so. I don't recall offhand why we did that instead of i32::MAX and -i32::MAX (not i32::MIN, because we want symmetric behavior).

    We used to have magic values of nscoord to represent "auto", I think, so maybe that's why? @dbaron might recall offhand.

    What is NS_COORD_IS_FLOAT?

    Compile-time switch to use floats for nscoord instead. May or may not build nowadays...

  5. added
    E-more-complex
    on Mar 13, 2017
  6. mcaveniathor commented on Mar 14, 2017

    @mcaveniathor

    I'm looking for a first contribution to make as I learn Rust -- if mentored, I would love to help with this in any way I can.

  7. emilio commented on Mar 26, 2017

    @emilio
    Member

    Yes, please! I'm happy to mentor this, please ask any questions you may have here (or open a PR with your progress and request feedback). Thanks!

  8. emilio commented on Mar 26, 2017

    @emilio
    Member

    (Oh, and sorry for the delay replying to this, @SimonSapin is away for a few weeks I believe, and I wasn't cc'd here).

  9. added a commit that references this issue on Mar 26, 2017
  10. added a commit that references this issue on Mar 26, 2017
  11. added
    C-assigned
    C-has-open-pr
    on Apr 4, 2017
  12. added 5 commits that reference this issue on Apr 4, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-styloC-assignedC-has-open-prE-more-complex

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions