Skip to content

Commit 40ca34e

Browse files
committed
Update quoting function to handl non-ascii characters better
1 parent f6eb62c commit 40ca34e

2 files changed

Lines changed: 21 additions & 2 deletions

File tree

ts/a11y/sre/worker_threads.d.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
declare module 'node:worker_threads' {
22
const parentPort: {
33
on(kind: string, listener: (event: Event) => void): void;
4-
once(kind: string, listener: (event: Event) => void): void;
54
postMessage(msg: any): void;
65
};
76
const workerData: any;

ts/adaptors/HTMLAdaptor.ts

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -625,7 +625,6 @@ export class HTMLAdaptor<
625625
) {
626626
const { path, maps, worker } = options;
627627
const file = `${path}/${worker}`;
628-
const quoted = (text: string) => text.replace(/(\\*)([\\'])/g, '$1$1\\$2');
629628
const content = `
630629
self.maps = '${quoted(maps)}';
631630
import('${quoted(file)}');
@@ -639,3 +638,24 @@ export class HTMLAdaptor<
639638
return webworker;
640639
}
641640
}
641+
642+
/**
643+
* Quote any backslashes or single quotes, and turn non-ASCII
644+
* characters into \u{...} so that the result can be inserted into
645+
* single quotes and return the original string when evaluated.
646+
*
647+
* @param {string} text The text to be quoted
648+
* @returns {string} The quoted text
649+
*/
650+
function quoted(text: string): string {
651+
return [...text]
652+
.map((c) => {
653+
if (c === '\\' || c === "'") {
654+
c = '\\' + c;
655+
} else if (c < ' ' || c > '\u007e') {
656+
c = `\\u{${c.codePointAt(0).toString(16)}}`;
657+
}
658+
return c;
659+
})
660+
.join('');
661+
}

0 commit comments

Comments
 (0)