Skip to content

fix(challenge): account for browser zoom, add note to challenge (for width challenge) - #37688

Merged
RandellDawson merged 5 commits into
freeCodeCamp:masterfrom
lasjorg:fix/account-for-browser-zoom-when-getting-element-width
Dec 13, 2019
Merged

RandellDawson merged 5 commits into
freeCodeCamp:masterfrom
lasjorg:fix/account-for-browser-zoom-when-getting-element-width

Conversation

@lasjorg

@lasjorg lasjorg commented Nov 5, 2019

Copy link
Copy Markdown
Contributor
  • I have read freeCodeCamp's contribution guidelines.
  • My pull request has a descriptive title (not a vague title like Update index.md)
  • My pull request targets the master branch of freeCodeCamp.
  • None of my changes are plagiarized from another source without proper attribution.
  • All the files I changed are in the same world language (for example: only English changes, or only Chinese changes, etc.)
  • My changes do not use shortened URLs or affiliate links.

This is the same fix as for the height challenge. I do not have any confirmation for this fix yet (forum post showing the issue). We may or may not want to wait for one.

I thought about wrapping this in a function to add a variable explaining the - 10 part, just to avoid any "magic" numbers. But that just makes the assert really long and without proper formatting it gets hard to read in my opinion.

The - 10 is subtracting the padding and border of the .fullCard element to get the "true" width.

@gitpod-io

gitpod-io Bot commented Nov 5, 2019 •

Copy link
Copy Markdown

@ojeytonwilliams

Copy link
Copy Markdown
Contributor

@lasjorg It's possible to format the YAML strings however you want, but you need to wrap them in single or double quotes. Here's the spec, but the main points are

  • " you have to escape " and \ with \
  • ' you have to repeat '

For example:

tests:
  - text: Your code should change the <code>width</code> property of the card to 245 pixels by using the <code>fullCard</code> class selector.
    testString: "assert(
  Math.round(
    document.querySelector('.fullCard').getBoundingClientRect().width
  ) -
    10 ===
    245 &&
    /\\.fullCard{\\S*width:245px(;\\S*}|})/.test(
      $('style')
        .text()
        .replace(/\\s/g, '')
    )
);"

@RandellDawson

Copy link
Copy Markdown
Member

@lasjorg You could also create a helper function and place it in the After Test section and call the helper function in the assert. Then add any applicable comments inside the helper function.

@lasjorg

lasjorg commented Nov 5, 2019

Copy link
Copy Markdown
Contributor Author

Thank you both for the info.

I don't think I have ever seen a challenge where the assert code was formatted. I like the formatting but I think the escaping makes the regex harder to read and is likely to confuse some if unaware of the YAML rules.

@ojeytonwilliams @RandellDawson Do you guys think I need to make it more clear what the - 10 is, or should i just leave it as is?

@camperbot camperbot added language: English scope: curriculum Lessons, Challenges, Projects and other Curricular Content in curriculum directory. labels Nov 6, 2019
@lasjorg lasjorg changed the title fix(challenge): account for browser zoom, add note to challenge fix(challenge): account for browser zoom, add note to challenge (for width challenge) Nov 7, 2019
@lasjorg

lasjorg commented Nov 14, 2019

Copy link
Copy Markdown
Contributor Author

Switched to just using regex.

@ojeytonwilliams ojeytonwilliams left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Looks good. Just need to remove the warning.

@ojeytonwilliams ojeytonwilliams left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM 👍

@RandellDawson

Copy link
Copy Markdown
Member

Are there any outstanding issues that this PR closes? If so, can we get them listed in the PR body?

@lasjorg

lasjorg commented Nov 14, 2019

Copy link
Copy Markdown
Contributor Author

@RandellDawson I don't think there are any open issues for this challenge (just the one closed). At least not that I found.

@RandellDawson RandellDawson left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I can pass the challenge with the following which do not actually affect the width.

  .fullCard {
    border: 1px solid #ccc;
    border-radius: 5px;
    margin: 10px 5px;
    padding: 4px;
    max-width: 245px;
  }

