w3c-html-validator 1.9.0 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -52,11 +52,12 @@ Command-line flags:
52
52
  | Flag | Description | Value |
53
53
  | ----------------- | ------------------------------------------------------------------- | ---------- |
54
54
  | `--continue` | Report messages but do not throw an error if validation failed. | N/A |
55
+ | `--default-rules` | Apply additional built-in opinionated ignore list. | N/A |
55
56
  | `--delay` | Debounce pause in milliseconds between each file validation. | **number** |
56
57
  | `--dry-run` | Bypass validation (for usage while building your CI). | N/A |
57
58
  | `--exclude` | Comma separated list of strings to match in paths to skip. | **string** |
58
- | `--ignore` | Skip validation messages containing a string or matching a regex. | **string** |
59
59
  | `--ignore-config` | File containing strings and regexes of validation messages to skip. | **string** |
60
+ | `--ignore` | Skip validation messages containing a string or matching a regex. | **string** |
60
61
  | `--note` | Place to add a comment only for humans. | **string** |
61
62
  | `--quiet` | Suppress messages for successful validations. | N/A |
62
63
  | `--trim` | Truncate validation messages to not exceed a maximum length. | **number** |
@@ -81,8 +82,8 @@ Examples:
81
82
  - `html-validator docs --ignore-config=spec/ignore-config.txt`<br>
82
83
  Similar to the pervious command but strings and regexes are stored in a configuration file (see the _Configuration File for Ignore Patterns_ section below).
83
84
 
84
- - `html-validator --quiet`<br>
85
- Suppresses all the "pass" status messages.
85
+ - `html-validator --default-rules --quiet`<br>
86
+ Skip all HTML validation messages in the built-in opinionated ignore list and also suppresses all the "pass" status messages.
86
87
 
87
88
  - `html-validator docs --delay=200`<br>
88
89
  Validates all HTML files in the "docs" folder at a rate of 1 file per 200 ms (default is 500 ms).
@@ -108,6 +109,16 @@ Example configuration file with 3 regexes:
108
109
  The caret (`^`) regex operator says to match from the beginning of the validation message.&nbsp;
109
110
  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.
110
111
 
112
+ ### 6. Default Ignore List
113
+ The optional `--default-rules` flag causes HTML validation messages to be skipped if they are on the opinionated pre-defined list of unhelpful messages.&nbsp;
114
+
115
+ Default ignore list:
116
+ | Pattern | Level | Explanation |
117
+ | ------------------------ | ----------- | ----------- |
118
+ | `Section lacks heading.` | **warning** | Rule is sensible for traditional print publishing but absurd for modern UI components not burning in nested `<div>` hell. |
119
+
120
+ If there is an additional W3C validation message you think is ridiculous, open an issue with a note explaining why the message should be ignored.&nbsp;
121
+
111
122
  ## D) Application Code and Testing Frameworks
112
123
  In addition to the CLI interface, the **w3c-html-validator** package can also be imported and called directly in ESM and TypeScript projects.
113
124
 
@@ -137,6 +148,7 @@ $ node examples.js
137
148
  | Name (key) | Type | Default | Description |
138
149
  | :--------------- | :---------------------- | :------------------------------- | :------------------------------------------------------ |
139
150
  | `checkUrl` | **string** | `'https://validator.w3.org/nu/'` | W3C validation API endpoint. |
151
+ | `defaultRules` | **boolean** | `false` | Apply additional built-in opinionated ignore list. |
140
152
  | `dryRun` | **boolean** | `false` | Bypass validation (for usage while building your CI). |
141
153
  | `filename` | **string** | `null` | HTML file to validate. |
142
154
  | `html` | **string** | `null` | HTML string to validate. |
package/bin/cli.js CHANGED
@@ -29,21 +29,24 @@ import fs from 'fs';
29
29
  import slash from 'slash';
30
30
 
31
31
  // Parameters and flags
32
- const validFlags =
33
- ['continue', 'delay', 'dry-run', 'exclude', 'ignore', 'ignore-config', 'note', 'quiet', 'trim'];
32
+ const validFlags = ['continue', 'default-rules', 'delay', 'dry-run', 'exclude',
33
+ 'ignore', 'ignore-config', 'note', 'quiet', 'trim'];
34
34
  const cli = cliArgvUtil.parse(validFlags);
35
35
  const files = cli.params.length ? cli.params.map(cliArgvUtil.cleanPath) : ['.'];
36
+ const excludeList = cli.flagMap.exclude?.split(',') ?? [];
36
37
  const ignore = cli.flagMap.ignore ?? null;
37
38
  const ignoreConfig = cli.flagMap.ignoreConfig ?? null;
39
+ const defaultRules = cli.flagOn.defaultRules;
38
40
  const delay = Number(cli.flagMap.delay) || 500; //default half second debounce pause
39
41
  const trim = Number(cli.flagMap.trim) || null;
40
- const dryRunMode = cli.flagOn.dryRun || process.env.w3cHtmlValidator === 'dry-run'; //bash: export w3cHtmlValidator=dry-run
42
+ const dryRun = cli.flagOn.dryRun || process.env.w3cHtmlValidator === 'dry-run'; //bash: export w3cHtmlValidator=dry-run
41
43
 
42
44
  const getFilenames = () => {
43
45
  const readFilenames = (file) => globSync(file, { ignore: '**/node_modules/**/*' }).map(slash);
44
46
  const readHtmlFiles = (folder) => readFilenames(folder + '/**/*.html');
45
47
  const addHtml = (file) => fs.lstatSync(file).isDirectory() ? readHtmlFiles(file) : file;
46
- return files.map(readFilenames).flat().map(addHtml).flat().sort();
48
+ const keep = (file) => excludeList.every(exclude => !file.includes(exclude));
49
+ return files.map(readFilenames).flat().map(addHtml).flat().filter(keep).sort();
47
50
  };
48
51
  const filenames = getFilenames();
49
52
  const error =
@@ -53,7 +56,7 @@ const error =
53
56
  null;
54
57
  if (error)
55
58
  throw new Error('[w3c-html-validator] ' + error);
56
- if (dryRunMode)
59
+ if (dryRun)
57
60
  w3cHtmlValidator.dryRunNotice();
58
61
  if (filenames.length > 1 && !cli.flagOn.quiet)
59
62
  w3cHtmlValidator.summary(filenames.length);
@@ -72,8 +75,8 @@ const getIgnoreMessages = () => {
72
75
  const isRegex = /^\/.*\/$/; //starts and ends with a slash indicating it's a regex
73
76
  return rawLines.map(line => isRegex.test(line) ? new RegExp(line.slice(1, -1)) : line);
74
77
  };
75
- const baseOptions = { ignoreMessages: getIgnoreMessages(), dryRun: dryRunMode };
76
- const options = (filename) => ({ filename: filename, ...baseOptions });
78
+ const ignoreMessages = getIgnoreMessages();
79
+ const options = (filename) => ({ filename, ignoreMessages, defaultRules, dryRun });
77
80
  const handleResults = (results) => w3cHtmlValidator.reporter(results, reporterOptions);
78
81
  const getReport = (filename) => w3cHtmlValidator.validate(options(filename)).then(handleResults);
79
82
  const processFile = (filename, i) => globalThis.setTimeout(() => getReport(filename), i * delay);
@@ -1,4 +1,4 @@
1
- //! w3c-html-validator v1.9.0 ~~ https://github.com/center-key/w3c-html-validator ~~ MIT License
1
+ //! w3c-html-validator v2.0.0 ~~ https://github.com/center-key/w3c-html-validator ~~ MIT License
2
2
 
