Skip to content

Commit

Permalink
Moved RegEx. More forgiving switch statements
Browse files Browse the repository at this point in the history
  • Loading branch information
Max van der Schee committed Nov 16, 2018
1 parent 9f67765 commit 21966ce
Show file tree
Hide file tree
Showing 8 changed files with 68 additions and 57 deletions.
15 changes: 15 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# CHANGELOG
## Version 0.1.1
* Moved RegEx patterns to Patterns.ts
* Refactored RegEx to be more readable
* Changed switch statements to be more forgiving with typos

## Version 0.1.0
* Added RegEx search qeuries for `<div>, <span>, <a>, <img>, <meta>, <html>`
* Added messages for above coding errors

## Version 0.0.2
* Changed extension type to `language-server`

## Version 0.0.1
* Auto-generated extension for Visual Studio Code with [Yocode](https://code.visualstudio.com/docs/extensions/yocode)
13 changes: 1 addition & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,7 @@ The following Issues will be dealt with soon.
* More informing messages with links to resources.

## Release Notes
All notable changes to this project will be documented here.

### 0.0.1
* Auto-generated extension for Visual Studio Code with [Yocode](https://code.visualstudio.com/docs/extensions/yocode)

### 0.0.2
* Changed extension type to `language-server`

### 0.1.0
* Added RegEx search qeuries for `<div>, <span>, <a>, <img>, <meta>, <html>`
* Added messages for above coding errors

All notable changes to this project will be documented in the [Changelog](./CHANGELOG.md).

## License
This resource can be used freely if integrated or build upon in personal or commercial projects such as websites, web apps and web templates intended for sale. It is not allowed to take the resource "as-is" and sell it, redistribute, re-publish it, or sell "pluginized" versions of it. Free plugins built using this resource should have a visible mention and link to the original work. Always consider the licenses of all included libraries, scripts and images used.
Expand Down
9 changes: 3 additions & 6 deletions client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,11 @@
"description": "Web Accessibility for Visual Studio Code",
"author": "MaxvanderSchee",
"license": "MIT",
"version": "0.0.2",
"version": "0.1.1",
"publisher": "MaxvanderSchee",
"repository": {
"type": "git",
"url": "https://github.com/mvdschee/web-accessibility"
},
"repository": "https://github.com/mvdschee/web-accessibility",
"engines": {
"vscode": "^1.23.0"
"vscode": "^1.25.0"
},
"scripts": {
"update-vscode": "vscode-install",
Expand Down
2 changes: 1 addition & 1 deletion client/src/extension.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*! extension.ts - v0.1.0 - 2018
/*! extension.ts
* Flamingos are pretty badass!
* Copyright (c) 2018 Max van der Schee; Licensed MIT */

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "web-accessibility",
"displayName": "Web Accessibility",
"description": "Web Accessibility for Visual Studio Code",
"version": "0.1.0",
"version": "0.1.1",
"publisher": "MaxvanderSchee",
"repository": "https://github.com/mvdschee/web-accessibility",
"engines": {
Expand Down
7 changes: 2 additions & 5 deletions server/package.json
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
{
"name": "web-accessibility-server",
"description": "Web Accessibility for Visual Studio Code",
"version": "0.0.2",
"version": "0.1.1",
"author": "MaxvanderSchee",
"license": "MIT",
"engines": {
"node": "*"
},
"repository": {
"type": "git",
"url": "https://github.com/mvdschee/web-accessibility"
},
"repository": "https://github.com/mvdschee/web-accessibility",
"dependencies": {
"vscode-languageserver": "^4.1.3"
},
Expand Down
15 changes: 15 additions & 0 deletions server/src/Patterns.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/*! Patterns.ts
* Flamingos are pretty badass!
* Copyright (c) 2018 Max van der Schee; Licensed MIT */

// Order based om most common types first
const patterns: string[] = [
"<div(?:[\\s\\S]*?[^-?])>",
"<span(?:[\\s\\S]*?[^-?])>",
"<a(?:.|\\n)*?>(?:.|\\n)*?<\/a>",
"<img(?:[\\s\\S]*?[^-?])>",
"<meta(?:[\\s\\S]*?[^-?])>",
"<html(?:[\\s\\S]*?[^-?])>"
];

export const pattern: RegExp = new RegExp(patterns.join('|'), 'ig');
62 changes: 30 additions & 32 deletions server/src/server.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*! server.ts - v0.1.0 - 2018
/*! server.ts
* Flamingos are pretty badass!
* Copyright (c) 2018 Max van der Schee; Licensed MIT */

Expand All @@ -12,6 +12,7 @@ import {
InitializeParams,
DidChangeConfigurationNotification
} from 'vscode-languageserver';
import { pattern } from './Patterns';

// Create a connection for the server. The connection uses Node's IPC as a transport.
// Also include all preview / proposed LSP features.
Expand Down Expand Up @@ -121,68 +122,61 @@ async function validateTextDocument(textDocument: TextDocument): Promise<void> {

// The validator creates diagnostics for all errors and more
let text = textDocument.getText();
// order based om most common types: div span a img meta html
let pattern: RegExp = /<div(?:[\s\S]*?[^-?])>|<span(?:[\s\S]*?[^-?])>|<a(?:.|\n)*?>(?:.|\n)*?<\/a>|<img(?:[\s\S]*?[^-?])>|<meta(?:[\s\S]*?[^-?])>|<html(?:[\s\S]*?[^-?])>/ig;
let m: RegExpExecArray | null;
let problems = 0;
let diagnostics: Diagnostic[] = [];

while ((m = pattern.exec(text)) && problems < settings.maxNumberOfProblems) {
m.forEach(el => {
if (el != undefined){
// problemsRecognition(el);
switch (true) {
// Div
case (/<div/.test(el) && !/role/i.test(el)):
case (/<div/.test(el) && !/role="(?:\s*?[a-z]+\s*?)"/i.test(el)):
problems++;
_diagnostics('try using Semantic HTML instead of <div> or specify a WAI-ARIA role=""');
_diagnostics('Use Semantic HTML5 or specify a WAI-ARIA role=""');
break;
// Span
case (/<span/.test(el) && !/role/i.test(el)):
if (/button/.test(el)) {
case (/<span/.test(el) && !/role="(?:\s*?[a-z]+\s*?)"/i.test(el)):
if (/<span(?:[\s\S]*?[^-?])button(?:[\s\S]*?[^-?])>/.test(el)) {
problems++;
_diagnostics('Change the <span> to a <button>');
_diagnostics('Change to a <button>');
} else {
problems++;
_diagnostics('Set a WAI-ARIA role="" for the <span>');
_diagnostics('Provide a WAI-ARIA role=""');
}
break;
// Links
case (/<a/.test(el) && !/>[a-z]+</i.test(el)):
case (/<a/.test(el) && !/>(?:\s*?[a-z]+\s*?)</i.test(el)):
problems++;
_diagnostics('<a> should have a descriptive text between the tags');
_diagnostics('Provide a descriptive text between the tags');
break;
// Images
case (/<img/.test(el) && !/alt="[a-z]+"/i.test(el)):
case (/<img/.test(el) && !/alt="(?:\s*?[a-z]+\s*?)"/i.test(el)):
problems++;
_diagnostics('<img> should have an alt="" text that describes the image');
_diagnostics('Provide an alt="" text that describes the image');
break;
// Meta
case (/<meta(?:[\s\S]*?[^-?])name="viewport"/.test(el) && !/scalable="yes"/i.test(el)):
problems++;
_diagnostics('set pinching to zoom with user-scalable="yes"');
case (/<meta(?:[\s\S]*?[^-?])name="viewport"/.test(el)):
if (!/scalable=(?:\s*?yes\s*?)/i.test(el)) {
problems++;
_diagnostics('Enable pinching to zoom with user-scalable=yes');
}
if (/maximum-scale=(?:\s*?1)/i.test(el)) {
problems++;
_diagnostics('Avoid using maximum-scale=1');
}
break;
// HTML
case (/<html/.test(el) && !/lang="[a-z]+"/i.test(el)):
case (/<html/.test(el) && !/lang="(?:\s*?[a-z]+\s*?)"/i.test(el)):
problems++;
_diagnostics('set the language for your websites content with lang=""');
_diagnostics('Provide a language with lang=""');
break;
// Head
// case (/<head/.test(el)):
// if(/<meta name="viewport"/.test(el) && !/scalable="yes"/i.test(el)) {
// problems++;
// _diagnostics('Consider setting pinching to zoom with user-scalable="yes"');
// }

// if (!/<title/i.test(el)) {
// problems++;
// _diagnostics('Consider setting a <title> in the <head> tags');
// }
// break;
default:
break;
}

}

connection.console.log(problems.toString());
});

async function _diagnostics(diagnosticsMessage: string) {
Expand All @@ -204,6 +198,10 @@ async function validateTextDocument(textDocument: TextDocument): Promise<void> {
connection.sendDiagnostics({ uri: textDocument.uri, diagnostics });
}

// async function problemsRecognition(el: string) {

// }

connection.onDidChangeWatchedFiles(_change => {
// Monitored files have change in VSCode
connection.console.log('We received an file change event');
Expand Down

0 comments on commit 21966ce

Please sign in to comment.