-
-
Notifications
You must be signed in to change notification settings - Fork 229
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d2a1438
commit 32d33ee
Showing
6 changed files
with
117 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { sanitizeOnPaste } from "./sanitizeOnPaste"; | ||
|
||
describe("sanitizeOnPaste", () => { | ||
test("returns null for valid text", () => { | ||
expect(sanitizeOnPaste("hello world")).toBe(null); | ||
}); | ||
test("returns text with escaped parentheses for invalid text", () => { | ||
expect(sanitizeOnPaste("hello (world)")).toBe("hello \\(world\\)"); | ||
}); | ||
|
||
test("returns escaped for multiple pointers", () => { | ||
expect(sanitizeOnPaste("hello (world) (again)")).toBe( | ||
"hello \\(world\\) \\(again\\)" | ||
); | ||
}); | ||
|
||
test("handles colon", () => { | ||
expect(sanitizeOnPaste("hello: world")).toBe("hello\\: world"); | ||
}); | ||
|
||
test("Edge missing indentation", () => { | ||
expect( | ||
sanitizeOnPaste( | ||
"export function TextEditor({ extendOptions = {}, ...props }: TextEditorProps) {" | ||
) | ||
).toBe( | ||
"export function TextEditor({ extendOptions = {}, ...props }\\: TextEditorProps) {" | ||
); | ||
}); | ||
|
||
test("Pointer bug", () => { | ||
expect( | ||
sanitizeOnPaste(`export function sanitizeOnPaste(text: string) {`) | ||
).toBe(`export function sanitizeOnPaste\\(text\\: string\\) {`); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { parse } from "graph-selector"; | ||
import { isError } from "./helpers"; | ||
|
||
/** | ||
* Given the pasted text, this function checks if it is valid | ||
* Flowchart Fun Syntax. | ||
* - If it returns any errors which indicate | ||
* parentheses issues, it will escape parentheses in the text | ||
* and return the sanitized text. | ||
*/ | ||
export function sanitizeOnPaste(text: string) { | ||
let newText: string | null = null, | ||
hasError = true, | ||
count = 0; | ||
|
||
while (hasError && count < 10) { | ||
count++; | ||
try { | ||
parse(getText()); | ||
hasError = false; | ||
} catch (error) { | ||
if (isError(error)) { | ||
if (error.message.includes("pointer")) { | ||
newText = getText().replace(/[()]/g, "\\$&"); | ||
} else if (error.message.includes("label without parent")) { | ||
newText = getText().replace(/:/g, "\\:"); | ||
} else if (error.message.includes("missing indentation")) { | ||
newText = getText().replace(/:/g, "\\:"); | ||
} else { | ||
console.log(error.message); | ||
} | ||
} | ||
} | ||
} | ||
return newText; | ||
|
||
function getText() { | ||
return newText ? newText : text; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters