Skip to content

Commit

Permalink
Merge 952a3ff into 4c93f91
Browse files Browse the repository at this point in the history
  • Loading branch information
zrrrzzt committed Feb 15, 2020
2 parents 4c93f91 + 952a3ff commit 26e19c7
Show file tree
Hide file tree
Showing 13 changed files with 659 additions and 243 deletions.
88 changes: 68 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,18 @@

# html-validator

A [Node.js](https://nodejs.org/) module for validating html using [validator.w3.org/nu](https://validator.w3.org/nu/)
A [Node.js](https://nodejs.org/) module for validating html using [validator.w3.org/nu](https://validator.w3.org/nu/) or [html-validate](https://html-validate.org/)

Requires Node >= 8.16.0 for older versions use v3.1.3
Requires Node >= 19.19.0 for older versions use v4.1.1

## Module

Supports the following modes from Validator.nu [Web Service Interface](https://github.com/validator/validator/wiki/Service-%C2%BB-HTTP-interface)
- Document URL as a GET parameter; the service retrieves the document by URL over HTTP or HTTPS.
- Document POSTed as the HTTP entity body; parameters in query string as with GET.

From html-validate it will only validate against the WHATWG standards.

### Installation

```sh
Expand All @@ -23,24 +25,27 @@ $ npm i html-validator

Create an options object.

**format** This is the formatting of the returned data. It supports json (default), html, xhtml, xml, gnu and text.
**format** This is the formatting of the returned data. It supports json (default), html, xhtml, xml, gnu and text for W3C and json for WHATWG.

**validator** You can override the default validator as long as it exposes the same REST interface.
**validator** You can override the default validator (W3C) as long as it exposes the same REST interface. To use WHATWG just pass `WHATWG` as a string

**url**/**data** The url to the page you want to validate or the data you want validated. Can be an HTML, CSS or SVG document.
**url**/**data** The url to the page you want to validate or the data you want validated.

**ignore** String or array of strings you want the checker to remove in the response
**ignore** String or array of strings or rules (when using WHATWG) you want the checker to remove in the response

**isLocal** Set this to true if you want to validate local urls

**isFragment** Set this to true if your data input is not a complete document


#### W3C (default)

```JavaScript
(async () => {
const validator = require('html-validator')
const options = {
url: 'http://url-to-validate.com',
format: 'text'
format: 'text'
}

try {
Expand All @@ -58,9 +63,9 @@ Create an options object.
const validator = require('html-validator')
const { readFileSync } = require('fs')
const options = {
url: 'http://url-to-validate.com',
format: 'text',
data: readFileSync('file-to-validate.html', 'utf8')
url: 'http://url-to-validate.com',
format: 'text',
data: readFileSync('file-to-validate.html', 'utf8')
}

try {
Expand All @@ -72,15 +77,33 @@ Create an options object.
})()
```

**validator** You can override the default validator as long as it exposes the same REST interface.
**validator** You can override the default validator as long as it exposes the same REST interface. Or use the WHATWG option for validating your files locally

```JavaScript
(async () => {
const validator = require('html-validator')
const options = {
url: 'http://url-to-validate.com',
validator: 'http://html5.validator.nu',
format: 'text'
url: 'http://url-to-validate.com',
validator: 'http://html5.validator.nu',
format: 'text'
}

try {
const result = await validator(options)
console.log(result)
} catch (error) {
console.error(error)
}
})()
```

```JavaScript
(async () => {
const validator = require('html-validator')
const options = {
url: 'http://url-to-validate.com',
validator: 'WHATWG',
format: 'text'
}

try {
Expand All @@ -98,9 +121,9 @@ Create an options object.
(async () => {
const validator = require('html-validator')
const options = {
url: 'http://url-to-validate.com',
format: 'text',
ignore: 'Error: Stray end tag “div”.'
url: 'http://url-to-validate.com',
format: 'text',
ignore: 'Error: Stray end tag “div”.'
}

try {
Expand All @@ -118,9 +141,9 @@ Create an options object.
(async () => {
const validator = require('html-validator')
const options = {
url: 'http://url-to-validate.com',
format: 'text',
headers: {foo:"bar"}
url: 'http://url-to-validate.com',
format: 'text',
headers: {foo:"bar"}
}

try {
Expand Down Expand Up @@ -171,10 +194,35 @@ Create an options object.
})()
```

#### WHATWG

Using this option will validate your files locally and will probably speed up your tests in addition to not sending data over the wire for validation.

You can follow all the examples from W3C as long as you changes the options validator property to `WHATWG`

```JavaScript
(async () => {
const validator = require('html-validator')
const options = {
validator: 'WHATWG',
data: '<p>This is a fragment</p>',
isFragment: true
}

try {
const result = await validator(options)
console.log(result)
} catch (error) {
console.error(error)
}
})()
```

## Related

- [site-validator-cli](https://github.com/p1ho/site-validator-cli) CLI for validating a whole site or multiple pages
- [html-validator-cli](https://github.com/zrrrzzt/html-validator-cli) CLI for this module
- [html-validate](https://www.npmjs.com/package/html-validate) offline HTML5 validator

## License

Expand Down
29 changes: 5 additions & 24 deletions lib/validate.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,8 @@
const request = require('request')
const config = require('./config')
const filterData = require('./filter-data')
const w3c = require('./w3c-validator')
const whatwg = require('./whatwg-validator')

module.exports = async options => {
const requestOptions = await config(options)
const ignore = options.ignore
return new Promise((resolve, reject) => {
request(requestOptions, function (error, response, result) {
if (error) {
return reject(error)
}

if (response && response.statusCode !== 200) {
const error = new Error('Validator returned unexpected statuscode: ' + response.statusCode)
return reject(error)
}

let data = options.format === 'json' && !ignore ? JSON.parse(result) : result

if (ignore) {
data = filterData(data, ignore)
}
return resolve(data)
})
})
const { validator } = options
const useWHATWG = validator && validator.toLowerCase() === 'whatwg'
return useWHATWG ? whatwg(options) : w3c(options)
}
27 changes: 27 additions & 0 deletions lib/w3c-validator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
const request = require('request')
const config = require('./config')
const filterData = require('./filter-data')

module.exports = async options => {
const requestOptions = await config(options)
const ignore = options.ignore
return new Promise((resolve, reject) => {
request(requestOptions, function (error, response, result) {
if (error) {
return reject(error)
}

if (response && response.statusCode !== 200) {
const error = new Error('Validator returned unexpected statuscode: ' + response.statusCode)
return reject(error)
}

let data = options.format === 'json' && !ignore ? JSON.parse(result) : result

if (ignore) {
data = filterData(data, ignore)
}
return resolve(data)
})
})
}
12 changes: 12 additions & 0 deletions lib/whatwg-filter-messages.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module.exports = function (messages, ignore) {
const filters = Array.isArray(ignore) ? ignore : [ignore]
const filtered = messages.filter(item => {
const { ruleId, message } = item
if (filters.includes(ruleId) || filters.includes(message)) {
return false
} else {
return true
}
})
return filtered
}
34 changes: 34 additions & 0 deletions lib/whatwg-validator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
const { HtmlValidate } = require('html-validate')
const filterMessages = require('./whatwg-filter-messages')
const config = require('./config')

module.exports = async options => {
const htmlvalidate = new HtmlValidate()
const { ignore } = options
options.isLocal = !options.data
const validatorOptions = await config(options)
const { body } = validatorOptions
const report = htmlvalidate.validateString(body)
let { valid, errorCount, warningCount, results } = report
let isValid = valid
const messages = results.length > 0 ? results[0].messages : []
let errors = messages.filter(message => message.severity === 2)
let warnings = messages.filter(message => message.severity === 1)

if (ignore) {
console.log('Lets ignore')
errors = filterMessages(errors, ignore)
warnings = filterMessages(warnings, ignore)
isValid = errors.length === 0
errorCount = errors.length
warningCount = warnings.length
}

return {
isValid,
errorCount,
warningCount,
errors,
warnings
}
}

0 comments on commit 26e19c7

Please sign in to comment.