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

Lint compiled HTML #2569

Merged
merged 21 commits into from
Feb 22, 2023
Merged

Lint compiled HTML #2569

merged 21 commits into from
Feb 22, 2023

Conversation

domoscargin
Copy link
Contributor

@domoscargin domoscargin commented Jan 31, 2023

We currently have no built-in way of ensuring our compiled HTML changes in the way we expect, and doesn't break.

This PR adds HTML linting using html-validate. There are quite a few commits, but they're all really small, promise!

There are some issues to consider:

@netlify
Copy link

netlify bot commented Jan 31, 2023

You can preview this change here:

Name Link
🔨 Latest commit ffd0391
🔍 Latest deploy log https://app.netlify.com/sites/govuk-design-system-preview/deploys/63f6270dfceebc0008281b0f
😎 Deploy Preview https://deploy-preview-2569--govuk-design-system-preview.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site settings.

@querkmachine
Copy link
Member

querkmachine commented Jan 31, 2023

Do you have a sample of what problems it's finding? Nearly 2000 issues seems like an excessive number!

Also I imagine there are examples of invalid code where things are wrong-on-purpose because they have better outcomes for the end user (e.g. our use of aria-attributes on conditional checkboxes/radios). We probably want to treat those exceptionally.

@domoscargin
Copy link
Contributor Author

domoscargin commented Jan 31, 2023

I swiped the config for this from the govuk-frontend component tests, so hopefully most of the special cases are already accounted for, but I haven't delved too deeply yet.

Rules broken:

https://html-validate.org/rules/void-style.html
https://html-validate.org/rules/wcag/h30.html
https://html-validate.org/rules/aria-label-misuse.html
https://html-validate.org/rules/attr-quotes.html
https://html-validate.org/rules/no-implicit-close.html
https://html-validate.org/rules/element-permitted-content.html
https://html-validate.org/rules/attr-case.html
https://html-validate.org/rules/no-deprecated-attr.html
https://html-validate.org/rules/no-dup-id.html
https://html-validate.org/rules/attribute-allowed-values.html
https://html-validate.org/rules/wcag/h32.html
https://html-validate.org/rules/long-title.html
https://html-validate.org/rules/attr-delimiter.html
https://html-validate.org/rules/wcag/h63.html
https://html-validate.org/rules/form-dup-name.html
https://html-validate.org/rules/close-order.html

There's a lot of what I'm pretty sure is noise:

  • aria-label-misuse because we've got aria-labels on our app-navigation__subnav ul elements, which I assume is a deliberate choice.
  • void-style errors mostly seem annoyed about us self-closing <link> elements which feels heavy-handed.
  • wcag/h30 on our top anchor: <a id="top"></a>

Probably legit errors include:

  • Most of the attr-quotes look like they could be switched to double quotes with no issue.
  • no-implicit-close: there's a few unclosed <p> tags floating about: <p>Share your experience, research and skills to help us reiterate the Task list pages pattern into a component.<p>

🤷

  • element-permitted-content: Doesn't like the "experimental" div contained in the h1 on the Resources and Tools page
  • The frameBorder attribute is the main culprit: causing both attr-case and no-deprecated-attr errors (about 530 errors in total)

Etc, etc.

So I'm assuming 90% of the errors could (safely) be ignored, but haven't really had a close look.

@colinrotherham
Copy link
Contributor

@domoscargin I think this it great 🙌

Gives us more confidence that Markdown/Nunjucks is outputting what we want

@domoscargin
Copy link
Contributor Author

I've created an issue to deal with some invalid HTML here: #2606

Down to 65 errors to check.

package.json Outdated Show resolved Hide resolved
@domoscargin
Copy link
Contributor Author

domoscargin commented Feb 17, 2023

I've added another issue to deal with nested form elements here: #2609

In addition, #2260 is causing form-dup-name errors. This wouldn't happen if the rule checked for unique values, as suggested by @colinrotherham: alphagov/govuk-frontend#3270 (review), but I feel like at a certain point, the rule becomes a bit toothless if we're basically ignoring all its results, so better to fix the cause.

Once #2606, #2609 and #2260 are fixed, we can remove some of the changes made here.

@domoscargin domoscargin marked this pull request as ready for review February 20, 2023 10:18
@domoscargin domoscargin changed the title [WIP] Add a validate task to validate compiled HTML Lint compiled HTML Feb 20, 2023
@domoscargin domoscargin requested a review from a team as a code owner February 20, 2023 10:32
@domoscargin domoscargin force-pushed the bk-html-validate branch 5 times, most recently from e4573d2 to 958e5a9 Compare February 20, 2023 10:49
.htmlvalidateignore Outdated Show resolved Hide resolved
@@ -11,7 +11,7 @@
<button hidden class="app-navigation__button js-app-navigation__button">
{{ item.label }}
</button>