3
3
  export type ValidatorSettings = {
4
4
  html: string;
@@ -6,10 +6,12 @@ export type ValidatorSettings = {
6
6
  website: string;
7
7
  checkUrl: string;
8
8
  ignoreLevel: 'info' | 'warning';
9
- ignoreMessages: (string | RegExp)[];
9
+ ignoreMessages: ValidatorIgnorePattern[];
10
+ defaultRules: boolean;
10
11
  output: ValidatorResultsOutput;
11
12
  dryRun: boolean;
12
13
  };
14
+ export type ValidatorIgnorePattern = string | RegExp;
13
15
  export type ValidatorResultsMessage = {
14
16
  type: 'info' | 'error' | 'non-document-error' | 'network-error';
15
17
  subType?: 'warning' | 'fatal' | 'io' | 'schema' | 'internal';
@@ -45,6 +47,7 @@ export type ReporterSettings = {
45
47
  };
46
48
  declare const w3cHtmlValidator: {
47
49
  version: string;
50
+ defaultIgnoreList: string[];
48
51
  validate(options: Partial<ValidatorSettings>): Promise<ValidatorResults>;
49
52
  dryRunNotice(): void;
50
53
  summary(numFiles: number): void;
@@ -1,4 +1,4 @@
1
- //! w3c-html-validator v1.9.0 ~~ https://github.com/center-key/w3c-html-validator ~~ MIT License
1
+ //! w3c-html-validator v2.0.0 ~~ https://github.com/center-key/w3c-html-validator ~~ MIT License
2
2
 
3
3
  import chalk from 'chalk';
4
4
  import fs from 'fs';
@@ -6,10 +6,14 @@ import log from 'fancy-log';
6
6
  import request from 'superagent';
7
7
  import slash from 'slash';
8
8
  const w3cHtmlValidator = {
9
- version: '1.9.0',
9
+ version: '2.0.0',
10
+ defaultIgnoreList: [
11
+ 'Section lacks heading.'
12
+ ],
10
13
  validate(options) {
11
14
  const defaults = {
12
15
  checkUrl: 'https://validator.w3.org/nu/',
16
+ defaultRules: false,
13
17
  dryRun: false,
14
18
  ignoreLevel: null,
15
19
  ignoreMessages: [],
@@ -24,7 +28,8 @@ const w3cHtmlValidator = {
24
28
  throw new Error('[w3c-html-validator] Option "output" must be "json" or "html".');
25
29
  const filename = settings.filename ? slash(settings.filename) : null;
26
30
  const mode = settings.html ? 'html' : filename ? 'filename' : 'website';
27
- const readFile = (filename) => fs.readFileSync(filename, 'utf-8').replace(/\r/g, '');
31
+ const unixify = (text) => text.replace(/\r/g, '');
32
+ const readFile = (filename) => unixify(fs.readFileSync(filename, 'utf-8'));
28
33
  const inputHtml = settings.html ?? (filename ? readFile(filename) : null);
29
34
  const makePostRequest = () => request.post(settings.checkUrl)
30
35
  .set('Content-Type', 'text/html; encoding=utf-8')
@@ -32,7 +37,8 @@ const w3cHtmlValidator = {
32
37
  const makeGetRequest = () => request.get(settings.checkUrl)
33
38
  .query({ doc: settings.website });
34
39
  const w3cRequest = inputHtml ? makePostRequest() : makeGetRequest();
35
- w3cRequest.set('User-Agent', 'W3C HTML Validator ~ github.com/center-key/w3c-html-validator');
40
+ const userAgent = 'W3C HTML Validator ~ github.com/center-key/w3c-html-validator';
41
+ w3cRequest.set('User-Agent', userAgent);
36
42
  w3cRequest.query({ out: settings.output });
37
43
  const json = settings.output === 'json';
38
44
  const success = '<p class="success">';
@@ -44,8 +50,11 @@ const w3cHtmlValidator = {
44
50
  const filterMessages = (response) => {
45
51
  const aboveInfo = (subType) => settings.ignoreLevel === 'info' && !!subType;
46
52
  const aboveIgnoreLevel = (message) => !settings.ignoreLevel || message.type !== 'info' || aboveInfo(message.subType);
47
- const matchesSkipPattern = (title) => settings.ignoreMessages.some(pattern => typeof pattern === 'string' ? title.includes(pattern) : pattern.test(title));
48
- const isImportant = (message) => aboveIgnoreLevel(message) && !matchesSkipPattern(message.message);
53
+ const defaultList = settings.defaultRules ? w3cHtmlValidator.defaultIgnoreList : [];
54
+ const ignoreList = [...settings.ignoreMessages, ...defaultList];
55
+ const tester = (title) => (pattern) => typeof pattern === 'string' ? title.includes(pattern) : pattern.test(title);
56
+ const skipMatchFound = (title) => ignoreList.some(tester(title));
57
+ const isImportant = (message) => aboveIgnoreLevel(message) && !skipMatchFound(message.message);
49
58
  if (json)
50
59
  response.body.messages = response.body.messages?.filter(isImportant) ?? [];
51
60
  return response;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "w3c-html-validator",
3
- "version": "1.9.0",
3
+ "version": "2.0.0",
4
4
  "description": "Check the markup validity of HTML files using the W3C validator",
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -53,11 +53,11 @@
53
53
  },
54
54
  "scripts": {
55
55
  "pretest": "run-scripts clean lint build",
56
- "test": "mocha spec/*.spec.js --timeout 7000",
56
+ "test": "mocha spec --timeout 7000",
57
57
  "examples": "node examples.js"
58
58
  },
59
59
  "dependencies": {
60
- "chalk": "~5.4",
60
+ "chalk": "~5.6",
61
61
  "cli-argv-util": "~1.3",
62
62
  "fancy-log": "~2.0",
63
63
  "glob": "~11.0",
@@ -65,21 +65,20 @@
65
65
  "superagent": "~10.2"
66
66
  },
67
67
  "devDependencies": {
68
- "@eslint/js": "~9.30",
68
+ "@eslint/js": "~9.38",
69
69
  "@types/fancy-log": "~2.0",
70
- "@types/node": "~24.0",
70
+ "@types/node": "~24.9",
71
71
  "@types/superagent": "~8.1",
72
- "add-dist-header": "~1.4",
72
+ "add-dist-header": "~1.5",
73
73
  "assert-deep-strict-equal": "~1.2",
74
- "copy-file-util": "~1.2",
74
+ "copy-file-util": "~1.3",
75
75
  "copy-folder-util": "~1.1",
76
- "eslint": "~9.30",
76
+ "eslint": "~9.38",
77
77
  "jshint": "~2.13",
78
- "merge-stream": "~2.0",
79
78
  "mocha": "~11.7",
80
79
  "rimraf": "~6.0",
81
80
  "run-scripts-util": "~1.3",
82
- "typescript": "~5.8",
83
- "typescript-eslint": "~8.36"
81
+ "typescript": "~5.9",
82
+ "typescript-eslint": "~8.46"
84
83
  }
85
84
  }