@tko/binding.template 4.0.0-beta1.3 → 4.0.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/dist/foreach.js +5 -11
- package/dist/foreach.js.map +2 -2
- package/dist/index.cjs +1052 -806
- package/dist/index.cjs.map +4 -4
- package/dist/index.js +5 -11
- package/dist/index.js.map +2 -2
- package/dist/index.mjs +5 -11
- package/dist/index.mjs.map +2 -2
- package/dist/nativeTemplateEngine.js +8 -15
- package/dist/nativeTemplateEngine.js.map +2 -2
- package/dist/templateEngine.js +16 -20
- package/dist/templateEngine.js.map +3 -3
- package/dist/templateSources.js +69 -68
- package/dist/templateSources.js.map +2 -2
- package/dist/templating.js +128 -86
- package/dist/templating.js.map +2 -2
- package/helpers/dummyTemplateEngine.ts +94 -100
- package/package.json +7 -8
- package/LICENSE +0 -22
|
@@ -1,92 +1,85 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
function
|
|
16
|
-
|
|
1
|
+
import { extend, arrayPushAll, parseHtmlFragment } from '@tko/utils'
|
|
2
|
+
|
|
3
|
+
import { renderTemplate, anonymousTemplate, templateEngine } from '../dist'
|
|
4
|
+
|
|
5
|
+
import type { BindingContext } from '@tko/bind'
|
|
6
|
+
|
|
7
|
+
export function dummyTemplateEngine(templates?) {
|
|
8
|
+
const inMemoryTemplates = templates || {}
|
|
9
|
+
const inMemoryTemplateData = {}
|
|
10
|
+
|
|
11
|
+
function dummyTemplateSource(id) {
|
|
12
|
+
this.id = id
|
|
13
|
+
}
|
|
14
|
+
dummyTemplateSource.prototype = {
|
|
15
|
+
text: function (val) {
|
|
16
|
+
if (arguments.length >= 1) inMemoryTemplates[this.id] = val
|
|
17
|
+
return inMemoryTemplates[this.id]
|
|
18
|
+
},
|
|
19
|
+
data: function (key, val) {
|
|
20
|
+
if (arguments.length >= 2) {
|
|
21
|
+
inMemoryTemplateData[this.id] = inMemoryTemplateData[this.id] || {}
|
|
22
|
+
inMemoryTemplateData[this.id][key] = val
|
|
23
|
+
}
|
|
24
|
+
return (inMemoryTemplateData[this.id] || {})[key]
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
this.makeTemplateSource = function (template) {
|
|
29
|
+
if (typeof template == 'string')
|
|
30
|
+
return new dummyTemplateSource(template) // Named template comes from the in-memory collection
|
|
31
|
+
else if (template.nodeType === Node.ELEMENT_NODE || template.nodeType === Node.COMMENT_NODE)
|
|
32
|
+
return new anonymousTemplate(template) // Anonymous template
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
this.renderTemplateSource = function (templateSource, bindingContext: BindingContext, rt_options, templateDocument) {
|
|
36
|
+
let data = bindingContext['$data']
|
|
37
|
+
if (data && typeof data.get_value === 'function') {
|
|
38
|
+
// For cases when data is an Identifier/Expression.
|
|
39
|
+
data = data.get_value(data, bindingContext)
|
|
40
|
+
}
|
|
41
|
+
templateDocument = templateDocument || document
|
|
42
|
+
rt_options = rt_options || {}
|
|
43
|
+
let templateText = templateSource.text()
|
|
44
|
+
if (typeof templateText == 'function') templateText = templateText(data, rt_options)
|
|
45
|
+
|
|
46
|
+
templateText = rt_options.showParams ? templateText + ', data=' + data + ', options=' + rt_options : templateText
|
|
47
|
+
// var templateOptions = options.templateOptions; // Have templateOptions in scope to support [js:templateOptions.foo] syntax
|
|
48
|
+
|
|
49
|
+
let result
|
|
50
|
+
|
|
51
|
+
data = data || {}
|
|
52
|
+
// Builders (e.g. rollup) mangle `data` to e.g. `data$$1`.
|
|
53
|
+
// This workaround works as long as nomangle$data doesn't
|
|
54
|
+
// appear anywhere not in tests.
|
|
55
|
+
const nomangle$data: any = data
|
|
56
|
+
;(window as any).__prevent_tree_shaking__ = nomangle$data
|
|
57
|
+
delete (window as any).__prevent_tree_shaking__
|
|
58
|
+
|
|
59
|
+
rt_options.templateRenderingVariablesInScope = rt_options.templateRenderingVariablesInScope || {}
|
|
60
|
+
|
|
61
|
+
extend(data, rt_options.templateRenderingVariablesInScope)
|
|
62
|
+
|
|
63
|
+
// Dummy [renderTemplate:...] syntax
|
|
64
|
+
result = templateText.replace(/\[renderTemplate\:(.*?)\]/g, function (match, templateName) {
|
|
65
|
+
return renderTemplate(templateName, data, rt_options)
|
|
66
|
+
})
|
|
67
|
+
|
|
68
|
+
const evalHandler = function (match, script) {
|
|
69
|
+
try {
|
|
70
|
+
const evalResult = eval(script)
|
|
71
|
+
return evalResult === null || evalResult === undefined ? '' : evalResult.toString()
|
|
72
|
+
} catch (ex: any) {
|
|
73
|
+
throw new Error('Error evaluating script: [js: ' + script + ']\n\nException: ' + ex.toString())
|
|
74
|
+
}
|
|
17
75
|
}
|
|
18
|
-
dummyTemplateSource.prototype = {
|
|
19
|
-
text: function(val) {
|
|
20
|
-
if (arguments.length >= 1)
|
|
21
|
-
inMemoryTemplates[this.id] = val;
|
|
22
|
-
return inMemoryTemplates[this.id];
|
|
23
|
-
},
|
|
24
|
-
data: function(key, val) {
|
|
25
|
-
if (arguments.length >= 2) {
|
|
26
|
-
inMemoryTemplateData[this.id] = inMemoryTemplateData[this.id] || {};
|
|
27
|
-
inMemoryTemplateData[this.id][key] = val;
|
|
28
|
-
}
|
|
29
|
-
return (inMemoryTemplateData[this.id] || {})[key];
|
|
30
|
-
}
|
|
31
|
-
};
|
|
32
|
-
|
|
33
|
-
this.makeTemplateSource = function(template) {
|
|
34
|
-
if (typeof template == "string")
|
|
35
|
-
return new dummyTemplateSource(template); // Named template comes from the in-memory collection
|
|
36
|
-
else if ((template.nodeType == 1) || (template.nodeType == 8))
|
|
37
|
-
return new anonymousTemplate(template); // Anonymous template
|
|
38
|
-
};
|
|
39
|
-
|
|
40
|
-
this.renderTemplateSource = function (templateSource, bindingContext, rt_options, templateDocument) {
|
|
41
|
-
var data = bindingContext['$data'];
|
|
42
|
-
if (data && typeof data.get_value === 'function') {
|
|
43
|
-
// For cases when data is an Identifier/Expression.
|
|
44
|
-
data = data.get_value(data, bindingContext);
|
|
45
|
-
}
|
|
46
|
-
templateDocument = templateDocument || document;
|
|
47
|
-
rt_options = rt_options || {};
|
|
48
|
-
var templateText = templateSource.text();
|
|
49
|
-
if (typeof templateText == "function")
|
|
50
|
-
templateText = templateText(data, rt_options);
|
|
51
|
-
|
|
52
|
-
templateText = rt_options.showParams ? templateText + ", data=" + data + ", options=" + rt_options : templateText;
|
|
53
|
-
// var templateOptions = options.templateOptions; // Have templateOptions in scope to support [js:templateOptions.foo] syntax
|
|
54
|
-
|
|
55
|
-
var result;
|
|
56
|
-
|
|
57
|
-
data = data || {};
|
|
58
|
-
// Builders (e.g. rollup) mangle `data` to e.g. `data$$1`.
|
|
59
|
-
// This workaround works as long as nomangle$data doesn't
|
|
60
|
-
// appear anywhere not in tests.
|
|
61
|
-
const nomangle$data = data
|
|
62
|
-
window.__prevent_tree_shaking__ = nomangle$data
|
|
63
|
-
delete window.__prevent_tree_shaking__
|
|
64
|
-
|
|
65
|
-
rt_options.templateRenderingVariablesInScope = rt_options.templateRenderingVariablesInScope || {};
|
|
66
|
-
|
|
67
|
-
extend(data, rt_options.templateRenderingVariablesInScope);
|
|
68
|
-
|
|
69
|
-
// Dummy [renderTemplate:...] syntax
|
|
70
|
-
result = templateText.replace(/\[renderTemplate\:(.*?)\]/g, function (match, templateName) {
|
|
71
|
-
return renderTemplate(templateName, data, rt_options);
|
|
72
|
-
});
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
var evalHandler = function (match, script) {
|
|
76
|
-
try {
|
|
77
|
-
var evalResult = eval(script);
|
|
78
|
-
return (evalResult === null) || (evalResult === undefined) ? "" : evalResult.toString();
|
|
79
|
-
} catch (ex) {
|
|
80
|
-
throw new Error("Error evaluating script: [js: " + script + "]\n\nException: " + ex.toString());
|
|
81
|
-
}
|
|
82
|
-
};
|
|
83
76
|
|
|
84
|
-
|
|
85
|
-
|
|
77
|
+
// Dummy [[js:...]] syntax (in case you need to use square brackets inside the expression)
|
|
78
|
+
result = result.replace(/\[\[js\:([\s\S]*?)\]\]/g, evalHandler)
|
|
86
79
|
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
80
|
+
// Dummy [js:...] syntax
|
|
81
|
+
result = result.replace(/\[js\:([\s\S]*?)\]/g, evalHandler)
|
|
82
|
+
/*with (bindingContext) {
|
|
90
83
|
with (data || {}) {
|
|
91
84
|
with (options.templateRenderingVariablesInScope || {}) {
|
|
92
85
|
|
|
@@ -94,19 +87,20 @@ export function dummyTemplateEngine(templates) {
|
|
|
94
87
|
}
|
|
95
88
|
}*/
|
|
96
89
|
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
90
|
+
// Use same HTML parsing code as real template engine so as to trigger same combination of IE weirdnesses
|
|
91
|
+
// Also ensure resulting nodelist is an array to mimic what the default templating engine does, so we see the effects of not being able to remove dead memo comment nodes.
|
|
92
|
+
return arrayPushAll([], parseHtmlFragment(result, templateDocument))
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
this.rewriteTemplate = function (template, rewriterCallback, templateDocument) {
|
|
96
|
+
// Only rewrite if the template isn't a function (can't rewrite those)
|
|
97
|
+
const templateSource = this.makeTemplateSource(template, templateDocument)
|
|
98
|
+
if (typeof templateSource.text() != 'function')
|
|
99
|
+
return templateEngine.prototype.rewriteTemplate.call(this, template, rewriterCallback, templateDocument)
|
|
100
|
+
}
|
|
101
|
+
this.createJavaScriptEvaluatorBlock = function (script) {
|
|
102
|
+
return '[js:' + script + ']'
|
|
103
|
+
}
|
|
109
104
|
}
|
|
110
105
|
|
|
111
|
-
|
|
112
|
-
dummyTemplateEngine.prototype = new templateEngine();
|
|
106
|
+
dummyTemplateEngine.prototype = new templateEngine()
|
package/package.json
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
{
|
|
2
|
-
"version": "4.0.0
|
|
2
|
+
"version": "4.0.0",
|
|
3
3
|
"name": "@tko/binding.template",
|
|
4
4
|
"description": "TKO Template bindings",
|
|
5
5
|
"module": "dist/binding.template.js",
|
|
@@ -23,10 +23,10 @@
|
|
|
23
23
|
},
|
|
24
24
|
"homepage": "https://tko.io",
|
|
25
25
|
"dependencies": {
|
|
26
|
-
"@tko/bind": "^4.0.0
|
|
27
|
-
"@tko/computed": "^4.0.0
|
|
28
|
-
"@tko/observable": "^4.0.0
|
|
29
|
-
"@tko/utils": "^4.0.0
|
|
26
|
+
"@tko/bind": "^4.0.0",
|
|
27
|
+
"@tko/computed": "^4.0.0",
|
|
28
|
+
"@tko/observable": "^4.0.0",
|
|
29
|
+
"@tko/utils": "^4.0.0",
|
|
30
30
|
"tslib": "^2.2.0"
|
|
31
31
|
},
|
|
32
32
|
"karma": {
|
|
@@ -35,7 +35,7 @@
|
|
|
35
35
|
]
|
|
36
36
|
},
|
|
37
37
|
"peerDependencies": {
|
|
38
|
-
"@tko/binding.if": "^4.0.0
|
|
38
|
+
"@tko/binding.if": "^4.0.0"
|
|
39
39
|
},
|
|
40
40
|
"licenses": [
|
|
41
41
|
{
|
|
@@ -49,6 +49,5 @@
|
|
|
49
49
|
"import": "./dist/index.js"
|
|
50
50
|
},
|
|
51
51
|
"./helpers/*": "./helpers/*"
|
|
52
|
-
}
|
|
53
|
-
"gitHead": "a8843acb8ae085915115e53a4e057b30731c635e"
|
|
52
|
+
}
|
|
54
53
|
}
|
package/LICENSE
DELETED
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
The MIT License (MIT) - http://www.opensource.org/licenses/mit-license.php
|
|
2
|
-
|
|
3
|
-
Copyright (c) Steven Sanderson, the Knockout.js team, and other contributors
|
|
4
|
-
http://knockoutjs.com/
|
|
5
|
-
|
|
6
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
7
|
-
of this software and associated documentation files (the "Software"), to deal
|
|
8
|
-
in the Software without restriction, including without limitation the rights
|
|
9
|
-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
10
|
-
copies of the Software, and to permit persons to whom the Software is
|
|
11
|
-
furnished to do so, subject to the following conditions:
|
|
12
|
-
|
|
13
|
-
The above copyright notice and this permission notice shall be included in
|
|
14
|
-
all copies or substantial portions of the Software.
|
|
15
|
-
|
|
16
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
17
|
-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
18
|
-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
19
|
-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
20
|
-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
21
|
-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
22
|
-
THE SOFTWARE.
|