Skip to content

HTML

gspot validates HTML files with html-validate, its own check reports inline scripts and event handler attributes, and Prettier formats them:

Check Stage Level What it does
html/html-validate commit basic Validates HTML files for nesting, required attributes, and text alternatives.
html/inline-scripts commit basic Reports inline scripts, event handler attributes, and executable URLs in HTML files.

The site configuration also turns on html, and checks the pages that your build writes, as Static sites describes.

html/html-validate starts from the html-validate:recommended preset, which checks nesting, required attributes, and text alternatives. For example, an image without alt text and a button without a type attribute get these findings:

failed html/html-validate 2 findings
index.html:9:14 wcag/h37 <img> is missing required "alt" attribute
index.html:10:14 no-implicit-button-type <button> is missing recommended "type" attribute

gspot also turns on require-sri for scripts and stylesheets from another origin, such as a content delivery network. Add an integrity hash to each one, so the browser refuses a changed file.

gspot turns off the style rules that Prettier decides, such as attr-quotes and void-style. At level basic, it also turns off no-inline-style, prefer-button, and prefer-native-element. Level all runs them, so a style attribute or a div with role="button" gets a finding.

A strict content security policy blocks event handlers and any inline script without a nonce or a hash, so html/inline-scripts reports three kinds of code in the HTML:

  • inline-script: a <script> element whose body holds code that runs. A script with a src attribute, and a data block such as application/ld+json, pass.
  • handler-attribute: an event handler attribute, such as onclick.
  • script-link: a javascript: or vbscript: URL, or a data: URL that holds a script or a page.

Move the code into a script file, and attach each handler with addEventListener.

To turn off one html-validate rule, ignore it with a reason, or allow one element with a comment above it, such as <!-- [html-validate-disable-next no-inline-style -- reason: the email client strips classes] -->:

gspot ignore html/html-validate --rule no-inline-style --reason "The email template needs inline styles."