Check the markup validity of HTML files using the W3C validator
npm install w3c-html-validator_Check the markup validity of HTML files using the W3C validator_



w3c-html-validator takes HTML files and returns detailed validation results.
The reporter produces formatted output indented for use in build scripts and test suites.
width=800 alt=screenshot>
shell
$ npm install --save-dev w3c-html-validator
`B) Usage
$3
`
html-validator [INPUT1] [INPUT2] [INPUT3] [...]
`
Parameters:
Each parameter is a folder or file to be sent to the HTML validator.$3
Run html-validator from the "scripts" section of your package.json file.The parameters are folders and files to be validated.
Example package.json scripts:
`json
"scripts": {
"validate": "html-validator docs flyer.html",
"one-folder": "html-validator docs",
"all": "html-validator --quiet"
},
`Passing no parameters defaults to validating all HTML files in the project (skipping the
node_modules folder).
$3
Example terminal commands:
`shell
$ npm install --save-dev w3c-html-validator
$ npx html-validator docs
`
The above npx line validates all the HTML files in the docs folder.You can also install w3c-html-validator globally (
--global) and then run it anywhere directly from the terminal.$3
Command-line flags:
| Flag | Description | Value |
| ----------------- | ------------------------------------------------------------------- | ---------- |
| --check-url | W3C validation API endpoint (example: http://localhost/nu/). | string |
| --continue | Report messages but do not throw an error if validation failed. | N/A |
| --default-rules | Apply additional built-in opinionated ignore list. | N/A |
| --delay | Debounce pause in milliseconds between each file validation. | number |
| --dry-run | Bypass validation (for usage while building your CI). | N/A |
| --exclude | Comma separated list of strings to match in paths to skip. | string |
| --ignore-config | File containing strings and regexes of validation messages to skip. | string |
| --ignore | Skip validation messages containing a string or matching a regex. | string |
| --note | Place to add a comment only for humans. | string |
| --quiet | Suppress messages for successful validations. | N/A |
| --trim | Truncate validation messages to not exceed a maximum length. | number |$3
- html-validator
Validates all HTML files in the project. -
html-validator docs --exclude=build,tmp
Validates all HTML files in the docs folder except files which have "build" or "tmp" anywhere in their pathname or filename. -
html-validator docs '--ignore=Trailing slash on void elements'
Allows the ugly slashes of self-closing tags despite XHTML being a hideous scourge on the web. -
html-validator docs '--ignore=/^Duplicate ID/'
Uses a regex (regular expression) to skip all HTML validation messages that start with "Duplicate ID". -
html-validator docs '--ignore=/^Duplicate ID|^Section lacks|^Element .blockquote. not allowed/'
Uses a regex with "or" operators (|) to skip multiple HTML validation messages. -
html-validator docs --ignore-config=spec/ignore-config.txt
Similar to the pervious command but strings and regexes are stored in a configuration file (see the _Configuration File for Ignore Patterns_ section below). -
html-validator --default-rules --quiet
Skip all HTML validation messages in the built-in opinionated ignore list and also suppresses all the "pass" status messages. -
html-validator docs --delay=200
Validates all HTML files in the docs folder at a rate of 1 file per 200 ms (default is 500 ms). -
html-validator docs --trim=30 --continue
Truncates validation messages to 30 characters and does not abort CI if validation fails. -
html-validator docs --check-url=http://localhost/nu/ --delay=0
Uses a locally hosted W3C validator, such as the docker-validator-w3c server.> [!NOTE]
> _Single quotes in commands are normalized so they work cross-platform and avoid the errors often encountered on Microsoft Windows._
$3
The optional --ignore-config=FILENAME flag specifies a configuration file with one string or regex per line.
HTML validation messages containing any of the strings or matching any of the regexes will be skipped.
Empty lines and lines starting with a hash sign (#) are treated as comments and do nothing.Example configuration file with 3 regexes:
`config
Ignore Config for w3c-html-validator
/^Duplicate ID/
/^Element .blockquote. not allowed/
/^Element .style. not allowed/
`
The caret (^) regex operator says to match from the beginning of the validation message.
The dot (.) regex operator says to match any one character, which is a handy way to avoid typing the special curly quote characters in some of the validation messages.$3
The optional --default-rules flag causes HTML validation messages to be skipped if they are on the opinionated pre-defined list of unhelpful messages. Default ignore list:
| Pattern | Level | Explanation |
| ------------------------ | ----------- | ----------- |
|
with computed level | error | It's ridiculous that adding an