Skip to content

feat(dev): add css helper function - #42148

Merged
naomi-lgbt merged 19 commits into
freeCodeCamp:mainfrom
ShaunSHamilton:feat/add-css-helper
May 25, 2021
Merged

naomi-lgbt merged 19 commits into
freeCodeCamp:mainfrom
ShaunSHamilton:feat/add-css-helper

Conversation

@ShaunSHamilton

@ShaunSHamilton ShaunSHamilton commented May 16, 2021 •

Copy link
Copy Markdown
Member

Checklist:

  • 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 main branch of freeCodeCamp.
  • All the files I changed are in the same world language, for example: only English changes, or only Chinese changes, etc.

This PR adds a small css helper function for testing the curriculum. Example usage:

const astStyles = __helpers.getStyles(document, '*');
assert.equal(astStyles.boxSizing, 'border-box');
// Appears to work with the input value (not the processed value) - Good
const bodyStyles = __helpers.getStyles(document, 'body');
assert.equal(bodyStyles.height, '100vh');

Some notes about the reliability of this function:

  • Originally, document.styleSheets[1].cssRules returned an array - I do not know why it no longer does, but, honestly, it worked for a few tests, then changed to needing the Object.values. We might need to add a check for isArray
  • I have not tested this on non-Chromium browsers - if they do not follow the spec, then this will be bust (unless we find out what spec they use, and test for browser before using a specific function..)

PS: I am also working on an HTML helper function, because I am allergic to jQuery, and vanilla JS has so many DOM methods we could use better

@ShaunSHamilton
ShaunSHamilton requested a review from a team May 16, 2021 08:33
@gitpod-io

gitpod-io Bot commented May 16, 2021

Copy link
Copy Markdown

@ShaunSHamilton

Copy link
Copy Markdown
Member Author

Re-running Actions, because of this error:

remote: Internal Server Error.
  remote: 
  Error: fatal: unable to access 'https://github.com/freeCodeCamp/freeCodeCamp/': The requested URL returned error: 500
  The process '/usr/bin/git' failed with exit code 128

@raisedadead

Copy link
Copy Markdown
Member

Looks like they have an ongoing incident: https://www.githubstatus.com/incidents/zbpwygxwb3gw

@naomi-lgbt

Copy link
Copy Markdown
Member

I have not tested this on non-Chromium browsers - if they do not follow the spec, then this will be bust

MDN says .cssRules is on all browsers.
https://developer.mozilla.org/en-US/docs/Web/API/CSSStyleSheet/cssRules

@github-actions github-actions Bot added the platform: learn UI side of the client application that needs familiarity with React, Gatsby etc. label May 17, 2021
@raisedadead raisedadead added the status: waiting review To be applied to PR's that are ready for QA, especially when additional review is pending. label May 17, 2021
@ojeytonwilliams

ojeytonwilliams commented May 17, 2021 •

Copy link
Copy Markdown
Contributor

Any idea how this will handle browser specific css weirdness?

Cases like #39133, #37688 and all the issues we've had with zooming over the years.

@ShaunSHamilton

Copy link
Copy Markdown
Member Author

Any idea how this will handle browser specific css weirdness?

I have not tested outside of Chromium-based browsers. This is what I meant by whether they follow spec or not. Ideally, it should allow the tests to follow the exact input of the CSS value (provided it is valid CSS), which means this would solve the issue of testing against values which have gone through compilation (e.g. 100vw -> 996px).

I would appreciate this being tested on Safari, before adoption.

@ojeytonwilliams

Copy link
Copy Markdown
Contributor

If the spec is respected by all modern browsers, that's awesome - it should mean we don't need to worry about CSS parsers. Do you know how media queries are handled?

@ShaunSHamilton

Copy link
Copy Markdown
Member Author
cssRules: CSSRuleList
  0: CSSStyleRule {selectorText: "body", style: CSSStyleDeclaration, styleMap: StylePropertyMap, type: 1, cssText: "body { 
    background: black; color: cornsilk; }", …}
  1: CSSMediaRule
    conditionText: "(max-width: 996px)"
    cssRules: CSSRuleList {0: CSSStyleRule, length: 1}
    cssText: "@media (max-width: 996px) {\n  p { border: 1px solid black; }\n}"
    media: MediaList {0: "(max-width: 996px)", length: 1, mediaText: "(max-width: 996px)"}
    parentRule: null
    parentStyleSheet: CSSStyleSheet {ownerRule: null, cssRules: CSSRuleList, rules: CSSRuleList, type: "text/css", href: 
      "http://localhost:1234/styles.8986bff4.css", …}
  type: 4
  __proto__: CSSMediaRule
  length: 2
  __proto__: CSSRuleList
  disabled: false

