@lwrjs/base-view-provider 0.9.0-alpha.8 → 0.9.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/build/cjs/index.cjs +31 -2
- package/build/es/index.d.ts +3 -2
- package/build/es/index.js +33 -2
- package/package.json +8 -8
package/build/cjs/index.cjs
CHANGED
|
@@ -27,7 +27,7 @@ __export(exports, {
|
|
|
27
27
|
default: () => src_default
|
|
28
28
|
});
|
|
29
29
|
var import_shared_utils = __toModule(require("@lwrjs/shared-utils"));
|
|
30
|
-
var
|
|
30
|
+
var import_escape_goat = __toModule(require("escape-goat"));
|
|
31
31
|
var import_template_layouts = __toModule(require("./template-layouts.cjs"));
|
|
32
32
|
var BaseViewProvider = class {
|
|
33
33
|
constructor() {
|
|
@@ -49,6 +49,35 @@ var BaseViewProvider = class {
|
|
|
49
49
|
filePath: "<virtual>"
|
|
50
50
|
};
|
|
51
51
|
}
|
|
52
|
+
renderView(template, data) {
|
|
53
|
+
if (typeof template !== "string") {
|
|
54
|
+
throw new TypeError(`Expected a \`string\` in the first argument, got \`${typeof template}\``);
|
|
55
|
+
}
|
|
56
|
+
if (typeof data !== "object") {
|
|
57
|
+
throw new TypeError(`Expected an \`object\` or \`Array\` in the second argument, got \`${typeof data}\``);
|
|
58
|
+
}
|
|
59
|
+
const triplebraceRegex = /{{{(.*?)}}}/g;
|
|
60
|
+
if (triplebraceRegex.test(template)) {
|
|
61
|
+
template = template.replace(triplebraceRegex, (_, key) => {
|
|
62
|
+
let result = data;
|
|
63
|
+
for (const property of key.split(".")) {
|
|
64
|
+
result = result && result[property] ? result[property] : "";
|
|
65
|
+
}
|
|
66
|
+
return String(result);
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
const doubleBraceRegex = /{{(.*?)}}/g;
|
|
70
|
+
if (doubleBraceRegex.test(template)) {
|
|
71
|
+
template = template.replace(doubleBraceRegex, (_, key) => {
|
|
72
|
+
let result = data;
|
|
73
|
+
for (const property of key.split(".")) {
|
|
74
|
+
result = result && result[property] ? result[property] : "";
|
|
75
|
+
}
|
|
76
|
+
return (0, import_escape_goat.htmlEscape)(String(result));
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
return template;
|
|
80
|
+
}
|
|
52
81
|
async initialize() {
|
|
53
82
|
return;
|
|
54
83
|
}
|
|
@@ -61,7 +90,7 @@ var BaseViewProvider = class {
|
|
|
61
90
|
layoutTemplate: "/",
|
|
62
91
|
render: async (context) => {
|
|
63
92
|
return {
|
|
64
|
-
renderedView:
|
|
93
|
+
renderedView: this.renderView(viewSource.originalSource, context),
|
|
65
94
|
metadata: {
|
|
66
95
|
customElements: rootComponent ? [{tagName: rootComponent}] : [],
|
|
67
96
|
assetReferences: []
|
package/build/es/index.d.ts
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
|
-
import { ViewProvider,
|
|
1
|
+
import type { ViewProvider, CompiledView, ViewIdentity } from '@lwrjs/types';
|
|
2
2
|
export default class BaseViewProvider implements ViewProvider {
|
|
3
3
|
name: string;
|
|
4
|
-
getViewSource
|
|
4
|
+
private getViewSource;
|
|
5
|
+
protected renderView(template: string, data: Record<string, any>): string;
|
|
5
6
|
initialize(): Promise<void>;
|
|
6
7
|
getView(viewId: ViewIdentity): Promise<CompiledView | undefined>;
|
|
7
8
|
}
|
package/build/es/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { moduleSpecifierToKebabCase } from '@lwrjs/shared-utils';
|
|
2
|
-
import {
|
|
2
|
+
import { htmlEscape } from 'escape-goat';
|
|
3
3
|
import { defaultHtmlLayout } from './template-layouts.js';
|
|
4
4
|
export default class BaseViewProvider {
|
|
5
5
|
constructor() {
|
|
@@ -23,6 +23,37 @@ export default class BaseViewProvider {
|
|
|
23
23
|
filePath: '<virtual>',
|
|
24
24
|
};
|
|
25
25
|
}
|
|
26
|
+
renderView(template, data) {
|
|
27
|
+
if (typeof template !== 'string') {
|
|
28
|
+
throw new TypeError(`Expected a \`string\` in the first argument, got \`${typeof template}\``);
|
|
29
|
+
}
|
|
30
|
+
if (typeof data !== 'object') {
|
|
31
|
+
throw new TypeError(`Expected an \`object\` or \`Array\` in the second argument, got \`${typeof data}\``);
|
|
32
|
+
}
|
|
33
|
+
// Ported and inspired by from https://github.com/sindresorhus/pupa/blob/master/index.js
|
|
34
|
+
// The semantics of double and triple brackets follow handlebars.
|
|
35
|
+
const triplebraceRegex = /{{{(.*?)}}}/g;
|
|
36
|
+
if (triplebraceRegex.test(template)) {
|
|
37
|
+
template = template.replace(triplebraceRegex, (_, key) => {
|
|
38
|
+
let result = data;
|
|
39
|
+
for (const property of key.split('.')) {
|
|
40
|
+
result = result && result[property] ? result[property] : '';
|
|
41
|
+
}
|
|
42
|
+
return String(result);
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
const doubleBraceRegex = /{{(.*?)}}/g;
|
|
46
|
+
if (doubleBraceRegex.test(template)) {
|
|
47
|
+
template = template.replace(doubleBraceRegex, (_, key) => {
|
|
48
|
+
let result = data;
|
|
49
|
+
for (const property of key.split('.')) {
|
|
50
|
+
result = result && result[property] ? result[property] : '';
|
|
51
|
+
}
|
|
52
|
+
return htmlEscape(String(result));
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
return template;
|
|
56
|
+
}
|
|
26
57
|
// -- Public API --------------------------------------------------------------------
|
|
27
58
|
async initialize() {
|
|
28
59
|
return;
|
|
@@ -38,7 +69,7 @@ export default class BaseViewProvider {
|
|
|
38
69
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
39
70
|
render: async (context) => {
|
|
40
71
|
return {
|
|
41
|
-
renderedView:
|
|
72
|
+
renderedView: this.renderView(viewSource.originalSource, context),
|
|
42
73
|
metadata: {
|
|
43
74
|
customElements: rootComponent ? [{ tagName: rootComponent }] : [],
|
|
44
75
|
assetReferences: [],
|
package/package.json
CHANGED
|
@@ -4,15 +4,15 @@
|
|
|
4
4
|
"publishConfig": {
|
|
5
5
|
"access": "public"
|
|
6
6
|
},
|
|
7
|
-
"version": "0.9.0
|
|
7
|
+
"version": "0.9.0",
|
|
8
8
|
"homepage": "https://developer.salesforce.com/docs/platform/lwr/overview",
|
|
9
9
|
"repository": {
|
|
10
10
|
"type": "git",
|
|
11
|
-
"url": "https://github.com/salesforce/lwr.git",
|
|
11
|
+
"url": "https://github.com/salesforce-experience-platform-emu/lwr.git",
|
|
12
12
|
"directory": "packages/@lwrjs/base-view-provider"
|
|
13
13
|
},
|
|
14
14
|
"bugs": {
|
|
15
|
-
"url": "https://github.com/salesforce/lwr/issues"
|
|
15
|
+
"url": "https://github.com/salesforce-experience-platform-emu/lwr/issues"
|
|
16
16
|
},
|
|
17
17
|
"type": "module",
|
|
18
18
|
"types": "build/es/index.d.ts",
|
|
@@ -30,14 +30,14 @@
|
|
|
30
30
|
"build/**/*.d.ts"
|
|
31
31
|
],
|
|
32
32
|
"dependencies": {
|
|
33
|
-
"@lwrjs/
|
|
34
|
-
"
|
|
33
|
+
"@lwrjs/shared-utils": "0.9.0",
|
|
34
|
+
"escape-goat": "^3.0.0"
|
|
35
35
|
},
|
|
36
36
|
"devDependencies": {
|
|
37
|
-
"@lwrjs/types": "0.9.0
|
|
37
|
+
"@lwrjs/types": "0.9.0"
|
|
38
38
|
},
|
|
39
39
|
"engines": {
|
|
40
|
-
"node": ">=
|
|
40
|
+
"node": ">=16.0.0 <20"
|
|
41
41
|
},
|
|
42
|
-
"gitHead": "
|
|
42
|
+
"gitHead": "4e42b0dc5453f92b36b42aa8132c5bc281e616b7"
|
|
43
43
|
}
|