html-validate 9.2.2 → 9.4.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.
@@ -1,16 +1,16 @@
1
1
  'use strict';
2
2
 
3
- var path = require('node:path');
4
- var core = require('./core.js');
5
3
  var fs = require('node:fs');
4
+ var core = require('./core.js');
5
+ var path = require('node:path');
6
6
  var fs$1 = require('node:fs/promises');
7
7
  var node_url = require('node:url');
8
8
  var kleur = require('kleur');
9
9
 
10
10
  function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
11
11
 
12
- var path__default = /*#__PURE__*/_interopDefault(path);
13
12
  var fs__default = /*#__PURE__*/_interopDefault(fs);
13
+ var path__default = /*#__PURE__*/_interopDefault(path);
14
14
  var fs__default$1 = /*#__PURE__*/_interopDefault(fs$1);
15
15
  var kleur__default = /*#__PURE__*/_interopDefault(kleur);
16
16
 
@@ -29,6 +29,425 @@ function requireUncached(require, moduleId) {
29
29
  return require(filename);
30
30
  }
31
31
 
32
+ const defaultFS = {
33
+ readFileSync: fs__default.default.readFileSync
34
+ };
35
+ function isSourceHooks(value) {
36
+ if (!value || typeof value === "string") {
37
+ return false;
38
+ }
39
+ return Boolean(value.processAttribute || value.processElement);
40
+ }
41
+ function isConfigData(value) {
42
+ if (!value || typeof value === "string") {
43
+ return false;
44
+ }
45
+ return !(value.processAttribute || value.processElement);
46
+ }
47
+ class HtmlValidate {
48
+ configLoader;
49
+ constructor(arg) {
50
+ const [loader, config] = arg instanceof core.ConfigLoader ? [arg, void 0] : [void 0, arg];
51
+ this.configLoader = loader ?? new core.StaticConfigLoader(config);
52
+ }
53
+ /* eslint-enable @typescript-eslint/unified-signatures */
54
+ validateString(str, arg1, arg2, arg3) {
55
+ const filename = typeof arg1 === "string" ? arg1 : "inline";
56
+ const options = isConfigData(arg1) ? arg1 : isConfigData(arg2) ? arg2 : void 0;
57
+ const hooks = isSourceHooks(arg1) ? arg1 : isSourceHooks(arg2) ? arg2 : arg3;
58
+ const source = {
59
+ data: str,
60
+ filename,
61
+ line: 1,
62
+ column: 1,
63
+ offset: 0,
64
+ hooks
65
+ };
66
+ return this.validateSource(source, options);
67
+ }
68
+ /* eslint-enable @typescript-eslint/unified-signatures */
69
+ validateStringSync(str, arg1, arg2, arg3) {
70
+ const filename = typeof arg1 === "string" ? arg1 : "inline";
71
+ const options = isConfigData(arg1) ? arg1 : isConfigData(arg2) ? arg2 : void 0;
72
+ const hooks = isSourceHooks(arg1) ? arg1 : isSourceHooks(arg2) ? arg2 : arg3;
73
+ const source = {
74
+ data: str,
75
+ filename,
76
+ line: 1,
77
+ column: 1,
78
+ offset: 0,
79
+ hooks
80
+ };
81
+ return this.validateSourceSync(source, options);
82
+ }
83
+ /**
84
+ * Parse and validate HTML from [[Source]].
85
+ *
86
+ * @public
87
+ * @param input - Source to parse.
88
+ * @returns Report output.
89
+ */
90
+ async validateSource(input, configOverride) {
91
+ const source = core.normalizeSource(input);
92
+ const config = await this.getConfigFor(source.filename, configOverride);
93
+ const resolvers = this.configLoader.getResolvers();
94
+ const transformedSource = await core.transformSource(resolvers, config, source);
95
+ const engine = new core.Engine(config, core.Parser);
96
+ return engine.lint(transformedSource);
97
+ }
98
+ /**
99
+ * Parse and validate HTML from [[Source]].
100
+ *
101
+ * @public
102
+ * @param input - Source to parse.
103
+ * @returns Report output.
104
+ */
105
+ validateSourceSync(input, configOverride) {
106
+ const source = core.normalizeSource(input);
107
+ const config = this.getConfigForSync(source.filename, configOverride);
108
+ const resolvers = this.configLoader.getResolvers();
109
+ const transformedSource = core.transformSourceSync(resolvers, config, source);
110
+ const engine = new core.Engine(config, core.Parser);
111
+ return engine.lint(transformedSource);
112
+ }
113
+ /**
114
+ * Parse and validate HTML from file.
115
+ *
116
+ * @public
117
+ * @param filename - Filename to read and parse.
118
+ * @returns Report output.
119
+ */
120
+ async validateFile(filename, fs2 = defaultFS) {
121
+ const config = await this.getConfigFor(filename);
122
+ const resolvers = this.configLoader.getResolvers();
123
+ const source = await core.transformFilename(resolvers, config, filename, fs2);
124
+ const engine = new core.Engine(config, core.Parser);
125
+ return Promise.resolve(engine.lint(source));
126
+ }
127
+ /**
128
+ * Parse and validate HTML from file.
129
+ *
130
+ * @public
131
+ * @param filename - Filename to read and parse.
132
+ * @returns Report output.
133
+ */
134
+ validateFileSync(filename, fs2 = defaultFS) {
135
+ const config = this.getConfigForSync(filename);
136
+ const resolvers = this.configLoader.getResolvers();
137
+ const source = core.transformFilenameSync(resolvers, config, filename, fs2);
138
+ const engine = new core.Engine(config, core.Parser);
139
+ return engine.lint(source);
140
+ }
141
+ /**
142
+ * Parse and validate HTML from multiple files. Result is merged together to a
143
+ * single report.
144
+ *
145
+ * @param filenames - Filenames to read and parse.
146
+ * @returns Report output.
147
+ */
148
+ async validateMultipleFiles(filenames, fs2 = defaultFS) {
149
+ return core.Reporter.merge(filenames.map((filename) => this.validateFile(filename, fs2)));
150
+ }
151
+ /**
152
+ * Parse and validate HTML from multiple files. Result is merged together to a
153
+ * single report.
154
+ *
155
+ * @param filenames - Filenames to read and parse.
156
+ * @returns Report output.
157
+ */
158
+ validateMultipleFilesSync(filenames, fs2 = defaultFS) {
159
+ return core.Reporter.merge(filenames.map((filename) => this.validateFileSync(filename, fs2)));
160
+ }
161
+ /**
162
+ * Returns true if the given filename can be validated.
163
+ *
164
+ * A file is considered to be validatable if the extension is `.html` or if a
165
+ * transformer matches the filename.
166
+ *
167
+ * This is mostly useful for tooling to determine whenever to validate the
168
+ * file or not. CLI tools will run on all the given files anyway.
169
+ */
170
+ async canValidate(filename) {
171
+ if (filename.toLowerCase().endsWith(".html")) {
172
+ return true;
173
+ }
174
+ const config = await this.getConfigFor(filename);
175
+ return config.canTransform(filename);
176
+ }
177
+ /**
178
+ * Returns true if the given filename can be validated.
179
+ *
180
+ * A file is considered to be validatable if the extension is `.html` or if a
181
+ * transformer matches the filename.
182
+ *
183
+ * This is mostly useful for tooling to determine whenever to validate the
184
+ * file or not. CLI tools will run on all the given files anyway.
185
+ */
186
+ canValidateSync(filename) {
187
+ if (filename.toLowerCase().endsWith(".html")) {
188
+ return true;
189
+ }
190
+ const config = this.getConfigForSync(filename);
191
+ return config.canTransform(filename);
192
+ }
193
+ /**
194
+ * Tokenize filename and output all tokens.
195
+ *
196
+ * Using CLI this is enabled with `--dump-tokens`. Mostly useful for
197
+ * debugging.
198
+ *
199
+ * @internal
200
+ * @param filename - Filename to tokenize.
201
+ */
202
+ async dumpTokens(filename, fs2 = defaultFS) {
203
+ const config = await this.getConfigFor(filename);
204
+ const resolvers = this.configLoader.getResolvers();
205
+ const source = await core.transformFilename(resolvers, config, filename, fs2);
206
+ const engine = new core.Engine(config, core.Parser);
207
+ return engine.dumpTokens(source);
208
+ }
209
+ /**
210
+ * Parse filename and output all events.
211
+ *
212
+ * Using CLI this is enabled with `--dump-events`. Mostly useful for
213
+ * debugging.
214
+ *
215
+ * @internal
216
+ * @param filename - Filename to dump events from.
217
+ */
218
+ async dumpEvents(filename, fs2 = defaultFS) {
219
+ const config = await this.getConfigFor(filename);
220
+ const resolvers = this.configLoader.getResolvers();
221
+ const source = await core.transformFilename(resolvers, config, filename, fs2);
222
+ const engine = new core.Engine(config, core.Parser);
223
+ return engine.dumpEvents(source);
224
+ }
225
+ /**
226
+ * Parse filename and output DOM tree.
227
+ *
228
+ * Using CLI this is enabled with `--dump-tree`. Mostly useful for
229
+ * debugging.
230
+ *
231
+ * @internal
232
+ * @param filename - Filename to dump DOM tree from.
233
+ */
234
+ async dumpTree(filename, fs2 = defaultFS) {
235
+ const config = await this.getConfigFor(filename);
236
+ const resolvers = this.configLoader.getResolvers();
237
+ const source = await core.transformFilename(resolvers, config, filename, fs2);
238
+ const engine = new core.Engine(config, core.Parser);
239
+ return engine.dumpTree(source);
240
+ }
241
+ /**
242
+ * Transform filename and output source data.
243
+ *
244
+ * Using CLI this is enabled with `--dump-source`. Mostly useful for
245
+ * debugging.
246
+ *
247
+ * @internal
248
+ * @param filename - Filename to dump source from.
249
+ */
250
+ async dumpSource(filename, fs2 = defaultFS) {
251
+ const config = await this.getConfigFor(filename);
252
+ const resolvers = this.configLoader.getResolvers();
253
+ const sources = await core.transformFilename(resolvers, config, filename, fs2);
254
+ return sources.reduce((result, source) => {
255
+ const line = String(source.line);
256
+ const column = String(source.column);
257
+ const offset = String(source.offset);
258
+ result.push(`Source ${source.filename}@${line}:${column} (offset: ${offset})`);
259
+ if (source.transformedBy) {
260
+ result.push("Transformed by:");
261
+ result = result.concat(source.transformedBy.reverse().map((name) => ` - ${name}`));
262
+ }
263
+ if (source.hooks && Object.keys(source.hooks).length > 0) {
264
+ result.push("Hooks");
265
+ for (const [key, present] of Object.entries(source.hooks)) {
266
+ if (present) {
267
+ result.push(` - ${key}`);
268
+ }
269
+ }
270
+ }
271
+ result.push("---");
272
+ result = result.concat(source.data.split("\n"));
273
+ result.push("---");
274
+ return result;
275
+ }, []);
276
+ }
277
+ /**
278
+ * Get effective configuration schema.
279
+ */
280
+ getConfigurationSchema() {
281
+ return Promise.resolve(core.configurationSchema);
282
+ }
283
+ /**
284
+ * Get effective metadata element schema.
285
+ *
286
+ * If a filename is given the configured plugins can extend the
287
+ * schema. Filename must not be an existing file or a filetype normally
288
+ * handled by html-validate but the path will be used when resolving
289
+ * configuration. As a rule-of-thumb, set it to the elements json file.
290
+ */
291
+ async getElementsSchema(filename) {
292
+ const config = await this.getConfigFor(filename ?? "inline");
293
+ const metaTable = config.getMetaTable();
294
+ return metaTable.getJSONSchema();
295
+ }
296
+ /**
297
+ * Get effective metadata element schema.
298
+ *
299
+ * If a filename is given the configured plugins can extend the
300
+ * schema. Filename must not be an existing file or a filetype normally
301
+ * handled by html-validate but the path will be used when resolving
302
+ * configuration. As a rule-of-thumb, set it to the elements json file.
303
+ */
304
+ getElementsSchemaSync(filename) {
305
+ const config = this.getConfigForSync(filename ?? "inline");
306
+ const metaTable = config.getMetaTable();
307
+ return metaTable.getJSONSchema();
308
+ }
309
+ async getContextualDocumentation(message, filenameOrConfig = "inline") {
310
+ const config = typeof filenameOrConfig === "string" ? await this.getConfigFor(filenameOrConfig) : await filenameOrConfig;
311
+ const engine = new core.Engine(config, core.Parser);
312
+ return engine.getRuleDocumentation(message);
313
+ }
314
+ getContextualDocumentationSync(message, filenameOrConfig = "inline") {
315
+ const config = typeof filenameOrConfig === "string" ? this.getConfigForSync(filenameOrConfig) : filenameOrConfig;
316
+ const engine = new core.Engine(config, core.Parser);
317
+ return engine.getRuleDocumentation(message);
318
+ }
319
+ /**
320
+ * Get contextual documentation for the given rule.
321
+ *
322
+ * Typical usage:
323
+ *
324
+ * ```js
325
+ * const report = await htmlvalidate.validateFile("my-file.html");
326
+ * for (const result of report.results){
327
+ * const config = await htmlvalidate.getConfigFor(result.filePath);
328
+ * for (const message of result.messages){
329
+ * const documentation = await htmlvalidate.getRuleDocumentation(message.ruleId, config, message.context);
330
+ * // do something with documentation
331
+ * }
332
+ * }
333
+ * ```
334
+ *
335
+ * @public
336
+ * @deprecated Deprecated since 8.0.0, use [[getContextualDocumentation]] instead.
337
+ * @param ruleId - Rule to get documentation for.
338
+ * @param config - If set it provides more accurate description by using the
339
+ * correct configuration for the file.
340
+ * @param context - If set to `Message.context` some rules can provide
341
+ * contextual details and suggestions.
342
+ */
343
+ async getRuleDocumentation(ruleId, config = null, context = null) {
344
+ const c = config ?? this.getConfigFor("inline");
345
+ const engine = new core.Engine(await c, core.Parser);
346
+ return engine.getRuleDocumentation({ ruleId, context });
347
+ }
348
+ /**
349
+ * Get contextual documentation for the given rule.
350
+ *
351
+ * Typical usage:
352
+ *
353
+ * ```js
354
+ * const report = htmlvalidate.validateFileSync("my-file.html");
355
+ * for (const result of report.results){
356
+ * const config = htmlvalidate.getConfigForSync(result.filePath);
357
+ * for (const message of result.messages){
358
+ * const documentation = htmlvalidate.getRuleDocumentationSync(message.ruleId, config, message.context);
359
+ * // do something with documentation
360
+ * }
361
+ * }
362
+ * ```
363
+ *
364
+ * @public
365
+ * @deprecated Deprecated since 8.0.0, use [[getContextualDocumentationSync]] instead.
366
+ * @param ruleId - Rule to get documentation for.
367
+ * @param config - If set it provides more accurate description by using the
368
+ * correct configuration for the file.
369
+ * @param context - If set to `Message.context` some rules can provide
370
+ * contextual details and suggestions.
371
+ */
372
+ getRuleDocumentationSync(ruleId, config = null, context = null) {
373
+ const c = config ?? this.getConfigForSync("inline");
374
+ const engine = new core.Engine(c, core.Parser);
375
+ return engine.getRuleDocumentation({ ruleId, context });
376
+ }
377
+ /**
378
+ * Create a parser configured for given filename.
379
+ *
380
+ * @internal
381
+ * @param source - Source to use.
382
+ */
383
+ async getParserFor(source) {
384
+ const config = await this.getConfigFor(source.filename);
385
+ return new core.Parser(config);
386
+ }
387
+ /**
388
+ * Get configuration for given filename.
389
+ *
390
+ * See [[FileSystemConfigLoader]] for details.
391
+ *
392
+ * @public
393
+ * @param filename - Filename to get configuration for.
394
+ * @param configOverride - Configuration to apply last.
395
+ */
396
+ getConfigFor(filename, configOverride) {
397
+ const config = this.configLoader.getConfigFor(filename, configOverride);
398
+ return Promise.resolve(config);
399
+ }
400
+ /**
401
+ * Get configuration for given filename.
402
+ *
403
+ * See [[FileSystemConfigLoader]] for details.
404
+ *
405
+ * @public
406
+ * @param filename - Filename to get configuration for.
407
+ * @param configOverride - Configuration to apply last.
408
+ */
409
+ getConfigForSync(filename, configOverride) {
410
+ const config = this.configLoader.getConfigFor(filename, configOverride);
411
+ if (core.isThenable(config)) {
412
+ throw new core.UserError("Cannot use asynchronous config loader with synchronous api");
413
+ }
414
+ return config;
415
+ }
416
+ /**
417
+ * Get current configuration loader.
418
+ *
419
+ * @public
420
+ * @since %version%
421
+ * @returns Current configuration loader.
422
+ */
423
+ /* istanbul ignore next -- not testing setters/getters */
424
+ getConfigLoader() {
425
+ return this.configLoader;
426
+ }
427
+ /**
428
+ * Set configuration loader.
429
+ *
430
+ * @public
431
+ * @since %version%
432
+ * @param loader - New configuration loader to use.
433
+ */
434
+ /* istanbul ignore next -- not testing setters/getters */
435
+ setConfigLoader(loader) {
436
+ this.configLoader = loader;
437
+ }
438
+ /**
439
+ * Flush configuration cache. Clears full cache unless a filename is given.
440
+ *
441
+ * See [[FileSystemConfigLoader]] for details.
442
+ *
443
+ * @public
444
+ * @param filename - If set, only flush cache for given filename.
445
+ */
446
+ flushConfigCache(filename) {
447
+ this.configLoader.flushCache(filename);
448
+ }
449
+ }
450
+
32
451
  const legacyRequire = require;
33
452
  const importResolve = (specifier) => {
34
453
  return node_url.pathToFileURL(require.resolve(specifier));
@@ -446,6 +865,7 @@ function compatibilityCheck(name, declared, options) {
446
865
  }
447
866
 
448
867
  exports.FileSystemConfigLoader = FileSystemConfigLoader;
868
+ exports.HtmlValidate = HtmlValidate;
449
869
  exports.cjsResolver = cjsResolver;
450
870
  exports.compatibilityCheck = compatibilityCheck;
451
871
  exports.esmResolver = esmResolver;