pptx-templates 0.3.9 → 0.5.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/AI-INSTRUCTOR.md +46 -3
- package/CHANGELOG.md +236 -1
- package/README.md +61 -168
- package/dist/automizer.d.ts +7 -0
- package/dist/automizer.js +168 -176
- package/dist/automizer.js.map +1 -1
- package/dist/classes/content-type-registry.js +23 -36
- package/dist/classes/content-type-registry.js.map +1 -1
- package/dist/classes/element-importer.js +130 -148
- package/dist/classes/element-importer.js.map +1 -1
- package/dist/classes/has-shapes.js +81 -126
- package/dist/classes/has-shapes.js.map +1 -1
- package/dist/classes/layout.js +47 -67
- package/dist/classes/layout.js.map +1 -1
- package/dist/classes/master.js +70 -89
- package/dist/classes/master.js.map +1 -1
- package/dist/classes/placeholder-normalizer.js +48 -37
- package/dist/classes/placeholder-normalizer.js.map +1 -1
- package/dist/classes/related-content-copier.js +65 -86
- package/dist/classes/related-content-copier.js.map +1 -1
- package/dist/classes/shape.js +79 -110
- package/dist/classes/shape.js.map +1 -1
- package/dist/classes/slide-master-remover.js +108 -130
- package/dist/classes/slide-master-remover.js.map +1 -1
- package/dist/classes/slide-notes-copier.js +24 -41
- package/dist/classes/slide-notes-copier.js.map +1 -1
- package/dist/classes/slide.js +112 -141
- package/dist/classes/slide.js.map +1 -1
- package/dist/classes/template.js +82 -117
- package/dist/classes/template.js.map +1 -1
- package/dist/constants/constants.js.map +1 -1
- package/dist/enums/chart-type.js.map +1 -1
- package/dist/enums/content-type-map.js.map +1 -1
- package/dist/enums/element-type.js.map +1 -1
- package/dist/errors.js +5 -5
- package/dist/errors.js.map +1 -1
- package/dist/extensions/cli/index.js +3 -13
- package/dist/extensions/cli/index.js.map +1 -1
- package/dist/extensions/hide-slide/index.js +48 -46
- package/dist/extensions/hide-slide/index.js.map +1 -1
- package/dist/extensions/index.d.ts +13 -3
- package/dist/extensions/index.js +3 -1
- package/dist/extensions/index.js.map +1 -1
- package/dist/extensions/link-scheme/index.d.ts +12 -0
- package/dist/extensions/link-scheme/index.js +40 -0
- package/dist/extensions/link-scheme/index.js.map +1 -0
- package/dist/extensions/master-parts/index.js +36 -49
- package/dist/extensions/master-parts/index.js.map +1 -1
- package/dist/extensions/pptxgenjs-compat/img-broken-png.js.map +1 -1
- package/dist/extensions/pptxgenjs-compat/index.js.map +1 -1
- package/dist/extensions/pptxgenjs-compat/svg-fallback.js +56 -70
- package/dist/extensions/pptxgenjs-compat/svg-fallback.js.map +1 -1
- package/dist/extensions/regexp-escape/index.d.ts +10 -0
- package/dist/extensions/regexp-escape/index.js +15 -0
- package/dist/extensions/regexp-escape/index.js.map +1 -0
- package/dist/extensions/run-merge/index.js +4 -5
- package/dist/extensions/run-merge/index.js.map +1 -1
- package/dist/extensions/shape-geometry/index.js +33 -11
- package/dist/extensions/shape-geometry/index.js.map +1 -1
- package/dist/extensions/shape-geometry/presets.d.ts +1 -1
- package/dist/extensions/shape-geometry/presets.js.map +1 -1
- package/dist/extensions/shape-geometry/types.js.map +1 -1
- package/dist/extensions/template-lint/cli.js +71 -49
- package/dist/extensions/template-lint/cli.js.map +1 -1
- package/dist/extensions/template-lint/config.js +29 -13
- package/dist/extensions/template-lint/config.js.map +1 -1
- package/dist/extensions/template-lint/data-model.js +17 -20
- package/dist/extensions/template-lint/data-model.js.map +1 -1
- package/dist/extensions/template-lint/hints.js.map +1 -1
- package/dist/extensions/template-lint/index.js.map +1 -1
- package/dist/extensions/template-lint/init-cli.d.ts +2 -0
- package/dist/extensions/template-lint/init-cli.js +33 -23
- package/dist/extensions/template-lint/init-cli.js.map +1 -1
- package/dist/extensions/template-lint/lint.d.ts +1 -1
- package/dist/extensions/template-lint/lint.js +83 -64
- package/dist/extensions/template-lint/lint.js.map +1 -1
- package/dist/extensions/template-lint/skeleton.js +5 -6
- package/dist/extensions/template-lint/skeleton.js.map +1 -1
- package/dist/extensions/template-lint/types.js.map +1 -1
- package/dist/extensions/template-lint/usage-cli.js +17 -18
- package/dist/extensions/template-lint/usage-cli.js.map +1 -1
- package/dist/extensions/template-lint/usage.js +8 -9
- package/dist/extensions/template-lint/usage.js.map +1 -1
- package/dist/extensions/template-lint/watch.d.ts +2 -0
- package/dist/extensions/template-lint/watch.js +52 -7
- package/dist/extensions/template-lint/watch.js.map +1 -1
- package/dist/extensions/template-logic/clone.d.ts +2 -16
- package/dist/extensions/template-logic/clone.js +15 -90
- package/dist/extensions/template-logic/clone.js.map +1 -1
- package/dist/extensions/template-logic/commands.d.ts +1 -1
- package/dist/extensions/template-logic/commands.js +12 -11
- package/dist/extensions/template-logic/commands.js.map +1 -1
- package/dist/extensions/template-logic/expression.d.ts +15 -2
- package/dist/extensions/template-logic/expression.js +143 -63
- package/dist/extensions/template-logic/expression.js.map +1 -1
- package/dist/extensions/template-logic/images.js +58 -55
- package/dist/extensions/template-logic/images.js.map +1 -1
- package/dist/extensions/template-logic/index.js +16 -24
- package/dist/extensions/template-logic/index.js.map +1 -1
- package/dist/extensions/template-logic/list-commands.js +8 -19
- package/dist/extensions/template-logic/list-commands.js.map +1 -1
- package/dist/extensions/template-logic/renderer.d.ts +9 -0
- package/dist/extensions/template-logic/renderer.js +480 -477
- package/dist/extensions/template-logic/renderer.js.map +1 -1
- package/dist/extensions/template-logic/types.d.ts +8 -1
- package/dist/extensions/template-logic/types.js.map +1 -1
- package/dist/extensions/template-render/cli.js +34 -22
- package/dist/extensions/template-render/cli.js.map +1 -1
- package/dist/extensions/template-render/index.js +152 -107
- package/dist/extensions/template-render/index.js.map +1 -1
- package/dist/extensions/template-render/prune.d.ts +14 -0
- package/dist/extensions/template-render/prune.js +131 -0
- package/dist/extensions/template-render/prune.js.map +1 -0
- package/dist/extensions/template-render/zip-text.d.ts +9 -0
- package/dist/extensions/template-render/zip-text.js +27 -0
- package/dist/extensions/template-render/zip-text.js.map +1 -0
- package/dist/extensions/template-render/zip-writer.d.ts +16 -0
- package/dist/extensions/template-render/zip-writer.js +151 -0
- package/dist/extensions/template-render/zip-writer.js.map +1 -0
- package/dist/extensions/text-flow/flow.js +183 -207
- package/dist/extensions/text-flow/flow.js.map +1 -1
- package/dist/extensions/text-flow/font-file.d.ts +9 -0
- package/dist/extensions/text-flow/font-file.js +40 -22
- package/dist/extensions/text-flow/font-file.js.map +1 -1
- package/dist/extensions/text-flow/fonts.d.ts +4 -0
- package/dist/extensions/text-flow/fonts.js +67 -23
- package/dist/extensions/text-flow/fonts.js.map +1 -1
- package/dist/extensions/text-flow/index.js.map +1 -1
- package/dist/extensions/text-flow/layout.js +8 -9
- package/dist/extensions/text-flow/layout.js.map +1 -1
- package/dist/extensions/text-flow/slide-styles.js +18 -22
- package/dist/extensions/text-flow/slide-styles.js.map +1 -1
- package/dist/extensions/text-flow/styles.d.ts +7 -1
- package/dist/extensions/text-flow/styles.js +108 -64
- package/dist/extensions/text-flow/styles.js.map +1 -1
- package/dist/helper/archive/archive-fs.d.ts +11 -0
- package/dist/helper/archive/archive-fs.js +195 -142
- package/dist/helper/archive/archive-fs.js.map +1 -1
- package/dist/helper/archive/archive-jszip.js +101 -125
- package/dist/helper/archive/archive-jszip.js.map +1 -1
- package/dist/helper/archive/archive.d.ts +6 -0
- package/dist/helper/archive/archive.js +21 -30
- package/dist/helper/archive/archive.js.map +1 -1
- package/dist/helper/archive/jszip-fast-read.d.ts +26 -0
- package/dist/helper/archive/jszip-fast-read.js +166 -0
- package/dist/helper/archive/jszip-fast-read.js.map +1 -0
- package/dist/helper/cell-id-helper.d.ts +1 -0
- package/dist/helper/cell-id-helper.js +6 -2
- package/dist/helper/cell-id-helper.js.map +1 -1
- package/dist/helper/compute-src-rect.js +4 -5
- package/dist/helper/compute-src-rect.js.map +1 -1
- package/dist/helper/content-tracker.js +86 -114
- package/dist/helper/content-tracker.js.map +1 -1
- package/dist/helper/count-helper.js +48 -78
- package/dist/helper/count-helper.js.map +1 -1
- package/dist/helper/css-style-parser.d.ts +1 -1
- package/dist/helper/css-style-parser.js +1 -2
- package/dist/helper/css-style-parser.js.map +1 -1
- package/dist/helper/file-helper.js +30 -48
- package/dist/helper/file-helper.js.map +1 -1
- package/dist/helper/general-helper.js.map +1 -1
- package/dist/helper/generate/generate-pptxgenjs.js +36 -48
- package/dist/helper/generate/generate-pptxgenjs.js.map +1 -1
- package/dist/helper/html-to-multitext-helper.d.ts +4 -4
- package/dist/helper/html-to-multitext-helper.js +46 -23
- package/dist/helper/html-to-multitext-helper.js.map +1 -1
- package/dist/helper/hyperlink-processor.js +125 -139
- package/dist/helper/hyperlink-processor.js.map +1 -1
- package/dist/helper/image-dimensions.js.map +1 -1
- package/dist/helper/jszip-helper.d.ts +1 -1
- package/dist/helper/jszip-helper.js +40 -33
- package/dist/helper/jszip-helper.js.map +1 -1
- package/dist/helper/layout-index.js +51 -71
- package/dist/helper/layout-index.js.map +1 -1
- package/dist/helper/logger.js +4 -4
- package/dist/helper/logger.js.map +1 -1
- package/dist/helper/media-deduplicator.d.ts +24 -0
- package/dist/helper/media-deduplicator.js +81 -34
- package/dist/helper/media-deduplicator.js.map +1 -1
- package/dist/helper/modify-background-helper.js +46 -46
- package/dist/helper/modify-background-helper.js.map +1 -1
- package/dist/helper/modify-chart-helper.d.ts +1 -1
- package/dist/helper/modify-chart-helper.js +597 -591
- package/dist/helper/modify-chart-helper.js.map +1 -1
- package/dist/helper/modify-cleanup-helper.js +112 -112
- package/dist/helper/modify-cleanup-helper.js.map +1 -1
- package/dist/helper/modify-color-helper.d.ts +1 -1
- package/dist/helper/modify-color-helper.js +53 -53
- package/dist/helper/modify-color-helper.js.map +1 -1
- package/dist/helper/modify-helper.js +25 -25
- package/dist/helper/modify-helper.js.map +1 -1
- package/dist/helper/modify-hyperlink-element.js.map +1 -1
- package/dist/helper/modify-hyperlink-helper.js +152 -158
- package/dist/helper/modify-hyperlink-helper.js.map +1 -1
- package/dist/helper/modify-image-helper.d.ts +1 -1
- package/dist/helper/modify-image-helper.js +121 -132
- package/dist/helper/modify-image-helper.js.map +1 -1
- package/dist/helper/modify-presentation-helper.js +137 -154
- package/dist/helper/modify-presentation-helper.js.map +1 -1
- package/dist/helper/modify-shape-helper.js +262 -262
- package/dist/helper/modify-shape-helper.js.map +1 -1
- package/dist/helper/modify-table-helper.js +95 -84
- package/dist/helper/modify-table-helper.js.map +1 -1
- package/dist/helper/modify-text-helper.d.ts +8 -0
- package/dist/helper/modify-text-helper.js +202 -190
- package/dist/helper/modify-text-helper.js.map +1 -1
- package/dist/helper/modify-xml-helper.js +36 -36
- package/dist/helper/modify-xml-helper.js.map +1 -1
- package/dist/helper/multitext-helper.js +13 -9
- package/dist/helper/multitext-helper.js.map +1 -1
- package/dist/helper/ppt-paths.js +17 -17
- package/dist/helper/ppt-paths.js.map +1 -1
- package/dist/helper/shape-type-detector.js +18 -29
- package/dist/helper/shape-type-detector.js.map +1 -1
- package/dist/helper/text-replace-helper.js +8 -8
- package/dist/helper/text-replace-helper.js.map +1 -1
- package/dist/helper/xml/lnLRTB.js.map +1 -1
- package/dist/helper/xml/prstGeom.js.map +1 -1
- package/dist/helper/xml-clone.d.ts +24 -0
- package/dist/helper/xml-clone.js +269 -0
- package/dist/helper/xml-clone.js.map +1 -0
- package/dist/helper/xml-elements.js +17 -20
- package/dist/helper/xml-elements.js.map +1 -1
- package/dist/helper/xml-find.d.ts +4 -0
- package/dist/helper/xml-find.js +48 -0
- package/dist/helper/xml-find.js.map +1 -0
- package/dist/helper/xml-helper.js +102 -135
- package/dist/helper/xml-helper.js.map +1 -1
- package/dist/helper/xml-parse-cache.d.ts +31 -0
- package/dist/helper/xml-parse-cache.js +71 -0
- package/dist/helper/xml-parse-cache.js.map +1 -0
- package/dist/helper/xml-placeholder-helper.js +10 -10
- package/dist/helper/xml-placeholder-helper.js.map +1 -1
- package/dist/helper/xml-pretty-print.js.map +1 -1
- package/dist/helper/xml-relationship-helper.js +132 -147
- package/dist/helper/xml-relationship-helper.js.map +1 -1
- package/dist/helper/xml-serializer.d.ts +2 -0
- package/dist/helper/xml-serializer.js +192 -0
- package/dist/helper/xml-serializer.js.map +1 -0
- package/dist/helper/xml-slide-helper.js +109 -129
- package/dist/helper/xml-slide-helper.js.map +1 -1
- package/dist/helper/xml-template-helper.js +120 -144
- package/dist/helper/xml-template-helper.js.map +1 -1
- package/dist/helper/xmldom-sax-patch.js +52 -2
- package/dist/helper/xmldom-sax-patch.js.map +1 -1
- package/dist/index.d.ts +4 -4
- package/dist/index.js +4 -2
- package/dist/index.js.map +1 -1
- package/dist/interfaces/iarchive.js.map +1 -1
- package/dist/interfaces/ichart.js.map +1 -1
- package/dist/interfaces/icounter.js.map +1 -1
- package/dist/interfaces/igenerator.js.map +1 -1
- package/dist/interfaces/iimage.js.map +1 -1
- package/dist/interfaces/ilayout.js.map +1 -1
- package/dist/interfaces/imaster.js.map +1 -1
- package/dist/interfaces/imulti-text.js.map +1 -1
- package/dist/interfaces/ipptxgenjs-slide.js.map +1 -1
- package/dist/interfaces/ipresentation-props.js.map +1 -1
- package/dist/interfaces/ishape-action.js.map +1 -1
- package/dist/interfaces/ishape.js.map +1 -1
- package/dist/interfaces/islide.js.map +1 -1
- package/dist/interfaces/itemplate.js.map +1 -1
- package/dist/interfaces/pres-template.js.map +1 -1
- package/dist/interfaces/root-pres-template.js.map +1 -1
- package/dist/modify/modify-chart.d.ts +4 -4
- package/dist/modify/modify-chart.js +35 -23
- package/dist/modify/modify-chart.js.map +1 -1
- package/dist/modify/modify-table.d.ts +0 -1
- package/dist/modify/modify-table.js +19 -27
- package/dist/modify/modify-table.js.map +1 -1
- package/dist/shapes/chart.js +238 -287
- package/dist/shapes/chart.js.map +1 -1
- package/dist/shapes/diagram.d.ts +14 -0
- package/dist/shapes/diagram.js +167 -103
- package/dist/shapes/diagram.js.map +1 -1
- package/dist/shapes/generic.js +62 -83
- package/dist/shapes/generic.js.map +1 -1
- package/dist/shapes/hyperlink.d.ts +0 -1
- package/dist/shapes/hyperlink.js +84 -124
- package/dist/shapes/hyperlink.js.map +1 -1
- package/dist/shapes/image.js +118 -149
- package/dist/shapes/image.js.map +1 -1
- package/dist/shapes/ole.js +148 -188
- package/dist/shapes/ole.js.map +1 -1
- package/dist/types/chart-types.js.map +1 -1
- package/dist/types/modify-types.js.map +1 -1
- package/dist/types/shape-types.js.map +1 -1
- package/dist/types/table-types.js.map +1 -1
- package/dist/types/types.d.ts +7 -0
- package/dist/types/types.js.map +1 -1
- package/dist/types/xml-types.js.map +1 -1
- package/dist/vendor/pptxgenjs/README.md +8 -3
- package/dist/vendor/pptxgenjs/index.js +19 -2
- package/package.json +13 -16
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"clone.js","sourceRoot":"","sources":["../../../src/extensions/template-logic/clone.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"clone.js","sourceRoot":"","sources":["../../../src/extensions/template-logic/clone.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,sEAAsE;AACtE,yDAAuC","sourcesContent":["/** Moved to src/helper/xml-clone.ts (used by the archive as well). */\nexport * from '../../helper/xml-clone';\n"]}
|
|
@@ -13,7 +13,7 @@ export type ParsedCommand = {
|
|
|
13
13
|
*/
|
|
14
14
|
export declare const isTableMarker: (inner: string) => boolean;
|
|
15
15
|
/** Resolved delimiters plus a global regex matching one tag. */
|
|
16
|
-
export declare const tagSyntax: (options?: Pick<RenderTemplateOptions,
|
|
16
|
+
export declare const tagSyntax: (options?: Pick<RenderTemplateOptions, 'openingTag' | 'closingTag' | 'cmdDelimiter'>) => {
|
|
17
17
|
opening: string;
|
|
18
18
|
closing: string;
|
|
19
19
|
pattern: RegExp;
|
|
@@ -1,10 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
3
|
exports.resolveAlias = exports.fixSmartQuotes = exports.parseCommand = exports.isAltTextTemplate = exports.tagSyntax = exports.isTableMarker = void 0;
|
|
7
|
-
const regexp_escape_1 =
|
|
4
|
+
const regexp_escape_1 = require("../regexp-escape");
|
|
8
5
|
const IDENT = '[\\p{L}_$][\\p{L}\\p{N}_$]*';
|
|
9
6
|
/** An operator that needs a value before it: no command's code starts so. */
|
|
10
7
|
const CONTINUES_EXPRESSION = /^(?:[+\-*/%<>=&|?:.,)\]]|!=)/;
|
|
@@ -17,17 +14,22 @@ const isTableMarker = (inner) => /^each:[^\s{}]+$/.test(inner.trim());
|
|
|
17
14
|
exports.isTableMarker = isTableMarker;
|
|
18
15
|
/** Resolved delimiters plus a global regex matching one tag. */
|
|
19
16
|
const tagSyntax = (options) => {
|
|
20
|
-
|
|
21
|
-
const delimiter = options === null || options === void 0 ? void 0 : options.cmdDelimiter;
|
|
17
|
+
const delimiter = options?.cmdDelimiter;
|
|
22
18
|
const [open, close] = Array.isArray(delimiter)
|
|
23
19
|
? delimiter
|
|
24
20
|
: [delimiter, delimiter];
|
|
25
|
-
const opening =
|
|
26
|
-
const closing =
|
|
21
|
+
const opening = options?.openingTag ?? open ?? '{{';
|
|
22
|
+
const closing = options?.closingTag ?? close ?? '}}';
|
|
23
|
+
// an empty opening tag would match everywhere, `(?!)` never
|
|
24
|
+
if (!opening || !closing) {
|
|
25
|
+
throw new Error('The opening and closing tag must not be empty');
|
|
26
|
+
}
|
|
27
27
|
return {
|
|
28
28
|
opening,
|
|
29
29
|
closing,
|
|
30
|
-
|
|
30
|
+
// A tag never contains the opening tag, so an unclosed one scans only to
|
|
31
|
+
// the next opening tag, not to the end of the text (quadratic).
|
|
32
|
+
pattern: new RegExp(`${(0, regexp_escape_1.escapeRegExp)(opening)}((?:(?!${(0, regexp_escape_1.escapeRegExp)(opening)})[\\s\\S])*?)${(0, regexp_escape_1.escapeRegExp)(closing)}`, 'g'),
|
|
31
33
|
};
|
|
32
34
|
};
|
|
33
35
|
exports.tagSyntax = tagSyntax;
|
|
@@ -51,7 +53,6 @@ exports.isAltTextTemplate = isAltTextTemplate;
|
|
|
51
53
|
* resolved by the caller before.
|
|
52
54
|
*/
|
|
53
55
|
const parseCommand = (inner, tag) => {
|
|
54
|
-
var _a;
|
|
55
56
|
const content = inner.replace(/\u00A0/g, ' ').trim();
|
|
56
57
|
if (!content)
|
|
57
58
|
throw new Error(`Empty template tag ${tag}`);
|
|
@@ -73,7 +74,7 @@ const parseCommand = (inner, tag) => {
|
|
|
73
74
|
return { type: 'END-FOR', code: '' };
|
|
74
75
|
// `{{HIDE-SLIDE cond}}` in the speaker notes; without a condition: always
|
|
75
76
|
if ((m = /^HIDE-SLIDE(?:\s+([\s\S]*))?$/i.exec(content))) {
|
|
76
|
-
return { type: 'HIDE-SLIDE', code: (
|
|
77
|
+
return { type: 'HIDE-SLIDE', code: (m[1] ?? '').trim() };
|
|
77
78
|
}
|
|
78
79
|
if ((m = /^(?:EXEC\s+|!)([\s\S]+)$/i.exec(content))) {
|
|
79
80
|
return { type: 'EXEC', code: m[1].trim() };
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"commands.js","sourceRoot":"","sources":["../../../src/extensions/template-logic/commands.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"commands.js","sourceRoot":"","sources":["../../../src/extensions/template-logic/commands.ts"],"names":[],"mappings":";;;AAAA,oDAA0D;AAW1D,MAAM,KAAK,GAAG,6BAA6B,CAAC;AAE5C,6EAA6E;AAC7E,MAAM,oBAAoB,GAAG,8BAA8B,CAAC;AAE5D;;;;GAIG;AACI,MAAM,aAAa,GAAG,CAAC,KAAa,EAAW,EAAE,CACtD,iBAAiB,CAAC,IAAI,CAAC,KAAK,CAAC,IAAI,EAAE,CAAC,CAAC;AAD1B,QAAA,aAAa,GAAb,aAAa,CACa;AAEvC,gEAAgE;AACzD,MAAM,SAAS,GAAG,CACvB,OAGC,EACD,EAAE;IACF,MAAM,SAAS,GAAG,OAAO,EAAE,YAAY,CAAC;IACxC,MAAM,CAAC,IAAI,EAAE,K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,IAAI,CAAC,CAAC,GAAG,yBAAyB,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC,EAAE,CAAC;QAClD,OAAO,EAAE,IAAI,EAAE,KAAK,EAAE,IAAI,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC;IAC5C,CAAC;IACD,OAAO,EAAE,IAAI,EAAE,KAAK,EAAE,IAAI,EAAE,OAAO,EAAE,CAAC;AACxC,CAAC,CAAC;AAhDW,QAAA,YAAY,GAAZ,YAAY,CAgDvB;AAEF;;;GAGG;AACI,MAAM,cAAc,GAAG,CAAC,OAAe,EAAU,EAAE,CACxD,OAAO,CAAC,OAAO,CAAC,QAAQ,EAAE,GAAG,CAAC,CAAC,OAAO,CAAC,QAAQ,EAAE,GAAG,CAAC,CAAC;AAD3C,QAAA,cAAc,GAAd,cAAc,CAC6B;AAExD,8DAA8D;AACvD,MAAM,YAAY,GAAG,CAC1B,KAAa,EACb,OAA4B,EAC5B,GAAW,EACH,EAAE;IACV,MAAM,CAAC,GAAG,IAAI,MAAM,CAAC,YAAY,KAAK,QAAQ,EAAE,GAAG,CAAC,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;IACjE,IAAI,CAAC,CAAC;QAAE,OAAO,KAAK,CAAC;IACrB,MAAM,OAAO,GAAG,OAAO,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;IAClC,IAAI,OAAO,KAAK,SAAS,EAAE,CAAC;QAC1B,MAAM,IAAI,KAAK,CAAC,GAAG,GAAG,oBAAoB,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC;IACrD,CAAC;IACD,OAAO,OAAO,CAAC;AACjB,CAAC,CAAC;AAZW,QAAA,YAAY,GAAZ,YAAY,CAYvB","sourcesContent":["import { escapeRegExp as escape } from '../regexp-escape';\nimport { RenderTemplateOptions, TemplateCommandType } from './types';\n\nexport type ParsedCommand = {\n type: TemplateCommandType;\n /** The command without keyword. */\n code: string;\n /** FOR: loop variable; ALIAS: alias name. */\n name?: string;\n};\n\nconst IDENT = '[\\\\p{L}_$][\\\\p{L}\\\\p{N}_$]*';\n\n/** An operator that needs a value before it: no command's code starts so. */\nconst CONTINUES_EXPRESSION = /^(?:[+\\-*/%<>=&|?:.,)\\]]|!=)/;\n\n/**\n * `{{each:row}}`: a marker for `modify.setTable`'s `expand` (nested\n * tables), not a template command. It is never a valid expression, so\n * renderTemplate leaves it as text for setTable, and the linter skips it.\n */\nexport const isTableMarker = (inner: string): boolean =>\n /^each:[^\\s{}]+$/.test(inner.trim());\n\n/** Resolved delimiters plus a global regex matching one tag. */\nexport const tagSyntax = (\n options?: Pick<\n RenderTemplateOptions,\n 'openingTag' | 'closingTag' | 'cmdDelimiter'\n >,\n) => {\n const delimiter = options?.cmdDelimiter;\n const [open, close] = Array.isArray(delimiter)\n ? delimiter\n : [delimiter, delimiter];\n const opening = options?.openingTag ?? open ?? '{{';\n const closing = options?.closingTag ?? close ?? '}}';\n // an empty opening tag would match everywhere, `(?!)` never\n if (!opening || !closing) {\n throw new Error('The opening and closing tag must not be empty');\n }\n return {\n opening,\n closing,\n // A tag never contains the opening tag, so an unclosed one scans only to\n // the next opening tag, not to the end of the text (quadratic).\n pattern: new RegExp(\n `${escape(opening)}((?:(?!${escape(opening)})[\\\\s\\\\S])*?)${escape(closing)}`,\n 'g',\n ),\n };\n};\n\n/**\n * An alt text is a template only when it holds nothing but tags and one\n * of them is an IMAGE. Any other alt text (`Preis {{ in Euro`,\n * `Foto: {{IMAGE foto}}`, `{{name}}`) stays as it is, as it did before\n * alt texts were rendered. `commandType` gives the command of a tag, or\n * undefined if it does not parse.\n */\nexport const isAltTextTemplate = (\n descr: string,\n pattern: RegExp,\n commandType: (inner: string, tag: string) => string | undefined,\n): boolean => {\n const matches = [...descr.matchAll(pattern)];\n return (\n matches.length > 0 &&\n descr.replace(pattern, '').trim() === '' &&\n matches.some((match) => commandType(match[1], match[0]) === 'IMAGE')\n );\n};\n\n/**\n * Classifies the inside of a tag (`IF a > 1`, `= name`, `item.price`, …),\n * following the docx-templates command set. `*alias` references must be\n * resolved by the caller before.\n */\nexport const parseCommand = (inner: string, tag: string): ParsedCommand => {\n const content = inner.replace(/\\u00A0/g, ' ').trim();\n if (!content) throw new Error(`Empty template tag ${tag}`);\n let m: RegExpExecArray;\n\n if ((m = /^IF\\s+([\\s\\S]+)$/i.exec(content))) {\n return { type: 'IF', code: m[1].trim() };\n }\n if ((m = /^ELSE[-\\s]?IF\\s+([\\s\\S]+)$/i.exec(content))) {\n return { type: 'ELSE-IF', code: m[1].trim() };\n }\n if (/^ELSE$/i.test(content)) return { type: 'ELSE', code: '' };\n if (/^END-?IF\\b/i.test(content)) return { type: 'END-IF', code: '' };\n if (\n (m = new RegExp(`^FOR\\\\s+\\\\$?(${IDENT})\\\\s+IN\\\\s+([\\\\s\\\\S]+)$`, 'iu').exec(\n content,\n ))\n ) {\n return { type: 'FOR', name: m[1], code: m[2].trim() };\n }\n if (/^END-?FOR\\b/i.test(content)) return { type: 'END-FOR', code: '' };\n // `{{HIDE-SLIDE cond}}` in the speaker notes; without a condition: always\n if ((m = /^HIDE-SLIDE(?:\\s+([\\s\\S]*))?$/i.exec(content))) {\n return { type: 'HIDE-SLIDE', code: (m[1] ?? '').trim() };\n }\n if ((m = /^(?:EXEC\\s+|!)([\\s\\S]+)$/i.exec(content))) {\n return { type: 'EXEC', code: m[1].trim() };\n }\n if (\n (m = new RegExp(`^ALIAS\\\\s+(${IDENT})\\\\s+([\\\\s\\\\S]+)$`, 'iu').exec(content))\n ) {\n return { type: 'ALIAS', name: m[1], code: m[2].trim() };\n }\n // `{{link + 1}}`, `{{image ? 'a' : 'b'}}`: data called like a command. What\n // follows continues an expression, so the tag is an insertion.\n if (\n (m = /^(HTML|LINK|IMAGE)\\s+([\\s\\S]+)$/i.exec(content)) &&\n !CONTINUES_EXPRESSION.test(m[2])\n ) {\n return {\n type: m[1].toUpperCase() as 'HTML' | 'LINK' | 'IMAGE',\n code: m[2].trim(),\n };\n }\n if ((m = /^(?:=|INS\\s)([\\s\\S]+)$/i.exec(content))) {\n return { type: 'INS', code: m[1].trim() };\n }\n return { type: 'INS', code: content };\n};\n\n/**\n * docx-templates' `fixSmartQuotes`: typographic quotes from AutoCorrect\n * become straight ones, in the whole command.\n */\nexport const fixSmartQuotes = (command: string): string =>\n command.replace(/[“”„]/g, '\"').replace(/[‘’‚]/g, \"'\");\n\n/** `*name` → the aliased command; anything else unchanged. */\nexport const resolveAlias = (\n inner: string,\n aliases: Map<string, string>,\n tag: string,\n): string => {\n const m = new RegExp(`^\\\\s*\\\\*(${IDENT})\\\\s*$`, 'u').exec(inner);\n if (!m) return inner;\n const command = aliases.get(m[1]);\n if (command === undefined) {\n throw new Error(`${tag}: unknown alias \"${m[1]}\"`);\n }\n return command;\n};\n"]}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Small
|
|
2
|
+
* Small expression language for template tags
|
|
3
3
|
* (`{{IF user.age >= 18}}`, `{{price.toFixed(2)}}`,
|
|
4
4
|
* `{{FOR p IN projects.filter(p => p.active)}}`, …).
|
|
5
5
|
*
|
|
@@ -9,7 +9,8 @@
|
|
|
9
9
|
* calls of functions reachable from the data, arrow functions
|
|
10
10
|
* (`x => x.name`, `(a, b) => a - b`), unary `! - +`, `* / %`, `+ -`,
|
|
11
11
|
* comparisons, `== != === !==`, `&& || ??` and the ternary `? :`.
|
|
12
|
-
* Prototype-reaching keys are rejected.
|
|
12
|
+
* Prototype-reaching keys are rejected. Methods of the data still run, so
|
|
13
|
+
* `items.pop()` changes the caller's array (see docs/template-logic.md).
|
|
13
14
|
*/
|
|
14
15
|
export type TemplateScope = {
|
|
15
16
|
lookup(name: string): {
|
|
@@ -141,6 +142,18 @@ export declare class TemplateExpression {
|
|
|
141
142
|
* building huge strings.
|
|
142
143
|
*/
|
|
143
144
|
private checkCall;
|
|
145
|
+
/**
|
|
146
|
+
* Counts strings and lists an expression produces against MAX_OUTPUT, so
|
|
147
|
+
* doubling (`a.concat(a)`, `s + s`) or a huge `map` stops before it
|
|
148
|
+
* exhausts memory. Lists count their length plus their strings' lengths.
|
|
149
|
+
*/
|
|
150
|
+
private charge;
|
|
151
|
+
/**
|
|
152
|
+
* Rejects a forbidden native reached without a member read, e.g. found in
|
|
153
|
+
* a list (`Object.values(…).find(…)`). Raw match/repeat/… count too: read
|
|
154
|
+
* as a member they would have been wrapped.
|
|
155
|
+
*/
|
|
156
|
+
private checkResult;
|
|
144
157
|
private checkKey;
|
|
145
158
|
private getMember;
|
|
146
159
|
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
/**
|
|
3
|
-
* Small
|
|
3
|
+
* Small expression language for template tags
|
|
4
4
|
* (`{{IF user.age >= 18}}`, `{{price.toFixed(2)}}`,
|
|
5
5
|
* `{{FOR p IN projects.filter(p => p.active)}}`, …).
|
|
6
6
|
*
|
|
@@ -10,30 +10,9 @@
|
|
|
10
10
|
* calls of functions reachable from the data, arrow functions
|
|
11
11
|
* (`x => x.name`, `(a, b) => a - b`), unary `! - +`, `* / %`, `+ -`,
|
|
12
12
|
* comparisons, `== != === !==`, `&& || ??` and the ternary `? :`.
|
|
13
|
-
* Prototype-reaching keys are rejected.
|
|
13
|
+
* Prototype-reaching keys are rejected. Methods of the data still run, so
|
|
14
|
+
* `items.pop()` changes the caller's array (see docs/template-logic.md).
|
|
14
15
|
*/
|
|
15
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
16
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
17
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
18
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
19
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
20
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
21
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
22
|
-
});
|
|
23
|
-
};
|
|
24
|
-
var __await = (this && this.__await) || function (v) { return this instanceof __await ? (this.v = v, this) : new __await(v); }
|
|
25
|
-
var __asyncGenerator = (this && this.__asyncGenerator) || function (thisArg, _arguments, generator) {
|
|
26
|
-
if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
|
|
27
|
-
var g = generator.apply(thisArg, _arguments || []), i, q = [];
|
|
28
|
-
return i = Object.create((typeof AsyncIterator === "function" ? AsyncIterator : Object).prototype), verb("next"), verb("throw"), verb("return", awaitReturn), i[Symbol.asyncIterator] = function () { return this; }, i;
|
|
29
|
-
function awaitReturn(f) { return function (v) { return Promise.resolve(v).then(f, reject); }; }
|
|
30
|
-
function verb(n, f) { if (g[n]) { i[n] = function (v) { return new Promise(function (a, b) { q.push([n, v, a, b]) > 1 || resume(n, v); }); }; if (f) i[n] = f(i[n]); } }
|
|
31
|
-
function resume(n, v) { try { step(g[n](v)); } catch (e) { settle(q[0][3], e); } }
|
|
32
|
-
function step(r) { r.value instanceof __await ? Promise.resolve(r.value.v).then(fulfill, reject) : settle(q[0][2], r); }
|
|
33
|
-
function fulfill(value) { resume("next", value); }
|
|
34
|
-
function reject(value) { resume("throw", value); }
|
|
35
|
-
function settle(f, v) { if (f(v), q.shift(), q.length) resume(q[0][0], q[0][1]); }
|
|
36
|
-
};
|
|
37
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
38
17
|
exports.parseStatements = exports.TemplateExpression = exports.TemplateExpressionError = exports.isForbiddenKey = exports.closesQuote = exports.isQuote = void 0;
|
|
39
18
|
const OPERATORS = [
|
|
@@ -102,14 +81,12 @@ exports.isForbiddenKey = isForbiddenKey;
|
|
|
102
81
|
const CODE_CONSTRUCTORS = [
|
|
103
82
|
Function,
|
|
104
83
|
eval,
|
|
105
|
-
Object.getPrototypeOf(() =>
|
|
84
|
+
Object.getPrototypeOf(async () => undefined).constructor,
|
|
106
85
|
Object.getPrototypeOf(function* () {
|
|
107
86
|
yield 0;
|
|
108
87
|
}).constructor,
|
|
109
|
-
Object.getPrototypeOf(function () {
|
|
110
|
-
|
|
111
|
-
yield yield __await(0);
|
|
112
|
-
});
|
|
88
|
+
Object.getPrototypeOf(async function* () {
|
|
89
|
+
yield 0;
|
|
113
90
|
}).constructor,
|
|
114
91
|
];
|
|
115
92
|
/** `fn.call(…)` and friends would hide the real callee from the checks. */
|
|
@@ -125,7 +102,43 @@ const REGEX_FROM_STRING = [
|
|
|
125
102
|
String.prototype.search,
|
|
126
103
|
];
|
|
127
104
|
/** Longest string `repeat`, `padStart` and `padEnd` may build. */
|
|
128
|
-
const MAX_STRING_LENGTH =
|
|
105
|
+
const MAX_STRING_LENGTH = 1_000_000;
|
|
106
|
+
/** Checked by checkCall; wrapped when read as a value (a native callback). */
|
|
107
|
+
const GUARDED_CALLS = [
|
|
108
|
+
...REGEX_FROM_STRING,
|
|
109
|
+
RegExp.prototype.compile,
|
|
110
|
+
String.prototype.repeat,
|
|
111
|
+
String.prototype.padStart,
|
|
112
|
+
String.prototype.padEnd,
|
|
113
|
+
];
|
|
114
|
+
/**
|
|
115
|
+
* Reach prototypes and property descriptors past the key checks
|
|
116
|
+
* (`getOwnPropertyDescriptor(proto, 'constructor')`); never callable.
|
|
117
|
+
*/
|
|
118
|
+
const REFLECTION = [
|
|
119
|
+
Object.getPrototypeOf,
|
|
120
|
+
Object.setPrototypeOf,
|
|
121
|
+
Object.getOwnPropertyDescriptor,
|
|
122
|
+
Object.getOwnPropertyDescriptors,
|
|
123
|
+
Object.defineProperty,
|
|
124
|
+
Object.defineProperties,
|
|
125
|
+
...Object.getOwnPropertyNames(Reflect)
|
|
126
|
+
.map((key) => Reflect[key])
|
|
127
|
+
.filter((value) => typeof value === 'function'),
|
|
128
|
+
];
|
|
129
|
+
/** Never a value inside an expression: read, returned or passed on. */
|
|
130
|
+
const UNSAFE_VALUES = new Set([
|
|
131
|
+
...CODE_CONSTRUCTORS,
|
|
132
|
+
...INDIRECT_CALLS,
|
|
133
|
+
...REFLECTION,
|
|
134
|
+
]);
|
|
135
|
+
/** Nodes one evaluate() may visit, arrow bodies included. */
|
|
136
|
+
const MAX_STEPS = 1_000_000;
|
|
137
|
+
/** Characters and list items one evaluate() may produce in total. */
|
|
138
|
+
const MAX_OUTPUT = 20_000_000;
|
|
139
|
+
// Only set during a synchronous evaluate() and restored after it, so
|
|
140
|
+
// concurrent Automizer instances never see each other's budget.
|
|
141
|
+
let budget;
|
|
129
142
|
const rangeOf = (token) => ({
|
|
130
143
|
start: token.start,
|
|
131
144
|
end: token.end,
|
|
@@ -180,7 +193,14 @@ class TemplateExpression {
|
|
|
180
193
|
}
|
|
181
194
|
/** `options` as a boolean is `strict`. */
|
|
182
195
|
evaluate(scope, options = {}) {
|
|
183
|
-
|
|
196
|
+
const outer = budget;
|
|
197
|
+
budget = { steps: MAX_STEPS, output: MAX_OUTPUT };
|
|
198
|
+
try {
|
|
199
|
+
return this.evaluateNode(this.ast, scope, typeof options === 'boolean' ? { strict: options } : options);
|
|
200
|
+
}
|
|
201
|
+
finally {
|
|
202
|
+
budget = outer;
|
|
203
|
+
}
|
|
184
204
|
}
|
|
185
205
|
/**
|
|
186
206
|
* The names the expression reads from its scope, each with the static
|
|
@@ -197,7 +217,7 @@ class TemplateExpression {
|
|
|
197
217
|
const path = staticPath(node);
|
|
198
218
|
if (path) {
|
|
199
219
|
if (!bound.includes(path.name)) {
|
|
200
|
-
refs.push(
|
|
220
|
+
refs.push({ ...path, whole, ...(operand && { operand }) });
|
|
201
221
|
}
|
|
202
222
|
return;
|
|
203
223
|
}
|
|
@@ -211,7 +231,7 @@ class TemplateExpression {
|
|
|
211
231
|
const callee = staticPath(node.callee);
|
|
212
232
|
if (callee) {
|
|
213
233
|
if (!bound.includes(callee.name)) {
|
|
214
|
-
refs.push(
|
|
234
|
+
refs.push({ ...callee, whole: false, called: true });
|
|
215
235
|
}
|
|
216
236
|
}
|
|
217
237
|
else {
|
|
@@ -390,11 +410,11 @@ class TemplateExpression {
|
|
|
390
410
|
* the source); the tokenizer passes its own range.
|
|
391
411
|
*/
|
|
392
412
|
fail(message, range) {
|
|
393
|
-
var _a;
|
|
394
413
|
const end = this.source.length;
|
|
395
|
-
const at = range
|
|
396
|
-
|
|
397
|
-
|
|
414
|
+
const at = range ??
|
|
415
|
+
(this.tokens
|
|
416
|
+
? (this.tokens[this.pos] ?? { start: end, end })
|
|
417
|
+
: undefined);
|
|
398
418
|
throw new TemplateExpressionError(message, this.source, at && { start: at.start, end: at.end });
|
|
399
419
|
}
|
|
400
420
|
/*
|
|
@@ -461,7 +481,7 @@ class TemplateExpression {
|
|
|
461
481
|
}
|
|
462
482
|
opAt(pos, ...ops) {
|
|
463
483
|
const token = this.tokens[pos];
|
|
464
|
-
return
|
|
484
|
+
return token?.type === 'op' && ops.includes(token.value)
|
|
465
485
|
? token.value
|
|
466
486
|
: undefined;
|
|
467
487
|
}
|
|
@@ -471,13 +491,12 @@ class TemplateExpression {
|
|
|
471
491
|
this.pos++;
|
|
472
492
|
}
|
|
473
493
|
parseExpression() {
|
|
474
|
-
|
|
475
|
-
return (_a = this.parseArrow()) !== null && _a !== void 0 ? _a : this.parseTernary();
|
|
494
|
+
return this.parseArrow() ?? this.parseTernary();
|
|
476
495
|
}
|
|
477
496
|
/** `x => body` or `(a, b) => body`; undefined when not an arrow. */
|
|
478
497
|
parseArrow() {
|
|
479
498
|
const token = this.tokens[this.pos];
|
|
480
|
-
if (
|
|
499
|
+
if (token?.type === 'id' && this.opAt(this.pos + 1, '=>')) {
|
|
481
500
|
this.pos += 2;
|
|
482
501
|
return {
|
|
483
502
|
type: 'arrow',
|
|
@@ -491,7 +510,7 @@ class TemplateExpression {
|
|
|
491
510
|
let p = this.pos + 1;
|
|
492
511
|
while (!this.opAt(p, ')')) {
|
|
493
512
|
const param = this.tokens[p];
|
|
494
|
-
if (
|
|
513
|
+
if (param?.type !== 'id')
|
|
495
514
|
return undefined;
|
|
496
515
|
params.push(param.value);
|
|
497
516
|
p++;
|
|
@@ -514,6 +533,15 @@ class TemplateExpression {
|
|
|
514
533
|
this.expectOp(':');
|
|
515
534
|
return { type: 'cond', test, then: thenNode, else: this.parseExpression() };
|
|
516
535
|
}
|
|
536
|
+
static { this.PRECEDENCE = [
|
|
537
|
+
['??'],
|
|
538
|
+
['||'],
|
|
539
|
+
['&&'],
|
|
540
|
+
['==', '!=', '===', '!=='],
|
|
541
|
+
['<', '>', '<=', '>='],
|
|
542
|
+
['+', '-'],
|
|
543
|
+
['*', '/', '%'],
|
|
544
|
+
]; }
|
|
517
545
|
parseBinary(level) {
|
|
518
546
|
if (level >= TemplateExpression.PRECEDENCE.length)
|
|
519
547
|
return this.parseUnary();
|
|
@@ -547,7 +575,7 @@ class TemplateExpression {
|
|
|
547
575
|
continue;
|
|
548
576
|
}
|
|
549
577
|
const token = this.tokens[this.pos++];
|
|
550
|
-
if (
|
|
578
|
+
if (token?.type !== 'id') {
|
|
551
579
|
this.fail('Expected property name', token && rangeOf(token));
|
|
552
580
|
}
|
|
553
581
|
node = {
|
|
@@ -614,16 +642,16 @@ class TemplateExpression {
|
|
|
614
642
|
while (!this.peekOp('}')) {
|
|
615
643
|
const token = this.tokens[this.pos++];
|
|
616
644
|
let key;
|
|
617
|
-
if (
|
|
618
|
-
|
|
619
|
-
|
|
645
|
+
if (token?.type === 'id' ||
|
|
646
|
+
token?.type === 'str' ||
|
|
647
|
+
token?.type === 'num') {
|
|
620
648
|
key = {
|
|
621
649
|
type: 'lit',
|
|
622
650
|
value: String(token.value),
|
|
623
651
|
range: rangeOf(token),
|
|
624
652
|
};
|
|
625
653
|
}
|
|
626
|
-
else if (
|
|
654
|
+
else if (token?.type === 'op' && token.value === '[') {
|
|
627
655
|
key = this.parseExpression();
|
|
628
656
|
this.expectOp(']');
|
|
629
657
|
}
|
|
@@ -646,6 +674,9 @@ class TemplateExpression {
|
|
|
646
674
|
*/
|
|
647
675
|
evaluateNode(node, scope, options) {
|
|
648
676
|
const evaluate = (n) => this.evaluateNode(n, scope, options);
|
|
677
|
+
if (budget && --budget.steps < 0) {
|
|
678
|
+
this.fail(`More than ${MAX_STEPS} evaluation steps`);
|
|
679
|
+
}
|
|
649
680
|
switch (node.type) {
|
|
650
681
|
case 'lit':
|
|
651
682
|
return node.value;
|
|
@@ -661,7 +692,7 @@ class TemplateExpression {
|
|
|
661
692
|
if (node.optional && (object === null || object === undefined)) {
|
|
662
693
|
return undefined;
|
|
663
694
|
}
|
|
664
|
-
return this.getMember(object, evaluate(node.property));
|
|
695
|
+
return this.getMember(object, evaluate(node.property), options);
|
|
665
696
|
}
|
|
666
697
|
case 'call': {
|
|
667
698
|
let thisArg;
|
|
@@ -672,7 +703,7 @@ class TemplateExpression {
|
|
|
672
703
|
(thisArg === null || thisArg === undefined)) {
|
|
673
704
|
return undefined;
|
|
674
705
|
}
|
|
675
|
-
fn = this.getMember(thisArg, evaluate(node.callee.property));
|
|
706
|
+
fn = this.getMember(thisArg, evaluate(node.callee.property), options);
|
|
676
707
|
}
|
|
677
708
|
else {
|
|
678
709
|
fn = evaluate(node.callee);
|
|
@@ -681,7 +712,7 @@ class TemplateExpression {
|
|
|
681
712
|
this.fail('Not a function');
|
|
682
713
|
const args = node.args.map(evaluate);
|
|
683
714
|
this.checkCall(fn, thisArg, args, options);
|
|
684
|
-
return fn.apply(thisArg, args);
|
|
715
|
+
return this.checkResult(this.charge(fn.apply(thisArg, args)));
|
|
685
716
|
}
|
|
686
717
|
case 'unary': {
|
|
687
718
|
const arg = evaluate(node.arg);
|
|
@@ -692,7 +723,7 @@ class TemplateExpression {
|
|
|
692
723
|
case 'cond':
|
|
693
724
|
return evaluate(node.test) ? evaluate(node.then) : evaluate(node.else);
|
|
694
725
|
case 'binary':
|
|
695
|
-
return this.evaluateBinary(node, evaluate);
|
|
726
|
+
return this.charge(this.evaluateBinary(node, evaluate));
|
|
696
727
|
case 'array':
|
|
697
728
|
return node.items.map(evaluate);
|
|
698
729
|
case 'object': {
|
|
@@ -707,7 +738,7 @@ class TemplateExpression {
|
|
|
707
738
|
return (...args) => {
|
|
708
739
|
const vars = {};
|
|
709
740
|
node.params.forEach((param, i) => (vars[param] = args[i]));
|
|
710
|
-
return this.evaluateNode(node.body, new ArrowScope(vars, scope), options);
|
|
741
|
+
return this.charge(this.evaluateNode(node.body, new ArrowScope(vars, scope), options));
|
|
711
742
|
};
|
|
712
743
|
}
|
|
713
744
|
}
|
|
@@ -720,7 +751,7 @@ class TemplateExpression {
|
|
|
720
751
|
if (node.op === '||')
|
|
721
752
|
return left || evaluate(node.right);
|
|
722
753
|
if (node.op === '??')
|
|
723
|
-
return left
|
|
754
|
+
return left ?? evaluate(node.right);
|
|
724
755
|
const right = evaluate(node.right);
|
|
725
756
|
switch (node.op) {
|
|
726
757
|
case '===':
|
|
@@ -764,6 +795,12 @@ class TemplateExpression {
|
|
|
764
795
|
if (INDIRECT_CALLS.includes(fn)) {
|
|
765
796
|
this.fail('call, apply and bind are not allowed');
|
|
766
797
|
}
|
|
798
|
+
if (REFLECTION.includes(fn)) {
|
|
799
|
+
this.fail('Reflection (getPrototypeOf, property descriptors, Reflect) is not allowed');
|
|
800
|
+
}
|
|
801
|
+
// a native callee would call these itself (`list.map(F.call, F)`)
|
|
802
|
+
for (const arg of args)
|
|
803
|
+
this.checkResult(arg);
|
|
767
804
|
if (!options.allowStringPatterns &&
|
|
768
805
|
REGEX_FROM_STRING.includes(fn) &&
|
|
769
806
|
!(args[0] instanceof RegExp)) {
|
|
@@ -785,6 +822,44 @@ class TemplateExpression {
|
|
|
785
822
|
this.fail(`A string longer than ${MAX_STRING_LENGTH} characters`);
|
|
786
823
|
}
|
|
787
824
|
}
|
|
825
|
+
/**
|
|
826
|
+
* Counts strings and lists an expression produces against MAX_OUTPUT, so
|
|
827
|
+
* doubling (`a.concat(a)`, `s + s`) or a huge `map` stops before it
|
|
828
|
+
* exhausts memory. Lists count their length plus their strings' lengths.
|
|
829
|
+
*/
|
|
830
|
+
charge(value) {
|
|
831
|
+
if (!budget)
|
|
832
|
+
return value;
|
|
833
|
+
let size = 0;
|
|
834
|
+
if (typeof value === 'string') {
|
|
835
|
+
size = value.length;
|
|
836
|
+
}
|
|
837
|
+
else if (Array.isArray(value)) {
|
|
838
|
+
size = value.length;
|
|
839
|
+
for (const item of value) {
|
|
840
|
+
if (typeof item === 'string')
|
|
841
|
+
size += item.length;
|
|
842
|
+
}
|
|
843
|
+
}
|
|
844
|
+
budget.output -= size;
|
|
845
|
+
if (budget.output < 0) {
|
|
846
|
+
this.fail(`More than ${MAX_OUTPUT} characters or list items produced`);
|
|
847
|
+
}
|
|
848
|
+
return value;
|
|
849
|
+
}
|
|
850
|
+
/**
|
|
851
|
+
* Rejects a forbidden native reached without a member read, e.g. found in
|
|
852
|
+
* a list (`Object.values(…).find(…)`). Raw match/repeat/… count too: read
|
|
853
|
+
* as a member they would have been wrapped.
|
|
854
|
+
*/
|
|
855
|
+
checkResult(value) {
|
|
856
|
+
const unsafe = (item) => typeof item === 'function' &&
|
|
857
|
+
(UNSAFE_VALUES.has(item) || GUARDED_CALLS.includes(item));
|
|
858
|
+
if (unsafe(value) || (Array.isArray(value) && value.some(unsafe))) {
|
|
859
|
+
this.fail('A built-in function that is not allowed as a value');
|
|
860
|
+
}
|
|
861
|
+
return value;
|
|
862
|
+
}
|
|
788
863
|
checkKey(property) {
|
|
789
864
|
const key = String(property);
|
|
790
865
|
if ((0, exports.isForbiddenKey)(key)) {
|
|
@@ -792,7 +867,7 @@ class TemplateExpression {
|
|
|
792
867
|
}
|
|
793
868
|
return key;
|
|
794
869
|
}
|
|
795
|
-
getMember(object, property) {
|
|
870
|
+
getMember(object, property, options) {
|
|
796
871
|
const key = this.checkKey(property);
|
|
797
872
|
if (object === null || object === undefined)
|
|
798
873
|
return undefined;
|
|
@@ -805,19 +880,24 @@ class TemplateExpression {
|
|
|
805
880
|
if (CODE_CONSTRUCTORS.includes(value)) {
|
|
806
881
|
this.fail('Calling the Function constructor is not allowed');
|
|
807
882
|
}
|
|
883
|
+
if (REFLECTION.includes(value)) {
|
|
884
|
+
this.fail('Reflection (getPrototypeOf, property descriptors, Reflect) is not allowed');
|
|
885
|
+
}
|
|
886
|
+
// Passed to a native callback (`list.map(''.match, text)`), these would
|
|
887
|
+
// run without checkCall; the wrapper checks every call.
|
|
888
|
+
if (GUARDED_CALLS.includes(value)) {
|
|
889
|
+
const fn = value;
|
|
890
|
+
// eslint-disable-next-line @typescript-eslint/no-this-alias
|
|
891
|
+
const self = this;
|
|
892
|
+
return function (...args) {
|
|
893
|
+
self.checkCall(fn, this, args, options);
|
|
894
|
+
return self.checkResult(self.charge(fn.apply(this, args)));
|
|
895
|
+
};
|
|
896
|
+
}
|
|
808
897
|
return value;
|
|
809
898
|
}
|
|
810
899
|
}
|
|
811
900
|
exports.TemplateExpression = TemplateExpression;
|
|
812
|
-
TemplateExpression.PRECEDENCE = [
|
|
813
|
-
['??'],
|
|
814
|
-
['||'],
|
|
815
|
-
['&&'],
|
|
816
|
-
['==', '!=', '===', '!=='],
|
|
817
|
-
['<', '>', '<=', '>='],
|
|
818
|
-
['+', '-'],
|
|
819
|
-
['*', '/', '%'],
|
|
820
|
-
];
|
|
821
901
|
/**
|
|
822
902
|
* Splits `a = 1; b = a + 1` at top-level semicolons (outside strings and
|
|
823
903
|
* brackets) and separates assignments from plain expressions.
|