@ojeytonwilliams Good catch. I will add some logic to check for CSSMedia Rule

@ShaunSHamilton ShaunSHamilton added status: PR in works Work in Progress (WIP) Issues. and removed status: waiting review To be applied to PR's that are ready for QA, especially when additional review is pending. labels May 17, 2021
@ojeytonwilliams

Copy link
Copy Markdown
Contributor

Originally, document.styleSheets[1].cssRules returned an array - I do not know why it no longer does, but, honestly, it worked for a few tests, then changed to needing the Object.values. We might need to add a check for isArray

It looks like it's not an array, exactly, so it probably doesn't have all of Array's methods.

@github-actions github-actions Bot added platform: api Server application that needs familiarity with Express, Fastify, MongoDB etc. scope: curriculum Lessons, Challenges, Projects and other Curricular Content in curriculum directory. scope: docs scope: i18n language translation/internationalization. Often combined with language type label scope: tools/scripts Scripts for supporting dev work, generating config and build artifacts, etc. labels May 18, 2021
@naomi-lgbt

Copy link
Copy Markdown
Member

@ShaunSHamilton did you make the bot mad 😛

@github-actions github-actions Bot removed scope: tools/scripts Scripts for supporting dev work, generating config and build artifacts, etc. platform: api Server application that needs familiarity with Express, Fastify, MongoDB etc. scope: i18n language translation/internationalization. Often combined with language type label scope: docs labels May 18, 2021
@ShaunSHamilton

Copy link
Copy Markdown
Member Author

@ShaunSHamilton did you make the bot mad 😛

Umm.... I did an "oops" - Git betrayed me

@ShaunSHamilton ShaunSHamilton left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Forgive the mess. Also, I could not leave the example file without the small changes - OCD acting up.

@ojeytonwilliams

Copy link
Copy Markdown
Contributor

@ShaunSHamilton It doesn't seem strictly necessary, but it's not that onerous to add it. Without fully understanding why the rule exists in the first place, I'd rather not just rip it out.

Comment thread jest.transform.js Resolved
@ShaunSHamilton

Copy link
Copy Markdown
Member Author

The last 2 tests will keep failing because JSDOM does not implement conditionText property on CSSMediaRule:

So, shall I change the test? Or, block this until I can get a PR opened on CSSOM?

@naomi-lgbt

Copy link
Copy Markdown
Member

Or, block this until I can get a PR opened on CSSOM?

There hasn't been any activity on that project in like 6 months. Can we inject that property ourselves in the test environment?

@naomi-lgbt

Copy link
Copy Markdown
Member

Hmm I don't see a real clean way to do that - might be worth yoinking the test until/if the lib is updated.

@ShaunSHamilton

Copy link
Copy Markdown
Member Author

Can we inject that property ourselves in the test environment?

@nhcarrigan We can. The exact value exists on the MediaList (media property)

@ShaunSHamilton ShaunSHamilton added status: waiting review To be applied to PR's that are ready for QA, especially when additional review is pending. and removed status: PR in works Work in Progress (WIP) Issues. labels May 24, 2021
naomi-lgbt
naomi-lgbt previously approved these changes May 24, 2021

@naomi-lgbt naomi-lgbt 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've played with this a bit, and it is leaps and bounds above the regex approach.

Not 100% sure if it covers all of our use-cases, but as it stands now it's a significant improvement. Happy to merge.

@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.

Nitpicking about test structure, mostly.

It looks good, though, I think this is basically done.

Comment thread client/src/utils/CSSHelp.test.ts Resolved
Comment thread client/src/utils/CSSHelp.test.ts Resolved
Comment thread client/src/utils/CSSHelp.test.ts Resolved
Comment thread client/src/utils/CSSHelp.test.ts Resolved
Comment thread client/src/utils/CSSHelp.test.ts Resolved
Comment thread client/src/utils/css-help.test.ts Resolved
Co-authored-by: Oliver Eyton-Williams <[email protected]>
@naomi-lgbt
naomi-lgbt merged commit d5cc9ba into freeCodeCamp:main May 25, 2021
@ShaunSHamilton
ShaunSHamilton deleted the feat/add-css-helper branch May 25, 2021 14:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

platform: learn UI side of the client application that needs familiarity with React, Gatsby etc. scope: curriculum Lessons, Challenges, Projects and other Curricular Content in curriculum directory. status: waiting review To be applied to PR's that are ready for QA, especially when additional review is pending.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants