@rhinostone/swig-twig 2.1.0 → 2.3.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/lib/filters.js +23 -20
- package/lib/index.js +11 -0
- package/lib/tags/from.js +31 -2
- package/lib/tags/import.js +22 -2
- package/lib/tags/include.js +10 -1
- package/package.json +2 -2
package/lib/filters.js
CHANGED
|
@@ -278,26 +278,33 @@ exports.raw.safe = true;
|
|
|
278
278
|
* @param {string} [type='html'] Pass `'js'` for JavaScript-safe escaping.
|
|
279
279
|
* @return {string}
|
|
280
280
|
*/
|
|
281
|
+
function escapeHtmlRest(ch) {
|
|
282
|
+
return ch === '<' ? '<' : ch === '>' ? '>' : ch === '"' ? '"' : ''';
|
|
283
|
+
}
|
|
284
|
+
|
|
281
285
|
exports.escape = function (input, type) {
|
|
282
|
-
var
|
|
283
|
-
inp = input,
|
|
284
|
-
i = 0,
|
|
285
|
-
code;
|
|
286
|
+
var t, inp, out, i, code;
|
|
286
287
|
|
|
287
|
-
if (
|
|
288
|
-
return
|
|
288
|
+
if (input === null || input === undefined) {
|
|
289
|
+
return input;
|
|
289
290
|
}
|
|
290
291
|
|
|
291
|
-
|
|
292
|
+
t = typeof input;
|
|
293
|
+
|
|
294
|
+
if (t !== 'string') {
|
|
295
|
+
if (t === 'object') {
|
|
296
|
+
out = iterateFilter.apply(exports.escape, arguments);
|
|
297
|
+
if (out !== undefined) {
|
|
298
|
+
return out;
|
|
299
|
+
}
|
|
300
|
+
}
|
|
292
301
|
return input;
|
|
293
302
|
}
|
|
294
303
|
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
inp = inp.replace(/\\/g, '\\u005C');
|
|
300
|
-
for (i; i < inp.length; i += 1) {
|
|
304
|
+
if (type === 'js') {
|
|
305
|
+
inp = input.replace(/\\/g, '\\u005C');
|
|
306
|
+
out = '';
|
|
307
|
+
for (i = 0; i < inp.length; i += 1) {
|
|
301
308
|
code = inp.charCodeAt(i);
|
|
302
309
|
if (code < 32) {
|
|
303
310
|
code = code.toString(16).toUpperCase();
|
|
@@ -315,14 +322,10 @@ exports.escape = function (input, type) {
|
|
|
315
322
|
.replace(/\=/g, '\\u003D')
|
|
316
323
|
.replace(/-/g, '\\u002D')
|
|
317
324
|
.replace(/;/g, '\\u003B');
|
|
318
|
-
|
|
319
|
-
default:
|
|
320
|
-
return inp.replace(/&(?!amp;|lt;|gt;|quot;|#39;)/g, '&')
|
|
321
|
-
.replace(/</g, '<')
|
|
322
|
-
.replace(/>/g, '>')
|
|
323
|
-
.replace(/"/g, '"')
|
|
324
|
-
.replace(/'/g, ''');
|
|
325
325
|
}
|
|
326
|
+
|
|
327
|
+
return input.replace(/&(?!amp;|lt;|gt;|quot;|#39;)/g, '&')
|
|
328
|
+
.replace(/[<>"']/g, escapeHtmlRest);
|
|
326
329
|
};
|
|
327
330
|
exports.e = exports.escape;
|
|
328
331
|
|
package/lib/index.js
CHANGED
|
@@ -185,6 +185,11 @@ exports.Twig = function (opts) {
|
|
|
185
185
|
* (<code>{% extends parent_var %}</code>) are not pre-resolved and will
|
|
186
186
|
* throw at render time as they would on the sync path.
|
|
187
187
|
*
|
|
188
|
+
* @deprecated since 2.2.0 — use {@link Twig#renderFile} with a loader that
|
|
189
|
+
* sets <code>loader.async === true</code>. The async-codegen dispatch
|
|
190
|
+
* handles dynamic include paths the pre-walker cannot. This method will
|
|
191
|
+
* be removed in 3.0.
|
|
192
|
+
*
|
|
188
193
|
* @example
|
|
189
194
|
* twig.setDefaults({ loader: myAsyncLoader });
|
|
190
195
|
* twig.renderFileAsync('page.twig', { name: 'world' }, function (err, output) {
|
|
@@ -245,6 +250,12 @@ exports.Twig = function (opts) {
|
|
|
245
250
|
* <code>include</code>s resolve correctly without re-running the
|
|
246
251
|
* pre-walk.
|
|
247
252
|
*
|
|
253
|
+
* @deprecated since 2.2.0 — use {@link Twig#compileFile} with
|
|
254
|
+
* <code>options.codegenMode === 'async'</code> on a loader that sets
|
|
255
|
+
* <code>loader.async === true</code>. The returned compiled function
|
|
256
|
+
* yields a <code>Promise<{output, exports}></code> instead of a
|
|
257
|
+
* string. This method will be removed in 3.0.
|
|
258
|
+
*
|
|
248
259
|
* @example
|
|
249
260
|
* twig.compileFileAsync('page.twig', {}, function (err, fn) {
|
|
250
261
|
* if (err) { return done(err); }
|
package/lib/tags/from.js
CHANGED
|
@@ -30,6 +30,7 @@
|
|
|
30
30
|
*/
|
|
31
31
|
|
|
32
32
|
var utils = require('@rhinostone/swig-core/lib/utils');
|
|
33
|
+
var ir = require('@rhinostone/swig-core/lib/ir');
|
|
33
34
|
var backend = require('@rhinostone/swig-core/lib/backend');
|
|
34
35
|
var _dangerousProps = require('@rhinostone/swig-core/lib/security').dangerousProps;
|
|
35
36
|
|
|
@@ -135,11 +136,20 @@ exports.parse = function (str, line, parser, types, stack, opts, swig, token) {
|
|
|
135
136
|
consume();
|
|
136
137
|
}
|
|
137
138
|
|
|
139
|
+
var path = pathTok.match.replace(/^['"]|['"]$/g, '');
|
|
140
|
+
|
|
141
|
+
if (opts && opts.codegenMode === 'async') {
|
|
142
|
+
// Phase 2 (#T22): async mode skips parse-time parseFile + macro
|
|
143
|
+
// pre-render. compile() emits IRFromImportDeferred; runtime resolves
|
|
144
|
+
// the template via _swig.getTemplate and binds each entry on _ctx.
|
|
145
|
+
token.args = [{ path: path, entries: entries }];
|
|
146
|
+
return true;
|
|
147
|
+
}
|
|
148
|
+
|
|
138
149
|
if (!swig || typeof swig.parseFile !== 'function') {
|
|
139
150
|
utils.throwError('"from" tag requires an engine context with a loader', line, opts.filename);
|
|
140
151
|
}
|
|
141
152
|
|
|
142
|
-
var path = pathTok.match.replace(/^['"]|['"]$/g, '');
|
|
143
153
|
var parseOpts = { resolveFrom: opts.filename };
|
|
144
154
|
var compileOpts = utils.extend({}, opts, parseOpts);
|
|
145
155
|
var parsed = swig.parseFile(path, parseOpts);
|
|
@@ -195,7 +205,26 @@ exports.parse = function (str, line, parser, types, stack, opts, swig, token) {
|
|
|
195
205
|
* `_ctx.<aliasName>`. Backend lifts it into
|
|
196
206
|
* `IRLegacyJS`.
|
|
197
207
|
*/
|
|
198
|
-
exports.compile = function (compiler, args) {
|
|
208
|
+
exports.compile = function (compiler, args, content, parents, options) {
|
|
209
|
+
// Phase 2 (#T22): async-codegen branch. Parse stashed a single bundle
|
|
210
|
+
// `[{path, entries: [{origName, aliasName}, ...]}]` in async mode (no
|
|
211
|
+
// macro pre-render); emit IRFromImportDeferred so the backend's
|
|
212
|
+
// `_swig.getTemplate` + per-entry `_ctx.<bind>` assignment happens at
|
|
213
|
+
// runtime.
|
|
214
|
+
if (options && options.codegenMode === 'async') {
|
|
215
|
+
var bundle = args[0];
|
|
216
|
+
var imports = utils.map(bundle.entries, function (e) {
|
|
217
|
+
return {
|
|
218
|
+
name: e.origName,
|
|
219
|
+
alias: e.aliasName === e.origName ? null : e.aliasName
|
|
220
|
+
};
|
|
221
|
+
});
|
|
222
|
+
return ir.fromImportDeferred(
|
|
223
|
+
ir.literal('string', bundle.path),
|
|
224
|
+
imports,
|
|
225
|
+
options.filename || ''
|
|
226
|
+
);
|
|
227
|
+
}
|
|
199
228
|
var allOrigNames = utils.map(args, function (arg) { return arg.origName; }).join('|');
|
|
200
229
|
var replacements = utils.map(args, function (arg) {
|
|
201
230
|
return {
|
package/lib/tags/import.js
CHANGED
|
@@ -31,6 +31,7 @@
|
|
|
31
31
|
*/
|
|
32
32
|
|
|
33
33
|
var utils = require('@rhinostone/swig-core/lib/utils');
|
|
34
|
+
var ir = require('@rhinostone/swig-core/lib/ir');
|
|
34
35
|
var backend = require('@rhinostone/swig-core/lib/backend');
|
|
35
36
|
var _dangerousProps = require('@rhinostone/swig-core/lib/security').dangerousProps;
|
|
36
37
|
|
|
@@ -109,11 +110,20 @@ exports.parse = function (str, line, parser, types, stack, opts, swig, token) {
|
|
|
109
110
|
utils.throwError('Unexpected token "' + peek().match + '" after alias in "import" tag', line, opts.filename);
|
|
110
111
|
}
|
|
111
112
|
|
|
113
|
+
var path = pathTok.match.replace(/^['"]|['"]$/g, '');
|
|
114
|
+
|
|
115
|
+
if (opts && opts.codegenMode === 'async') {
|
|
116
|
+
// Phase 2 (#T22): async mode skips the parse-time parseFile + macro
|
|
117
|
+
// pre-render. compile() emits IRImportDeferred; runtime resolves the
|
|
118
|
+
// template via _swig.getTemplate and binds .exports under the alias.
|
|
119
|
+
token.args = [path, aliasTok.match];
|
|
120
|
+
return true;
|
|
121
|
+
}
|
|
122
|
+
|
|
112
123
|
if (!swig || typeof swig.parseFile !== 'function') {
|
|
113
124
|
utils.throwError('"import" tag requires an engine context with a loader', line, opts.filename);
|
|
114
125
|
}
|
|
115
126
|
|
|
116
|
-
var path = pathTok.match.replace(/^['"]|['"]$/g, '');
|
|
117
127
|
var parseOpts = { resolveFrom: opts.filename };
|
|
118
128
|
var compileOpts = utils.extend({}, opts, parseOpts);
|
|
119
129
|
var parsed = swig.parseFile(path, parseOpts);
|
|
@@ -145,7 +155,17 @@ exports.parse = function (str, line, parser, types, stack, opts, swig, token) {
|
|
|
145
155
|
* @return {string} JS source that initialises `_ctx.<alias>` and
|
|
146
156
|
* assigns every imported macro into it.
|
|
147
157
|
*/
|
|
148
|
-
exports.compile = function (compiler, args) {
|
|
158
|
+
exports.compile = function (compiler, args, content, parents, options) {
|
|
159
|
+
// Phase 2 (#T22): async-codegen branch. Parse stashed `[path, alias]`
|
|
160
|
+
// in async mode (no macro pre-render); emit IRImportDeferred so the
|
|
161
|
+
// backend's `_swig.getTemplate` + `.exports` bind happens at runtime.
|
|
162
|
+
if (options && options.codegenMode === 'async') {
|
|
163
|
+
return ir.importDeferred(
|
|
164
|
+
ir.literal('string', args[0]),
|
|
165
|
+
args[args.length - 1],
|
|
166
|
+
options.filename || ''
|
|
167
|
+
);
|
|
168
|
+
}
|
|
149
169
|
var ctx = args.pop();
|
|
150
170
|
var allMacros = utils.map(args, function (arg) { return arg.name; }).join('|');
|
|
151
171
|
var out = '_ctx.' + ctx + ' = {};\n var _output = "";\n';
|
package/lib/tags/include.js
CHANGED
|
@@ -163,8 +163,17 @@ function sliceTrim(tokens, start, end, types) {
|
|
|
163
163
|
* context emission, isolated-vs-merged selector, resolveFrom, optional
|
|
164
164
|
* try/catch for ignoreMissing).
|
|
165
165
|
*
|
|
166
|
-
*
|
|
166
|
+
* In async codegen mode (`options.codegenMode === 'async'`), derive an
|
|
167
|
+
* `IRIncludeDeferred` from the same fields so the backend routes through
|
|
168
|
+
* the `_swig.getTemplate` + `await` deferred-resolution path instead of
|
|
169
|
+
* the sync `_swig.compileFile` call.
|
|
170
|
+
*
|
|
171
|
+
* @return {object} IRInclude or IRIncludeDeferred node.
|
|
167
172
|
*/
|
|
168
173
|
exports.compile = function (compiler, args, content, parents, options, blockName, token) {
|
|
174
|
+
if (options && options.codegenMode === 'async') {
|
|
175
|
+
var i = token.irExpr;
|
|
176
|
+
return ir.includeDeferred(i.path, i.context, i.isolated, i.ignoreMissing, i.resolveFrom);
|
|
177
|
+
}
|
|
169
178
|
return token.irExpr;
|
|
170
179
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@rhinostone/swig-twig",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.3.0",
|
|
4
4
|
"description": "Twig-syntax frontend for the @rhinostone/swig-core template engine. Part of the @rhinostone/swig multi-flavor family.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"template",
|
|
@@ -22,7 +22,7 @@
|
|
|
22
22
|
"node": ">=12"
|
|
23
23
|
},
|
|
24
24
|
"peerDependencies": {
|
|
25
|
-
"@rhinostone/swig-core": "2.
|
|
25
|
+
"@rhinostone/swig-core": "2.3.0"
|
|
26
26
|
},
|
|
27
27
|
"publishConfig": {
|
|
28
28
|
"access": "public"
|