wcag-scanner 1.2.2 → 1.2.3

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.
package/dist/cli/index.js CHANGED
@@ -80,12 +80,26 @@ commander_1.program
80
80
  }
81
81
  console.log(`Scanning URL: ${urlString}`);
82
82
  // Fetch the URL content
83
- const html = await fetchUrl(urlString);
83
+ let html;
84
+ try {
85
+ html = await fetchUrl(urlString);
86
+ }
87
+ catch (error) {
88
+ if (error instanceof Error) {
89
+ console.error(`Error fetching URL: ${error.message}`);
90
+ }
91
+ else {
92
+ console.error('Error scanning URL:', error);
93
+ }
94
+ console.log('Trying to continue with partial content...');
95
+ html = await fetchUrlWithFallback(urlString);
96
+ }
84
97
  const scannerOptions = {
85
98
  level: options.level,
86
99
  verbose: options.verbose || false,
87
100
  baseUrl: urlString,
88
- rules: options.rules ? options.rules.split(',') : undefined
101
+ rules: options.rules ? options.rules.split(',') : undefined,
102
+ ignoreScriptErrors: true
89
103
  };
90
104
  const results = await (0, __1.scanHtml)(html, scannerOptions);
91
105
  // Generate report
@@ -181,3 +195,43 @@ if (process.argv.length <= 2) {
181
195
  }
182
196
  // Parse command line arguments
183
197
  commander_1.program.parse(process.argv);
198
+ /**
199
+ * Fallback function to fetch a URL when the primary method fails
200
+ * @param urlString The URL to fetch
201
+ * @returns Promise<string> HTML content
202
+ */
203
+ async function fetchUrlWithFallback(urlString) {
204
+ return new Promise((resolve, reject) => {
205
+ try {
206
+ // Use a simpler approach that ignores some errors
207
+ const url = new url_1.URL(urlString);
208
+ const options = {
209
+ method: 'GET',
210
+ headers: {
211
+ 'User-Agent': 'Mozilla/5.0 (WCAG Scanner Bot) Chrome/91.0.4472.124'
212
+ },
213
+ timeout: 10000
214
+ };
215
+ const client = url.protocol === 'https:' ? https_1.default : http_1.default;
216
+ const req = client.request(url, options, (res) => {
217
+ let data = '';
218
+ res.on('data', (chunk) => {
219
+ data += chunk;
220
+ });
221
+ res.on('end', () => {
222
+ // Return whatever we got, even if status code isn't 200
223
+ resolve(data || '<html><body><p>Failed to fetch content</p></body></html>');
224
+ });
225
+ });
226
+ req.on('error', () => {
227
+ // Provide minimal HTML if we can't fetch anything
228
+ resolve('<html><body><p>Failed to fetch content</p></body></html>');
229
+ });
230
+ req.end();
231
+ }
232
+ catch (error) {
233
+ // Return minimal HTML on any error
234
+ resolve('<html><body><p>Failed to fetch content</p></body></html>');
235
+ }
236
+ });
237
+ }
package/dist/scanner.js CHANGED
@@ -40,7 +40,20 @@ class WCAGScanner {
40
40
  this.dom = new jsdom_1.JSDOM(html, {
41
41
  url: baseUrl,
42
42
  resources: 'usable',
43
- runScripts: 'dangerously'
43
+ runScripts: 'dangerously',
44
+ beforeParse(window) {
45
+ window.addEventListener('error', (event) => {
46
+ console.log(`Ignored script error: ${event.message}`);
47
+ event.preventDefault();
48
+ });
49
+ // Mock modern browser APIs if needed
50
+ if (!window.ReadableStream) {
51
+ window.ReadableStream = class MockReadableStream {
52
+ constructor() { }
53
+ getReader() { return { read: () => { } }; }
54
+ };
55
+ }
56
+ }
44
57
  });
45
58
  this.document = this.dom.window.document;
46
59
  this.window = this.dom.window;
@@ -49,6 +62,11 @@ class WCAGScanner {
49
62
  }
50
63
  catch (error) {
51
64
  console.error('Error loading HTML:', error);
65
+ if (this.dom) {
66
+ this.document = this.dom.window.document;
67
+ this.window = this.dom.window;
68
+ return true;
69
+ }
52
70
  return false;
53
71
  }
54
72
  }
@@ -64,24 +82,34 @@ class WCAGScanner {
64
82
  * Load built-in rules
65
83
  */
66
84
  async loadRules() {
67
- const rulesDir = path_1.default.join(__dirname, 'rules');
68
- // Skip in a test environment
69
- if (!fs_1.default.existsSync(rulesDir))
85
+ // Skip this process if running from the declaration files
86
+ if (__filename.endsWith(".d.ts"))
70
87
  return;
71
- const ruleFiles = fs_1.default.readdirSync(rulesDir)
72
- .filter(file => file.endsWith('.js') || file.endsWith('.ts'));
73
- for (const file of ruleFiles) {
74
- try {
75
- const ruleName = path_1.default.basename(file, path_1.default.extname(file));
76
- const rulePath = path_1.default.join(rulesDir, file);
77
- const rule = require(rulePath).default;
78
- if (rule && typeof rule.check === 'function') {
79
- this.registerRule(ruleName, rule);
88
+ try {
89
+ const rulesDir = path_1.default.join(__dirname, 'rules');
90
+ // Skip in a test environment
91
+ if (!fs_1.default.existsSync(rulesDir))
92
+ return;
93
+ const ruleFiles = fs_1.default.readdirSync(rulesDir)
94
+ .filter(file => file.endsWith('.js') || file.endsWith('.d.ts'));
95
+ for (const file of ruleFiles) {
96
+ try {
97
+ const ruleName = path_1.default.basename(file, '.js');
98
+ const rulePath = path_1.default.join(rulesDir, file);
99
+ // Import the rule module properly
100
+ const ruleModule = require(rulePath);
101
+ const rule = ruleModule.default || ruleModule;
102
+ if (rule && typeof rule.check === 'function') {
103
+ this.registerRule(ruleName, rule);
104
+ }
105
+ }
106
+ catch (error) {
107
+ console.error(`Error loading rule from ${file}:`, error);
80
108
  }
81
109
  }
82
- catch (error) {
83
- console.error(`Error loading rule from ${file}:`, error);
84
- }
110
+ }
111
+ catch (error) {
112
+ console.error('Error loading rules:', error);
85
113
  }
86
114
  }
87
115
  /**
@@ -12,6 +12,8 @@ export interface ScannerOptions {
12
12
  baseUrl?: string;
13
13
  /** Enable verbose output */
14
14
  verbose?: boolean;
15
+ /** Ignore JavaScript errors in scanned pages */
16
+ ignoreScriptErrors?: boolean;
15
17
  }
16
18
  /**
17
19
  * Element information for reporting
package/package.json CHANGED
@@ -1,8 +1,8 @@
1
1
  {
2
2
  "name": "wcag-scanner",
3
- "version": "1.2.2",
3
+ "version": "1.2.3",
4
4
  "description": "Scan HTML for WCAG accessibility violations with AI-powered fix suggestions",
5
- "main": "main/index.js",
5
+ "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
7
7
  "files": [
8
8
  "dist",