|
1 | | -var request = require('superagent'); |
2 | | -var proxyRequest = require('superagent-proxy')(request); |
3 | | - |
4 | | -var fs = require('fs'); |
5 | | - |
6 | | -var w3cCheckUrl = 'http://validator.w3.org/nu/'; |
7 | | -var defaultOutput = 'json'; |
8 | | -var defaultDoctype = null; |
9 | | -var defaultCharset = null; |
10 | | -var defaultProxy = null; |
11 | | - |
12 | | -var defaultCallback = function (res) { |
13 | | - console.log(res); |
14 | | -} |
15 | | - |
16 | | -function setW3cCheckUrl(newW3cCheckUrl) { |
17 | | - w3cCheckUrl = newW3cCheckUrl; |
18 | | -} |
19 | | - |
20 | | -function validate(options) { |
21 | | - var output = options.output || defaultOutput; |
22 | | - var callback = options.callback || defaultCallback; |
23 | | - var doctype = options.doctype || defaultDoctype; |
24 | | - var charset = options.charset || defaultCharset; |
25 | | - var file = options.file; |
26 | | - var input = options.input; |
27 | | - var context = ''; |
28 | | - |
29 | | - var type; |
30 | | - if(typeof input !== 'undefined'){ |
31 | | - type = 'string'; |
32 | | - context = input; |
33 | | - } else if(typeof file !== 'undefined' && (file.substr(0,5) === 'http:' || file.substr(0, 6) === 'https:')){ |
34 | | - type = 'remote'; |
35 | | - context = file; |
36 | | - } else if(typeof file !== 'undefined'){ |
37 | | - type = 'local'; |
38 | | - context = file; |
39 | | - } else { |
40 | | - return false; |
41 | | - } |
42 | | - |
43 | | - var req = getRequest(type !== 'remote', options); |
44 | | - |
45 | | - if(type === 'remote') { |
46 | | - req.query({ out: output }); |
47 | | - req.query({ doc: file }); |
48 | | - } else { |
49 | | - req.query({ out: output }); |
50 | | - req.send((type === 'local') ? fs.readFileSync(file, 'utf8') : input + ""); |
51 | | - }; |
52 | | - req.end(function(error, res){ |
53 | | - if(error) { |
54 | | - callback(error); |
55 | | - } else if(output === 'json'){ |
56 | | - res.body.context = context; |
57 | | - callback(null, res.body); |
58 | | - } else { |
59 | | - callback(null, res.text); |
60 | | - } |
61 | | - }); |
62 | | -} |
63 | | - |
64 | | -var getRequest = function(isLocal, options) { |
65 | | - var req = isLocal ? proxyRequest.post(w3cCheckUrl) : proxyRequest.get(w3cCheckUrl); |
66 | | - |
67 | | - var proxy = options.proxy || defaultProxy; |
68 | | - if (proxy !== null) { |
69 | | - req.proxy(proxy); |
70 | | - } |
71 | | - |
72 | | - req.set('User-Agent', 'w3cjs - npm module'); |
73 | | - req.set('Content-Type', 'text/html; encoding=utf-8'); |
74 | | - |
75 | | - return req; |
76 | | -} |
77 | | - |
78 | | -var w3cjs = { |
79 | | - validate: validate, |
80 | | - setW3cCheckUrl: setW3cCheckUrl |
81 | | -} |
82 | | -if (typeof exports !== 'undefined') { |
83 | | - if (typeof module !== 'undefined' && module.exports) { |
84 | | - exports = module.exports = w3cjs; |
85 | | - } |
86 | | - exports.w3cjs = w3cjs |
87 | | -} else { |
88 | | - root['w3cjs'] = w3cjs; |
89 | | -} |
| 1 | +//! W3C HTML Validator v0.5.0 ~ github.com/center-key/w3c-html-validator ~ MIT License |
| 2 | + |
| 3 | +import { readFileSync } from 'fs'; |
| 4 | +import * as request from 'superagent'; |
| 5 | +import withProxy from 'superagent-proxy'; |
| 6 | +withProxy(request); |
| 7 | +const w3cHtmlValidator = { |
| 8 | + version: '0.5.0', |
| 9 | + w3cCheckUrl: 'https://validator.w3.org/nu/', |
| 10 | + setW3cCheckUrl(newW3cCheckUrl) { |
| 11 | + w3cHtmlValidator.w3cCheckUrl = newW3cCheckUrl; |
| 12 | + }, |
| 13 | + validate(options) { |
| 14 | + const defaults = { |
| 15 | + output: 'json', |
| 16 | + doctype: null, |
| 17 | + charset: null, |
| 18 | + proxy: null, |
| 19 | + callback: (response) => console.log(response), |
| 20 | + }; |
| 21 | + const settings = { ...defaults, ...options }; |
| 22 | + const checkUrl = w3cHtmlValidator.w3cCheckUrl; |
| 23 | + const getRequest = (isLocal) => { |
| 24 | + const req = isLocal ? request.default.post(checkUrl) : request.default.get(checkUrl); |
| 25 | + if (settings.proxy) |
| 26 | + req.proxy(settings.proxy); |
| 27 | + req.set('User-Agent', 'w3c-html-validator'); |
| 28 | + req.set('Content-Type', 'text/html; encoding=utf-8'); |
| 29 | + return req; |
| 30 | + }; |
| 31 | + if (!settings.input && !settings.file) |
| 32 | + throw Error('No "input" or "file" specified.'); |
| 33 | + const remoteMode = /^http[s]?:/.test(settings.file); |
| 34 | + const type = settings.input ? 'string' : remoteMode ? 'remote' : 'local'; |
| 35 | + const context = settings.input || settings.file; |
| 36 | + const req = getRequest(type !== 'remote'); |
| 37 | + if (type === 'remote') { |
| 38 | + req.query({ out: settings.output }); |
| 39 | + req.query({ doc: settings.file }); |
| 40 | + } |
| 41 | + else { |
| 42 | + req.query({ out: settings.output }); |
| 43 | + req.send((type === 'local') ? readFileSync(settings.file, 'utf8') : settings.input + ''); |
| 44 | + } |
| 45 | + req.end(function (error, res) { |
| 46 | + if (error) { |
| 47 | + settings.callback(error); |
| 48 | + } |
| 49 | + else if (settings.output === 'json') { |
| 50 | + res.body.context = context; |
| 51 | + settings.callback(null, res.body); |
| 52 | + } |
| 53 | + else { |
| 54 | + settings.callback(null, res.text); |
| 55 | + } |
| 56 | + }); |
| 57 | + }, |
| 58 | +}; |
| 59 | +export { w3cHtmlValidator }; |
0 commit comments