Skip to content

Commit

Permalink
feat: changes to autoEscape and autoFilter
Browse files Browse the repository at this point in the history
  • Loading branch information
nebrelbug committed May 29, 2023
1 parent 6eeffe9 commit e30a863
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 23 deletions.
6 changes: 3 additions & 3 deletions src/compile-string.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export function compileToString(this: Eta, str: string, options?: Partial<Option
let include = (template, data) => this.render(template, data, options);
let includeAsync = (template, data) => this.renderAsync(template, data, options);
let __eta = {res: "", e: this.escapeFunction, f: this.filterFunction};
let __eta = {res: "", e: this.config.escapeFunction, f: this.config.filterFunction};
function layout(path, data) {
__eta.layout = path;
Expand Down Expand Up @@ -88,15 +88,15 @@ function compileBody(this: Eta, buff: Array<AstObject>) {
if (type === "r") {
// raw

if (config.filter) {
if (config.autoFilter) {
content = "__eta.f(" + content + ")";
}

returnStr += "__eta.res+=" + content + "\n";
} else if (type === "i") {
// interpolate

if (config.filter) {
if (config.autoFilter) {
content = "__eta.f(" + content + ")";
}

Expand Down
32 changes: 20 additions & 12 deletions src/config.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { XMLEscape } from "./utils";

/* TYPES */

type trimConfig = "nl" | "slurp" | false;
Expand All @@ -14,43 +16,45 @@ export interface EtaConfig {
/** Whether or not to automatically XML-escape interpolations. Default true */
autoEscape: boolean;

/** Apply a filter function defined on the class to every interpolation or raw interpolation */
autoFilter: boolean;

/** Configure automatic whitespace trimming. Default `[false, 'nl']` */
autoTrim: trimConfig | [trimConfig, trimConfig];

/** Whether or not to cache templates if `name` or `filename` is passed */
cache: boolean;

// TODO: Mention that Eta can't use "-" or "_" for config.parse since they're used for whitespace slurping.
escapeFunction: (str: unknown) => string;

filterFunction: (val: unknown) => string;

/** Holds cache of resolved filepaths. Set to `false` to disable. */
cacheFilepaths: boolean;

/** Parsing options */
parse: {
/** Which prefix to use for evaluation. Default `""` */
/** Which prefix to use for evaluation. Default `""`, does not support `"-"` or `"_"` */
exec: string;

/** Which prefix to use for interpolation. Default `"="` */
/** Which prefix to use for interpolation. Default `"="`, does not support `"-"` or `"_"` */
interpolate: string;

/** Which prefix to use for raw interpolation. Default `"~"` */
/** Which prefix to use for raw interpolation. Default `"~"`, does not support `"-"` or `"_"` */
raw: string;
};

/** Array of plugins */
plugins: Array<{ processFnString?: Function; processAST?: Function; processTemplate?: Function }>;

/** Holds cache of resolved filepaths. Set to `false` to disable. */
filepathCache?: Record<string, string> | false;

/** Apply a filter function defined on the class to every interpolation or raw interpolation */
filter?: boolean;

/** Remove all safe-to-remove whitespace */
rmWhitespace: boolean;

/** Delimiters: by default `['<%', '%>']` */
tags: [string, string];

/** Make data available on the global object instead of varName */
useWith?: boolean;
useWith: boolean;

/** Name of the data object. Default `it` */
varName: string;
Expand All @@ -64,9 +68,13 @@ export interface EtaConfig {
/** Eta's base (global) configuration */
const defaultConfig: EtaConfig = {
autoEscape: true,
autoFilter: false,
autoTrim: [false, "nl"],
cache: false,
filepathCache: {},
cacheFilepaths: true,
escapeFunction: XMLEscape,
// default filter function (not used unless enables) just stringifies the input
filterFunction: (val) => String(val),
parse: {
exec: "",
interpolate: "=",
Expand Down
5 changes: 1 addition & 4 deletions src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import { compile } from "./compile.js";
import { compileToString } from "./compile-string.js";
import { render, renderAsync, renderString, renderStringAsync } from "./render.js";
import { TemplateFunction } from "./compile.js";
import { XMLEscape } from "./utils.js";

/* TYPES */
import type { EtaConfig, Options } from "./config.js";
Expand All @@ -30,12 +29,10 @@ export class Eta {
renderString = renderString;
renderStringAsync = renderStringAsync;

filepathCache: Record<string, string> = {};
templatesSync = new Cacher<TemplateFunction>({});
templatesAsync = new Cacher<TemplateFunction>({});

escapeFunction = XMLEscape;
filterFunction = null;

// resolvePath takes a relative path from the "views" directory
resolvePath: null | ((this: Eta, template: string, options?: Partial<Options>) => string) = null;
readFile: null | ((this: Eta, path: string) => string) = null;
Expand Down
8 changes: 4 additions & 4 deletions src/file-handling.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ export function resolvePath(this: EtaCore, template: string, options?: Partial<O
// if the file was included from another template
if (baseFilePath) {
// check the cache
if (this.config.filepathCache && this.config.filepathCache[baseFilePath]) {
return this.config.filepathCache[baseFilePath];
if (this.config.cacheFilepaths && this.filepathCache[baseFilePath]) {
return this.filepathCache[baseFilePath];
}

const absolutePathTest = absolutePathRegExp.exec(template);
Expand All @@ -60,8 +60,8 @@ export function resolvePath(this: EtaCore, template: string, options?: Partial<O

if (dirIsChild(views, resolvedFilePath)) {
// add resolved path to the cache
if (baseFilePath && this.config.filepathCache) {
this.config.filepathCache[baseFilePath] = resolvedFilePath;
if (baseFilePath && this.config.cacheFilepaths) {
this.filepathCache[baseFilePath] = resolvedFilePath;
}

return resolvedFilePath;
Expand Down

0 comments on commit e30a863

Please sign in to comment.