@microsoft/fast-html 1.0.0-alpha.1 → 1.0.0-alpha.11
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/README.md +100 -7
- package/dist/dts/components/index.d.ts +1 -0
- package/dist/dts/components/template.d.ts +74 -0
- package/dist/dts/components/utilities.d.ts +125 -0
- package/dist/dts/components/utilities.spec.d.ts +1 -0
- package/dist/dts/debug.d.ts +3 -0
- package/dist/dts/fixtures/attribute/attribute.spec.d.ts +1 -0
- package/dist/dts/fixtures/attribute/main.d.ts +1 -0
- package/dist/dts/fixtures/binding/binding.spec.d.ts +1 -0
- package/dist/dts/fixtures/binding/main.d.ts +1 -0
- package/dist/dts/fixtures/children/children.spec.d.ts +1 -0
- package/dist/dts/fixtures/children/main.d.ts +1 -0
- package/dist/dts/fixtures/dot-syntax/dot-syntax.spec.d.ts +1 -0
- package/dist/dts/fixtures/dot-syntax/main.d.ts +1 -0
- package/dist/dts/fixtures/event/event.spec.d.ts +1 -0
- package/dist/dts/fixtures/event/main.d.ts +1 -0
- package/dist/dts/fixtures/partial/main.d.ts +1 -0
- package/dist/dts/fixtures/partial/partial.spec.d.ts +1 -0
- package/dist/dts/fixtures/ref/main.d.ts +1 -0
- package/dist/dts/fixtures/ref/ref.spec.d.ts +1 -0
- package/dist/dts/fixtures/repeat/main.d.ts +1 -0
- package/dist/dts/fixtures/repeat/repeat.spec.d.ts +1 -0
- package/dist/dts/fixtures/slotted/main.d.ts +1 -0
- package/dist/dts/fixtures/slotted/slotted.spec.d.ts +1 -0
- package/dist/dts/fixtures/when/main.d.ts +1 -0
- package/dist/dts/fixtures/when/when.spec.d.ts +1 -0
- package/dist/dts/index.d.ts +1 -0
- package/dist/dts/interfaces.d.ts +7 -0
- package/dist/dts/tsdoc-metadata.json +11 -0
- package/dist/esm/components/index.js +1 -0
- package/dist/esm/components/template.js +259 -0
- package/dist/esm/components/utilities.js +465 -0
- package/dist/esm/components/utilities.spec.js +277 -0
- package/dist/esm/debug.js +3 -0
- package/dist/esm/fixtures/attribute/attribute.spec.js +23 -0
- package/dist/esm/fixtures/attribute/main.js +26 -0
- package/dist/esm/fixtures/binding/binding.spec.js +23 -0
- package/dist/esm/fixtures/binding/main.js +40 -0
- package/dist/esm/fixtures/children/children.spec.js +37 -0
- package/dist/esm/fixtures/children/main.js +31 -0
- package/dist/esm/fixtures/dot-syntax/dot-syntax.spec.js +9 -0
- package/dist/esm/fixtures/dot-syntax/main.js +23 -0
- package/dist/esm/fixtures/event/event.spec.js +28 -0
- package/dist/esm/fixtures/event/main.js +35 -0
- package/dist/esm/fixtures/partial/main.js +38 -0
- package/dist/esm/fixtures/partial/partial.spec.js +14 -0
- package/dist/esm/fixtures/ref/main.js +21 -0
- package/dist/esm/fixtures/ref/ref.spec.js +13 -0
- package/dist/esm/fixtures/repeat/main.js +27 -0
- package/dist/esm/fixtures/repeat/repeat.spec.js +29 -0
- package/dist/esm/fixtures/slotted/main.js +29 -0
- package/dist/esm/fixtures/slotted/slotted.spec.js +25 -0
- package/dist/esm/fixtures/when/main.js +198 -0
- package/dist/esm/fixtures/when/when.spec.js +82 -0
- package/dist/esm/index.js +4 -0
- package/dist/esm/interfaces.js +1 -0
- package/dist/esm/tsconfig.tsbuildinfo +1 -0
- package/dist/fast-html.api.json +356 -0
- package/dist/fast-html.d.ts +78 -0
- package/dist/fast-html.untrimmed.d.ts +78 -0
- package/package.json +12 -6
- package/rules/attribute-directives.yml +38 -0
- package/rules/call-expression-with-event-argument.yml +41 -0
- package/rules/member-expression.yml +33 -0
- package/rules/tag-function-to-template-literal.yml +16 -0
- package/CHANGELOG.json +0 -26
- package/CHANGELOG.md +0 -14
- package/docs/api-report.api.md +0 -18
- package/webpack.common.config.js +0 -18
|
@@ -0,0 +1,259 @@
|
|
|
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, getExpressionChain, getNextBehavior, pathResolver, resolveWhen, 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 expressionChain = getExpressionChain(behaviorConfig.value);
|
|
98
|
+
const whenLogic = resolveWhen(self, expressionChain);
|
|
99
|
+
externalValues.push(when(whenLogic, this.resolveTemplateOrBehavior(strings, values)));
|
|
100
|
+
}
|
|
101
|
+
break;
|
|
102
|
+
case "repeat":
|
|
103
|
+
{
|
|
104
|
+
const valueAttr = behaviorConfig.value.split(" "); // syntax {{x in y}}
|
|
105
|
+
const { strings, values } = yield this.resolveStringsAndValues(innerHTML.slice(behaviorConfig.openingTagEndIndex, behaviorConfig.closingTagStartIndex), true);
|
|
106
|
+
const { repeat } = yield import("@microsoft/fast-element");
|
|
107
|
+
externalValues.push(repeat((x, c) => pathResolver(valueAttr[2], self)(x, c), this.resolveTemplateOrBehavior(strings, values)));
|
|
108
|
+
}
|
|
109
|
+
break;
|
|
110
|
+
case "apply": {
|
|
111
|
+
const openingTag = innerHTML.slice(behaviorConfig.openingTagStartIndex, behaviorConfig.openingTagEndIndex);
|
|
112
|
+
const partial = (_a = openingTag
|
|
113
|
+
.split(" ")
|
|
114
|
+
.find(tagPart => {
|
|
115
|
+
return tagPart.startsWith("partial");
|
|
116
|
+
})) === null || _a === void 0 ? void 0 : _a.split('"')[1];
|
|
117
|
+
if (partial) {
|
|
118
|
+
const { when } = yield import("@microsoft/fast-element");
|
|
119
|
+
externalValues.push(when((x, c) => pathResolver(behaviorConfig.value, self)(x, c), () => this.partials[partial]));
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
});
|
|
124
|
+
}
|
|
125
|
+
/**
|
|
126
|
+
* Resolve a template directive
|
|
127
|
+
* @param name - The name of the directive.
|
|
128
|
+
* @param propName - The property name to pass to the directive.
|
|
129
|
+
* @param externalValues - The interpreted values from the parent.
|
|
130
|
+
*/
|
|
131
|
+
resolveAttributeDirective(name, propName, externalValues) {
|
|
132
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
133
|
+
switch (name) {
|
|
134
|
+
case "children":
|
|
135
|
+
{
|
|
136
|
+
const { children } = yield import("@microsoft/fast-element");
|
|
137
|
+
externalValues.push(children(propName));
|
|
138
|
+
}
|
|
139
|
+
break;
|
|
140
|
+
case "slotted":
|
|
141
|
+
{
|
|
142
|
+
const { slotted } = yield import("@microsoft/fast-element");
|
|
143
|
+
externalValues.push(slotted(propName));
|
|
144
|
+
}
|
|
145
|
+
break;
|
|
146
|
+
case "ref":
|
|
147
|
+
{
|
|
148
|
+
const { ref } = yield import("@microsoft/fast-element");
|
|
149
|
+
externalValues.push(ref(propName));
|
|
150
|
+
}
|
|
151
|
+
break;
|
|
152
|
+
}
|
|
153
|
+
});
|
|
154
|
+
}
|
|
155
|
+
/**
|
|
156
|
+
* Resolver of a data binding
|
|
157
|
+
* @param innerHTML - The innerHTML.
|
|
158
|
+
* @param strings - The strings array.
|
|
159
|
+
* @param values - The interpreted values.
|
|
160
|
+
* @param self - Indicates that this should refer to itself instead of a property when creating bindings.
|
|
161
|
+
* @param behaviorConfig - The binding behavior configuration object.
|
|
162
|
+
*/
|
|
163
|
+
resolveDataBinding(innerHTML, strings, values, self = false, behaviorConfig) {
|
|
164
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
165
|
+
switch (behaviorConfig.subtype) {
|
|
166
|
+
case "content":
|
|
167
|
+
{
|
|
168
|
+
strings.push(innerHTML.slice(0, behaviorConfig.openingStartIndex));
|
|
169
|
+
const propName = innerHTML.slice(behaviorConfig.openingEndIndex, behaviorConfig.closingStartIndex);
|
|
170
|
+
const binding = (x, c) => pathResolver(propName, self)(x, c);
|
|
171
|
+
values.push(binding);
|
|
172
|
+
yield this.resolveInnerHTML(innerHTML.slice(behaviorConfig.closingEndIndex, innerHTML.length), strings, values, self);
|
|
173
|
+
}
|
|
174
|
+
break;
|
|
175
|
+
case "attribute":
|
|
176
|
+
strings.push(innerHTML.slice(0, behaviorConfig.openingStartIndex));
|
|
177
|
+
if (behaviorConfig.aspect === "@") {
|
|
178
|
+
const bindingHTML = innerHTML.slice(behaviorConfig.openingEndIndex, behaviorConfig.closingStartIndex);
|
|
179
|
+
const openingParenthesis = bindingHTML.indexOf("(");
|
|
180
|
+
const closingParenthesis = bindingHTML.indexOf(")");
|
|
181
|
+
const propName = innerHTML.slice(behaviorConfig.openingEndIndex, behaviorConfig.closingStartIndex -
|
|
182
|
+
(closingParenthesis - openingParenthesis) -
|
|
183
|
+
1);
|
|
184
|
+
const arg = bindingHTML.slice(openingParenthesis + 1, closingParenthesis);
|
|
185
|
+
const binding = (x, c) => pathResolver(propName, self)(x, c)(...(arg === "e" ? [c.event] : []), ...(arg !== "e" && arg !== ""
|
|
186
|
+
? [pathResolver(arg)(x, c)]
|
|
187
|
+
: []));
|
|
188
|
+
values.push(binding);
|
|
189
|
+
}
|
|
190
|
+
else {
|
|
191
|
+
const propName = innerHTML.slice(behaviorConfig.openingEndIndex, behaviorConfig.closingStartIndex);
|
|
192
|
+
const binding = (x, c) => pathResolver(propName, self)(x, c);
|
|
193
|
+
values.push(binding);
|
|
194
|
+
}
|
|
195
|
+
yield this.resolveInnerHTML(innerHTML.slice(behaviorConfig.closingEndIndex, innerHTML.length), strings, values, self);
|
|
196
|
+
break;
|
|
197
|
+
case "attributeDirective":
|
|
198
|
+
{
|
|
199
|
+
strings.push(innerHTML.slice(0, behaviorConfig.openingStartIndex -
|
|
200
|
+
behaviorConfig.name.length -
|
|
201
|
+
4));
|
|
202
|
+
const propName = innerHTML.slice(behaviorConfig.openingEndIndex, behaviorConfig.closingStartIndex);
|
|
203
|
+
yield this.resolveAttributeDirective(behaviorConfig.name, propName, values);
|
|
204
|
+
yield this.resolveInnerHTML(innerHTML.slice(behaviorConfig.closingEndIndex + 1, innerHTML.length), strings, values, self);
|
|
205
|
+
}
|
|
206
|
+
break;
|
|
207
|
+
}
|
|
208
|
+
});
|
|
209
|
+
}
|
|
210
|
+
/**
|
|
211
|
+
* Resolver of the innerHTML string
|
|
212
|
+
* @param innerHTML - The innerHTML.
|
|
213
|
+
* @param strings - The strings array.
|
|
214
|
+
* @param values - The interpreted values.
|
|
215
|
+
* @param self - Indicates that this should refer to itself instead of a property when creating bindings.
|
|
216
|
+
*/
|
|
217
|
+
resolveInnerHTML(innerHTML, strings, values, self = false) {
|
|
218
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
219
|
+
const behaviorConfig = getNextBehavior(innerHTML);
|
|
220
|
+
if (behaviorConfig === null) {
|
|
221
|
+
strings.push(innerHTML);
|
|
222
|
+
}
|
|
223
|
+
else {
|
|
224
|
+
switch (behaviorConfig.type) {
|
|
225
|
+
case "dataBinding":
|
|
226
|
+
yield this.resolveDataBinding(innerHTML, strings, values, self, behaviorConfig);
|
|
227
|
+
break;
|
|
228
|
+
case "templateDirective":
|
|
229
|
+
strings.push(innerHTML.slice(0, behaviorConfig.openingTagStartIndex));
|
|
230
|
+
yield this.resolveTemplateDirective(behaviorConfig, values, innerHTML, self);
|
|
231
|
+
yield this.resolveInnerHTML(innerHTML.slice(behaviorConfig.closingTagEndIndex, innerHTML.length), strings, values, self);
|
|
232
|
+
break;
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
});
|
|
236
|
+
}
|
|
237
|
+
/**
|
|
238
|
+
* Resolve all partial templates
|
|
239
|
+
* @param unresolvedInnerHTML - The innerHTML.
|
|
240
|
+
*/
|
|
241
|
+
resolveAllPartials(unresolvedInnerHTML) {
|
|
242
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
243
|
+
const allPartials = Object.entries(getAllPartials(unresolvedInnerHTML));
|
|
244
|
+
for (let i = 0, partialLength = allPartials.length; i < partialLength; i++) {
|
|
245
|
+
const { strings, values } = yield this.resolveStringsAndValues(allPartials[i][1].innerHTML);
|
|
246
|
+
this.partials[allPartials[i][0]] = this.resolveTemplateOrBehavior(strings, values);
|
|
247
|
+
}
|
|
248
|
+
});
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
/**
|
|
252
|
+
* A dictionary of custom element options
|
|
253
|
+
*/
|
|
254
|
+
TemplateElement.elementOptions = {};
|
|
255
|
+
__decorate([
|
|
256
|
+
attr,
|
|
257
|
+
__metadata("design:type", String)
|
|
258
|
+
], TemplateElement.prototype, "name", void 0);
|
|
259
|
+
export { TemplateElement };
|