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 generating example IDs #813

Merged
merged 4 commits into from
Mar 8, 2019
Merged

Fix generating example IDs #813

merged 4 commits into from
Mar 8, 2019

Conversation

hannalaakso
Copy link
Member

@hannalaakso hannalaakso commented Mar 1, 2019

This reverts a hotfix #809 that removed the changes, in favour of a fix for generating example IDs for #784.

Generating example IDs from example titles can be brittle if there are unexpected characters in titles. This goes back to using exampleTitle to generate the IDs with slugger and uses the solution from #620 to fix duplicate IDs that were flagged by Axe. Alternatively we could keep the kebab casing filter and try to make sure that the Regex in it can cope with foreign characters but it seems kind of liable to breaking.

If someone adds the same example to a page several times, they'll need to specify titleSuffix for the example macro as example parameter from the macro won't be unique but this issue existed also with the previous solution. We could think about testing all the pages to catch this.

Best just to review these commits directly:

This reverts commit dc6c2c4.
@govuk-design-system-ci
Copy link
Collaborator

govuk-design-system-ci commented Mar 1, 2019

You can preview this change here:

Built with commit ba39de7

https://deploy-preview-813--govuk-design-system-preview.netlify.com

@NickColley
Copy link
Contributor

NickColley commented Mar 1, 2019

What was the issue with the previous version?

This version relies on the 'titleSuffix' being a single word so will break as well I think...

@NickColley
Copy link
Contributor

Looks like it was an issue with titles like https://design-system.service.gov.uk/styles/typography/captions/index.html since they have endashes

@hannalaakso
Copy link
Member Author

Yeah the dashes 👍 I wrote up an incident report.

That's a good point about the titleSuffix. I guess I was naively relying on the convention of them being non spaced along with the other parameters we pass to the example macros but that's not robust enough.

I've also just realised that I meant to add some error catching to the JS that broke this so need to do that.

@NickColley
Copy link
Contributor

Here's a change to the tests that would have caught the JavaScript errors: 517692c

Here's suggestion to change the filter: 2d79945

Might be safer to do another approach though...

hannalaakso and others added 2 commits March 4, 2019 15:40
This reverts a hotfix #809
that removed the changes in favour of a proper fix for
#620

Generating example IDs from example titles can be brittle if there
are unexpected characters in titles. Use slugger for more robust converting of
exampleTitle to exampleId. This also uses the solution from #620 to fix
duplicate IDs by adding an extra parameter "titleSuffix" to examples if
necessary.

Co-authored-by: Nick Colley <[email protected]>
@hannalaakso hannalaakso force-pushed the fix-axe-tests-pr branch 2 times, most recently from fd5700a to 48fcf34 Compare March 4, 2019 15:52
@hannalaakso
Copy link
Member Author

Thanks a lot @NickColley I think those look like sensible suggestions 👍 I've also added some error catching to tabs.js that would have caught these errors and shown a console error instead.

try {
$tabContainer = this.$module.querySelector($currentToggler.hash)
} catch (exception) {
if ((typeof console !== 'undefined' || typeof console.log !== 'undefined')) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Does this swallow the exception?

Potentially this would make it less likely for us to be able to catch bugs since it would not break the build?

If we think it's useful to have a more helpful error message and that is true, we could consider throwing a custom error here rather than just logging it to the console?

Copy link
Member Author

Choose a reason for hiding this comment

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

A good catch, thanks @NickColley

// make current active
$tabContainer.classList.remove(tabContainerHiddenClass)
$tabContainer.setAttribute('aria-hidden', 'false')
if ($tabContainer) {
Copy link
Contributor

Choose a reason for hiding this comment

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

To avoid unnecessary nesting you could consider returning early here, although I haven't seen the rest of the code so might not be possible :)

Copy link
Member Author

Choose a reason for hiding this comment

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

Hmmm my reasoning was that it's often better to stick to a single return point to make the code more readable. But I think in this case the readability increased with your equally valid suggestion so have changed it. Thanks 🙂

{% endif %}
{% set exampleTitle = exampleTitle + " example" %}

{% if params.id %}
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think there are any examples that pass through 'id' now so this may be unnecessary.

Copy link
Member Author

Choose a reason for hiding this comment

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

id is still used by at least header and footer examples.

Copy link
Contributor

Choose a reason for hiding this comment

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

I wonder if it's necessary though given that we can generate a unique id from the unique title?

Not blocking just thought this could simplify the example partial a bit

@hannalaakso hannalaakso merged commit c9a5b9d into master Mar 8, 2019
@hannalaakso hannalaakso deleted the fix-axe-tests-pr branch March 8, 2019 10:50
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.

3 participants