Comparing version 1.1.7 to 1.2.0
'use strict'; | ||
require('./utils'); | ||
function compilePageSync(html, model, viewData, context, isDebugMode) { | ||
function compilePageSync(html, model, viewData, scope, isDebugMode) { | ||
if (isDebugMode) { | ||
let sandbox = html._sandbox; | ||
let vm = html._vm; | ||
// Creates cope variables. | ||
if (scope) { | ||
Object.keys(scope).forEach((k) => { | ||
defineConstant(sandbox, k, scope[k]); | ||
}); | ||
} | ||
defineConstant(sandbox, "Html", html); | ||
defineConstant(sandbox, "Model", model); | ||
defineConstant(sandbox, "ViewData", viewData); | ||
defineConstant(sandbox, "Context", context); | ||
defineConstant(sandbox, "debug", isDebugMode); | ||
@@ -16,9 +23,17 @@ vm.runInNewContext(html._js, sandbox); | ||
else { | ||
const Html = html; | ||
const Model = model; | ||
const ViewData = viewData; | ||
const Context = context; | ||
const debug = isDebugMode; | ||
html = model = viewData = context = isDebugMode = undefined; | ||
eval(Html._js); | ||
const argNames = ["Html", "Model", "ViewData", "debug"]; | ||
const argValues = [html, model, viewData, isDebugMode]; | ||
if (scope) { | ||
// Add cope variables (we should but can't make them constants because of `eval` limitation in sctict-mode). | ||
Object.keys(scope).forEach((k) => { | ||
argNames.push(k); | ||
argValues.push(scope[k]); | ||
}); | ||
} | ||
// Put the JS-scipt to be executed. | ||
argNames.push(html._js); | ||
// Execute JS-script via function with arguments. | ||
Function.apply(undefined, argNames).apply(undefined, argValues); | ||
} | ||
@@ -34,5 +49,5 @@ | ||
function compilePage(html, model, viewData, context, isDebugMode, done) { | ||
function compilePage(html, model, viewData, scope, isDebugMode, done) { | ||
try { | ||
compilePageSync(html, model, viewData, context, isDebugMode); | ||
compilePageSync(html, model, viewData, scope, isDebugMode); | ||
return html.__renderLayout(done); | ||
@@ -67,6 +82,7 @@ } | ||
// function (process,...){...}() prevents [this] to exist for the 'vm.runInNewContext()' method | ||
this._js = ` | ||
'use strict'; | ||
(function (process, window, global, module, compilePage, compilePageSync, undefined) { | ||
'use strict'; | ||
delete Html._js; | ||
@@ -102,3 +118,3 @@ delete Html._vm; | ||
let compileOpt = { | ||
context: args.context, | ||
scope: args.scope, | ||
template: result.data, | ||
@@ -200,3 +216,3 @@ filePath: result.filePath, | ||
let compileOpt = { | ||
context: args.context, | ||
scope: args.scope, | ||
model: viewModel || args.model, // if is not set explicitly, set default (parent) model | ||
@@ -319,3 +335,2 @@ findPartial: args.findPartial, | ||
const RazorError = require('./errors/RazorError'); | ||
const ErrorsFactory = require('./errors/errors'); | ||
@@ -346,3 +361,3 @@ const voidTags = "area|base|br|col|embed|hr|img|input|link|meta|param|source|track|wbr".toUpperCase().split("|").map(s => s.trim()); | ||
compilePage(htmlObj, this.args.model, this.args.viewData, this.args.context, debugMode, (err, html) => { | ||
compilePage(htmlObj, this.args.model, this.args.viewData, this.args.scope, debugMode, (err, html) => { | ||
if (err) | ||
@@ -373,3 +388,3 @@ return error(err, htmlObj.__dbg); | ||
var html = this.getHtml(htmlArgs); | ||
compilePageSync(html, this.args.model, this.args.viewData, this.args.context, debugMode); | ||
compilePageSync(html, this.args.model, this.args.viewData, this.args.scope, debugMode); | ||
this.checkSections(); | ||
@@ -387,2 +402,11 @@ } | ||
log.debug(this.args.filePath); | ||
// extract scope.. | ||
var model = this.args.model; | ||
if (model && model.$) { | ||
this.args.scope = model.$; | ||
delete model.$; | ||
} | ||
this.args.parsedSections = this.args.parsedSections || {}; | ||
@@ -1478,6 +1502,3 @@ this.args.viewData = this.args.viewData || this.args.ViewData || {}; | ||
// Is not "born" as RazorError. | ||
let isNotRazorError = !err.__dbg && !err.isRazorError; | ||
if (isNotRazorError || err.__dbg && err.__dbg.viewName !== (err.data && err.data.filename)) | ||
if (debugMode && !err.isRazorError || err.__dbg && err.__dbg.viewName !== (err.data && err.data.filename)) | ||
errorFactory.extendError(err); | ||
@@ -1484,0 +1505,0 @@ |
@@ -32,3 +32,2 @@ require('./utils'); | ||
this.ext = options.settings['view engine'] || razorOpts.ext; | ||
this.context = razorOpts.context; | ||
this.env = options.settings.env; | ||
@@ -65,3 +64,2 @@ const debug = dbg.isDebugMode(this.env); | ||
let parserArgs = { | ||
context: this.context, | ||
filePath: filepath, | ||
@@ -68,0 +66,0 @@ template: viewStartsJsHtml + jsHtml, |
46
index.js
@@ -5,11 +5,12 @@ 'use strict'; | ||
const Razor = require('./core/Razor'); | ||
const DefaultContext = require('./core/RazorContext'); | ||
var razor, parser, _requestContext; | ||
var isSetUp = false; | ||
var _settings = { ext: 'raz', context: DefaultContext }; | ||
const HtmlString = require('./core/HtmlString'); | ||
var parser; | ||
var settings = { ext: 'raz' }; | ||
module.exports = { | ||
__express: renderFile, | ||
setup, | ||
initContext, | ||
set ext(val){ | ||
settings.ext = val || settings.ext; | ||
}, | ||
register, | ||
renderFile, | ||
@@ -19,19 +20,7 @@ render: getParser().compileSync, | ||
debug: isDebugMode(), | ||
DefaultContext | ||
HtmlString | ||
} | ||
function setup(app, settings) { | ||
if (isSetUp) | ||
throw new Error('The RAZ app has been setup already.'); | ||
_settings = Object.assign(_settings, settings); | ||
if (_settings.context) | ||
initContext(app, _settings.context); | ||
if (_settings.register || _settings.ext !== "raz") | ||
register(app, _settings.ext) | ||
} | ||
function register(app, ext) { | ||
settings.ext = ext = settings.ext || ext; | ||
app.engine(ext, renderFile); | ||
@@ -42,11 +31,7 @@ app.set('view engine', ext); | ||
function renderFile(filepath, options, done) { | ||
const razorOpts = { ext: _settings.ext, context: _requestContext } | ||
if (!razor) | ||
razor = new Razor(options, razorOpts); | ||
const razorOpts = { ext: settings.ext }; | ||
const razor = new Razor(options, razorOpts); | ||
razor.renderFile(filepath, done); | ||
} | ||
function getParser() { | ||
@@ -85,10 +70,1 @@ if (!parser) { | ||
} | ||
function initContext(app, context) { | ||
const Context = context; | ||
app.use((req, res, next) => { | ||
_requestContext = new Context(req); | ||
next(); | ||
}); | ||
} |
{ | ||
"name": "raz", | ||
"description": "Razor like template engine for NodeJS Express library based on ASP.NET MVC Razor syntax. Template your views by mixing HTML markup with JavaScript server-side code!", | ||
"version": "1.1.7", | ||
"version": "1.2.0", | ||
"author": { | ||
@@ -6,0 +6,0 @@ "name": "Sergey F.", |
@@ -193,3 +193,3 @@ # RAZ: Razor-Express view template engine for NodeJS | ||
// This code is meant for Node.js | ||
const razor = require("raz")(); | ||
const razor = require("raz"); | ||
var html = razor.render({ model, template }); | ||
@@ -238,3 +238,3 @@ ``` | ||
//const raz = require('raz'); | ||
//razor.setup(app, { register: true }); | ||
//raz.register(app); | ||
@@ -329,3 +329,3 @@ // Create the route for the "Index.raz" view-template. | ||
const razor = require("raz") | ||
const razor = require("raz"); | ||
@@ -332,0 +332,0 @@ try{ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Uses eval
Supply chain riskPackage uses dynamic code execution (e.g., eval()), which is a dangerous practice. This can prevent the code from running in certain environments and increases the risk that the code may contain exploits or malicious behavior.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Uses eval
Supply chain riskPackage uses dynamic code execution (e.g., eval()), which is a dangerous practice. This can prevent the code from running in certain environments and increases the risk that the code may contain exploits or malicious behavior.
Found 1 instance in 1 package
128945
17
2414