w3c-html-validator 1.0.2 → 1.1.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 +9 -5
- package/dist/w3c-html-validator.d.ts +2 -2
- package/dist/w3c-html-validator.js +38 -29
- package/dist/w3c-html-validator.umd.cjs +38 -29
- package/package.json +14 -14
package/README.md
CHANGED
|
@@ -42,7 +42,7 @@ $ node examples.js
|
|
|
42
42
|
width=800 alt=screenshot>
|
|
43
43
|
|
|
44
44
|
## 3) Options
|
|
45
|
-
### validate()
|
|
45
|
+
### w3cHtmlValidator.validate(options)
|
|
46
46
|
| Name (key) | Type | Default | Description |
|
|
47
47
|
| :--------------- | :---------------------- | :------------------------------- | :------------------------------------------------------------------- |
|
|
48
48
|
| `html` | **string** | `null` | HTML string to validate. |
|
|
@@ -56,7 +56,7 @@ width=800 alt=screenshot>
|
|
|
56
56
|
*The `ignoreMessages` and `ignoreLevel` options only work for `'json'` output.
|
|
57
57
|
Option value `'warning'` also skips `'info'`.
|
|
58
58
|
|
|
59
|
-
### reporter()
|
|
59
|
+
### w3cHtmlValidator.reporter(options)
|
|
60
60
|
| Name (key) | Type | Default | Description |
|
|
61
61
|
| :-------------- | :--------- | :------ | :------------------------------------------------------------- |
|
|
62
62
|
| `maxMessageLen` | **number** | `null` | Trim validation messages to not exceed a maximum length. |
|
|
@@ -119,6 +119,10 @@ or as an npm script in **package.json**:
|
|
|
119
119
|
Passing no parameters defaults to validating all HTML files in the projects (skipping the
|
|
120
120
|
**node_modules** folder).
|
|
121
121
|
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
122
|
+
<br>
|
|
123
|
+
|
|
124
|
+
---
|
|
125
|
+
Feel free to submit questions at:<br>
|
|
126
|
+
[github.com/center-key/w3c-html-validator/issues](https://github.com/center-key/w3c-html-validator/issues)
|
|
127
|
+
|
|
128
|
+
[MIT License](LICENSE.txt)
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
//! w3c-html-validator v1.0
|
|
1
|
+
//! w3c-html-validator v1.1.0 ~~ https://github.com/center-key/w3c-html-validator ~~ MIT License
|
|
2
2
|
|
|
3
3
|
export declare type ValidatorOptions = {
|
|
4
4
|
html?: string;
|
|
@@ -42,6 +42,6 @@ export declare type ReporterOptions = {
|
|
|
42
42
|
declare const w3cHtmlValidator: {
|
|
43
43
|
version: string;
|
|
44
44
|
validate(options: ValidatorOptions): Promise<ValidatorResults>;
|
|
45
|
-
reporter(results: ValidatorResults, options?: ReporterOptions
|
|
45
|
+
reporter(results: ValidatorResults, options?: ReporterOptions): ValidatorResults;
|
|
46
46
|
};
|
|
47
47
|
export { w3cHtmlValidator };
|
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
//! w3c-html-validator v1.0
|
|
1
|
+
//! w3c-html-validator v1.1.0 ~~ https://github.com/center-key/w3c-html-validator ~~ MIT License
|
|
2
2
|
|
|
3
3
|
import { readFileSync } from 'fs';
|
|
4
4
|
import chalk from 'chalk';
|
|
5
5
|
import log from 'fancy-log';
|
|
6
6
|
import request from 'superagent';
|
|
7
7
|
const w3cHtmlValidator = {
|
|
8
|
-
version: '1.0
|
|
8
|
+
version: '1.1.0',
|
|
9
9
|
validate(options) {
|
|
10
10
|
const defaults = {
|
|
11
11
|
checkUrl: 'https://validator.w3.org/nu/',
|
|
@@ -13,7 +13,7 @@ const w3cHtmlValidator = {
|
|
|
13
13
|
ignoreMessages: null,
|
|
14
14
|
output: 'json',
|
|
15
15
|
};
|
|
16
|
-
const settings = {
|
|
16
|
+
const settings = Object.assign(Object.assign({}, defaults), options);
|
|
17
17
|
if (!settings.html && !settings.filename && !settings.website)
|
|
18
18
|
throw Error('[w3c-html-validator] Must specify the "html", "filename", or "website" option.');
|
|
19
19
|
if (![null, 'info', 'warning'].includes(settings.ignoreLevel))
|
|
@@ -34,66 +34,75 @@ const w3cHtmlValidator = {
|
|
|
34
34
|
const json = settings.output === 'json';
|
|
35
35
|
const success = '<p class="success">';
|
|
36
36
|
const titleLookup = {
|
|
37
|
-
html: 'HTML String (characters: ' + inputHtml
|
|
37
|
+
html: 'HTML String (characters: ' + (inputHtml === null || inputHtml === void 0 ? void 0 : inputHtml.length) + ')',
|
|
38
38
|
filename: settings.filename,
|
|
39
39
|
website: settings.website,
|
|
40
40
|
};
|
|
41
41
|
const filterMessages = (response) => {
|
|
42
|
+
var _a, _b;
|
|
42
43
|
const aboveInfo = (subType) => settings.ignoreLevel === 'info' && !!subType;
|
|
43
44
|
const aboveIgnoreLevel = (message) => !settings.ignoreLevel || message.type !== 'info' || aboveInfo(message.subType);
|
|
44
45
|
const skipSubstr = (title) => typeof settings.ignoreMessages === 'string' && title.includes(settings.ignoreMessages);
|
|
45
|
-
const skipRegEx = (title) =>
|
|
46
|
-
|
|
46
|
+
const skipRegEx = (title) => {
|
|
47
|
+
var _a;
|
|
48
|
+
return ((_a = settings.ignoreMessages) === null || _a === void 0 ? void 0 : _a.constructor.name) === 'RegExp' &&
|
|
49
|
+
settings.ignoreMessages.test(title);
|
|
50
|
+
};
|
|
47
51
|
const isImportant = (message) => aboveIgnoreLevel(message) && !skipSubstr(message.message) && !skipRegEx(message.message);
|
|
48
52
|
if (json)
|
|
49
|
-
response.body.messages = response.body.messages
|
|
53
|
+
response.body.messages = (_b = (_a = response.body.messages) === null || _a === void 0 ? void 0 : _a.filter(isImportant)) !== null && _b !== void 0 ? _b : [];
|
|
50
54
|
return response;
|
|
51
55
|
};
|
|
52
|
-
const toValidatorResults = (response) =>
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
56
|
+
const toValidatorResults = (response) => {
|
|
57
|
+
var _a;
|
|
58
|
+
return ({
|
|
59
|
+
validates: json ? !response.body.messages.length : !!((_a = response.text) === null || _a === void 0 ? void 0 : _a.includes(success)),
|
|
60
|
+
mode: mode,
|
|
61
|
+
title: titleLookup[mode],
|
|
62
|
+
html: inputHtml,
|
|
63
|
+
filename: settings.filename || null,
|
|
64
|
+
website: settings.website || null,
|
|
65
|
+
output: settings.output,
|
|
66
|
+
status: response.statusCode || -1,
|
|
67
|
+
messages: json ? response.body.messages : null,
|
|
68
|
+
display: json ? null : response.text,
|
|
69
|
+
});
|
|
70
|
+
};
|
|
64
71
|
const handleError = (reason) => {
|
|
65
72
|
const response = reason['response'];
|
|
66
73
|
const getMsg = () => [response.status, response.res.statusMessage, response.request.url];
|
|
67
74
|
const message = response ? getMsg() : [reason['errno'], reason.message];
|
|
68
75
|
const networkErr = { type: 'network-error', message: message.join(' ') };
|
|
69
|
-
return toValidatorResults({
|
|
76
|
+
return toValidatorResults(Object.assign(Object.assign({}, response), { body: { messages: [networkErr] } }));
|
|
70
77
|
};
|
|
71
78
|
return w3cRequest.then(filterMessages).then(toValidatorResults).catch(handleError);
|
|
72
79
|
},
|
|
73
80
|
reporter(results, options) {
|
|
81
|
+
var _a, _b;
|
|
74
82
|
const defaults = {
|
|
75
83
|
maxMessageLen: null,
|
|
76
84
|
title: null,
|
|
77
85
|
};
|
|
78
|
-
const settings = {
|
|
79
|
-
if (typeof results
|
|
86
|
+
const settings = Object.assign(Object.assign({}, defaults), options);
|
|
87
|
+
if (typeof (results === null || results === void 0 ? void 0 : results.validates) !== 'boolean')
|
|
80
88
|
throw Error('[w3c-html-validator] Invalid results for reporter(): ' + String(results));
|
|
81
|
-
const messages = results.messages
|
|
82
|
-
const title = settings.title
|
|
83
|
-
const
|
|
84
|
-
const
|
|
85
|
-
log(chalk.gray('w3c-html-validator'), chalk.blue.bold(title),
|
|
89
|
+
const messages = (_a = results.messages) !== null && _a !== void 0 ? _a : [];
|
|
90
|
+
const title = (_b = settings.title) !== null && _b !== void 0 ? _b : results.title;
|
|
91
|
+
const status = results.validates ? chalk.green.bold('✔ pass') : chalk.red.bold('✘ fail');
|
|
92
|
+
const count = results.validates ? '' : '(messages: ' + messages.length + ')';
|
|
93
|
+
log(chalk.gray('w3c-html-validator'), status, chalk.blue.bold(title), chalk.white(count));
|
|
86
94
|
const typeColorMap = {
|
|
87
95
|
error: chalk.red.bold,
|
|
88
96
|
warning: chalk.yellow.bold,
|
|
89
97
|
info: chalk.white.bold,
|
|
90
98
|
};
|
|
91
99
|
const logMessage = (message) => {
|
|
100
|
+
var _a, _b;
|
|
92
101
|
const type = message.subType || message.type;
|
|
93
102
|
const typeColor = typeColorMap[type] || chalk.redBright.bold;
|
|
94
103
|
const location = `line ${message.lastLine}, column ${message.firstColumn}:`;
|
|
95
|
-
const lineText = message.extract
|
|
96
|
-
const maxLen = settings.maxMessageLen
|
|
104
|
+
const lineText = (_a = message.extract) === null || _a === void 0 ? void 0 : _a.replace(/\n/g, '\\n');
|
|
105
|
+
const maxLen = (_b = settings.maxMessageLen) !== null && _b !== void 0 ? _b : undefined;
|
|
97
106
|
log(typeColor('HTML ' + type + ':'), message.message.substring(0, maxLen));
|
|
98
107
|
if (message.lastLine)
|
|
99
108
|
log(chalk.white(location), chalk.magenta(lineText));
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
//! w3c-html-validator v1.0
|
|
1
|
+
//! w3c-html-validator v1.1.0 ~~ https://github.com/center-key/w3c-html-validator ~~ MIT License
|
|
2
2
|
|
|
3
3
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
4
4
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
@@ -20,7 +20,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
20
20
|
const fancy_log_1 = __importDefault(require("fancy-log"));
|
|
21
21
|
const superagent_1 = __importDefault(require("superagent"));
|
|
22
22
|
const w3cHtmlValidator = {
|
|
23
|
-
version: '1.0
|
|
23
|
+
version: '1.1.0',
|
|
24
24
|
validate(options) {
|
|
25
25
|
const defaults = {
|
|
26
26
|
checkUrl: 'https://validator.w3.org/nu/',
|
|
@@ -28,7 +28,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
28
28
|
ignoreMessages: null,
|
|
29
29
|
output: 'json',
|
|
30
30
|
};
|
|
31
|
-
const settings = {
|
|
31
|
+
const settings = Object.assign(Object.assign({}, defaults), options);
|
|
32
32
|
if (!settings.html && !settings.filename && !settings.website)
|
|
33
33
|
throw Error('[w3c-html-validator] Must specify the "html", "filename", or "website" option.');
|
|
34
34
|
if (![null, 'info', 'warning'].includes(settings.ignoreLevel))
|
|
@@ -49,66 +49,75 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
49
49
|
const json = settings.output === 'json';
|
|
50
50
|
const success = '<p class="success">';
|
|
51
51
|
const titleLookup = {
|
|
52
|
-
html: 'HTML String (characters: ' + inputHtml
|
|
52
|
+
html: 'HTML String (characters: ' + (inputHtml === null || inputHtml === void 0 ? void 0 : inputHtml.length) + ')',
|
|
53
53
|
filename: settings.filename,
|
|
54
54
|
website: settings.website,
|
|
55
55
|
};
|
|
56
56
|
const filterMessages = (response) => {
|
|
57
|
+
var _a, _b;
|
|
57
58
|
const aboveInfo = (subType) => settings.ignoreLevel === 'info' && !!subType;
|
|
58
59
|
const aboveIgnoreLevel = (message) => !settings.ignoreLevel || message.type !== 'info' || aboveInfo(message.subType);
|
|
59
60
|
const skipSubstr = (title) => typeof settings.ignoreMessages === 'string' && title.includes(settings.ignoreMessages);
|
|
60
|
-
const skipRegEx = (title) =>
|
|
61
|
-
|
|
61
|
+
const skipRegEx = (title) => {
|
|
62
|
+
var _a;
|
|
63
|
+
return ((_a = settings.ignoreMessages) === null || _a === void 0 ? void 0 : _a.constructor.name) === 'RegExp' &&
|
|
64
|
+
settings.ignoreMessages.test(title);
|
|
65
|
+
};
|
|
62
66
|
const isImportant = (message) => aboveIgnoreLevel(message) && !skipSubstr(message.message) && !skipRegEx(message.message);
|
|
63
67
|
if (json)
|
|
64
|
-
response.body.messages = response.body.messages
|
|
68
|
+
response.body.messages = (_b = (_a = response.body.messages) === null || _a === void 0 ? void 0 : _a.filter(isImportant)) !== null && _b !== void 0 ? _b : [];
|
|
65
69
|
return response;
|
|
66
70
|
};
|
|
67
|
-
const toValidatorResults = (response) =>
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
71
|
+
const toValidatorResults = (response) => {
|
|
72
|
+
var _a;
|
|
73
|
+
return ({
|
|
74
|
+
validates: json ? !response.body.messages.length : !!((_a = response.text) === null || _a === void 0 ? void 0 : _a.includes(success)),
|
|
75
|
+
mode: mode,
|
|
76
|
+
title: titleLookup[mode],
|
|
77
|
+
html: inputHtml,
|
|
78
|
+
filename: settings.filename || null,
|
|
79
|
+
website: settings.website || null,
|
|
80
|
+
output: settings.output,
|
|
81
|
+
status: response.statusCode || -1,
|
|
82
|
+
messages: json ? response.body.messages : null,
|
|
83
|
+
display: json ? null : response.text,
|
|
84
|
+
});
|
|
85
|
+
};
|
|
79
86
|
const handleError = (reason) => {
|
|
80
87
|
const response = reason['response'];
|
|
81
88
|
const getMsg = () => [response.status, response.res.statusMessage, response.request.url];
|
|
82
89
|
const message = response ? getMsg() : [reason['errno'], reason.message];
|
|
83
90
|
const networkErr = { type: 'network-error', message: message.join(' ') };
|
|
84
|
-
return toValidatorResults({
|
|
91
|
+
return toValidatorResults(Object.assign(Object.assign({}, response), { body: { messages: [networkErr] } }));
|
|
85
92
|
};
|
|
86
93
|
return w3cRequest.then(filterMessages).then(toValidatorResults).catch(handleError);
|
|
87
94
|
},
|
|
88
95
|
reporter(results, options) {
|
|
96
|
+
var _a, _b;
|
|
89
97
|
const defaults = {
|
|
90
98
|
maxMessageLen: null,
|
|
91
99
|
title: null,
|
|
92
100
|
};
|
|
93
|
-
const settings = {
|
|
94
|
-
if (typeof results
|
|
101
|
+
const settings = Object.assign(Object.assign({}, defaults), options);
|
|
102
|
+
if (typeof (results === null || results === void 0 ? void 0 : results.validates) !== 'boolean')
|
|
95
103
|
throw Error('[w3c-html-validator] Invalid results for reporter(): ' + String(results));
|
|
96
|
-
const messages = results.messages
|
|
97
|
-
const title = settings.title
|
|
98
|
-
const
|
|
99
|
-
const
|
|
100
|
-
(0, fancy_log_1.default)(chalk_1.default.gray('w3c-html-validator'), chalk_1.default.blue.bold(title),
|
|
104
|
+
const messages = (_a = results.messages) !== null && _a !== void 0 ? _a : [];
|
|
105
|
+
const title = (_b = settings.title) !== null && _b !== void 0 ? _b : results.title;
|
|
106
|
+
const status = results.validates ? chalk_1.default.green.bold('✔ pass') : chalk_1.default.red.bold('✘ fail');
|
|
107
|
+
const count = results.validates ? '' : '(messages: ' + messages.length + ')';
|
|
108
|
+
(0, fancy_log_1.default)(chalk_1.default.gray('w3c-html-validator'), status, chalk_1.default.blue.bold(title), chalk_1.default.white(count));
|
|
101
109
|
const typeColorMap = {
|
|
102
110
|
error: chalk_1.default.red.bold,
|
|
103
111
|
warning: chalk_1.default.yellow.bold,
|
|
104
112
|
info: chalk_1.default.white.bold,
|
|
105
113
|
};
|
|
106
114
|
const logMessage = (message) => {
|
|
115
|
+
var _a, _b;
|
|
107
116
|
const type = message.subType || message.type;
|
|
108
117
|
const typeColor = typeColorMap[type] || chalk_1.default.redBright.bold;
|
|
109
118
|
const location = `line ${message.lastLine}, column ${message.firstColumn}:`;
|
|
110
|
-
const lineText = message.extract
|
|
111
|
-
const maxLen = settings.maxMessageLen
|
|
119
|
+
const lineText = (_a = message.extract) === null || _a === void 0 ? void 0 : _a.replace(/\n/g, '\\n');
|
|
120
|
+
const maxLen = (_b = settings.maxMessageLen) !== null && _b !== void 0 ? _b : undefined;
|
|
112
121
|
(0, fancy_log_1.default)(typeColor('HTML ' + type + ':'), message.message.substring(0, maxLen));
|
|
113
122
|
if (message.lastLine)
|
|
114
123
|
(0, fancy_log_1.default)(chalk_1.default.white(location), chalk_1.default.magenta(lineText));
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "w3c-html-validator",
|
|
3
|
-
"version": "1.0
|
|
4
|
-
"description": "A package for testing HTML files or URLs against the W3C validator (written in TypeScript)",
|
|
3
|
+
"version": "1.1.0",
|
|
4
|
+
"description": "A package for testing HTML files or URLs against the W3C validator (written in functional TypeScript)",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|
|
7
7
|
"module": "dist/w3c-html-validator.js",
|
|
@@ -66,7 +66,7 @@
|
|
|
66
66
|
"step:03": "eslint --max-warnings 0 . --ext .ts",
|
|
67
67
|
"step:04": "tsc",
|
|
68
68
|
"step:05": "tsc --module UMD --outDir build/umd",
|
|
69
|
-
"step:06": "cpy build/umd/w3c-html-validator.js build --rename=w3c-html-validator.umd.cjs",
|
|
69
|
+
"step:06": "cpy build/umd/w3c-html-validator.js build --rename=w3c-html-validator.umd.cjs --flat=true",
|
|
70
70
|
"step:07": "add-dist-header build dist",
|
|
71
71
|
"pretest": "npm-run-all step:*",
|
|
72
72
|
"test": "mocha spec/*.spec.js --timeout 5000",
|
|
@@ -75,25 +75,25 @@
|
|
|
75
75
|
"dependencies": {
|
|
76
76
|
"chalk": "~5.0",
|
|
77
77
|
"fancy-log": "~2.0",
|
|
78
|
-
"glob": "~
|
|
79
|
-
"superagent": "~
|
|
78
|
+
"glob": "~8.0",
|
|
79
|
+
"superagent": "~8.0"
|
|
80
80
|
},
|
|
81
81
|
"devDependencies": {
|
|
82
|
-
"@types/fancy-log": "~
|
|
82
|
+
"@types/fancy-log": "~2.0",
|
|
83
83
|
"@types/glob": "~7.2",
|
|
84
|
-
"@types/node": "~
|
|
84
|
+
"@types/node": "~18.0",
|
|
85
85
|
"@types/superagent": "~4.1",
|
|
86
|
-
"@typescript-eslint/eslint-plugin": "~5.
|
|
87
|
-
"@typescript-eslint/parser": "~5.
|
|
86
|
+
"@typescript-eslint/eslint-plugin": "~5.30",
|
|
87
|
+
"@typescript-eslint/parser": "~5.30",
|
|
88
88
|
"add-dist-header": "~0.1",
|
|
89
89
|
"assert-deep-strict-equal": "~1.0",
|
|
90
|
-
"cpy-cli": "~
|
|
91
|
-
"eslint": "~8.
|
|
90
|
+
"cpy-cli": "~4.1",
|
|
91
|
+
"eslint": "~8.19",
|
|
92
92
|
"jshint": "~2.13",
|
|
93
93
|
"merge-stream": "~2.0",
|
|
94
|
-
"mocha": "~
|
|
95
|
-
"npm-run-all2": "~
|
|
94
|
+
"mocha": "~10.0",
|
|
95
|
+
"npm-run-all2": "~6.0",
|
|
96
96
|
"rimraf": "~3.0",
|
|
97
|
-
"typescript": "~4.
|
|
97
|
+
"typescript": "~4.7"
|
|
98
98
|
}
|
|
99
99
|
}
|