svelte-rtf-editor 1.8.0 → 1.8.2
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/rtf-parser.js +23 -5
- package/package.json +1 -1
package/dist/rtf-parser.js
CHANGED
|
@@ -185,13 +185,16 @@ function tokenize(rtf) {
|
|
|
185
185
|
if (i >= len)
|
|
186
186
|
break;
|
|
187
187
|
const next = rtf[i];
|
|
188
|
-
// Hex escape \'xx
|
|
188
|
+
// Hex escape \'xx. One without two hex digits after it is damaged: it is dropped, and
|
|
189
|
+
// what follows it is read as usual, rather than turned into a NUL or a control character.
|
|
189
190
|
if (next === "'") {
|
|
190
191
|
i++;
|
|
191
192
|
const hex = rtf.substring(i, i + 2);
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
193
|
+
if (/^[0-9a-fA-F]{2}$/.test(hex)) {
|
|
194
|
+
i += 2;
|
|
195
|
+
const code = parseInt(hex, 16);
|
|
196
|
+
tokens.push({ type: 'text', value: String.fromCharCode(WINDOWS_1252_C1[code - 0x80] ?? code) });
|
|
197
|
+
}
|
|
195
198
|
}
|
|
196
199
|
// Special escapes
|
|
197
200
|
else if (next === '\\' || next === '{' || next === '}') {
|
|
@@ -274,18 +277,33 @@ function tokenize(rtf) {
|
|
|
274
277
|
return tokens;
|
|
275
278
|
}
|
|
276
279
|
// ── AST Builder ──
|
|
280
|
+
/**
|
|
281
|
+
* The deepest group the tree holds. The renderer walks groups recursively, and thousands of
|
|
282
|
+
* nested groups (damaged or hostile input) would overflow the stack; real documents nest a few
|
|
283
|
+
* dozen deep at most. Groups below this depth are folded into the one at it: their text is kept,
|
|
284
|
+
* and their formatting carries on to the end of that group.
|
|
285
|
+
*/
|
|
286
|
+
const MAX_GROUP_DEPTH = 500;
|
|
277
287
|
function buildTree(tokens) {
|
|
278
288
|
const root = { type: 'group', children: [] };
|
|
279
289
|
const stack = [root];
|
|
290
|
+
/** Groups opened past MAX_GROUP_DEPTH and not closed yet. */
|
|
291
|
+
let folded = 0;
|
|
280
292
|
for (const tok of tokens) {
|
|
281
293
|
const current = stack[stack.length - 1];
|
|
282
294
|
if (tok.type === 'open') {
|
|
295
|
+
if (stack.length > MAX_GROUP_DEPTH) {
|
|
296
|
+
folded++;
|
|
297
|
+
continue;
|
|
298
|
+
}
|
|
283
299
|
const group = { type: 'group', children: [] };
|
|
284
300
|
current.children.push(group);
|
|
285
301
|
stack.push(group);
|
|
286
302
|
}
|
|
287
303
|
else if (tok.type === 'close') {
|
|
288
|
-
if (
|
|
304
|
+
if (folded > 0)
|
|
305
|
+
folded--;
|
|
306
|
+
else if (stack.length > 1)
|
|
289
307
|
stack.pop();
|
|
290
308
|
}
|
|
291
309
|
else if (tok.type === 'control') {
|
package/package.json
CHANGED