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