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

Fix conditional reveal fullstop. #2270

Conversation

nataliecarey
Copy link
Contributor

@nataliecarey nataliecarey commented Jul 8, 2021

Fixes #1808

While working with an HMRC service team I discovered this bug. The service had used IDs with a full stop in them implying hierarchy. The format was roughly question1.option1. This is valid HTML but due to the selectors in the JS this was turning into the selector #questoin1.option1 which looks for an element with an ID of question1 as well as a class of option1. This change escapes the full stop to make the query #question1\.option1 which will look for an ID of question1.answer1.

In the process of fixing this it became clear that some of the examples and tests (as well as real production code) have situations where a selector of #null will be used, this occurs when some items have conditional reveal and others don't. I've checked for this case and therefore #null will no longer be used.

This is intended to be backward compatible and to only fix the bugs related to the issues we've experienced in one of our services.

@36degrees 36degrees added this to Needs review 🔍 in Design System Sprint Board via automation Jul 8, 2021
@36degrees
Copy link
Contributor

Hey @natcarey,

Thanks for raising this. Based on your explanation and the code change I think that this is the same issue as #1808. Can you take a look and see if you agree?

We've got an alternate fix which we've previously explored – switching to using document.getElementById means we can avoid needing to escape characters in the ID which could be interpreted as CSS selector syntax. This should address the issue you're seeing with full stops in IDs, but also works for IDs that include other characters like [ and ].

Unfortunately, because this changes the scope of the lookup from the $module to the document, we're treating that fix as a breaking change as there's a chance that users may be using non-unique IDs and – even though that's technically invalid – this would break conditional reveals for those users.

However, we were already planning to include that fix in 4.0 which is now likely to be the next release – at the minute we have no plans to do any more 3.x releases.

How does that sound? Can you wait for a proper fix in the 4.0 release?

@nataliecarey
Copy link
Contributor Author

There are a few answers here:

  1. I don't think this is urgent, the team are looking to replace full stops with hyphens - I'm not sure how that's going but it's likely to be fine. If you're not doing any more 3.x releases I'm imagining there'll be a 4.0 release soon - if that's the case then I'm pretty sure we can wait for it.
  2. This shouldn't change the scope - it only change the construction fo the selector, I think that's a more appropriate fix than getElementByID (which I used to prove the concept and then moved away from)
  3. This also fixes the issue where #null is used as a selector - I don't think that's likely to cause a problem in

I'm going to update the PR to include [ and ] as well confirming that other valid special characters - that way you've got an option which supports those two cases without a breaking change so it can be used if you want to include it before 4.0.

@nataliecarey nataliecarey force-pushed the fix-conditional-reveal-fullstop branch from e324147 to de275cb Compare July 8, 2021 21:16
@nataliecarey
Copy link
Contributor Author

I've updated this to deal with all special chars, to be clear this is a nonbreaking change and doesn't change the scope of the selector.

@Izabela-16 Izabela-16 moved this from Needs review 🔍 to Blocked ⛔ in Design System Sprint Board Jul 19, 2021
@trang-erskine trang-erskine added this to the v4.0.0 milestone Aug 9, 2021
@vanitabarrett vanitabarrett moved this from All work to Include in v4.0.0 in GOV.UK Frontend v4.0.0 Aug 24, 2021
@vanitabarrett vanitabarrett removed this from the v4.0.0 milestone Sep 22, 2021
@vanitabarrett
Copy link
Contributor

Not de-scoping this from v4.0.0, but removing it from the milestone as the associated issue is already linked there.

@36degrees
Copy link
Contributor

We're going to go with the approach in #2370 instead – replacing the calls to $module.querySelector with document.getElementById.

Thanks again @natcarey – really appreciate you taking the time to put this together.

@36degrees 36degrees closed this Oct 5, 2021
Design System Sprint Board automation moved this from Blocked ⛔ to Done 🏁 Oct 5, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Development

Successfully merging this pull request may close these issues.

Conditional reveals fail if ID contains characters that have a special meaning in CSS
4 participants