OR

  .fullCard {
    border: 1px solid #ccc;
    border-radius: 5px;
    margin: 10px 5px;
    padding: 4px;
    Width: 245px;
  }

OR

  .fullCard {
    border: 1px solid #ccc;
    border-radius: 5px;
    margin: 10px 5px;
    padding: 4px;
    mywidth: 245px;
  }

@RandellDawson RandellDawson added the status: waiting update Is awaiting update, after feedback or request for changes label Nov 14, 2019
@lasjorg

lasjorg commented Nov 15, 2019

Copy link
Copy Markdown
Contributor Author

@RandellDawson Yes I know, sorry. I did see that the regex(s) was lax but I just kept it like it was. It also allows for incorrect syntax (also because of the \S*).

.fullCard {{{{}}}
  border: 1px solid #ccc;
  border-radius: 5px;
  margin: 10px 5px;
  padding: 4px;;;
  width: 245px;;;;
}}}}

I guess we have to rewrite them. If anyone has any good suggestions I'm all ears.

Not going to lie. I'm half tempted to just put back the zoom note and leave it how it was to begin with. This zoom nonsense is apparently a "feature" of Blink and not a bug.

Who knows just how many challenges are affected. Rewriting 3 or 4 challenges is not so bad, but if we have to rewrite a bunch of them just to accommodate this one problem then I'm not so sure it's worth it. Adding a note to each affected challenge might make more sense. That way we also won't inadvertently introduce new bugs.

I'm just confused why we haven't seen this reported before now? These (new?) bug reports seems to oddly correspond in time with the latest major site update. But maybe it's just a coincidence.

@ojeytonwilliams

ojeytonwilliams commented Nov 15, 2019 •

Copy link
Copy Markdown
Contributor

This is why I thought we'd better off without regex. It accepts both correct ways of doing it (I checked that much), but so much more besides (which I should have checked, but didn't). Apparently we have to use it.

As for solving this, aren't there two types of tests that have the bug. Dimensions set inline and dimensions set by css? The first type seems to be solved by checking .style (like you (@lasjorg) did in #37756). The second type is the real problem, but how about we try to come up with a robust regex for extracting a css rule and apply that to all the relevant challenges?

Adding a note to each affected challenge might make more sense.

Except that that is a pain for people that actually need it to be zoomed to be able to read.

This zoom nonsense is apparently a "feature" of Blink and not a bug.

Eh, I don't agree with them. Since they aren't consistent with Firefox, at least one of them has a bug.

@lasjorg

lasjorg commented Nov 16, 2019

Copy link
Copy Markdown
Contributor Author

I have reverted to my initial code for now.

@ojeytonwilliams Sure some fixes are pretty easy. I'm just not sure how many challenges are actually affected by this.

Eh, I don't agree with them. Since they aren't consistent with Firefox, at least one of them has a bug.

It seems they have decided that precision of 1/64th of a pixel is enough. I guess it might be a performance thing.

Unless we can come up with a good regex, I think my initial code for the PR is the best we have so far. I don't see the rounding being a problem here because we are limiting the valid input to 245 using the regex so the user can not enter some weird value. Also, this is on a fixed sized element so the viewport dimension does not dynamically change the element dimension. These two facts combined makes me think we can get away with rounding the number.

Theoretical issues aside. I think we should implement it and get it tested, at least until we have something better. It can't be worse than what we have in production now.

@moT01

moT01 commented Nov 19, 2019 •

Copy link
Copy Markdown
Member

I've used a regex like this a number of times in the past @lasjorg...

const fullCard = code.match(/\.fullCard\s*{[\s\S]+?[^}]}/g)[0];
assert(/width\s*:\s*245px\s*(;|})/gi.test(fullCard));

not sure if you want to keep changing this around, but it's pretty robust - it could apply to the other PR as well.

@lasjorg

lasjorg commented Nov 28, 2019