<!-- [html-validate-disable-next aria-label-misuse --- inline fix for html-validate rule, rather than disable it entirely] -->
Copy link
Contributor

Choose a reason for hiding this comment

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

Have we got an issue for this one?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It was a deliberate choice for accessibility: 4ff06e4

@36degrees dya reckon I should make an issue to review this?

Copy link
Contributor

Choose a reason for hiding this comment

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

I'm not sure where the list of 'allowed elements' on https://html-validate.org/rules/aria-label-misuse.html comes from… according to https://www.w3.org/TR/html-aria/#el-li li is not listed as 'Naming Prohibited' and the listitem role inherits the aria-label property… 🤔

The rationale in the linked commit makes sense to me, but I think given it's been flagged it might be worth asking @davidc-gds to review it just in case? Whether or not we need an issue for that I'm unsure – maybe ask David?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Interesting. Might dig about a bit in the html-validate source code and figure out if it's worth filing a bug. Will speak to David as well.

Copy link
Contributor Author

@domoscargin domoscargin Feb 23, 2023

Choose a reason for hiding this comment

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

Ah, so the <ul> text reads:

Global aria-* attributes and any aria-* attributes applicable to the allowed roles.

Which is sort of ambiguous, but I think basically means we need to apply a role to the <ul> element - if we do that, then html-validate is happy.

By default, the <ul> has the list role, which is:

allowed, but NOT RECOMMENDED.

I guess the menu role or navigation feels most appropriate here? Though it's already contained within a nav.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Basically, I think this could do with an issue 😂

I'll make one

Copy link
Contributor

Choose a reason for hiding this comment

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

Aah, of course – not sure why I was looking at <li>!

By default, the <ul> has the list role, which is:

allowed, but NOT RECOMMENDED.

I believe when they say it's 'allowed but not recommended' they mean it's not recommended to set this role explicitly – if you take a look at the headings for the table it says "Implicit ARIA semantics (explicitly assigning these in markup is NOT RECOMMENDED)".

I guess the menu role or navigation feels most appropriate here? Though it's already contained within a nav.

We should avoid the navigation role for exactly that reason – it's already in a nav region. The menu role is designed for interfaces that mimic desktop app style interfaces where you have menus with lists of items, like in a 'menu bar' and wouldn't be appropriate here.

list still feels like the appropriate role – I think if we changed it, we'd lose the information like 'List, 10 items' that screen reader users get which helps them understand how many links there are, for example.

Still worth having an issue to explore further, but I think either 1) having an aria-label is fine on a <ul> and is useful for users or 2) we should just remove the aria-label. I don't think changing the role makes sense.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've added an issue and linked to this discussion.

#2624

@domoscargin domoscargin force-pushed the bk-html-validate branch 2 times, most recently from 0dad696 to 8d9c836 Compare February 22, 2023 11:43
.github/workflows/test.yaml Outdated Show resolved Hide resolved
@domoscargin domoscargin force-pushed the bk-html-validate branch 2 times, most recently from f01d5af to f8447a6 Compare February 22, 2023 13:50
The `frameborder` attribute is deprecated, but we use it to make things look a bit better on IE8, so for now we can tell html-validate to treat it as undeprecated.
The self-closing / has no effect on the start tags of void elements in HTML5.

For this reason, we should standardise on omitting them.

https://html.spec.whatwg.org/dev/syntax.html#start-tags
#1122
This technique relates to providing descriptive link text to anchor elements.

Our anchor for the Back To Top button is flagged, but automated tests don't flag it, so I think we can probably ignore this.
We use double quotes for strings in our Nunjucks macros, and occasionally this means we need to use single quotes for the text content within them.
There doesn't appear to be any easy way to hook into the rule via config, and it's worth having to catch cases where we DIDN'T intend it, but in our case we've added it purposefully:

#1038
The first govukCheckboxes (named "checkbox") gets items id'd checkbox, checkbox-2, checkbox-3.

The second govukCheckboxes (named "checkbox-2" gets items id'd checkbox-2, checkbox-2-2, checkbox-2-3.

So we get a duplicate "checkbox-2" id unless we change the names.
@domoscargin domoscargin force-pushed the bk-html-validate branch 2 times, most recently from 8097466 to e6b9853 Compare February 22, 2023 14:05
Previously, we didn't actually need to build, since the tests didn't use the compiled files.

Now, the HTML linting does require a build, but that's handled within `npm test`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Development

Successfully merging this pull request may close these issues.

4 participants