Skip to content

Comments

Implement universal selector handling in getStyle#561

Open
Iyeroo wants to merge 9 commits intofreeCodeCamp:mainfrom
Iyeroo:Iyeroo-patch-2
Open

Implement universal selector handling in getStyle#561
Iyeroo wants to merge 9 commits intofreeCodeCamp:mainfrom
Iyeroo:Iyeroo-patch-2

Conversation

@Iyeroo
Copy link

@Iyeroo Iyeroo commented Jan 31, 2026

fix(curriculum):Add a private method to check for universal selectors and update getStyle to handle them correctly.

Checklist:

Closes freeCodeCamp/freeCodeCamp#64218

This pull request addresses an issue in the CSSHelp class where CSS rules containing universal selectors (*) were being matched even when the user did not explicitly request them.

Changes made:

  1. Added a private method selectorHasUniversal(selector: string): boolean in the CSSHelp class.

    • This method detects whether a selector includes a universal selector (*).
  2. Updated the getStyle(selector: string) method to correctly handle universal selectors:

    • If a CSS rule contains a universal selector but the requested selector does not, that rule will no longer be matched.
    • Preserves the existing functionality for rules without universal selectors.
    • The getPropVal helper remains unchanged for retrieving CSS property values, with optional whitespace stripping.

Why this is important:

Previously, rules like span[class~="one"] *:first-of-type would match selectors such as span.one div:first-of-type even if the universal selector (*) was not requested. This could cause false positives in challenges checking for allowed selectors.

With this fix:

  • Only explicitly requested universal selectors are matched.
  • Challenge validation behaves correctly.
  • Prevents unintentional matching of * rules.

Add a private method to check for universal selectors and update getStyle to handle them correctly.
@Iyeroo Iyeroo requested a review from a team as a code owner January 31, 2026 20:52
@majestic-owl448
Copy link
Contributor

is there a reason why you have both this and #562 ?

the tests should go in the same PR so that everything can be tested together

@Iyeroo
Copy link
Author

Iyeroo commented Feb 1, 2026

is there a reason why you have both this and #562 ?

the tests should go in the same PR so that everything can be tested together

Hello Maintainers,
I have merged #562 into #561 I apologise for the mistake I made waiting for review

Added tests for the universal selector functionality in CSSHelp, including mock document setup and validation of styles.
@majestic-owl448
Copy link
Contributor

your changes do not pass the tests, please test locally and make sure to solve the issues

@Iyeroo
Copy link
Author

Iyeroo commented Feb 2, 2026

Hello Maintainers,
How to run the tests locally so that it can ensure all tests pass apart from test in watch mode,Lint tests

@majestic-owl448
Copy link
Contributor

you can always find the scripts available in the package.json file

