# All files / src/patches/nunjucks context-overrides-frame.js

33.46% Statements 88/263
18.79% Branches 25/133
28.57% Functions 12/42
33.97% Lines 88/259

Press n or j to go to the next uncovered block, b, p or k for the previous block.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301
    302
    303
    304
    305
    306
    307
    308
    309
    310
    311
    312
    313
    314
    315
    316
    317
    318
    319
    320
    321
    322
    323
    324
    325
    326
    327
    328
    329
    330
    331
    332
    333
    334
    335
    336
    337
    338
    339
    340
    341
    342
    343
    344
    345
    346
    347
    348
    349
    350
    351
    352
    353
    354
    355
    356
    357
    358
    359
    360
    361
    362
    363
    364
    365
    366
    367
    368
    369
    370
    371
    372
    373
    374
    375
    376
    377
    378
    379
    380
    381
    382
    383
    384
    385
    386
    387
    388
    389
    390
    391
    392
    393
    394
    395
    396
    397
    398
    399
    400
    401
    402
    403
    404
    405
    406
    407
    408
    409
    410
    411
    412
    413
    414
    415
    416
    417
    418
    419
    420
    421
    422
    423
    424
    425
    426
    427
    428
    429
    430
    431
    432
    433
    434
    435
    436
    437
    438
    439
    440
    441
    442
    443
    444
    445
    446
    447
    448
    449
    450
    451
    452
    453
    454
    455
    456
    457
    458
    459
    460
    461
    462
    463
    464
    465
    466
    467
    468
    469
    470
    471
    472
    473
    474
    475
    476
    477
    478
    479
    480
    481
    482
    483
    484
    485
    486
    487
    488
    489
    490
    491
    492
    493
    494
    495
    496
    497
    498
    499
    500
    501
    502
    503
    504
    505
    506
    507
    508
    509
    510
    511
    512
    513
    514
    515
    516
    517
    518
    519
    520
    521
    522
    523
    524
    525
    526
    527
    528
    529
    530
    531
    532
    533
    534
    535
    536
    537
    538
    539
    540
    541
    542
    543
    544
    545
    546
    547
    548
    549
    550
    551
    552
    553
    554
    555
    556
    557
    558
    559
    560
    561
    562
    563
    564
    565
    566
    567
    568

    7x

    7x
    7x

    7x

    7x

    7x
    224x

    224x
    224x

    224x
    224x
    224x

    224x

    224x

    224x

    7x
    9x

    9x
    9x

    9x
    9x
    9x

    233x

    233x

    233x
    233x

    233x

    2x

    268x
    268x
    268x

    268x

    2x
    2x
    2x

    2x

    7x

    7x

    7x

    7x
    2x

    2x

    2x

    2x

    7x

    2x
    2x
    2x

    7x
    233x

    233x

    233x

    233x

    233x
    233x

    233x
    233x
    233x
    233x
    233x
    233x

    233x

    233x

    233x

    233x

    233x

    233x

    7x

    7x

    7x
    35x

    35x

    35x
    35x
    35x
    35x
    35x
    35x
    35x
    35x
    35x

    35x

    35x

    35x

    35x

    35x

    35x

    35x

     /*
     Patch for nunjucks to allow <include><variable>...</variable></include> to properly
     override the included file's {% set / import %}s.

     By design, nunjucks' variables are scoped, and declarations down the line have
     priority over earlier ones.
     This does not change from a user perspective; The change here affects only variables
     passed programmatically to its methods.

     This patch allows us to reverse this priority for MarkBind variables, facilitating content reuse.

     Patch is written against nunjucks v3.2.2
     The **only** changes are delimited with a // CHANGE HERE comment
     */

    /* eslint-disable */

    const runtime = require('nunjucks/src/runtime');
    const {
      Environment, Template, lib, compiler, nodes,
    } = require('nunjucks');
    const { Obj } = require('nunjucks/src/object');

    const globalRuntime = runtime;

    const MB_CTX_KEY = '_markBindReserved';

    Environment.prototype.render = function render(name, ctx, cb) {
      Iif (lib.isFunction(ctx)) {
        cb = ctx;
        ctx = null;
      } // We support a synchronous API to make it easier to migrate
      // existing code to async. This works because if you don't do
      // anything async work, the whole thing is actually run
      // synchronously.

      // CHANGE HERE
      // Store MarkBind variables inside a reserved key
      // This lets us keep as much of nunjucks' method signatures as possible
      Eif (ctx) {
        ctx = {
          [MB_CTX_KEY]: ctx
        }
      }

      var syncResult = null;
      this.getTemplate(name, function (err, tmpl) {
        Iif (err && cb) {
          callbackAsap(cb, err);
        } else Iif (err) {
          throw err;
        } else {
          syncResult = tmpl.render(ctx, cb);
        }
      });
      return syncResult;
    };

    Environment.prototype.renderString = function renderString(src, ctx, opts, cb) {
      Iif (lib.isFunction(opts)) {
        cb = opts;
        opts = {};
      }

      // CHANGE HERE
      // Store MarkBind variables inside a reserved key
      // This lets us keep as much of nunjucks' method signatures as possible
      Eif (ctx) {
        ctx = {
          [MB_CTX_KEY]: ctx
        }
      }

      opts = opts || {};
      var tmpl = new Template(src, this, opts.path);
      return tmpl.render(ctx, cb);
    };

    // Unfortunately the Context class isn't exposed so we can't patch it directly.
    // So we import methods that use it and let them use this custom implementation instead
    class Context extends Obj {
      init(ctx, blocks, env) {
        // Has to be tied to an environment so we can tap into its globals.
        this.env = env || new Environment();

        // Make a duplicate of ctx
        this.ctx = lib.extend({}, ctx);

        this.blocks = {};
        this.exported = [];

        lib.keys(blocks).forEach(name => {
          this.addBlock(name, blocks[name]);
        });
      }

      lookup(name) {
        // This is one of the most called functions, so optimize for
        // the typical case where the name isn't in the globals
        if (name in this.env.globals && !(name in this.ctx)) {
          return this.env.globals[name];
        } else {
          return this.ctx[name];
        }
      }

      // CHANGE HERE - new method
      lookupMbVariable(name) {
        return MB_CTX_KEY in this.ctx
          ? this.ctx[MB_CTX_KEY][name]
          : undefined;
      }

      setVariable(name, val) {
        this.ctx[name] = val;
      }

      getVariables() {
        return this.ctx;
      }

      addBlock(name, block) {
        this.blocks[name] = this.blocks[name] || [];
        this.blocks[name].push(block);
        return this;
      }

      getBlock(name) {
        if (!this.blocks[name]) {
          throw new Error('unknown block "' + name + '"');
        }

        return this.blocks[name][0];
      }

      getSuper(env, name, block, frame, runtime, cb) {
        var idx = lib.indexOf(this.blocks[name] || [], block);
        var blk = this.blocks[name][idx + 1];
        var context = this;

        if (idx === -1 || !blk) {
          throw new Error('no super block available for "' + name + '"');
        }

        blk(env, context, frame, runtime, cb);
      }

      addExport(name) {
        this.exported.push(name);
      }

      getExported() {
        var exported = {};
        this.exported.forEach((name) => {
          exported[name] = this.ctx[name];
        });
        return exported;
      }
    }

    class Frame {
      constructor(parent, isolateWrites) {
        this.variables = {};
        this.parent = parent;
        this.topLevel = false;
        // if this is true, writes (set) should never propagate upwards past
        // this frame to its parent (though reads may).
        this.isolateWrites = isolateWrites;
      }

      // CHANGE HERE
      // Additional parameter isImport
      set(name, val, resolveUp, isImport) {
        // Allow variables with dots by automatically creating the
        // nested structure
        var parts = name.split('.');
        var obj = this.variables;
        var frame = this;

        // CHANGE HERE
        // flag imports, so we can exclude them from Compiler#compileSymbol
        if (isImport) {
          this.imports = this.imports || new Set();
          this.imports.add(parts[0]);
        }

        if (resolveUp) {
          if ((frame = this.resolve(parts[0], true))) {
            frame.set(name, val);
            return;
          }
        }

        for (let i = 0; i < parts.length - 1; i++) {
          const id = parts[i];

          if (!obj[id]) {
            obj[id] = {};
          }
          obj = obj[id];
        }

        obj[parts[parts.length - 1]] = val;
      }

      get(name) {
        var val = this.variables[name];
        if (val !== undefined) {
          return val;
        }
        return null;
      }

      // CHANGE HERE - additional parameter checkIfIsImport, used for compileSymbol
      lookup(name, checkIfIsImport) {
        var p = this.parent;
        var val = this.variables[name];
        Iif (val !== undefined) {
          // CHANGE HERE
          // Terminate lookup if it is an import, and return undefined
          if (checkIfIsImport && this.imports && this.imports.has(name)) {
            return undefined;
          }

          return val;
        }
        return p && p.lookup(name);
      }

      resolve(name, forWrite) {
        var p = (forWrite && this.isolateWrites) ? undefined : this.parent;
        var val = this.variables[name];
        if (val !== undefined) {
          return this;
        }
        return p && p.resolve(name);
      }

      push(isolateWrites) {
        return new Frame(this, isolateWrites);
      }

      pop() {
        return this.parent;
      }
    }
    runtime.Frame = Frame;

    compiler.Compiler.prototype.compileImport = function compileImport(node, frame) {
      const target = node.target.value;
      const id = this._compileGetTemplate(node, frame, false, false);
      this._addScopeLevel();

      this._emitLine(id + '.getExported(' +
        (node.withContext ? 'context.getVariables(), frame, ' : '') +
        this._makeCallback(id));
      this._addScopeLevel();

      // CHANGE HERE - flag as import - see modified Frame class
      frame.set(target, id, undefined, true);

      if (frame.parent) {
        this._emitLine(`frame.set("${target}", ${id});`);
      } else {
        this._emitLine(`context.setVariable("${target}", ${id});`);
      }
    }

    compiler.Compiler.prototype.compileFromImport = function compileFromImport(node, frame) {
      const importedId = this._compileGetTemplate(node, frame, false, false);
      this._addScopeLevel();

      this._emitLine(importedId + '.getExported(' +
        (node.withContext ? 'context.getVariables(), frame, ' : '') +
        this._makeCallback(importedId));
      this._addScopeLevel();

      node.names.children.forEach((nameNode) => {
        var name;
        var alias;
        var id = this._tmpid();

        if (nameNode instanceof nodes.Pair) {
          name = nameNode.key.value;
          alias = nameNode.value.value;
        } else {
          name = nameNode.value;
          alias = name;
        }

        this._emitLine(`if(Object.prototype.hasOwnProperty.call(${importedId}, "${name}")) {`);
        this._emitLine(`var ${id} = ${importedId}.${name};`);
        this._emitLine('} else {');
        this._emitLine(`cb(new Error("cannot import '${name}'")); return;`);
        this._emitLine('}');

        // CHANGE HERE - flag as import - see modified Frame class
        frame.set(alias, id, undefined, true);

        if (frame.parent) {
          this._emitLine(`frame.set("${alias}", ${id});`);
        } else {
          this._emitLine(`context.setVariable("${alias}", ${id});`);
        }
      });
    }

    compiler.Compiler.prototype.compileSymbol = function compileSymbol(node, frame) {
      var name = node.value;
      // CHANGE HERE
      // returns undefined if it is an import
      var v = frame.lookup(name, true);

      Iif (v) {
        this._emit(v);
      } else {
        this._emit('runtime.contextOrFrameLookup(' + 'context, frame, "' + name + '")');
      }
    };

    runtime.contextOrFrameLookup = function contextOrFrameLookup(context, frame, name) {
      // CHANGE HERE - always look up MarkBind variables first
      var mbVar = context.lookupMbVariable(name);
      Eif (mbVar !== undefined) {
        return mbVar;
      }

      var val = frame.lookup(name);
      return val !== undefined ? val : context.lookup(name);
    }

    /*
     No modifications below here; The implementation is copy pasted only to redirect classes to our implementation
     */

    // No modifications, redefined only to redirect the Context class to our custom implementation
    Template.prototype.render = function render(ctx, parentFrame, cb) {
      var _this6 = this;

      Iif (typeof ctx === 'function') {
        cb = ctx;
        ctx = {};
      } else Iif (typeof parentFrame === 'function') {
        cb = parentFrame;
        parentFrame = null;
      } // If there is a parent frame, we are being called from internal
      // code of another template, and the internal system
      // depends on the sync/async nature of the parent template
      // to be inherited, so force an async callback

      var forceAsync = !parentFrame; // Catch compile errors for async rendering

      try {
        this.compile();
      } catch (e) {
        var err = lib._prettifyError(this.path, this.env.opts.dev, e);

        if (cb) {
          return callbackAsap(cb, err);
        } else {
          throw err;
        }
      }

      var context = new Context(ctx || {}, this.blocks, this.env);
      var frame = parentFrame ? parentFrame.push(true) : new Frame();
      frame.topLevel = true;
      var syncResult = null;
      var didError = false;
      this.rootRenderFunc(this.env, context, frame, globalRuntime, function (err, res) {
        // TODO: this is actually a bug in the compiled template (because waterfall
        // tasks are both not passing errors up the chain of callbacks AND are not
        // causing a return from the top-most render function). But fixing that
        // will require a more substantial change to the compiler.
        Iif (didError && cb && typeof res !== 'undefined') {
          // prevent multiple calls to cb
          return;
        }

        Iif (err) {
          err = lib._prettifyError(_this6.path, _this6.env.opts.dev, err);
          didError = true;
        }

        Iif (cb) {
          if (forceAsync) {
            callbackAsap(cb, err, res);
          } else {
            cb(err, res);
          }
        } else {
          Iif (err) {
            throw err;
          }

          syncResult = res;
        }
      });
      return syncResult;
    };

    // No modifications, redefined only to redirect the Context class to our custom implementation
    Template.prototype.getExported = function getExported(ctx, parentFrame, cb) {
      // eslint-disable-line consistent-return
      if (typeof ctx === 'function') {
        cb = ctx;
        ctx = {};
      }

      if (typeof parentFrame === 'function') {
        cb = parentFrame;
        parentFrame = null;
      } // Catch compile errors for async rendering

      try {
        this.compile();
      } catch (e) {
        if (cb) {
          return cb(e);
        } else {
          throw e;
        }
      }

      var frame = parentFrame ? parentFrame.push() : new Frame();
      frame.topLevel = true; // Run the rootRenderFunc to populate the context with exported vars

      var context = new Context(ctx || {}, this.blocks, this.env);
      this.rootRenderFunc(this.env, context, frame, globalRuntime, function (err) {
        if (err) {
          cb(err, null);
        } else {
          cb(null, context.getExported());
        }
      });
    };

    // No modifications, redefined only to redirect the Context class to our custom implementation
    compiler.Compiler.prototype._compileMacro = function _compileMacro(node, frame) {
      var args = [];
      var kwargs = null;
      var funcId = 'macro_' + this._tmpid();
      var keepFrame = (frame !== undefined);

      // Type check the definition of the args
      node.args.children.forEach((arg, i) => {
        if (i === node.args.children.length - 1 && arg instanceof nodes.Dict) {
          kwargs = arg;
        } else {
          this.assertType(arg, nodes.Symbol);
          args.push(arg);
        }
      });

      const realNames = [...args.map((n) => `l_${n.value}`), 'kwargs'];

      // Quoted argument names
      const argNames = args.map((n) => `"${n.value}"`);
      const kwargNames = ((kwargs && kwargs.children) || []).map((n) => `"${n.key.value}"`);

      // We pass a function to makeMacro which destructures the
      // arguments so support setting positional args with keywords
      // args and passing keyword args as positional args
      // (essentially default values). See runtime.js.
      let currFrame;
      if (keepFrame) {
        currFrame = frame.push(true);
      } else {
        currFrame = new Frame();
      }
      this._emitLines(
        `var ${funcId} = runtime.makeMacro(`,
        `[${argNames.join(', ')}], `,
        `[${kwargNames.join(', ')}], `,
        `function (${realNames.join(', ')}) {`,
        'var callerFrame = frame;',
        'frame = ' + ((keepFrame) ? 'frame.push(true);' : 'new runtime.Frame();'),
        'kwargs = kwargs || {};',
        'if (Object.prototype.hasOwnProperty.call(kwargs, "caller")) {',
        'frame.set("caller", kwargs.caller); }');

      // Expose the arguments to the template. Don't need to use
      // random names because the function
      // will create a new run-time scope for us
      args.forEach((arg) => {
        this._emitLine(`frame.set("${arg.value}", l_${arg.value});`);
        currFrame.set(arg.value, `l_${arg.value}`);
      });

      // Expose the keyword arguments
      if (kwargs) {
        kwargs.children.forEach((pair) => {
          const name = pair.key.value;
          this._emit(`frame.set("${name}", `);
          this._emit(`Object.prototype.hasOwnProperty.call(kwargs, "${name}")`);
          this._emit(` ? kwargs["${name}"] : `);
          this._compileExpression(pair.value, currFrame);
          this._emit(');');
        });
      }

      const bufferId = this._pushBuffer();

      this._withScopedSyntax(() => {
        this.compile(node.body, currFrame);
      });

      this._emitLine('frame = ' + ((keepFrame) ? 'frame.pop();' : 'callerFrame;'));
      this._emitLine(`return new runtime.SafeString(${bufferId});`);
      this._emitLine('});');
      this._popBuffer();

      return funcId;
    }

    // No modifications, redefined only to redirect the Context class to our custom implementation
    compiler.Compiler.prototype.compileRoot = function compileRoot(node, frame) {
      Iif (frame) {
        this.fail('compileRoot: root node can\'t have frame');
      }

      frame = new Frame();

      this._emitFuncBegin(node, 'root');
      this._emitLine('var parentTemplate = null;');
      this._compileChildren(node, frame);
      this._emitLine('if(parentTemplate) {');
      this._emitLine('parentTemplate.rootRenderFunc(env, context, frame, runtime, cb);');
      this._emitLine('} else {');
      this._emitLine(`cb(null, ${this.buffer});`);
      this._emitLine('}');
      this._emitFuncEnd(true);

      this.inBlock = true;

      const blockNames = [];

      const blocks = node.findAll(nodes.Block);

      blocks.forEach((block, i) => {
        const name = block.name.value;

        if (blockNames.indexOf(name) !== -1) {
          throw new Error(`Block "${name}" defined more than once.`);
        }
        blockNames.push(name);

        this._emitFuncBegin(block, `b_${name}`);

        const tmpFrame = new Frame();
        this._emitLine('var frame = frame.push(true);');
        this.compile(block.body, tmpFrame);
        this._emitFuncEnd();
      });

      this._emitLine('return {');

      blocks.forEach((block, i) => {
        const blockName = `b_${block.name.value}`;
        this._emitLine(`${blockName}: ${blockName},`);
      });

      this._emitLine('root: root\n};');
    }