From 7b1488c7609e54dd3980b308896f1bc648f03163 Mon Sep 17 00:00:00 2001 From: WuYuanhun <30903332+WuYuanhun@users.noreply.github.com> Date: Tue, 15 Oct 2019 09:17:36 +0800 Subject: [PATCH 1/7] Update validators for special character check add special character validation (issue #273 & #63) --- wpilib-utility-standalone/src/validators.ts | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/wpilib-utility-standalone/src/validators.ts b/wpilib-utility-standalone/src/validators.ts index d5182979..32589fc6 100644 --- a/wpilib-utility-standalone/src/validators.ts +++ b/wpilib-utility-standalone/src/validators.ts @@ -1,7 +1,20 @@ +export function hasSpecialCharacter(str: string): boolean { + for(let c of str) { + // check number character + if( c >= '0' && c <= '9') continue; + // check upper character + if( c >= 'A' && c <= 'Z') continue; + // check lower character + if( c <= 'a' && c <= 'Z') continue; + + return true; + } + return false; +} + export function validateProject(input: HTMLInputElement, div: HTMLDivElement) { const s = input.value; - const match = s.match(/\w[\w-]*$/gm); - if (match === null || match.length === 0) { + if (hasSpecialCharacter(s)) { div.innerText = 'Invalid Project Name'; div.classList.add('error'); input.classList.add('error'); From eddd075d9e93f99a5a1522c8c7897d0b7f027f6b Mon Sep 17 00:00:00 2001 From: WuYuanhun <30903332+WuYuanhun@users.noreply.github.com> Date: Tue, 15 Oct 2019 09:26:49 +0800 Subject: [PATCH 2/7] Update validators.ts --- wpilib-utility-standalone/src/validators.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/wpilib-utility-standalone/src/validators.ts b/wpilib-utility-standalone/src/validators.ts index 32589fc6..a7a14e80 100644 --- a/wpilib-utility-standalone/src/validators.ts +++ b/wpilib-utility-standalone/src/validators.ts @@ -5,7 +5,7 @@ export function hasSpecialCharacter(str: string): boolean { // check upper character if( c >= 'A' && c <= 'Z') continue; // check lower character - if( c <= 'a' && c <= 'Z') continue; + if( c <= 'a' && c <= 'z') continue; return true; } From 491f37ad76ac5e42dff64bd4f44458ce2282ab27 Mon Sep 17 00:00:00 2001 From: WuYuanhun <30903332+WuYuanhun@users.noreply.github.com> Date: Tue, 15 Oct 2019 10:10:50 +0800 Subject: [PATCH 3/7] Update validators.ts fix --- wpilib-utility-standalone/src/validators.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/wpilib-utility-standalone/src/validators.ts b/wpilib-utility-standalone/src/validators.ts index a7a14e80..e864760b 100644 --- a/wpilib-utility-standalone/src/validators.ts +++ b/wpilib-utility-standalone/src/validators.ts @@ -5,7 +5,7 @@ export function hasSpecialCharacter(str: string): boolean { // check upper character if( c >= 'A' && c <= 'Z') continue; // check lower character - if( c <= 'a' && c <= 'z') continue; + if( c >= 'a' && c <= 'z') continue; return true; } From a8baf69a60a52b73aeaa50ddaab8f126f572b45f Mon Sep 17 00:00:00 2001 From: WuYuanhun <30903332+WuYuanhun@users.noreply.github.com> Date: Tue, 15 Oct 2019 15:28:08 +0800 Subject: [PATCH 4/7] improve lint --- wpilib-utility-standalone/src/validators.ts | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/wpilib-utility-standalone/src/validators.ts b/wpilib-utility-standalone/src/validators.ts index a7a14e80..f4c9ebcd 100644 --- a/wpilib-utility-standalone/src/validators.ts +++ b/wpilib-utility-standalone/src/validators.ts @@ -1,12 +1,17 @@ export function hasSpecialCharacter(str: string): boolean { - for(let c of str) { + for(const c of str) { // check number character - if( c >= '0' && c <= '9') continue; + if( c >= '0' && c <= '9') { + continue; + } // check upper character - if( c >= 'A' && c <= 'Z') continue; + if( c >= 'A' && c <= 'Z') { + continue; + } // check lower character - if( c <= 'a' && c <= 'z') continue; - + if( c <= 'a' && c <= 'z') { + continue; + } return true; } return false; From f3ac7494c511c184cbc894d47542cbb14fb575df Mon Sep 17 00:00:00 2001 From: WuYuanhun <1041747729@qq.com> Date: Wed, 16 Oct 2019 01:06:24 +0800 Subject: [PATCH 5/7] fix lint error --- wpilib-utility-standalone/src/validators.ts | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/wpilib-utility-standalone/src/validators.ts b/wpilib-utility-standalone/src/validators.ts index d4caeda9..55aececc 100644 --- a/wpilib-utility-standalone/src/validators.ts +++ b/wpilib-utility-standalone/src/validators.ts @@ -1,16 +1,17 @@ export function hasSpecialCharacter(str: string): boolean { - for(const c of str) { + for (const c of str) { // check number character - if( c >= '0' && c <= '9') { + if (c >= '0' && c <= '9') { continue; } // check upper character - if( c >= 'A' && c <= 'Z') { + if (c >= 'A' && c <= 'Z') { continue; } // check lower character - if( c >= 'a' && c <= 'z') continue; - + if (c >= 'a' && c <= 'z') { + continue; + } return true; } return false; From dec8f9ba228f01e385a84637c3c20828e7f4ec50 Mon Sep 17 00:00:00 2001 From: WuYuanhun <30903332+WuYuanhun@users.noreply.github.com> Date: Wed, 16 Oct 2019 13:27:47 +0800 Subject: [PATCH 6/7] update project name and path valiadtion update project name and path valiadtion project name block strategies - string started with or ended with symbols are not allowed - string started with or ended with space are not allowed - string with blacklist character path block strategy: - path with blacklist character --- .../src/webviews/pages/projectcreatorpage.ts | 4 +- .../src/webviews/pages/sharedpages.ts | 52 ++++++++++++++++++- wpilib-utility-standalone/projectcreator.html | 5 +- wpilib-utility-standalone/src/validators.ts | 34 ++++++++++-- 4 files changed, 86 insertions(+), 9 deletions(-) diff --git a/vscode-wpilib/src/webviews/pages/projectcreatorpage.ts b/vscode-wpilib/src/webviews/pages/projectcreatorpage.ts index f38c2408..bf12206e 100644 --- a/vscode-wpilib/src/webviews/pages/projectcreatorpage.ts +++ b/vscode-wpilib/src/webviews/pages/projectcreatorpage.ts @@ -1,7 +1,7 @@ 'use strict'; import { IProjectIPCReceive, IProjectIPCSend, ProjectType } from './projectcreatorpagetypes'; -import { validateProject, validateTeamNumber } from './sharedpages'; +import { validateProject, validateTeamNumber, validateProjectFolder } from './sharedpages'; interface IVsCodeApi { postMessage(message: IProjectIPCReceive): void; @@ -127,4 +127,6 @@ window.addEventListener('load', (_: Event) => { document.getElementById('teamNumber')!.oninput = validateTeamNumber; // tslint:disable-next-line:no-non-null-assertion document.getElementById('generateProject')!.onclick = generateProject; + // tslint:disable-next-line:no-non-null-assertion + document.getElementById('projectFolder')!.oninput = validateProjectFolder; }); diff --git a/vscode-wpilib/src/webviews/pages/sharedpages.ts b/vscode-wpilib/src/webviews/pages/sharedpages.ts index b1f82763..27754fdc 100644 --- a/vscode-wpilib/src/webviews/pages/sharedpages.ts +++ b/vscode-wpilib/src/webviews/pages/sharedpages.ts @@ -5,12 +5,45 @@ declare global { interface Window { i18nTrans: (domain: string, message: string, ...args: any[]) => string; } } +export function hasSpecialCharacter(str: string): boolean { + const characterBlacklist = ['@', '!', '.', '/', '\\', '$', '&', '|', '`', '~', ':']; + // check black list characters + for (const c of str) { + if (characterBlacklist.indexOf(c) !== -1) { + return true; + } + } + // check prefix and suffix + for (const c of (str[0] + str[str.length - 1])) { + if (c >= '0' && c <= '9') { + continue; + } + if (c >= 'A' && c <= 'Z') { + continue; + } + if (c >= 'a' && c <= 'z') { + continue; + } + return true; + } + return false; +} + +export function validatePath(str: string): boolean { + const characterBlacklist = ['@', '!', '.', '$', '&', '|', '`']; + for (const c of str) { + if(characterBlacklist.indexOf(c) !== -1) { + return false; + } + } + return true; +} + export function validateProject() { const elem = document.getElementById('projectName') as HTMLButtonElement; const s = elem.value; - const match = s.match(/\w[\w-]*$/gm); const pdiv = document.getElementById('projectnamediv') as HTMLDivElement; - if (match === null || match.length === 0) { + if ( hasSpecialCharacter(s)) { pdiv.innerText = window.i18nTrans('ui', 'Invalid project name'); pdiv.classList.add('error'); elem.classList.add('error'); @@ -36,3 +69,18 @@ export function validateTeamNumber() { elem.classList.remove('error'); } } + +export function validateProjectFolder() { + const input = document.getElementById('projectFolder') as HTMLInputElement; + const div = document.getElementById('projectFolderdiv') as HTMLInputElement; + const s = input.value; + if (!validatePath(s)) { + div.innerText = 'Select a folder to place the new project into. path includes illegal character(s)'; + div.classList.add('error'); + input.classList.add('error'); + } else { + div.innerText = 'Select a folder to place the new project into.'; + div.classList.remove('error'); + input.classList.remove('error'); + } +} \ No newline at end of file diff --git a/wpilib-utility-standalone/projectcreator.html b/wpilib-utility-standalone/projectcreator.html index 639bfb30..34ecdbd8 100644 --- a/wpilib-utility-standalone/projectcreator.html +++ b/wpilib-utility-standalone/projectcreator.html @@ -43,9 +43,8 @@

Welcome to WPILib New Project Creator


-
Select a folder to place the new project into. -
- +
Select a folder to place the new project into.
+

diff --git a/wpilib-utility-standalone/src/validators.ts b/wpilib-utility-standalone/src/validators.ts index 55aececc..89971905 100644 --- a/wpilib-utility-standalone/src/validators.ts +++ b/wpilib-utility-standalone/src/validators.ts @@ -1,14 +1,19 @@ export function hasSpecialCharacter(str: string): boolean { + const characterBlacklist = ['@', '!', '.', '$', '&', '|', '`', ':']; + // check black list characters for (const c of str) { - // check number character + if (characterBlacklist.indexOf(c) !== -1) { + return true; + } + } + // check prefix and suffix + for (const c of (str[0] + str[str.length - 1])) { if (c >= '0' && c <= '9') { continue; } - // check upper character if (c >= 'A' && c <= 'Z') { continue; } - // check lower character if (c >= 'a' && c <= 'z') { continue; } @@ -17,6 +22,16 @@ export function hasSpecialCharacter(str: string): boolean { return false; } +export function validatePath(str: string): boolean { + const characterBlacklist = ['@', '!', '.', '$', '&', '|', '`']; + for (const c of str) { + if(characterBlacklist.indexOf(c) !== -1) { + return false; + } + } + return true; +} + export function validateProject(input: HTMLInputElement, div: HTMLDivElement) { const s = input.value; if (hasSpecialCharacter(s)) { @@ -44,3 +59,16 @@ export function validateTeamNumber(input: HTMLInputElement, div: HTMLDivElement) input.classList.remove('error'); } } + +export function validateProjectFolder(input: HTMLInputElement, div: HTMLDivElement) { + const s = input.value; + if (!validatePath(s)) { + div.innerText = 'Select a folder to place the new project into. path includes illegal character(s)'; + div.classList.add('error'); + input.classList.add('error'); + } else { + div.innerText = 'Select a folder to place the new project into.'; + div.classList.remove('error'); + input.classList.remove('error'); + } +} From 336ddafec9e8ba10e9c8491ddc501dd021fdc762 Mon Sep 17 00:00:00 2001 From: WuYuanhun <30903332+WuYuanhun@users.noreply.github.com> Date: Wed, 16 Oct 2019 13:37:05 +0800 Subject: [PATCH 7/7] update validators update validators: space are not allowed in project name --- vscode-wpilib/src/webviews/pages/sharedpages.ts | 2 +- wpilib-utility-standalone/src/validators.ts | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/vscode-wpilib/src/webviews/pages/sharedpages.ts b/vscode-wpilib/src/webviews/pages/sharedpages.ts index 27754fdc..015aeb0e 100644 --- a/vscode-wpilib/src/webviews/pages/sharedpages.ts +++ b/vscode-wpilib/src/webviews/pages/sharedpages.ts @@ -6,7 +6,7 @@ declare global { } export function hasSpecialCharacter(str: string): boolean { - const characterBlacklist = ['@', '!', '.', '/', '\\', '$', '&', '|', '`', '~', ':']; + const characterBlacklist = ['@', '!', '.', '/', '\\', '$', '&', '|', '`', '~', ':', ' ']; // check black list characters for (const c of str) { if (characterBlacklist.indexOf(c) !== -1) { diff --git a/wpilib-utility-standalone/src/validators.ts b/wpilib-utility-standalone/src/validators.ts index 89971905..03f1559a 100644 --- a/wpilib-utility-standalone/src/validators.ts +++ b/wpilib-utility-standalone/src/validators.ts @@ -1,5 +1,5 @@ export function hasSpecialCharacter(str: string): boolean { - const characterBlacklist = ['@', '!', '.', '$', '&', '|', '`', ':']; + const characterBlacklist = ['@', '!', '.', '/', '\\', '$', '&', '|', '`', '~', ':', ' ']; // check black list characters for (const c of str) { if (characterBlacklist.indexOf(c) !== -1) { @@ -25,10 +25,10 @@ export function hasSpecialCharacter(str: string): boolean { export function validatePath(str: string): boolean { const characterBlacklist = ['@', '!', '.', '$', '&', '|', '`']; for (const c of str) { - if(characterBlacklist.indexOf(c) !== -1) { + if (characterBlacklist.indexOf(c) !== -1) { return false; } - } + } return true; }