"scripts": {

@Iyeroo
Copy link
Author

Iyeroo commented Feb 3, 2026

Hello maintainers,
Please check I have run the tests locally hope it has been resolved waiting for review

Copy link
Contributor

@majestic-owl448 majestic-owl448 left a comment

Choose a reason for hiding this comment

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

@Iyeroo
Copy link
Author

Iyeroo commented Feb 3, 2026

the tests for the helper are in https://github.com/freeCodeCamp/curriculum-helpers/blob/main/packages/tests/css-helper.test.ts, add it there, do not create a new file

you can add css to test to https://github.com/freeCodeCamp/curriculum-helpers/blob/main/packages/tests/__fixtures__/curriculum-helper-css.ts like the other tests are doing

ok thankyou for correcting me I will fix the structure and revert

@Iyeroo
Copy link
Author

Iyeroo commented Feb 4, 2026

the tests for the helper are in https://github.com/freeCodeCamp/curriculum-helpers/blob/main/packages/tests/css-helper.test.ts, add it there, do not create a new file

you can add css to test to https://github.com/freeCodeCamp/curriculum-helpers/blob/main/packages/tests/__fixtures__/curriculum-helper-css.ts like the other tests are doing

Hello maintainer,
I moved file to its expected place have a check waiting for review

Copy link
Contributor

@majestic-owl448 majestic-owl448 left a comment

Choose a reason for hiding this comment

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

please make sure to test all the cases:

  • want universal selector, universal selector in css
  • want universal selector, universal selector not in css
  • don't want universal selector, universal selector in css
  • don't want universal selector, universal selector not in css

you can add new strings to packages/tests/fixtures/curriculum-helper-css.ts if you need for tests

Comment on lines 10 to 13
import * as helper from "./../helpers/lib/index";
import { CSSHelp } from "./../helpers/lib/index";
Copy link
Contributor

Choose a reason for hiding this comment

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

it is not necessary to add the new import, the first one is importing everything fron the same file

Copy link
Author

Choose a reason for hiding this comment

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

I fixed it

Comment on lines 6 to 7
import cssTestValues from "./__fixtures__/curriculum-helper-css";
import { cssString } from "./__fixtures__/curriculum-helper-css";
Copy link
Contributor

Choose a reason for hiding this comment

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

not necessary to add the second import, it's already included in the first one

Copy link
Author

Choose a reason for hiding this comment

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

I revised it

Copy link
Contributor

@majestic-owl448 majestic-owl448 Feb 5, 2026

Choose a reason for hiding this comment

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

this file should be removed please, fold everything in the already existing test file

Copy link
Author

Choose a reason for hiding this comment

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

It is been removed

Comment on lines 307 to 311
}
span[class~="one"] *:first-of-type {
background-image: linear-gradient(#f93, #f93);
border-color: #d61;
}
Copy link
Contributor

Choose a reason for hiding this comment

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

please do not change the current existing strings, the current tests are based on these and changing them could interfere, if you need new css, create a new string

Copy link
Author

Choose a reason for hiding this comment

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

Thankyou for correcting me I will ensure I follow each command.I appreciate the community support

Copy link
Author

Choose a reason for hiding this comment

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

please do not change the current existing strings, the current tests are based on these and changing them could interfere, if you need new css, create a new string
I added new strings without modifying existing ones

Copy link
Contributor

Choose a reason for hiding this comment

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

the change to the existing string is still here

Copy link
Author

Choose a reason for hiding this comment

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

have a check I fixed

@Iyeroo
Copy link
Author

Iyeroo commented Feb 6, 2026

please make sure to test all the cases:

  • want universal selector, universal selector in css
  • want universal selector, universal selector not in css
  • don't want universal selector, universal selector in css
  • don't want universal selector, universal selector not in css

you can add new strings to packages/tests/fixtures/curriculum-helper-css.ts if you need for tests

Thanks for the feedback!
I’ve updated the tests to cover all universal selector combinations and added fixtures to keep things clear and reusable. I’ve also adjusted the exports so CSSHelp is accessible as expected.

Please let me know if there’s anything else you’d like me to refine.

Comment on lines 17 to 22
const {
cssFullExample,
cssCodeWithCommentsRemoved,
cssWithUniversal,
cssWithoutUniversal,
} = cssTestValues;
Copy link
Contributor

@majestic-owl448 majestic-owl448 Feb 6, 2026

Choose a reason for hiding this comment

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

you have these but you are not using these, or you use them or you don't import them

Comment on lines 880 to 884
styleEl.textContent = `
span[class~="one"] *:first-of-type {
border-color: #d61;
}
`;
Copy link
Contributor

Choose a reason for hiding this comment

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

why do this when you have create the strings in the fixtures file?

Comment on lines 897 to 901
styleEl.textContent = `
span[class~="one"] > p:first-of-type {
color: red;
}
`;
Copy link
Contributor

Choose a reason for hiding this comment

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

again, you are importing strings from the fixtures files, so why not use them? instead you are creating this.

I will not add more comments but it's the same below

Copy link
Author

Choose a reason for hiding this comment

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

Thanks for pointing that out — you’re absolutely right.

I’ve updated the tests to exclusively use the shared fixture strings and removed all inline CSS definitions.

Appreciate your patience and the guidance.

Copy link
Contributor

@majestic-owl448 majestic-owl448 left a comment

Choose a reason for hiding this comment

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

why have you chosen file packages/tests/curriculum-helper.test.tsx to write the tests when there is packages/tests/css-helper.test.ts?

@Iyeroo
Copy link
Author

Iyeroo commented Feb 6, 2026

why have you chosen file packages/tests/curriculum-helper.test.tsx to write the tests when there is packages/tests/css-helper.test.ts?

I’ve also moved the tests to packages/tests/css-helper.test.ts so they live alongside the existing CSSHelp tests.
Appreciate your patience and the guidance.

@Iyeroo
Copy link
Author

Iyeroo commented Feb 6, 2026

Hi @majestic-owl448, just checking in to see if anything else is needed from my side. Thanks!
have a check

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

CSSHelp issue with getStyleAny

2 participants