ovr 2.0.4 → 3.0.0
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/app/action.d.ts +1 -1
- package/dist/app/action.js +1 -1
- package/dist/app/context.js +9 -4
- package/dist/app/mw/csrf.d.ts +18 -0
- package/dist/app/mw/csrf.js +26 -0
- package/dist/app/page.d.ts +1 -1
- package/dist/index.d.ts +2 -1
- package/dist/index.js +2 -1
- package/dist/jsx/chunk/index.d.ts +26 -0
- package/dist/jsx/chunk/index.js +60 -0
- package/dist/jsx/index.d.ts +5 -4
- package/dist/jsx/index.js +54 -50
- package/dist/trie/insert-params.js +18 -16
- package/package.json +1 -1
- package/dist/escape/index.d.ts +0 -8
- package/dist/escape/index.js +0 -33
- /package/dist/{memo → app/memo}/index.d.ts +0 -0
- /package/dist/{memo → app/memo}/index.js +0 -0
- /package/dist/{hash/index.d.ts → util/hash.d.ts} +0 -0
- /package/dist/{hash/index.js → util/hash.js} +0 -0
package/dist/app/action.d.ts
CHANGED
|
@@ -13,7 +13,7 @@ export declare class Action<Pattern extends string = string> {
|
|
|
13
13
|
/** POST middleware */
|
|
14
14
|
middleware: Middleware<any>[];
|
|
15
15
|
/** `<form>` component with preset `method` and `action` attributes. */
|
|
16
|
-
Form: (props: FormProps<ExtractParams<Pattern>>) =>
|
|
16
|
+
Form: (props: FormProps<ExtractParams<Pattern>>) => JSX.Element;
|
|
17
17
|
/**
|
|
18
18
|
* @param middleware POST middleware
|
|
19
19
|
*
|
package/dist/app/action.js
CHANGED
package/dist/app/context.js
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { Chunk } from "../jsx/chunk/index.js";
|
|
2
2
|
import { toGenerator } from "../jsx/index.js";
|
|
3
|
-
import {
|
|
3
|
+
import { hash } from "../util/hash.js";
|
|
4
4
|
import { App, } from "./index.js";
|
|
5
|
+
import { Memo } from "./memo/index.js";
|
|
5
6
|
class TagNotFound extends Error {
|
|
6
7
|
constructor(tag) {
|
|
7
8
|
super(`No closing ${tag} tag found`);
|
|
@@ -155,14 +156,18 @@ export class Context {
|
|
|
155
156
|
}
|
|
156
157
|
elements[3] = bodyParts[0];
|
|
157
158
|
elements.push(Page, bodyClose + bodyParts[1]);
|
|
158
|
-
const gen = toGenerator(elements)
|
|
159
|
+
const gen = toGenerator(elements.map((el) => {
|
|
160
|
+
if (typeof el === "string")
|
|
161
|
+
return new Chunk(el, true);
|
|
162
|
+
return el;
|
|
163
|
+
}));
|
|
159
164
|
this.html(new ReadableStream({
|
|
160
165
|
async pull(c) {
|
|
161
166
|
const { value, done } = await gen.next();
|
|
162
167
|
if (done)
|
|
163
168
|
c.close();
|
|
164
169
|
else
|
|
165
|
-
c.enqueue(value);
|
|
170
|
+
c.enqueue(value?.value);
|
|
166
171
|
},
|
|
167
172
|
cancel() {
|
|
168
173
|
gen.return();
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import type { Middleware } from "../index.js";
|
|
2
|
+
/**
|
|
3
|
+
* Basic CSRF protection that checks the request's `method` and `Origin` header.
|
|
4
|
+
*
|
|
5
|
+
* @param options
|
|
6
|
+
* @returns CSRF middleware
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
*
|
|
10
|
+
* ```ts
|
|
11
|
+
* import { csrf } from "ovr";
|
|
12
|
+
*
|
|
13
|
+
* app.use(csrf({ origin: "https://example.com" }));
|
|
14
|
+
* ```
|
|
15
|
+
*/
|
|
16
|
+
export declare const csrf: (options: {
|
|
17
|
+
origin: string | string[];
|
|
18
|
+
}) => Middleware;
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Basic CSRF protection that checks the request's `method` and `Origin` header.
|
|
3
|
+
*
|
|
4
|
+
* @param options
|
|
5
|
+
* @returns CSRF middleware
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
*
|
|
9
|
+
* ```ts
|
|
10
|
+
* import { csrf } from "ovr";
|
|
11
|
+
*
|
|
12
|
+
* app.use(csrf({ origin: "https://example.com" }));
|
|
13
|
+
* ```
|
|
14
|
+
*/
|
|
15
|
+
export const csrf = (options) => {
|
|
16
|
+
if (typeof options.origin === "string")
|
|
17
|
+
options.origin = [options.origin];
|
|
18
|
+
return (c, next) => {
|
|
19
|
+
if (["GET", "HEAD"].includes(c.req.method))
|
|
20
|
+
return next();
|
|
21
|
+
const origin = c.req.headers.get("Origin");
|
|
22
|
+
if (origin && options.origin.includes(origin))
|
|
23
|
+
return next();
|
|
24
|
+
return c.text("Forbidden", 403);
|
|
25
|
+
};
|
|
26
|
+
};
|
package/dist/app/page.d.ts
CHANGED
|
@@ -13,7 +13,7 @@ export declare class Page<Pattern extends string = string> {
|
|
|
13
13
|
/** GET middleware */
|
|
14
14
|
middleware: Middleware<any>[];
|
|
15
15
|
/** `<a>` component with preset `href` attribute. */
|
|
16
|
-
Anchor: (props: AnchorProps<ExtractParams<Pattern>>) =>
|
|
16
|
+
Anchor: (props: AnchorProps<ExtractParams<Pattern>>) => JSX.Element;
|
|
17
17
|
/**
|
|
18
18
|
* @param pattern Route pattern
|
|
19
19
|
* @param middleware GET middleware
|
package/dist/index.d.ts
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
export { jsx, toGenerator, toString, type JSX } from "./jsx/index.js";
|
|
2
|
+
export { Chunk } from "./jsx/chunk/index.js";
|
|
2
3
|
export { Trie, Route } from "./trie/index.js";
|
|
3
4
|
export { App, type Middleware, type Params } from "./app/index.js";
|
|
4
5
|
export { Context } from "./app/context.js";
|
|
5
6
|
export { Page } from "./app/page.js";
|
|
6
7
|
export { Action } from "./app/action.js";
|
|
7
|
-
export {
|
|
8
|
+
export { csrf } from "./app/mw/csrf.js";
|
|
8
9
|
export type * from "./types/index.js";
|
package/dist/index.js
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
export { jsx, toGenerator, toString } from "./jsx/index.js";
|
|
2
|
+
export { Chunk } from "./jsx/chunk/index.js";
|
|
2
3
|
export { Trie, Route } from "./trie/index.js";
|
|
3
4
|
export { App } from "./app/index.js";
|
|
4
5
|
export { Context } from "./app/context.js";
|
|
5
6
|
export { Page } from "./app/page.js";
|
|
6
7
|
export { Action } from "./app/action.js";
|
|
7
|
-
export {
|
|
8
|
+
export { csrf } from "./app/mw/csrf.js";
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
export declare class Chunk {
|
|
2
|
+
#private;
|
|
3
|
+
/**
|
|
4
|
+
* Escapes strings of HTML.
|
|
5
|
+
*
|
|
6
|
+
* @param str String to escape
|
|
7
|
+
* @param attr Set to `true` if the value is an attribute, otherwise it's a string of HTML content
|
|
8
|
+
* @returns Escaped string of HTML
|
|
9
|
+
*/
|
|
10
|
+
static escape(str: string, attr?: boolean): string;
|
|
11
|
+
/** Safe value to render. */
|
|
12
|
+
value: string;
|
|
13
|
+
/**
|
|
14
|
+
* @param html converted to a string
|
|
15
|
+
* @param safe Set to `true` if the HTML is safe and should not be escaped
|
|
16
|
+
*/
|
|
17
|
+
constructor(html: unknown, safe?: boolean);
|
|
18
|
+
/**
|
|
19
|
+
* @returns `value`
|
|
20
|
+
*/
|
|
21
|
+
toString(): string;
|
|
22
|
+
/**
|
|
23
|
+
* @param chunks Chunks to append to the end of the chunk
|
|
24
|
+
*/
|
|
25
|
+
concat(...chunks: Chunk[]): void;
|
|
26
|
+
}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
export class Chunk {
|
|
2
|
+
static #attr = /[&"<]/g;
|
|
3
|
+
static #content = /[&<]/g;
|
|
4
|
+
/**
|
|
5
|
+
* Escapes strings of HTML.
|
|
6
|
+
*
|
|
7
|
+
* @param str String to escape
|
|
8
|
+
* @param attr Set to `true` if the value is an attribute, otherwise it's a string of HTML content
|
|
9
|
+
* @returns Escaped string of HTML
|
|
10
|
+
*/
|
|
11
|
+
static escape(str, attr) {
|
|
12
|
+
// adapted from https://github.com/sveltejs/svelte/blob/main/packages/svelte/src/escaping.js
|
|
13
|
+
const s = String(str ?? "");
|
|
14
|
+
const regex = attr ? Chunk.#attr : Chunk.#content;
|
|
15
|
+
// search starts at beginning of the string
|
|
16
|
+
regex.lastIndex = 0;
|
|
17
|
+
// tracks the position of the last successful match in the input string
|
|
18
|
+
let start = 0;
|
|
19
|
+
let result = "";
|
|
20
|
+
// since `g` flag is used, regex maintains state with each loop,
|
|
21
|
+
// checking from the lastIndex onward each time
|
|
22
|
+
while (regex.test(s)) {
|
|
23
|
+
// index of the match
|
|
24
|
+
const i = regex.lastIndex - 1;
|
|
25
|
+
const match = s[i];
|
|
26
|
+
result +=
|
|
27
|
+
// everything that didn't match during this test
|
|
28
|
+
s.slice(start, i) +
|
|
29
|
+
// replacement
|
|
30
|
+
(match === "&" ? "&" : match === '"' ? """ : "<");
|
|
31
|
+
start = regex.lastIndex;
|
|
32
|
+
}
|
|
33
|
+
return result + s.slice(start);
|
|
34
|
+
}
|
|
35
|
+
/** Safe value to render. */
|
|
36
|
+
value;
|
|
37
|
+
/**
|
|
38
|
+
* @param html converted to a string
|
|
39
|
+
* @param safe Set to `true` if the HTML is safe and should not be escaped
|
|
40
|
+
*/
|
|
41
|
+
constructor(html, safe) {
|
|
42
|
+
const str = String(html ?? "");
|
|
43
|
+
if (safe)
|
|
44
|
+
this.value = str;
|
|
45
|
+
else
|
|
46
|
+
this.value = Chunk.escape(str);
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* @returns `value`
|
|
50
|
+
*/
|
|
51
|
+
toString() {
|
|
52
|
+
return this.value;
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* @param chunks Chunks to append to the end of the chunk
|
|
56
|
+
*/
|
|
57
|
+
concat(...chunks) {
|
|
58
|
+
this.value += chunks.join("");
|
|
59
|
+
}
|
|
60
|
+
}
|
package/dist/jsx/index.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type { MaybeFunction, MaybePromise } from "../types/index.js";
|
|
2
|
+
import { Chunk } from "./chunk/index.js";
|
|
2
3
|
import type { Elements } from "./elements.js";
|
|
3
4
|
export declare namespace JSX {
|
|
4
5
|
interface IntrinsicElements extends Elements {
|
|
@@ -15,7 +16,7 @@ export type FC<Props> = (props: Props) => JSX.Element;
|
|
|
15
16
|
* @param props object containing all the properties and attributes passed to the element or component
|
|
16
17
|
* @returns an async generator that yields parts of HTML
|
|
17
18
|
*/
|
|
18
|
-
export declare function jsx(tag: FC<Props> | string, props: Props): AsyncGenerator<
|
|
19
|
+
export declare function jsx(tag: FC<Props> | string, props: Props): AsyncGenerator<Chunk, void, unknown>;
|
|
19
20
|
/**
|
|
20
21
|
* JSX requires a `Fragment` export to resolve <></>
|
|
21
22
|
*
|
|
@@ -24,12 +25,12 @@ export declare function jsx(tag: FC<Props> | string, props: Props): AsyncGenerat
|
|
|
24
25
|
*/
|
|
25
26
|
export declare function Fragment(props?: {
|
|
26
27
|
children?: JSX.Element;
|
|
27
|
-
}): AsyncGenerator<
|
|
28
|
+
}): AsyncGenerator<Chunk, void, unknown>;
|
|
28
29
|
/**
|
|
29
30
|
* @param element
|
|
30
|
-
* @yields
|
|
31
|
+
* @yields Chunks of HTML as the `Element` resolves.
|
|
31
32
|
*/
|
|
32
|
-
export declare function toGenerator(element: JSX.Element): AsyncGenerator<
|
|
33
|
+
export declare function toGenerator(element: JSX.Element): AsyncGenerator<Chunk, void, unknown>;
|
|
33
34
|
/**
|
|
34
35
|
* Converts a `JSX.Element` into a fully concatenated string of HTML.
|
|
35
36
|
*
|
package/dist/jsx/index.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { Chunk } from "./chunk/index.js";
|
|
1
2
|
import { merge } from "./merge-async-iterables.js";
|
|
2
3
|
// https://developer.mozilla.org/en-US/docs/Glossary/Void_element#self-closing_tags
|
|
3
4
|
const voidElements = new Set([
|
|
@@ -44,16 +45,16 @@ export async function* jsx(tag, props) {
|
|
|
44
45
|
else if (typeof value === "string" ||
|
|
45
46
|
typeof value === "number" ||
|
|
46
47
|
typeof value === "bigint") {
|
|
47
|
-
attrParts.push(` ${key}
|
|
48
|
+
attrParts.push(` ${key}="${Chunk.escape(String(value), true)}"`);
|
|
48
49
|
}
|
|
49
50
|
// otherwise, don't include the attribute
|
|
50
51
|
}
|
|
51
|
-
yield `<${tag}${attrParts.join("")}
|
|
52
|
+
yield new Chunk(`<${tag}${attrParts.join("")}>`, true);
|
|
52
53
|
if (voidElements.has(tag))
|
|
53
54
|
return;
|
|
54
55
|
if (children)
|
|
55
56
|
yield* toGenerator(children);
|
|
56
|
-
yield `</${tag}
|
|
57
|
+
yield new Chunk(`</${tag}>`, true);
|
|
57
58
|
}
|
|
58
59
|
}
|
|
59
60
|
/**
|
|
@@ -67,7 +68,7 @@ export async function* Fragment(props = {}) {
|
|
|
67
68
|
}
|
|
68
69
|
/**
|
|
69
70
|
* @param element
|
|
70
|
-
* @yields
|
|
71
|
+
* @yields Chunks of HTML as the `Element` resolves.
|
|
71
72
|
*/
|
|
72
73
|
export async function* toGenerator(element) {
|
|
73
74
|
if (typeof element === "function")
|
|
@@ -76,12 +77,17 @@ export async function* toGenerator(element) {
|
|
|
76
77
|
element = await element;
|
|
77
78
|
if (element == null || typeof element === "boolean" || element === "")
|
|
78
79
|
return;
|
|
80
|
+
if (element instanceof Chunk) {
|
|
81
|
+
yield element;
|
|
82
|
+
return;
|
|
83
|
+
}
|
|
79
84
|
if (typeof element === "object") {
|
|
80
85
|
if (Symbol.asyncIterator in element) {
|
|
81
86
|
for await (const children of element)
|
|
82
87
|
yield* toGenerator(children);
|
|
88
|
+
return;
|
|
83
89
|
}
|
|
84
|
-
|
|
90
|
+
if (Symbol.iterator in element) {
|
|
85
91
|
const iterator = element[Symbol.iterator]();
|
|
86
92
|
if (typeof iterator.next === "function" &&
|
|
87
93
|
typeof iterator.throw === "function" &&
|
|
@@ -94,55 +100,53 @@ export async function* toGenerator(element) {
|
|
|
94
100
|
break;
|
|
95
101
|
yield* toGenerator(result.value);
|
|
96
102
|
}
|
|
103
|
+
return;
|
|
97
104
|
}
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
queue[current] = "";
|
|
120
|
-
}
|
|
121
|
-
// if it hasn't completed, stop iterating to the next
|
|
122
|
-
if (!complete.has(current))
|
|
123
|
-
break;
|
|
105
|
+
// other iterable - array, set, etc.
|
|
106
|
+
// process children in parallel
|
|
107
|
+
const generators = [];
|
|
108
|
+
while (true) {
|
|
109
|
+
const result = iterator.next();
|
|
110
|
+
if (result.done)
|
|
111
|
+
break;
|
|
112
|
+
generators.push(toGenerator(result.value));
|
|
113
|
+
}
|
|
114
|
+
const queue = new Array(generators.length).fill(null);
|
|
115
|
+
let current = 0;
|
|
116
|
+
const complete = new Set();
|
|
117
|
+
for await (const { index, result: { done, value: chunk }, } of merge(generators)) {
|
|
118
|
+
if (done) {
|
|
119
|
+
complete.add(index);
|
|
120
|
+
if (index === current) {
|
|
121
|
+
while (++current < generators.length) {
|
|
122
|
+
if (queue[current]) {
|
|
123
|
+
// yield whatever is in the next queue even if it hasn't completed yet
|
|
124
|
+
yield queue[current];
|
|
125
|
+
queue[current] = null;
|
|
124
126
|
}
|
|
127
|
+
// if it hasn't completed, stop iterating to the next
|
|
128
|
+
if (!complete.has(current))
|
|
129
|
+
break;
|
|
125
130
|
}
|
|
126
131
|
}
|
|
127
|
-
else if (index === current) {
|
|
128
|
-
yield result.value; // stream the current value directly
|
|
129
|
-
}
|
|
130
|
-
else {
|
|
131
|
-
queue[index] += result.value; // queue the value for later
|
|
132
|
-
}
|
|
133
132
|
}
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
133
|
+
else if (index === current) {
|
|
134
|
+
yield chunk; // stream the current value directly
|
|
135
|
+
}
|
|
136
|
+
else {
|
|
137
|
+
// queue the value for later
|
|
138
|
+
if (queue[index])
|
|
139
|
+
queue[index].concat(chunk);
|
|
140
|
+
else
|
|
141
|
+
queue[index] = chunk;
|
|
142
|
+
}
|
|
137
143
|
}
|
|
144
|
+
// clear the queue
|
|
145
|
+
yield* queue.filter(Boolean);
|
|
146
|
+
return;
|
|
138
147
|
}
|
|
139
|
-
else {
|
|
140
|
-
yield JSON.stringify(element); // avoids things like [object Object]
|
|
141
|
-
}
|
|
142
|
-
}
|
|
143
|
-
else {
|
|
144
|
-
yield String(element); // primitive
|
|
145
148
|
}
|
|
149
|
+
yield new Chunk(element); // primitive or other object
|
|
146
150
|
}
|
|
147
151
|
/**
|
|
148
152
|
* Converts a `JSX.Element` into a fully concatenated string of HTML.
|
|
@@ -156,8 +160,8 @@ export async function* toGenerator(element) {
|
|
|
156
160
|
* @returns A promise that resolves to the concatenated HTML.
|
|
157
161
|
*/
|
|
158
162
|
export const toString = async (element) => {
|
|
159
|
-
const
|
|
160
|
-
for await (const
|
|
161
|
-
|
|
162
|
-
return
|
|
163
|
+
const chunks = [];
|
|
164
|
+
for await (const chunk of toGenerator(element))
|
|
165
|
+
chunks.push(chunk);
|
|
166
|
+
return chunks.join("");
|
|
163
167
|
};
|
|
@@ -3,20 +3,22 @@
|
|
|
3
3
|
* @param params Parameters to insert
|
|
4
4
|
* @returns Resolved pathname with params
|
|
5
5
|
*/
|
|
6
|
-
export const insertParams = (parts, params) =>
|
|
7
|
-
.map((part) => {
|
|
8
|
-
if (part.startsWith(":")) {
|
|
9
|
-
const param = part.slice(1);
|
|
10
|
-
if (!(param in params))
|
|
11
|
-
throw new Error(`Parameter "${param}" did not match pattern..`);
|
|
12
|
-
return params[param];
|
|
13
|
-
}
|
|
6
|
+
export const insertParams = (parts, params) => {
|
|
14
7
|
const wild = "*";
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
8
|
+
return parts
|
|
9
|
+
.map((part) => {
|
|
10
|
+
if (part.startsWith(":")) {
|
|
11
|
+
const param = part.slice(1);
|
|
12
|
+
if (!(param in params))
|
|
13
|
+
throw new Error(`Parameter "${param}" did not match pattern.`);
|
|
14
|
+
return params[param];
|
|
15
|
+
}
|
|
16
|
+
if (part === wild) {
|
|
17
|
+
if (!(wild in params))
|
|
18
|
+
throw new Error("No wildcard parameter found.");
|
|
19
|
+
return params[wild];
|
|
20
|
+
}
|
|
21
|
+
return part;
|
|
22
|
+
})
|
|
23
|
+
.join("/");
|
|
24
|
+
};
|
package/package.json
CHANGED
package/dist/escape/index.d.ts
DELETED
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Escapes strings of HTML.
|
|
3
|
-
*
|
|
4
|
-
* @param value Converted to a string, then escaped.
|
|
5
|
-
* @param attr Set to `true` if the value is an attribute, otherwise it's a string of HTML content
|
|
6
|
-
* @returns Escaped string of HTML
|
|
7
|
-
*/
|
|
8
|
-
export declare const escape: (value: unknown, attr?: boolean) => string;
|
package/dist/escape/index.js
DELETED
|
@@ -1,33 +0,0 @@
|
|
|
1
|
-
const attrRegex = /[&"<]/g;
|
|
2
|
-
const contentRegex = /[&<]/g;
|
|
3
|
-
/**
|
|
4
|
-
* Escapes strings of HTML.
|
|
5
|
-
*
|
|
6
|
-
* @param value Converted to a string, then escaped.
|
|
7
|
-
* @param attr Set to `true` if the value is an attribute, otherwise it's a string of HTML content
|
|
8
|
-
* @returns Escaped string of HTML
|
|
9
|
-
*/
|
|
10
|
-
export const escape = (value, attr) => {
|
|
11
|
-
// copied and adapted from https://github.com/sveltejs/svelte/blob/main/packages/svelte/src/escaping.js
|
|
12
|
-
const s = String(value ?? "");
|
|
13
|
-
const regex = attr ? attrRegex : contentRegex;
|
|
14
|
-
// search starts at beginning of the string
|
|
15
|
-
regex.lastIndex = 0;
|
|
16
|
-
// tracks the position of the last successful match in the input string
|
|
17
|
-
let start = 0;
|
|
18
|
-
let result = "";
|
|
19
|
-
// since `g` flag is used, regex maintains state with each loop,
|
|
20
|
-
// checking from the lastIndex onward each time
|
|
21
|
-
while (regex.test(s)) {
|
|
22
|
-
// index of the match
|
|
23
|
-
const i = regex.lastIndex - 1;
|
|
24
|
-
const match = s[i];
|
|
25
|
-
result +=
|
|
26
|
-
// everything that didn't match during this test
|
|
27
|
-
s.slice(start, i) +
|
|
28
|
-
// replacement
|
|
29
|
-
(match === "&" ? "&" : match === '"' ? """ : "<");
|
|
30
|
-
start = regex.lastIndex;
|
|
31
|
-
}
|
|
32
|
-
return result + s.slice(start);
|
|
33
|
-
};
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|