Types for Html-validator (#37607)

* draft

* clear to tests

* Clear for tests
This commit is contained in:
TokugawaT_YD 2019-08-15 06:35:43 +09:00 committed by Pranav Senthilnathan
parent a26e3956af
commit f18ba003c3
4 changed files with 154 additions and 0 deletions

View File

@ -0,0 +1,37 @@
import validateHtml = require('html-validator');
const testHtml = `
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title>Invalid</title>
</head>
<body>
<p>I'm baaaaaaaaaaaad code</div></p>
</body>
</html>
`;
validateHtml({
data: testHtml,
format: 'json'
}).then((validationResults: validateHtml.ParsedJsonAsValidationResults) => {
if (validationResults.messages.length === 0) {
console.warn(`File "test" contains W3C standard violations or guidelines neglects.`);
return;
}
validationResults.messages.forEach((violation: validateHtml.ValidationMessageObject) => {
if (violation.type === 'error') {
console.log(`W3C standard violation: ${violation.message}`);
}
if (violation.type === 'info') {
console.warn(`W3C guidelines neglect: ${violation.message}`);
}
console.log(violation.extract);
console.log(`line: ${violation.lastLine}, column: ${violation.firstColumn}-${violation.lastColumn}\n`);
});
});

91
types/html-validator/index.d.ts vendored Normal file
View File

@ -0,0 +1,91 @@
// Type definitions for html-validator 4.1
// Project: https://github.com/zrrrzzt/html-validator
// Definitions by: Takesi Tokugawa <https://github.com/TokugawaTakesi>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// TypeScript Version: 2.4
/// <reference types="node" />
declare function HtmlValidator(
options:
HtmlValidator.OptionsForHtmlFileAsValidationTargetAndObjectAsResult |
HtmlValidator.OptionsForExternalUrlAsValidationTargetAndObjectAsResult
): Promise<HtmlValidator.ParsedJsonAsValidationResults>;
declare function HtmlValidator(
options:
HtmlValidator.OptionsForHtmlFileAsValidationTargetAndTextAsResults |
HtmlValidator.OptionsForExternalUrlAsValidationTargetAndTextAsResults
): Promise<string>;
declare namespace HtmlValidator {
interface BasicOptions {
validator?: object;
ignore?: string | string[];
isLocal?: boolean;
isFragment?: boolean;
}
interface OptionsForHtmlFileAsValidationTarget extends BasicOptions {
data: string;
}
interface OptionsForExternalUrlAsValidationTarget extends BasicOptions {
url: string;
}
// Could be used to avoid string literals
enum ValidationResultsOutputFormats {
json = 'json',
html = 'html',
xhtml = 'xhtml',
xml = 'xml',
gnu = 'gnu',
text = 'text'
}
interface OptionsForHtmlFileAsValidationTargetAndObjectAsResult extends OptionsForHtmlFileAsValidationTarget {
format?: 'json';
}
interface OptionsForHtmlFileAsValidationTargetAndTextAsResults extends OptionsForHtmlFileAsValidationTarget {
format: 'html' | 'xhtml' | 'xml' | 'gnu' | 'text';
}
interface OptionsForExternalUrlAsValidationTargetAndObjectAsResult extends OptionsForExternalUrlAsValidationTarget {
format?: 'json';
}
interface OptionsForExternalUrlAsValidationTargetAndTextAsResults extends OptionsForHtmlFileAsValidationTarget {
format: 'html' | 'xhtml' | 'xml' | 'gnu' | 'text';
}
interface ParsedJsonAsValidationResults {
messages: ValidationMessageObject[];
}
// Could be used to avoid string literals
enum ValidationMessageTypes {
error = 'error',
info = 'info'
}
// Could be used to avoid string literals
enum ValidationMessageSubTypes {
warning = 'warning'
}
interface ValidationMessageObject {
type: 'error' | 'info';
subType?: 'warning';
lastLine: number;
firstColumn: number;
lastColumn: number;
hiliteStart: number;
hiliteLength: number;
message: string;
extract: string;
}
}
export = HtmlValidator;

View File

@ -0,0 +1,23 @@
{
"compilerOptions": {
"module": "commonjs",
"lib": [
"es6"
],
"noImplicitAny": true,
"noImplicitThis": true,
"strictFunctionTypes": true,
"strictNullChecks": true,
"baseUrl": "../",
"typeRoots": [
"../"
],
"types": [],
"noEmit": true,
"forceConsistentCasingInFileNames": true
},
"files": [
"index.d.ts",
"html-validator-tests.ts"
]
}

View File

@ -0,0 +1,3 @@
{
"extends": "dtslint/dt.json"
}