@microsoft/fast-html 1.0.0-alpha.17 → 1.0.0-alpha.19

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 CHANGED
@@ -22,12 +22,12 @@ npm install --save @microsoft/fast-html
22
22
  In your JS bundle you will need to include the `@microsoft/fast-html` package:
23
23
 
24
24
  ```typescript
25
- import { TemplateElement } from "@microsoft/fast-html";
25
+ import { RenderableFASTElement, TemplateElement } from "@microsoft/fast-html";
26
26
  import { MyCustomElement } from "./my-custom-element";
27
27
 
28
- MyCustomElement.define({
28
+ RenderableFASTElement(MyCustomElement).defineAsync({
29
29
  name: "my-custom-element",
30
- shadowOptions: null,
30
+ templateOptions: "defer-and-hydrate",
31
31
  });
32
32
 
33
33
  TemplateElement.define({
@@ -35,9 +35,7 @@ TemplateElement.define({
35
35
  });
36
36
  ```
37
37
 
38
- This will include the `<f-template>` custom element and all logic for interpreting the declarative HTML syntax for a FAST web component as well as the `shadowOptions` for any element an `<f-template>` has been used to define.
39
-
40
- It is necessary to set the initial `shadowOptions` of your custom elements to `null` otherwise a shadowRoot will be attached and cause a FOUC (Flash Of Unstyled Content). For more information about how this affects hydration, check out our [document](./RENDERING.md#setting-shadow-options) on rendering DOM from a non-browser environment.
38
+ This will include the `<f-template>` custom element and all logic for interpreting the declarative HTML syntax for a FAST web component.
41
39
 
42
40
  The template must be wrapped in `<f-template name="[custom-element-name]"><template>[template logic]</template></f-template>` with a `name` attribute for the custom elements name, and the template logic inside.
43
41
 
@@ -77,23 +75,19 @@ TemplateElement.options({
77
75
 
78
76
  #### Using the RenderableFASTElement
79
77
 
80
- The exported abstract class `RenderableFASTElement` is available for automatic addition and removal of `defer-hydration` and `needs-hydration`. If you use `FASTElement` you will need to add `defer-hydration` and `needs-hydration` attributes to your rendered markup and remove them via your component.
78
+ The use of `RenderableFASTElement` as a mixin for your custom element will automatically remove the `defer-hydration` attribute signalling for hydration to begin, and if you need to add state before hydration should occur you can make use of the `prepare` method.
81
79
 
82
80
  Example:
83
81
  ```typescript
84
- import { RenderableFASTElement, TemplateElement } from "@microsoft/fast-html";
85
-
86
- class MyCustomElement extends RenderableFASTElement {
87
- // component logic
82
+ class MyCustomElement extends FASTElement {
83
+ private prepare(): Promise<void> {
84
+ // Get initial state
85
+ }
88
86
  }
89
87
 
90
- MyCustomElement.define({
88
+ RenderableFASTElement(MyCustomElement).defineAsync({
91
89
  name: "my-custom-element",
92
- shadowOptions: null,
93
- });
94
-
95
- TemplateElement.define({
96
- name: "f-template",
90
+ templateOptions: "defer-and-hydrate",
97
91
  });
98
92
  ```
99
93
 
@@ -1,2 +1,3 @@
1
1
  export { TemplateElement } from "./template.js";
2
2
  export { RenderableFASTElement } from "./element.js";
3
+ export { ObserverMap } from "./observer-map.js";
@@ -0,0 +1,143 @@
1
+ import type { CachedPathMap, ContextCache, PathType } from "./utilities.js";
2
+ interface PathConfig {
3
+ path: string;
4
+ self: boolean;
5
+ parentContext: string | null;
6
+ contextPath: string | null;
7
+ type: PathType;
8
+ level: number;
9
+ rootPath: string | null;
10
+ context: ContextCache | null;
11
+ }
12
+ /**
13
+ * ObserverMap provides functionality for caching binding paths, extracting root properties,
14
+ * and defining observable properties on class prototypes
15
+ */
16
+ export declare class ObserverMap {
17
+ private cachePaths;
18
+ private contextCache;
19
+ private classPrototype;
20
+ constructor(classPrototype: any);
21
+ private getRootProperty;
22
+ /**
23
+ * Caches a binding path with context information for later use in generating observable properties
24
+ * @param context - The path context information containing path, self, parentContext, contextPath, type, and level
25
+ */
26
+ cachePathWithContext(config: PathConfig): void;
27
+ /**
28
+ * Handles caching of paths with relative navigation ("../")
29
+ * Determines the correct context level and caches the path accordingly
30
+ */
31
+ private handleRelativePathCaching;
32
+ /**
33
+ * Counts the number of "../" sequences in a path without using regex
34
+ */
35
+ private countRelativeNavigationLevels;
36
+ /**
37
+ * Extracts the property name from a relative path, removing all "../" sequences
38
+ */
39
+ private extractPropertyNameFromRelativePath;
40
+ /**
41
+ * Determines the target context after navigating up the specified number of levels
42
+ */
43
+ private getTargetContextForRelativePath;
44
+ /**
45
+ * Caches a path at the root level
46
+ */
47
+ private cacheAtRootLevel;
48
+ /**
49
+ * Caches a path at a specific context level
50
+ */
51
+ private cacheAtContextLevel;
52
+ /**
53
+ * Handles caching of access-type paths (property bindings)
54
+ */
55
+ private handleAccessPath;
56
+ /**
57
+ * Handles caching of repeat-type paths (array/loop contexts)
58
+ */
59
+ private handleRepeatPath;
60
+ /**
61
+ * Caches a simple access path without context complexity
62
+ */
63
+ private cacheSimpleAccessPath;
64
+ /**
65
+ * Finds a context item in the temporary context cache
66
+ */
67
+ private findContextInCache;
68
+ /**
69
+ * Attempts to place an access path under an existing repeat structure
70
+ * @returns true if successfully placed, false otherwise
71
+ */
72
+ private tryPlaceInExistingRepeat;
73
+ /**
74
+ * Recursively searches for and places access paths in matching repeat structures
75
+ */
76
+ private searchAndPlaceInRepeat;
77
+ /**
78
+ * Checks if a cached path is a repeat structure with matching context
79
+ */
80
+ private isMatchingRepeatStructure;
81
+ /**
82
+ * Determines if a cached path can be searched for nested structures
83
+ */
84
+ private canSearchNested;
85
+ /**
86
+ * Places an access path within an existing repeat structure
87
+ */
88
+ private placeAccessInRepeat;
89
+ /**
90
+ * Builds absolute paths for nested repeat access patterns
91
+ * @example "root.items.__index__.subitems.__index__.title"
92
+ */
93
+ private buildNestedRepeatAbsolutePath;
94
+ /**
95
+ * Caches access paths that have context information
96
+ */
97
+ private cacheAccessPathWithContext;
98
+ /**
99
+ * Extracts the relative path from a context's absolute path
100
+ * For nested contexts, this should match the cache structure path
101
+ */
102
+ private getRelativeContextPath;
103
+ /**
104
+ * Ensures a repeat structure exists in the cache
105
+ */
106
+ private ensureRepeatStructureExists;
107
+ /**
108
+ * Creates the cache structure for nested repeat patterns
109
+ */
110
+ private createNestedRepeatStructure;
111
+ /**
112
+ * Finds the cache path where a parent context's repeat structure is located
113
+ */
114
+ private findParentRepeatPath;
115
+ getCachedPathsWithContext(): CachedPathMap;
116
+ private resolveContextPath;
117
+ private resolveRootAndContextPath;
118
+ getAbsolutePath(config: PathConfig): string;
119
+ /**
120
+ * Builds the full context path by looking up parent contexts in contextCache
121
+ * @param parentContext - The immediate parent context to start from
122
+ * @param contextPath - The current context path (like "items")
123
+ * @returns The full absolute path including all parent contexts
124
+ */
125
+ private getPathFromCachedContext;
126
+ defineProperties(): void;
127
+ /**
128
+ * Creates a proxy for an object that intercepts property mutations and triggers Observable notifications
129
+ * @param target - The target instance that owns the root property
130
+ * @param rootProperty - The name of the root property for notification purposes
131
+ * @param object - The object to wrap with a proxy
132
+ * @returns A proxy that triggers notifications on property mutations
133
+ */
134
+ private getAndAssignObservables;
135
+ /**
136
+ * Creates a property change handler function for observable properties
137
+ * This handler is called when an observable property transitions from undefined to a defined value
138
+ * @param propertyName - The name of the property for which to create the change handler
139
+ * @returns A function that handles property changes and sets up proxies for object values
140
+ */
141
+ private defineChanged;
142
+ }
143
+ export {};
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,126 @@
1
+ interface JSONSchemaDefinition extends JSONSchemaCommon {
2
+ $fast_context: string;
3
+ $fast_parent_contexts: Array<string>;
4
+ }
5
+ interface JSONSchemaCommon {
6
+ type?: string;
7
+ properties?: any;
8
+ }
9
+ interface JSONSchema extends JSONSchemaCommon {
10
+ $schema: string;
11
+ $id: string;
12
+ $defs?: Record<string, JSONSchemaDefinition>;
13
+ $ref?: string;
14
+ }
15
+ type AccessCachedPathType = "access";
16
+ export interface AccessCachedPath {
17
+ type: AccessCachedPathType;
18
+ currentContext: string | null;
19
+ path: string;
20
+ }
21
+ type DefaultCachedPathType = "default";
22
+ export interface DefaultCachedPath {
23
+ type: DefaultCachedPathType;
24
+ currentContext: string | null;
25
+ path: string;
26
+ }
27
+ type EventCachedPathType = "event";
28
+ export interface EventCachedPath {
29
+ type: EventCachedPathType;
30
+ currentContext: string | null;
31
+ path: string;
32
+ }
33
+ type RepeatCachedPathType = "repeat";
34
+ export interface RepeatCachedPath {
35
+ type: RepeatCachedPathType;
36
+ parentContext: string | null;
37
+ currentContext: string;
38
+ path: string;
39
+ }
40
+ export type CachedPath = DefaultCachedPath | RepeatCachedPath | AccessCachedPath | EventCachedPath;
41
+ export type CachedPathMap = Map<string, JSONSchema>;
42
+ interface RegisterPathConfig {
43
+ rootPropertyName: string;
44
+ pathConfig: CachedPath;
45
+ }
46
+ /**
47
+ * A constructed JSON schema from a template
48
+ */
49
+ export declare class Schema {
50
+ /**
51
+ * The name of the custom element
52
+ */
53
+ private customElementName;
54
+ /**
55
+ * A JSON schema describing each root schema
56
+ */
57
+ private jsonSchemaMap;
58
+ constructor(name: string);
59
+ /**
60
+ * Add a path to a schema
61
+ * @param config RegisterPathConfig
62
+ */
63
+ addPath(config: RegisterPathConfig): void;
64
+ /**
65
+ * Gets the JSON schema for a property name
66
+ * @param rootPropertyName - the root property the JSON schema is mapped to
67
+ * @returns The JSON schema for the root property
68
+ */
69
+ getSchema(rootPropertyName: string): JSONSchema | null;
70
+ /**
71
+ * Get a path split into property names
72
+ * @param path The dot syntax path e.g. a.b.c
73
+ * @returns An array of items in the path
74
+ */
75
+ private getSplitPath;
76
+ /**
77
+ * Gets the path to the $def
78
+ * @param context The context name e.g. {{item in items}} in a repeat creates the "item" context
79
+ * @returns A string to use as a $ref
80
+ */
81
+ private getDefsPath;
82
+ /**
83
+ * Add a new JSON schema to the JSON schema map
84
+ * @param propertyName The name of the property to assign this JSON schema to
85
+ */
86
+ private addNewSchema;
87
+ /**
88
+ * Add properties to a context
89
+ * @param schema The schema to add the properties to
90
+ * @param splitPath The path split into property/context names
91
+ * @param context The paths context
92
+ */
93
+ private addPropertiesToAContext;
94
+ /**
95
+ * Add properties to an object
96
+ * @param schema The schema to add the properties to
97
+ * @param splitPath The path split into property/context names
98
+ * @param context The paths context
99
+ * @param type The data type (see JSON schema for details)
100
+ */
101
+ private addPropertiesToAnObject;
102
+ /**
103
+ * Add an array to an object property
104
+ * @param schema The schema to add the properties to
105
+ * @param context The name of the context
106
+ */
107
+ private addArrayToAnObject;
108
+ /**
109
+ * Add a context to the $defs property
110
+ * @param schema The schema to use
111
+ * @param propertyName The name of the property the context belongs to
112
+ * @param currentContext The current context
113
+ * @param parentContext The parent context
114
+ * @returns
115
+ */
116
+ private addContext;
117
+ /**
118
+ * Get parent contexts
119
+ * @param schema The schema to use
120
+ * @param parentContext The parent context
121
+ * @param contexts A list of parent contexts
122
+ * @returns
123
+ */
124
+ private getParentContexts;
125
+ }
126
+ export {};
@@ -0,0 +1 @@
1
+ export {};
@@ -1,7 +1,9 @@
1
1
  import { FASTElement, ShadowRootOptions } from "@microsoft/fast-element";
2
2
  import "@microsoft/fast-element/install-hydratable-view-templates.js";
3
+ export type ObserverMapOption = "all";
3
4
  export interface ElementOptions {
4
5
  shadowOptions?: ShadowRootOptions | undefined;
6
+ observerMap?: ObserverMapOption | undefined;
5
7
  }
6
8
  /**
7
9
  * A dictionary of element options the TemplateElement will use to update the registered element
@@ -22,6 +24,10 @@ declare class TemplateElement extends FASTElement {
22
24
  */
23
25
  static elementOptions: ElementOptionsDictionary;
24
26
  private partials;
27
+ /**
28
+ * ObserverMap instance for caching binding paths
29
+ */
30
+ private observerMap?;
25
31
  private static defaultElementOptions;
26
32
  static options(elementOptions?: ElementOptionsDictionary): typeof TemplateElement;
27
33
  constructor();
@@ -35,6 +41,7 @@ declare class TemplateElement extends FASTElement {
35
41
  * Resolve strings and values from an innerHTML string
36
42
  * @param innerHTML - The innerHTML.
37
43
  * @param self - Indicates that this should refer to itself instead of a property when creating bindings.
44
+ * @param observerMap - ObserverMap instance for caching binding paths (optional).
38
45
  */
39
46
  private resolveStringsAndValues;
40
47
  /**
@@ -48,6 +55,8 @@ declare class TemplateElement extends FASTElement {
48
55
  * @param behaviorConfig - The directive behavior configuration object.
49
56
  * @param externalValues - The interpreted values from the parent.
50
57
  * @param innerHTML - The innerHTML.
58
+ * @param self - Indicates that this should refer to itself instead of a property when creating bindings.
59
+ * @param observerMap - ObserverMap instance for caching binding paths (optional).
51
60
  */
52
61
  private resolveTemplateDirective;
53
62
  /**
@@ -64,6 +73,7 @@ declare class TemplateElement extends FASTElement {
64
73
  * @param values - The interpreted values.
65
74
  * @param self - Indicates that this should refer to itself instead of a property when creating bindings.
66
75
  * @param behaviorConfig - The binding behavior configuration object.
76
+ * @param observerMap - ObserverMap instance for caching binding paths (optional).
67
77
  */
68
78
  private resolveDataBinding;
69
79
  /**
@@ -72,6 +82,7 @@ declare class TemplateElement extends FASTElement {
72
82
  * @param strings - The strings array.
73
83
  * @param values - The interpreted values.
74
84
  * @param self - Indicates that this should refer to itself instead of a property when creating bindings.
85
+ * @param observerMap - ObserverMap instance for caching binding paths (optional).
75
86
  */
76
87
  private resolveInnerHTML;
77
88
  /**
@@ -1,3 +1,4 @@
1
+ import { type ObserverMap } from "./observer-map.js";
1
2
  type BehaviorType = "dataBinding" | "templateDirective";
2
3
  type TemplateDirective = "when" | "repeat" | "apply";
3
4
  export type AttributeDirective = "children" | "slotted" | "ref";
@@ -5,6 +6,7 @@ type DataBindingBindingType = "client" | "default" | "unescaped";
5
6
  interface BehaviorConfig {
6
7
  type: BehaviorType;
7
8
  }
9
+ export type PathType = "access" | "default" | "event" | "repeat";
8
10
  export interface ContentDataBindingBehaviorConfig extends BaseDataBindingBehaviorConfig {
9
11
  subtype: "content";
10
12
  }
@@ -39,6 +41,45 @@ interface PartialTemplateConfig {
39
41
  startIndex: number;
40
42
  endIndex: number;
41
43
  }
44
+ type AccessCachedPathType = "access";
45
+ export interface AccessCachedPath {
46
+ type: AccessCachedPathType;
47
+ relativePath: string;
48
+ absolutePath: string;
49
+ }
50
+ type DefaultCachedPathType = "default";
51
+ export interface DefaultCachedPath {
52
+ type: DefaultCachedPathType;
53
+ paths: Record<string, CachedPath>;
54
+ }
55
+ type EventCachedPathType = "event";
56
+ export interface EventCachedPath {
57
+ type: EventCachedPathType;
58
+ relativePath: string;
59
+ absolutePath: string;
60
+ }
61
+ type RepeatCachedPathType = "repeat";
62
+ export interface RepeatCachedPath {
63
+ type: RepeatCachedPathType;
64
+ context: string;
65
+ paths: Record<string, CachedPath>;
66
+ }
67
+ export type CachedPath = DefaultCachedPath | RepeatCachedPath | AccessCachedPath | EventCachedPath;
68
+ export type CachedPathMap = Record<string, CachedPath>;
69
+ export interface ContextCache {
70
+ /**
71
+ * The path to this context
72
+ */
73
+ absolutePath: string;
74
+ /**
75
+ * The self string of that context
76
+ */
77
+ context: string;
78
+ /**
79
+ * The parent of this context
80
+ */
81
+ parent: string | null;
82
+ }
42
83
  /**
43
84
  * Get the index of the next matching tag
44
85
  * @param openingTagStartSlice - The slice starting from the opening tag
@@ -74,6 +115,14 @@ export declare function getAllPartials(innerHTML: string, offset?: number, parti
74
115
  * @returns A function to access the value from a given path.
75
116
  */
76
117
  export declare function pathResolver(path: string, self?: boolean): (accessibleObject: any, context: any) => any;
118
+ export declare function bindingResolver(path: string, self: boolean | undefined, parentContext: string | null, type: PathType, observerMap: ObserverMap | null, contextPath: string | null, level: number): (accessibleObject: any, context: any) => any;
119
+ export declare function expressionResolver(self: boolean, expression: ChainedExpression, parentContext: string | null, level: number, observerMap?: ObserverMap): (accessibleObject: any, context: any) => any;
120
+ /**
121
+ * Extracts all paths from a ChainedExpression, including nested expressions
122
+ * @param chainedExpression - The chained expression to extract paths from
123
+ * @returns A Set containing all unique paths found in the expression chain
124
+ */
125
+ export declare function extractPathsFromChainedExpression(chainedExpression: ChainedExpression): Set<string>;
77
126
  /**
78
127
  * Available operators include:
79
128
  *
@@ -93,6 +142,7 @@ type ChainingOperator = "||" | "&&" | "&amp;&amp;";
93
142
  interface Expression {
94
143
  operator: Operator;
95
144
  left: string;
145
+ leftIsValue: boolean | null;
96
146
  right: string | boolean | number | null;
97
147
  rightIsValue: boolean | null;
98
148
  }
@@ -119,7 +169,16 @@ export declare function transformInnerHTML(innerHTML: string, index?: number): s
119
169
  * @param self - Where the first item in the path path refers to the item itself (used by repeat).
120
170
  * @param chainedExpression - The chained expression which includes the expression and the next expression
121
171
  * if there is another in the chain
172
+ * @param observerMap - Optional ObserverMap instance for caching paths during template processing
122
173
  * @returns - A binding that resolves the chained expression logic
123
174
  */
124
- export declare function resolveWhen(self: boolean, { expression, next }: ChainedExpression): (x: boolean, c: any) => any;
175
+ export declare function resolveWhen(self: boolean, expression: ChainedExpression, parentContext: string | null, level: number, observerMap?: ObserverMap): (x: boolean, c: any) => any;
176
+ /**
177
+ * Get the next property
178
+ * @param path The dot syntax data path
179
+ * @returns The next property
180
+ */
181
+ export declare function getNextProperty(path: string): string;
182
+ export declare function assignObservables(cachePath: CachedPath, data: any, target: any, rootProperty: string): typeof Proxy;
183
+ export declare function assignProxy(cachePath: CachedPath, target: any, rootProperty: string, object: any): typeof Proxy;
125
184
  export {};
@@ -0,0 +1 @@
1
+ export {};
@@ -1 +1 @@
1
- export { RenderableFASTElement, TemplateElement } from "./components/index.js";
1
+ export { RenderableFASTElement, TemplateElement, ObserverMap, } from "./components/index.js";
@@ -1,2 +1,3 @@
1
1
  export { TemplateElement } from "./template.js";
2
2
  export { RenderableFASTElement } from "./element.js";
3
+ export { ObserverMap } from "./observer-map.js";