Copy link
Copy Markdown
Contributor Author

@moT01 @ojeytonwilliams Do you think we need to use the better regex or should we just go with the one we already have?

@moT01 moT01 left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The current state of this PR doesn't address @RandellDawson's concerns, they are still a problem - although his second example should pass since capitalization still applies the style.

After playing around with this for a little bit, the rounding issue was just too much of a pain for me. I came up with this...

    testString: const fullCard = code.match(/\.fullCard\s*{[\s\S]+?[^}]}/g)[0];
assert(/width\s*:\s*245px\s*(;|})/gi.test(fullCard) && 
getComputedStyle(document.querySelector('.fullCard')).maxWidth === 'none');

It's similar to what I suggested earlier, but adds a check that max-width isn't used. That's what I would go with, but I don't really care as long as it works and, right now, it isn't.

@lasjorg

lasjorg commented Dec 2, 2019

Copy link
Copy Markdown
Contributor Author

@moT01 Thank you for the help.

There are edge cases for broken syntax with this pure regex based approach. You can break the output while still passing the test.

Invalid syntax after the opening brace:

.fullCard {{}
  width: 245px;
  border: 1px solid #ccc;
  border-radius: 5px;
  margin: 10px 5px;
  padding: 4px;
}

Extra character before width:

.fullCard {
  swidth: 245px;
  border: 1px solid #ccc;
  border-radius: 5px;
  margin: 10px 5px;
  padding: 4px;
}

Missing semicolon on the rule that comes before:

.fullCard {
  border: 1px solid #ccc;
  border-radius: 5px;
  margin: 10px 5px;
  padding: 4px
  width: 245px;
}

There are probably other edge cases I haven't considered. Not sure how, or if, we should test for this?

@ojeytonwilliams

Copy link
Copy Markdown
Contributor

@lasjorg I think any regex we come up with is likely to accept some incorrect answers. As long as it passes all the correct answers we can come up with and as long as it rejects typical mistakes, I'm okay with that. As long as @RandellDawson and @moT01 are happy, that's good enough for me.

@RandellDawson

Copy link
Copy Markdown
Member

The first edge case does not bother me as bad as the 2nd and 3rd. Those two would not create the desired effect in the preview pane, yet pass the challenge.

@lasjorg

lasjorg commented Dec 4, 2019

Copy link
Copy Markdown
Contributor Author

Just so I don't forget, I have push this "as is" for now.

  1. Made changes to avoid throwing an error if .match() returns null.

  2. Used jQuery for the maxWidth check just to shorten the assert a bit.

@moT01 moT01 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Alright, I'm fine adding it like this, it fixes the rounding issues that were the main problem. The only edge case that I found is that using min-width: 245px will pass. But, that input is unlikely and I hate to keep asking for changes for small things like that. If any other edge cases come up, we can deal with them then. Thanks for all the work on this @lasjorg 🎉

@RandellDawson RandellDawson removed the status: waiting update Is awaiting update, after feedback or request for changes label Dec 13, 2019
@RandellDawson
RandellDawson merged commit 63b9be8 into freeCodeCamp:master Dec 13, 2019
@RandellDawson

Copy link
Copy Markdown
Member

Thank you for your contribution to the page! 👍
We're happy to accept these changes, and look forward to future contributions. 🎉

@lasjorg
lasjorg deleted the fix/account-for-browser-zoom-when-getting-element-width branch December 13, 2019 15:43
abbathaw pushed a commit to abbathaw/freeCodeCamp that referenced this pull request Jul 24, 2020
…width challenge) (freeCodeCamp#37688)

* fix(challenge): account for browser zoom, add note to challenge

* fix(challenge): use regex for width assert

* fix(challenge): remove note

* fix(challenge): revert to initial PR code

* updated regex, add check for maxWidth
@ojeytonwilliams ojeytonwilliams mentioned this pull request May 17, 2021
4 tasks done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

scope: curriculum Lessons, Challenges, Projects and other Curricular Content in curriculum directory.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants