@microsoft/fast-html 1.0.0-alpha.1 → 1.0.0-alpha.10

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.
Files changed (69) hide show
  1. package/README.md +100 -7
  2. package/dist/dts/components/index.d.ts +1 -0
  3. package/dist/dts/components/template.d.ts +74 -0
  4. package/dist/dts/components/utilities.d.ts +111 -0
  5. package/dist/dts/components/utilities.spec.d.ts +1 -0
  6. package/dist/dts/debug.d.ts +3 -0
  7. package/dist/dts/fixtures/attribute/attribute.spec.d.ts +1 -0
  8. package/dist/dts/fixtures/attribute/main.d.ts +1 -0
  9. package/dist/dts/fixtures/binding/binding.spec.d.ts +1 -0
  10. package/dist/dts/fixtures/binding/main.d.ts +1 -0
  11. package/dist/dts/fixtures/children/children.spec.d.ts +1 -0
  12. package/dist/dts/fixtures/children/main.d.ts +1 -0
  13. package/dist/dts/fixtures/dot-syntax/dot-syntax.spec.d.ts +1 -0
  14. package/dist/dts/fixtures/dot-syntax/main.d.ts +1 -0
  15. package/dist/dts/fixtures/event/event.spec.d.ts +1 -0
  16. package/dist/dts/fixtures/event/main.d.ts +1 -0
  17. package/dist/dts/fixtures/partial/main.d.ts +1 -0
  18. package/dist/dts/fixtures/partial/partial.spec.d.ts +1 -0
  19. package/dist/dts/fixtures/ref/main.d.ts +1 -0
  20. package/dist/dts/fixtures/ref/ref.spec.d.ts +1 -0
  21. package/dist/dts/fixtures/repeat/main.d.ts +1 -0
  22. package/dist/dts/fixtures/repeat/repeat.spec.d.ts +1 -0
  23. package/dist/dts/fixtures/slotted/main.d.ts +1 -0
  24. package/dist/dts/fixtures/slotted/slotted.spec.d.ts +1 -0
  25. package/dist/dts/fixtures/when/main.d.ts +1 -0
  26. package/dist/dts/fixtures/when/when.spec.d.ts +1 -0
  27. package/dist/dts/index.d.ts +1 -0
  28. package/dist/dts/interfaces.d.ts +7 -0
  29. package/dist/dts/tsdoc-metadata.json +11 -0
  30. package/dist/esm/components/index.js +1 -0
  31. package/dist/esm/components/template.js +313 -0
  32. package/dist/esm/components/utilities.js +370 -0
  33. package/dist/esm/components/utilities.spec.js +189 -0
  34. package/dist/esm/debug.js +3 -0
  35. package/dist/esm/fixtures/attribute/attribute.spec.js +23 -0
  36. package/dist/esm/fixtures/attribute/main.js +26 -0
  37. package/dist/esm/fixtures/binding/binding.spec.js +23 -0
  38. package/dist/esm/fixtures/binding/main.js +40 -0
  39. package/dist/esm/fixtures/children/children.spec.js +37 -0
  40. package/dist/esm/fixtures/children/main.js +31 -0
  41. package/dist/esm/fixtures/dot-syntax/dot-syntax.spec.js +9 -0
  42. package/dist/esm/fixtures/dot-syntax/main.js +23 -0
  43. package/dist/esm/fixtures/event/event.spec.js +28 -0
  44. package/dist/esm/fixtures/event/main.js +35 -0
  45. package/dist/esm/fixtures/partial/main.js +38 -0
  46. package/dist/esm/fixtures/partial/partial.spec.js +14 -0
  47. package/dist/esm/fixtures/ref/main.js +21 -0
  48. package/dist/esm/fixtures/ref/ref.spec.js +13 -0
  49. package/dist/esm/fixtures/repeat/main.js +27 -0
  50. package/dist/esm/fixtures/repeat/repeat.spec.js +29 -0
  51. package/dist/esm/fixtures/slotted/main.js +29 -0
  52. package/dist/esm/fixtures/slotted/slotted.spec.js +25 -0
  53. package/dist/esm/fixtures/when/main.js +198 -0
  54. package/dist/esm/fixtures/when/when.spec.js +82 -0
  55. package/dist/esm/index.js +4 -0
  56. package/dist/esm/interfaces.js +1 -0
  57. package/dist/esm/tsconfig.tsbuildinfo +1 -0
  58. package/dist/fast-html.api.json +356 -0
  59. package/dist/fast-html.d.ts +78 -0
  60. package/dist/fast-html.untrimmed.d.ts +78 -0
  61. package/package.json +12 -6
  62. package/rules/attribute-directives.yml +38 -0
  63. package/rules/call-expression-with-event-argument.yml +41 -0
  64. package/rules/member-expression.yml +33 -0
  65. package/rules/tag-function-to-template-literal.yml +16 -0
  66. package/CHANGELOG.json +0 -26
  67. package/CHANGELOG.md +0 -14
  68. package/docs/api-report.api.md +0 -18
  69. package/webpack.common.config.js +0 -18
@@ -0,0 +1,313 @@
1
+ import { __awaiter, __decorate, __metadata } from "tslib";
2
+ import { attr, DOMAspect, FAST, FASTElement, fastElementRegistry, ViewTemplate, } from "@microsoft/fast-element";
3
+ import { DOMPolicy } from "@microsoft/fast-element/dom-policy.js";
4
+ import { getAllPartials, getNextBehavior, getOperator, pathResolver, transformInnerHTML, } from "./utilities.js";
5
+ function allow(tagName, aspect, aspectName, sink) {
6
+ return (target, name, value, ...rest) => {
7
+ sink(target, name, value, ...rest);
8
+ };
9
+ }
10
+ /**
11
+ * The <f-template> custom element that will provide view logic to the element
12
+ */
13
+ class TemplateElement extends FASTElement {
14
+ constructor() {
15
+ super(...arguments);
16
+ this.partials = {};
17
+ }
18
+ static options(elementOptions = {}) {
19
+ this.elementOptions = elementOptions;
20
+ return this;
21
+ }
22
+ connectedCallback() {
23
+ super.connectedCallback();
24
+ if (this.name) {
25
+ this.$fastController.definition.registry
26
+ .whenDefined(this.name)
27
+ .then((value) => __awaiter(this, void 0, void 0, function* () {
28
+ var _a;
29
+ const registeredFastElement = fastElementRegistry.getByType(value);
30
+ const template = this.getElementsByTagName("template").item(0);
31
+ if (template) {
32
+ const innerHTML = yield transformInnerHTML(this.innerHTML);
33
+ yield this.resolveAllPartials(innerHTML);
34
+ const { strings, values } = yield this.resolveStringsAndValues(innerHTML);
35
+ if (registeredFastElement) {
36
+ // all new elements will get the updated template
37
+ registeredFastElement.template =
38
+ this.resolveTemplateOrBehavior(strings, values);
39
+ // set shadow options as defined by the f-template
40
+ registeredFastElement.shadowOptions =
41
+ (_a = TemplateElement.elementOptions[this.name]) === null || _a === void 0 ? void 0 : _a.shadowOptions;
42
+ }
43
+ }
44
+ else {
45
+ throw FAST.error(2000 /* Message.noTemplateProvided */, { name: this.name });
46
+ }
47
+ }));
48
+ }
49
+ }
50
+ /**
51
+ * Resolve strings and values from an innerHTML string
52
+ * @param innerHTML - The innerHTML.
53
+ * @param self - Indicates that this should refer to itself instead of a property when creating bindings.
54
+ */
55
+ resolveStringsAndValues(innerHTML, self = false) {
56
+ return __awaiter(this, void 0, void 0, function* () {
57
+ const strings = [];
58
+ const values = []; // these can be bindings, directives, etc.
59
+ yield this.resolveInnerHTML(innerHTML, strings, values, self);
60
+ strings.raw = strings.map(value => String.raw({ raw: value }));
61
+ return {
62
+ strings,
63
+ values,
64
+ };
65
+ });
66
+ }
67
+ /**
68
+ * Resolve a template or behavior
69
+ * @param strings - The strings array.
70
+ * @param values - The interpreted values.
71
+ */
72
+ resolveTemplateOrBehavior(strings, values) {
73
+ return ViewTemplate.create(strings, values, DOMPolicy.create({
74
+ guards: {
75
+ aspects: {
76
+ [DOMAspect.property]: {
77
+ innerHTML: allow,
78
+ },
79
+ },
80
+ },
81
+ }));
82
+ }
83
+ /**
84
+ * Resolve a template directive
85
+ * @param behaviorConfig - The directive behavior configuration object.
86
+ * @param externalValues - The interpreted values from the parent.
87
+ * @param innerHTML - The innerHTML.
88
+ */
89
+ resolveTemplateDirective(behaviorConfig, externalValues, innerHTML, self = false) {
90
+ var _a;
91
+ return __awaiter(this, void 0, void 0, function* () {
92
+ switch (behaviorConfig.name) {
93
+ case "when":
94
+ {
95
+ const { strings, values } = yield this.resolveStringsAndValues(innerHTML.slice(behaviorConfig.openingTagEndIndex, behaviorConfig.closingTagStartIndex));
96
+ const { when } = yield import("@microsoft/fast-element");
97
+ const { operator, left, right, rightIsValue } = getOperator(behaviorConfig.value);
98
+ let whenLogic = (x, c) => pathResolver(left, self)(x, c);
99
+ switch (operator) {
100
+ case "!":
101
+ whenLogic = (x, c) => !pathResolver(left, self)(x, c);
102
+ break;
103
+ case "==":
104
+ whenLogic = (x, c) => pathResolver(left, self)(x, c) ==
105
+ (rightIsValue
106
+ ? right
107
+ : pathResolver(right, self)(x, c));
108
+ break;
109
+ case "!=":
110
+ whenLogic = (x, c) => pathResolver(left, self)(x, c) !=
111
+ (rightIsValue
112
+ ? right
113
+ : pathResolver(right, self)(x, c));
114
+ break;
115
+ case "&&":
116
+ case "&amp;&amp;":
117
+ whenLogic = (x, c) => pathResolver(left, self)(x, c) &&
118
+ (rightIsValue
119
+ ? right
120
+ : pathResolver(right, self)(x, c));
121
+ break;
122
+ case "||":
123
+ whenLogic = (x, c) => pathResolver(left, self)(x, c) ||
124
+ (rightIsValue
125
+ ? right
126
+ : pathResolver(right, self)(x, c));
127
+ break;
128
+ case ">=":
129
+ whenLogic = (x, c) => pathResolver(left, self)(x, c) >=
130
+ (rightIsValue
131
+ ? right
132
+ : pathResolver(right, self)(x, c));
133
+ break;
134
+ case ">":
135
+ whenLogic = (x, c) => pathResolver(left, self)(x, c) >
136
+ (rightIsValue
137
+ ? right
138
+ : pathResolver(right, self)(x, c));
139
+ break;
140
+ case "<=":
141
+ whenLogic = (x, c) => pathResolver(left, self)(x, c) <=
142
+ (rightIsValue
143
+ ? right
144
+ : pathResolver(right, self)(x, c));
145
+ break;
146
+ case "<":
147
+ whenLogic = (x, c) => pathResolver(left, self)(x, c) <
148
+ (rightIsValue
149
+ ? right
150
+ : pathResolver(right, self)(x, c));
151
+ break;
152
+ }
153
+ externalValues.push(when(whenLogic, this.resolveTemplateOrBehavior(strings, values)));
154
+ }
155
+ break;
156
+ case "repeat":
157
+ {
158
+ const valueAttr = behaviorConfig.value.split(" "); // syntax {{x in y}}
159
+ const { strings, values } = yield this.resolveStringsAndValues(innerHTML.slice(behaviorConfig.openingTagEndIndex, behaviorConfig.closingTagStartIndex), true);
160
+ const { repeat } = yield import("@microsoft/fast-element");
161
+ externalValues.push(repeat((x, c) => pathResolver(valueAttr[2], self)(x, c), this.resolveTemplateOrBehavior(strings, values)));
162
+ }
163
+ break;
164
+ case "apply": {
165
+ const openingTag = innerHTML.slice(behaviorConfig.openingTagStartIndex, behaviorConfig.openingTagEndIndex);
166
+ const partial = (_a = openingTag
167
+ .split(" ")
168
+ .find(tagPart => {
169
+ return tagPart.startsWith("partial");
170
+ })) === null || _a === void 0 ? void 0 : _a.split('"')[1];
171
+ if (partial) {
172
+ const { when } = yield import("@microsoft/fast-element");
173
+ externalValues.push(when((x, c) => pathResolver(behaviorConfig.value, self)(x, c), () => this.partials[partial]));
174
+ }
175
+ }
176
+ }
177
+ });
178
+ }
179
+ /**
180
+ * Resolve a template directive
181
+ * @param name - The name of the directive.
182
+ * @param propName - The property name to pass to the directive.
183
+ * @param externalValues - The interpreted values from the parent.
184
+ */
185
+ resolveAttributeDirective(name, propName, externalValues) {
186
+ return __awaiter(this, void 0, void 0, function* () {
187
+ switch (name) {
188
+ case "children":
189
+ {
190
+ const { children } = yield import("@microsoft/fast-element");
191
+ externalValues.push(children(propName));
192
+ }
193
+ break;
194
+ case "slotted":
195
+ {
196
+ const { slotted } = yield import("@microsoft/fast-element");
197
+ externalValues.push(slotted(propName));
198
+ }
199
+ break;
200
+ case "ref":
201
+ {
202
+ const { ref } = yield import("@microsoft/fast-element");
203
+ externalValues.push(ref(propName));
204
+ }
205
+ break;
206
+ }
207
+ });
208
+ }
209
+ /**
210
+ * Resolver of a data binding
211
+ * @param innerHTML - The innerHTML.
212
+ * @param strings - The strings array.
213
+ * @param values - The interpreted values.
214
+ * @param self - Indicates that this should refer to itself instead of a property when creating bindings.
215
+ * @param behaviorConfig - The binding behavior configuration object.
216
+ */
217
+ resolveDataBinding(innerHTML, strings, values, self = false, behaviorConfig) {
218
+ return __awaiter(this, void 0, void 0, function* () {
219
+ switch (behaviorConfig.subtype) {
220
+ case "content":
221
+ {
222
+ strings.push(innerHTML.slice(0, behaviorConfig.openingStartIndex));
223
+ const propName = innerHTML.slice(behaviorConfig.openingEndIndex, behaviorConfig.closingStartIndex);
224
+ const binding = (x, c) => pathResolver(propName, self)(x, c);
225
+ values.push(binding);
226
+ yield this.resolveInnerHTML(innerHTML.slice(behaviorConfig.closingEndIndex, innerHTML.length), strings, values, self);
227
+ }
228
+ break;
229
+ case "attribute":
230
+ strings.push(innerHTML.slice(0, behaviorConfig.openingStartIndex));
231
+ if (behaviorConfig.aspect === "@") {
232
+ const bindingHTML = innerHTML.slice(behaviorConfig.openingEndIndex, behaviorConfig.closingStartIndex);
233
+ const openingParenthesis = bindingHTML.indexOf("(");
234
+ const closingParenthesis = bindingHTML.indexOf(")");
235
+ const propName = innerHTML.slice(behaviorConfig.openingEndIndex, behaviorConfig.closingStartIndex -
236
+ (closingParenthesis - openingParenthesis) -
237
+ 1);
238
+ const arg = bindingHTML.slice(openingParenthesis + 1, closingParenthesis);
239
+ const binding = (x, c) => pathResolver(propName, self)(x, c)(...(arg === "e" ? [c.event] : []), ...(arg !== "e" && arg !== ""
240
+ ? [pathResolver(arg)(x, c)]
241
+ : []));
242
+ values.push(binding);
243
+ }
244
+ else {
245
+ const propName = innerHTML.slice(behaviorConfig.openingEndIndex, behaviorConfig.closingStartIndex);
246
+ const binding = (x, c) => pathResolver(propName, self)(x, c);
247
+ values.push(binding);
248
+ }
249
+ yield this.resolveInnerHTML(innerHTML.slice(behaviorConfig.closingEndIndex, innerHTML.length), strings, values, self);
250
+ break;
251
+ case "attributeDirective":
252
+ {
253
+ strings.push(innerHTML.slice(0, behaviorConfig.openingStartIndex -
254
+ behaviorConfig.name.length -
255
+ 4));
256
+ const propName = innerHTML.slice(behaviorConfig.openingEndIndex, behaviorConfig.closingStartIndex);
257
+ yield this.resolveAttributeDirective(behaviorConfig.name, propName, values);
258
+ yield this.resolveInnerHTML(innerHTML.slice(behaviorConfig.closingEndIndex + 1, innerHTML.length), strings, values, self);
259
+ }
260
+ break;
261
+ }
262
+ });
263
+ }
264
+ /**
265
+ * Resolver of the innerHTML string
266
+ * @param innerHTML - The innerHTML.
267
+ * @param strings - The strings array.
268
+ * @param values - The interpreted values.
269
+ * @param self - Indicates that this should refer to itself instead of a property when creating bindings.
270
+ */
271
+ resolveInnerHTML(innerHTML, strings, values, self = false) {
272
+ return __awaiter(this, void 0, void 0, function* () {
273
+ const behaviorConfig = getNextBehavior(innerHTML);
274
+ if (behaviorConfig === null) {
275
+ strings.push(innerHTML);
276
+ }
277
+ else {
278
+ switch (behaviorConfig.type) {
279
+ case "dataBinding":
280
+ yield this.resolveDataBinding(innerHTML, strings, values, self, behaviorConfig);
281
+ break;
282
+ case "templateDirective":
283
+ strings.push(innerHTML.slice(0, behaviorConfig.openingTagStartIndex));
284
+ yield this.resolveTemplateDirective(behaviorConfig, values, innerHTML, self);
285
+ yield this.resolveInnerHTML(innerHTML.slice(behaviorConfig.closingTagEndIndex, innerHTML.length), strings, values, self);
286
+ break;
287
+ }
288
+ }
289
+ });
290
+ }
291
+ /**
292
+ * Resolve all partial templates
293
+ * @param unresolvedInnerHTML - The innerHTML.
294
+ */
295
+ resolveAllPartials(unresolvedInnerHTML) {
296
+ return __awaiter(this, void 0, void 0, function* () {
297
+ const allPartials = Object.entries(getAllPartials(unresolvedInnerHTML));
298
+ for (let i = 0, partialLength = allPartials.length; i < partialLength; i++) {
299
+ const { strings, values } = yield this.resolveStringsAndValues(allPartials[i][1].innerHTML);
300
+ this.partials[allPartials[i][0]] = this.resolveTemplateOrBehavior(strings, values);
301
+ }
302
+ });
303
+ }
304
+ }
305
+ /**
306
+ * A dictionary of custom element options
307
+ */
308
+ TemplateElement.elementOptions = {};
309
+ __decorate([
310
+ attr,
311
+ __metadata("design:type", String)
312
+ ], TemplateElement.prototype, "name", void 0);
313
+ export { TemplateElement };
@@ -0,0 +1,370 @@
1
+ const openClientSideBinding = "{";
2
+ const closeClientSideBinding = "}";
3
+ const openContentBinding = "{{";
4
+ const closeContentBinding = "}}";
5
+ const openUnescapedBinding = "{{{";
6
+ const closeUnescapedBinding = "}}}";
7
+ const openTagStart = "<f-";
8
+ const tagEnd = ">";
9
+ const closeTagStart = "</f-";
10
+ const attributeDirectivePrefix = "f-";
11
+ const startInnerHTMLDiv = `<div :innerHTML="{{`;
12
+ const startInnerHTMLDivLength = startInnerHTMLDiv.length;
13
+ const endInnerHTMLDiv = `}}"></div>`;
14
+ const endInnerHTMLDivLength = endInnerHTMLDiv.length;
15
+ /**
16
+ * Get the index of the next matching tag
17
+ * @param openingTagStartSlice - The slice starting from the opening tag
18
+ * @param openingTag - The opening tag string
19
+ * @param closingTag - The closing tag
20
+ * @param openingTagStartIndex - The opening tag start index derived from the innerHTML
21
+ * @returns index
22
+ */
23
+ export function getIndexOfNextMatchingTag(openingTagStartSlice, openingTag, closingTag, openingTagStartIndex) {
24
+ let tagCount = 1;
25
+ let matchingCloseTagIndex = -1;
26
+ const openingTagLength = openingTag.length;
27
+ const closingTagLength = closingTag.length;
28
+ let nextSlice = openingTagStartSlice.slice(openingTagLength);
29
+ let nextOpenTag = nextSlice.indexOf(openingTag);
30
+ let nextCloseTag = nextSlice.indexOf(closingTag);
31
+ let tagOffset = openingTagStartIndex + openingTagLength;
32
+ do {
33
+ // if a closing tag has been found for the last open tag, decrement the tag count
34
+ if (nextOpenTag > nextCloseTag || nextOpenTag === -1) {
35
+ tagCount--;
36
+ if (tagCount === 0) {
37
+ matchingCloseTagIndex = nextCloseTag + tagOffset;
38
+ break;
39
+ }
40
+ tagOffset += nextCloseTag + closingTagLength;
41
+ nextSlice = nextSlice.slice(nextCloseTag + closingTagLength);
42
+ nextOpenTag = nextSlice.indexOf(openingTag);
43
+ nextCloseTag = nextSlice.indexOf(closingTag);
44
+ }
45
+ else if (nextOpenTag !== -1) {
46
+ tagCount++;
47
+ tagOffset += nextOpenTag + openingTagLength;
48
+ nextSlice = nextSlice.slice(nextOpenTag + openingTagLength);
49
+ nextOpenTag = nextSlice.indexOf(openingTag);
50
+ nextCloseTag = nextSlice.indexOf(closingTag);
51
+ }
52
+ if (tagCount === 0) {
53
+ matchingCloseTagIndex = nextCloseTag + tagOffset;
54
+ break;
55
+ }
56
+ } while (tagCount > 0);
57
+ return matchingCloseTagIndex;
58
+ }
59
+ /**
60
+ * Get the next directive
61
+ * @param innerHTML - The innerHTML string to evaluate
62
+ * @returns DirectiveBehaviorConfig - A configuration object
63
+ */
64
+ function getNextDirectiveBehavior(innerHTML) {
65
+ const openingTagStartIndex = innerHTML.indexOf(openTagStart);
66
+ const openingTagStartSlice = innerHTML.slice(openingTagStartIndex);
67
+ const openingTagEndIndex = // account for f-when which may include >= or > as operators, but will always include a condition attr
68
+ openingTagStartSlice.indexOf(`"${tagEnd}`) + openingTagStartIndex + 2;
69
+ const directiveTag = innerHTML
70
+ .slice(openingTagStartIndex + 3, openingTagEndIndex - 1)
71
+ .split(" ")[0];
72
+ const directiveValue = getNextDataBindingBehavior(innerHTML);
73
+ const openingTag = `${openTagStart}${directiveTag}`;
74
+ const closingTag = `${closeTagStart}${directiveTag}${tagEnd}`;
75
+ const matchingCloseTagIndex = getIndexOfNextMatchingTag(openingTagStartSlice, openingTag, closingTag, openingTagStartIndex);
76
+ return {
77
+ type: "templateDirective",
78
+ name: directiveTag,
79
+ value: innerHTML.slice(directiveValue.openingEndIndex, directiveValue.closingStartIndex),
80
+ openingTagStartIndex,
81
+ openingTagEndIndex,
82
+ closingTagStartIndex: matchingCloseTagIndex,
83
+ closingTagEndIndex: matchingCloseTagIndex + closingTag.length,
84
+ };
85
+ }
86
+ /**
87
+ * Determine if this binding is an attribute binding
88
+ * @param innerHTML - The innerHTML string to evaluate
89
+ * @param openingStartIndex - The index of the binding opening marker
90
+ * @returns boolean
91
+ */
92
+ function isAttribute(innerHTML, openingStartIndex) {
93
+ return innerHTML.slice(openingStartIndex - 2, openingStartIndex - 1) === "=";
94
+ }
95
+ /**
96
+ * Determine if this binding is an attribute directive binding
97
+ * @param innerHTML - The innerHTML string to evaluate
98
+ * @param openingStartIndex - The index of the binding opening marker
99
+ * @returns boolean
100
+ */
101
+ function isAttributeDirective(innerHTML, openingStartIndex) {
102
+ const splitHTML = innerHTML.slice(0, openingStartIndex - 2).split(" ");
103
+ return splitHTML[splitHTML.length - 1].startsWith(attributeDirectivePrefix);
104
+ }
105
+ /**
106
+ * Get the attribute binding config
107
+ * @param innerHTML - The innerHTML string to evaluate
108
+ * @param config - The base configuration of the binding
109
+ * @returns AttributeDataBindingBehaviorConfig
110
+ */
111
+ function getAttributeDataBindingConfig(innerHTML, config) {
112
+ const splitInnerHTML = innerHTML.slice(0, config.openingStartIndex).split(" ");
113
+ const firstCharOfAttribute = splitInnerHTML[splitInnerHTML.length - 1][0];
114
+ const aspect = firstCharOfAttribute === "?" ||
115
+ firstCharOfAttribute === "@" ||
116
+ firstCharOfAttribute === ":"
117
+ ? firstCharOfAttribute
118
+ : null;
119
+ return Object.assign(Object.assign({}, config), { subtype: "attribute", aspect });
120
+ }
121
+ /**
122
+ * Get the attribute directive binding config
123
+ * @param innerHTML - The innerHTML string to evaluate
124
+ * @param config - The base configuration of the binding
125
+ * @returns AttributeDirectiveBindingBehaviorConfig
126
+ */
127
+ function getAttributeDirectiveDataBindingConfig(innerHTML, config) {
128
+ const splitInnerHTML = innerHTML.slice(0, config.openingStartIndex).split(" ");
129
+ const lastItem = splitInnerHTML[splitInnerHTML.length - 1];
130
+ const equals = lastItem.indexOf("=");
131
+ const name = lastItem.slice(2, equals);
132
+ return Object.assign(Object.assign({}, config), { subtype: "attributeDirective", name: name });
133
+ }
134
+ /**
135
+ * Get the content data binding config
136
+ * @param config - The base configuration of the binding
137
+ * @returns ContentDataBindingBehaviorConfig
138
+ */
139
+ function getContentDataBindingConfig(config) {
140
+ return Object.assign(Object.assign({}, config), { subtype: "content" });
141
+ }
142
+ function getIndexAndBindingTypeOfNextDataBindingBehavior(innerHTML) {
143
+ // {{{}}} binding
144
+ const openingUnescapedStartIndex = innerHTML.indexOf(openUnescapedBinding);
145
+ const closingUnescapedStartIndex = innerHTML.indexOf(closeUnescapedBinding);
146
+ // {{}} binding
147
+ const openingContentStartIndex = innerHTML.indexOf(openContentBinding);
148
+ const closingContentStartIndex = innerHTML.indexOf(closeContentBinding);
149
+ // {} binding
150
+ const openingClientStartIndex = innerHTML.indexOf(openClientSideBinding);
151
+ const closingClientStartIndex = innerHTML.indexOf(closeClientSideBinding);
152
+ if (openingUnescapedStartIndex !== -1 &&
153
+ openingUnescapedStartIndex <= openingContentStartIndex &&
154
+ openingUnescapedStartIndex <= openingClientStartIndex) {
155
+ // is unescaped {{{}}}
156
+ return {
157
+ openingStartIndex: openingUnescapedStartIndex,
158
+ closingStartIndex: closingUnescapedStartIndex,
159
+ bindingType: "unescaped",
160
+ };
161
+ }
162
+ else if (openingContentStartIndex !== -1 &&
163
+ openingContentStartIndex <= openingClientStartIndex) {
164
+ // is default {{}}
165
+ return {
166
+ openingStartIndex: openingContentStartIndex,
167
+ closingStartIndex: closingContentStartIndex,
168
+ bindingType: "default",
169
+ };
170
+ }
171
+ // is client {}
172
+ return {
173
+ openingStartIndex: openingClientStartIndex,
174
+ closingStartIndex: closingClientStartIndex,
175
+ bindingType: "client",
176
+ };
177
+ }
178
+ /**
179
+ * Get the next data binding
180
+ * @param innerHTML - The innerHTML string to evaluate
181
+ * @returns DataBindingBehaviorConfig - A configuration object
182
+ */
183
+ function getNextDataBindingBehavior(innerHTML) {
184
+ const { openingStartIndex, closingStartIndex, bindingType } = getIndexAndBindingTypeOfNextDataBindingBehavior(innerHTML);
185
+ const bindingLength = bindingType === "client" ? 1 : bindingType === "default" ? 2 : 3;
186
+ const partialConfig = {
187
+ type: "dataBinding",
188
+ bindingType,
189
+ openingStartIndex,
190
+ openingEndIndex: openingStartIndex + bindingLength,
191
+ closingStartIndex,
192
+ closingEndIndex: closingStartIndex + bindingLength,
193
+ };
194
+ return isAttributeDirective(innerHTML, openingStartIndex)
195
+ ? getAttributeDirectiveDataBindingConfig(innerHTML, partialConfig)
196
+ : isAttribute(innerHTML, openingStartIndex)
197
+ ? getAttributeDataBindingConfig(innerHTML, partialConfig)
198
+ : getContentDataBindingConfig(partialConfig);
199
+ }
200
+ /**
201
+ * Get the next behavior
202
+ * @param innerHTML - The innerHTML string to evaluate
203
+ * @returns DataBindingBehaviorConfig | DirectiveBehaviorConfig | null - A configuration object or null
204
+ */
205
+ export function getNextBehavior(innerHTML) {
206
+ const dataBindingOpen = innerHTML.indexOf(openClientSideBinding); // client side binding will capture all bindings starting with "{"
207
+ const directiveBindingOpen = innerHTML.indexOf(openTagStart);
208
+ if (dataBindingOpen === -1 && directiveBindingOpen === -1) {
209
+ return null;
210
+ }
211
+ if (directiveBindingOpen !== -1 && dataBindingOpen > directiveBindingOpen) {
212
+ return getNextDirectiveBehavior(innerHTML);
213
+ }
214
+ return getNextDataBindingBehavior(innerHTML);
215
+ }
216
+ /**
217
+ * Gets all the partials with their IDs
218
+ * @param innerHTML - The innerHTML string to evaluate
219
+ * @param offset - The index offset from the innerHTML
220
+ * @param partials - The partials found
221
+ * @returns {[key: string]: PartialTemplateConfig}
222
+ */
223
+ export function getAllPartials(innerHTML, offset = 0, partials = {}) {
224
+ const openingTag = `${openTagStart}partial`;
225
+ const openingTagStartIndex = innerHTML.indexOf(openingTag);
226
+ if (openingTagStartIndex >= 0) {
227
+ const openingTagStartSlice = innerHTML.slice(openingTagStartIndex);
228
+ const closingTag = `${closeTagStart}partial${tagEnd}`;
229
+ const closingTagLength = closingTag.length;
230
+ const matchingCloseTagIndex = getIndexOfNextMatchingTag(openingTagStartSlice, openingTag, closingTag, openingTagStartIndex) + closingTagLength;
231
+ const startId = openingTagStartIndex + ' id="'.length + openingTag.length;
232
+ const endId = innerHTML.slice(startId).indexOf('"') + startId;
233
+ const id = innerHTML.slice(startId, endId);
234
+ const openingTagEndIndex = openingTagStartSlice.indexOf(tagEnd) + 1 + openingTagStartIndex;
235
+ const closingTagStartIndex = matchingCloseTagIndex - closingTagLength;
236
+ partials[id] = {
237
+ innerHTML: innerHTML.slice(openingTagEndIndex, closingTagStartIndex),
238
+ startIndex: openingTagEndIndex + offset,
239
+ endIndex: closingTagStartIndex + offset,
240
+ };
241
+ offset += matchingCloseTagIndex;
242
+ return getAllPartials(innerHTML.slice(matchingCloseTagIndex), offset, partials);
243
+ }
244
+ return partials;
245
+ }
246
+ /**
247
+ * Create a function to resolve a value from an object using a path with dot syntax.
248
+ * e.g. "foo.bar"
249
+ * @param path - The dot syntax path to an objects property.
250
+ * @param self - Where the first item in the path path refers to the item itself (used by repeat).
251
+ * @returns A function to access the value from a given path.
252
+ */
253
+ export function pathResolver(path, self = false) {
254
+ let splitPath = path.split(".");
255
+ const usesContext = path.startsWith("../");
256
+ if (self && !usesContext) {
257
+ if (splitPath.length > 1) {
258
+ splitPath = splitPath.slice(1);
259
+ }
260
+ else {
261
+ return (accessibleObject) => {
262
+ return accessibleObject;
263
+ };
264
+ }
265
+ }
266
+ if (splitPath.length === 1 && !usesContext) {
267
+ return (accessibleObject) => {
268
+ return accessibleObject === null || accessibleObject === void 0 ? void 0 : accessibleObject[splitPath[0]];
269
+ };
270
+ }
271
+ return (accessibleObject, context) => {
272
+ if (usesContext) {
273
+ splitPath = [];
274
+ path.split("../").forEach(pathItem => {
275
+ if (pathItem === "") {
276
+ splitPath.unshift("parent");
277
+ }
278
+ else {
279
+ splitPath.push(...pathItem.split("."));
280
+ }
281
+ });
282
+ return splitPath.reduce((previousAccessors, pathItem) => {
283
+ return previousAccessors === null || previousAccessors === void 0 ? void 0 : previousAccessors[pathItem];
284
+ }, context);
285
+ }
286
+ return splitPath.reduce((previousAccessors, pathItem) => {
287
+ return previousAccessors === null || previousAccessors === void 0 ? void 0 : previousAccessors[pathItem];
288
+ }, accessibleObject);
289
+ };
290
+ }
291
+ /**
292
+ * Determine if the operand is a value (boolean, number, string) or an accessor.
293
+ * @param operand
294
+ */
295
+ function isOperandValue(operand) {
296
+ try {
297
+ const value = JSON.parse(operand);
298
+ return {
299
+ value,
300
+ isValue: true,
301
+ };
302
+ }
303
+ catch (e) {
304
+ return {
305
+ value: operand,
306
+ isValue: false,
307
+ };
308
+ }
309
+ }
310
+ /**
311
+ * Get the operator used.
312
+ * @param value the binded value
313
+ * @returns Operator
314
+ */
315
+ export function getOperator(value) {
316
+ if (value[0] === "!") {
317
+ return {
318
+ operator: "!",
319
+ left: value.slice(1),
320
+ right: null,
321
+ rightIsValue: null,
322
+ };
323
+ }
324
+ const split = value.split(" ");
325
+ if (split.length === 3) {
326
+ const operator = split[1];
327
+ const { value, isValue } = isOperandValue(split[2]);
328
+ return {
329
+ operator,
330
+ left: split[0],
331
+ right: isValue ? value : split[2],
332
+ rightIsValue: isValue,
333
+ };
334
+ }
335
+ return {
336
+ operator: "access",
337
+ left: value,
338
+ right: null,
339
+ rightIsValue: null,
340
+ };
341
+ }
342
+ /**
343
+ * This is the transform utility for rationalizing declarative HTML syntax
344
+ * with bindings in the ViewTemplate
345
+ * @param innerHTML The innerHTML to transform
346
+ * @param index The index to start the current slice of HTML to evaluate
347
+ */
348
+ export function transformInnerHTML(innerHTML, index = 0) {
349
+ const sliceToEvaluate = innerHTML.slice(index);
350
+ const nextBinding = getNextBehavior(sliceToEvaluate);
351
+ let transformedInnerHTML = innerHTML;
352
+ if (nextBinding && nextBinding.type === "dataBinding") {
353
+ if (nextBinding.bindingType === "unescaped") {
354
+ transformedInnerHTML = `${innerHTML.slice(0, index)}${sliceToEvaluate.slice(0, nextBinding.openingStartIndex)}${startInnerHTMLDiv}${sliceToEvaluate.slice(nextBinding.openingStartIndex + 3, nextBinding.closingStartIndex)}${endInnerHTMLDiv}${sliceToEvaluate.slice(nextBinding.closingStartIndex + 3)}`;
355
+ return transformInnerHTML(transformedInnerHTML, index +
356
+ startInnerHTMLDivLength +
357
+ endInnerHTMLDivLength +
358
+ nextBinding.closingStartIndex -
359
+ 3);
360
+ }
361
+ else if (nextBinding.bindingType === "client") {
362
+ return transformInnerHTML(transformedInnerHTML, index + nextBinding.closingEndIndex);
363
+ }
364
+ return transformInnerHTML(transformedInnerHTML, index + nextBinding.closingEndIndex);
365
+ }
366
+ else if (nextBinding) {
367
+ return transformInnerHTML(transformedInnerHTML, index + nextBinding.closingTagEndIndex);
368
+ }
369
+ return transformedInnerHTML;
370
+ }