@microsoft/fast-element 2.0.0-beta.10 → 2.0.0-beta.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/CHANGELOG.json CHANGED
@@ -1,6 +1,21 @@
1
1
  {
2
2
  "name": "@microsoft/fast-element",
3
3
  "entries": [
4
+ {
5
+ "date": "Wed, 05 Oct 2022 23:26:01 GMT",
6
+ "tag": "@microsoft/fast-element_v2.0.0-beta.11",
7
+ "version": "2.0.0-beta.11",
8
+ "comments": {
9
+ "prerelease": [
10
+ {
11
+ "author": "nicholasrice@users.noreply.github.com",
12
+ "package": "@microsoft/fast-element",
13
+ "commit": "21d1de94be2b9399ddc43a2b93c6483fa7304591",
14
+ "comment": "fix: removed a shadowRoot leak from ElementController"
15
+ }
16
+ ]
17
+ }
18
+ },
4
19
  {
5
20
  "date": "Mon, 03 Oct 2022 23:44:38 GMT",
6
21
  "tag": "@microsoft/fast-element_v2.0.0-beta.10",
package/CHANGELOG.md CHANGED
@@ -1,9 +1,17 @@
1
1
  # Change Log - @microsoft/fast-element
2
2
 
3
- This log was last generated on Mon, 03 Oct 2022 23:44:38 GMT and should not be manually modified.
3
+ This log was last generated on Wed, 05 Oct 2022 23:26:01 GMT and should not be manually modified.
4
4
 
5
5
  <!-- Start content -->
6
6
 
7
+ ## 2.0.0-beta.11
8
+
9
+ Wed, 05 Oct 2022 23:26:01 GMT
10
+
11
+ ### Changes
12
+
13
+ - fix: removed a shadowRoot leak from ElementController (nicholasrice@users.noreply.github.com)
14
+
7
15
  ## 2.0.0-beta.10
8
16
 
9
17
  Mon, 03 Oct 2022 23:44:38 GMT
@@ -1,8 +1,9 @@
1
+ import { StyleStrategy, StyleTarget } from "../interfaces.js";
1
2
  import type { HostBehavior, HostController } from "../styles/host.js";
2
3
  import { PropertyChangeNotifier } from "../observation/notifier.js";
3
4
  import type { ElementViewTemplate } from "../templating/template.js";
4
5
  import type { ElementView } from "../templating/view.js";
5
- import type { ElementStyles } from "../styles/element-styles.js";
6
+ import { ElementStyles } from "../styles/element-styles.js";
6
7
  import { FASTElementDefinition } from "./fast-definitions.js";
7
8
  /**
8
9
  * Controls the lifecycle and rendering of a `FASTElement`.
@@ -124,3 +125,27 @@ export declare class ElementController<TElement extends HTMLElement = HTMLElemen
124
125
  */
125
126
  static forCustomElement(element: HTMLElement): ElementController;
126
127
  }
128
+ /**
129
+ * https://wicg.github.io/construct-stylesheets/
130
+ * https://developers.google.com/web/updates/2019/02/constructable-stylesheets
131
+ *
132
+ * @internal
133
+ */
134
+ export declare class AdoptedStyleSheetsStrategy implements StyleStrategy {
135
+ private static styleSheetCache;
136
+ /** @internal */
137
+ readonly sheets: CSSStyleSheet[];
138
+ constructor(styles: (string | CSSStyleSheet)[]);
139
+ addStylesTo(target: StyleTarget): void;
140
+ removeStylesFrom(target: StyleTarget): void;
141
+ }
142
+ /**
143
+ * @internal
144
+ */
145
+ export declare class StyleElementStrategy implements StyleStrategy {
146
+ private readonly styles;
147
+ private readonly styleClass;
148
+ constructor(styles: string[]);
149
+ addStylesTo(target: StyleTarget): void;
150
+ removeStylesFrom(target: StyleTarget): void;
151
+ }
@@ -24,4 +24,4 @@ export * from "./templating/node-observation.js";
24
24
  export * from "./components/fast-element.js";
25
25
  export * from "./components/fast-definitions.js";
26
26
  export * from "./components/attributes.js";
27
- export * from "./components/element-controller.js";
27
+ export { ElementController } from "./components/element-controller.js";
@@ -112,14 +112,13 @@ export declare const enum KernelServiceId {
112
112
  updateQueue = 1,
113
113
  observable = 2,
114
114
  contextEvent = 3,
115
- elementRegistry = 4,
116
- styleSheetStrategy = 5
115
+ elementRegistry = 4
117
116
  }
118
117
  /**
119
118
  * A node that can be targeted by styles.
120
119
  * @public
121
120
  */
122
- export interface StyleTarget {
121
+ export interface StyleTarget extends Pick<Node, "getRootNode"> {
123
122
  /**
124
123
  * Stylesheets to be adopted by the node.
125
124
  */
@@ -1,4 +1,5 @@
1
1
  import type { FASTGlobal } from "./interfaces.js";
2
+ import "./polyfills.js";
2
3
  /**
3
4
  * The FAST global.
4
5
  * @internal
@@ -1,8 +1 @@
1
- import type { StyleStrategy, StyleTarget } from "./interfaces.js";
2
- export declare class StyleElementStrategy implements StyleStrategy {
3
- private readonly styles;
4
- private readonly styleClass;
5
- constructor(styles: string[]);
6
- addStylesTo(target: StyleTarget): void;
7
- removeStylesFrom(target: StyleTarget): void;
8
- }
1
+ export {};
@@ -1,4 +1,4 @@
1
- import { StyleStrategy, StyleTarget } from "../interfaces.js";
1
+ import type { StyleStrategy, StyleTarget } from "../interfaces.js";
2
2
  import type { HostBehavior } from "./host.js";
3
3
  /**
4
4
  * Represents styles that can be composed into the ShadowDOM of a custom element.
@@ -69,16 +69,3 @@ export declare class ElementStyles {
69
69
  */
70
70
  static readonly supportsAdoptedStyleSheets: boolean;
71
71
  }
72
- /**
73
- * https://wicg.github.io/construct-stylesheets/
74
- * https://developers.google.com/web/updates/2019/02/constructable-stylesheets
75
- *
76
- * @internal
77
- */
78
- export declare class AdoptedStyleSheetsStrategy implements StyleStrategy {
79
- /** @internal */
80
- readonly sheets: CSSStyleSheet[];
81
- constructor(styles: (string | CSSStyleSheet)[]);
82
- addStylesTo(target: StyleTarget): void;
83
- removeStylesFrom(target: StyleTarget): void;
84
- }
@@ -2,6 +2,7 @@ import "../interfaces.js";
2
2
  import { PropertyChangeNotifier } from "../observation/notifier.js";
3
3
  import { Observable, SourceLifetime } from "../observation/observable.js";
4
4
  import { FAST } from "../platform.js";
5
+ import { ElementStyles } from "../styles/element-styles.js";
5
6
  import { FASTElementDefinition } from "./fast-definitions.js";
6
7
  const defaultEventOptions = {
7
8
  bubbles: true,
@@ -210,13 +211,13 @@ export class ElementController extends PropertyChangeNotifier {
210
211
  return;
211
212
  }
212
213
  const source = this.source;
213
- const target = (_a = getShadowRoot(source)) !== null && _a !== void 0 ? _a : source.getRootNode();
214
214
  if (styles instanceof HTMLElement) {
215
+ const target = (_a = getShadowRoot(source)) !== null && _a !== void 0 ? _a : this.source;
215
216
  target.append(styles);
216
217
  }
217
- else if (!styles.isAttachedTo(target)) {
218
+ else if (!styles.isAttachedTo(source)) {
218
219
  const sourceBehaviors = styles.behaviors;
219
- styles.addStylesTo(target);
220
+ styles.addStylesTo(source);
220
221
  if (sourceBehaviors !== null) {
221
222
  for (let i = 0, ii = sourceBehaviors.length; i < ii; ++i) {
222
223
  this.addBehavior(sourceBehaviors[i]);
@@ -234,13 +235,13 @@ export class ElementController extends PropertyChangeNotifier {
234
235
  return;
235
236
  }
236
237
  const source = this.source;
237
- const target = (_a = getShadowRoot(source)) !== null && _a !== void 0 ? _a : source.getRootNode();
238
238
  if (styles instanceof HTMLElement) {
239
+ const target = (_a = getShadowRoot(source)) !== null && _a !== void 0 ? _a : source;
239
240
  target.removeChild(styles);
240
241
  }
241
- else if (styles.isAttachedTo(target)) {
242
+ else if (styles.isAttachedTo(source)) {
242
243
  const sourceBehaviors = styles.behaviors;
243
- styles.removeStylesFrom(target);
244
+ styles.removeStylesFrom(source);
244
245
  if (sourceBehaviors !== null) {
245
246
  for (let i = 0, ii = sourceBehaviors.length; i < ii; ++i) {
246
247
  this.addBehavior(sourceBehaviors[i]);
@@ -375,3 +376,91 @@ export class ElementController extends PropertyChangeNotifier {
375
376
  return (element.$fastController = new ElementController(element, definition));
376
377
  }
377
378
  }
379
+ /**
380
+ * Converts a styleTarget into the operative target. When the provided target is an Element
381
+ * that is a FASTElement, the function will return the ShadowRoot for that element. Otherwise,
382
+ * it will return the root node for the element.
383
+ * @param target
384
+ * @returns
385
+ */
386
+ function normalizeStyleTarget(target) {
387
+ var _a;
388
+ if ("adoptedStyleSheets" in target) {
389
+ return target;
390
+ }
391
+ else {
392
+ return ((_a = getShadowRoot(target)) !== null && _a !== void 0 ? _a : target.getRootNode());
393
+ }
394
+ }
395
+ // Default StyleStrategy implementations are defined in this module because they
396
+ // require access to element shadowRoots, and we don't want to leak shadowRoot
397
+ // objects out of this module.
398
+ /**
399
+ * https://wicg.github.io/construct-stylesheets/
400
+ * https://developers.google.com/web/updates/2019/02/constructable-stylesheets
401
+ *
402
+ * @internal
403
+ */
404
+ export class AdoptedStyleSheetsStrategy {
405
+ constructor(styles) {
406
+ const styleSheetCache = AdoptedStyleSheetsStrategy.styleSheetCache;
407
+ this.sheets = styles.map((x) => {
408
+ if (x instanceof CSSStyleSheet) {
409
+ return x;
410
+ }
411
+ let sheet = styleSheetCache.get(x);
412
+ if (sheet === void 0) {
413
+ sheet = new CSSStyleSheet();
414
+ sheet.replaceSync(x);
415
+ styleSheetCache.set(x, sheet);
416
+ }
417
+ return sheet;
418
+ });
419
+ }
420
+ addStylesTo(target) {
421
+ const t = normalizeStyleTarget(target);
422
+ t.adoptedStyleSheets = [...t.adoptedStyleSheets, ...this.sheets];
423
+ }
424
+ removeStylesFrom(target) {
425
+ const t = normalizeStyleTarget(target);
426
+ const sheets = this.sheets;
427
+ t.adoptedStyleSheets = t.adoptedStyleSheets.filter((x) => sheets.indexOf(x) === -1);
428
+ }
429
+ }
430
+ AdoptedStyleSheetsStrategy.styleSheetCache = new Map();
431
+ let id = 0;
432
+ const nextStyleId = () => `fast-${++id}`;
433
+ function usableStyleTarget(target) {
434
+ return target === document ? document.body : target;
435
+ }
436
+ /**
437
+ * @internal
438
+ */
439
+ export class StyleElementStrategy {
440
+ constructor(styles) {
441
+ this.styles = styles;
442
+ this.styleClass = nextStyleId();
443
+ }
444
+ addStylesTo(target) {
445
+ target = usableStyleTarget(normalizeStyleTarget(target));
446
+ const styles = this.styles;
447
+ const styleClass = this.styleClass;
448
+ for (let i = 0; i < styles.length; i++) {
449
+ const element = document.createElement("style");
450
+ element.innerHTML = styles[i];
451
+ element.className = styleClass;
452
+ target.append(element);
453
+ }
454
+ }
455
+ removeStylesFrom(target) {
456
+ target = usableStyleTarget(normalizeStyleTarget(target));
457
+ const styles = target.querySelectorAll(`.${this.styleClass}`);
458
+ styles[0].parentNode;
459
+ for (let i = 0, ii = styles.length; i < ii; ++i) {
460
+ target.removeChild(styles[i]);
461
+ }
462
+ }
463
+ }
464
+ ElementStyles.setDefaultStrategy(ElementStyles.supportsAdoptedStyleSheets
465
+ ? AdoptedStyleSheetsStrategy
466
+ : StyleElementStrategy);
package/dist/esm/index.js CHANGED
@@ -27,4 +27,4 @@ export * from "./templating/node-observation.js";
27
27
  export * from "./components/fast-element.js";
28
28
  export * from "./components/fast-definitions.js";
29
29
  export * from "./components/attributes.js";
30
- export * from "./components/element-controller.js";
30
+ export { ElementController } from "./components/element-controller.js";
@@ -1,3 +1,4 @@
1
+ import "./polyfills.js";
1
2
  // ensure FAST global - duplicated in polyfills.ts and debug.ts
2
3
  const propConfig = {
3
4
  configurable: false,
@@ -28,58 +28,4 @@ if (!globalThis.trustedTypes) {
28
28
  createPolicy: (n, r) => r,
29
29
  };
30
30
  }
31
- // ensure FAST global - duplicated in platform.ts
32
- const propConfig = {
33
- configurable: false,
34
- enumerable: false,
35
- writable: false,
36
- };
37
- if (globalThis.FAST === void 0) {
38
- Reflect.defineProperty(globalThis, "FAST", Object.assign({ value: Object.create(null) }, propConfig));
39
- }
40
- const FAST = globalThis.FAST;
41
- if (FAST.getById === void 0) {
42
- const storage = Object.create(null);
43
- Reflect.defineProperty(FAST, "getById", Object.assign({ value(id, initialize) {
44
- let found = storage[id];
45
- if (found === void 0) {
46
- found = initialize ? (storage[id] = initialize()) : null;
47
- }
48
- return found;
49
- } }, propConfig));
50
- }
51
- // duplicated from DOM
52
- const supportsAdoptedStyleSheets = Array.isArray(document.adoptedStyleSheets) &&
53
- "replace" in CSSStyleSheet.prototype;
54
- function usableStyleTarget(target) {
55
- return target === document ? document.body : target;
56
- }
57
- let id = 0;
58
- const nextStyleId = () => `fast-${++id}`;
59
- export class StyleElementStrategy {
60
- constructor(styles) {
61
- this.styles = styles;
62
- this.styleClass = nextStyleId();
63
- }
64
- addStylesTo(target) {
65
- target = usableStyleTarget(target);
66
- const styles = this.styles;
67
- const styleClass = this.styleClass;
68
- for (let i = 0; i < styles.length; i++) {
69
- const element = document.createElement("style");
70
- element.innerHTML = styles[i];
71
- element.className = styleClass;
72
- target.append(element);
73
- }
74
- }
75
- removeStylesFrom(target) {
76
- const styles = target.querySelectorAll(`.${this.styleClass}`);
77
- target = usableStyleTarget(target);
78
- for (let i = 0, ii = styles.length; i < ii; ++i) {
79
- target.removeChild(styles[i]);
80
- }
81
- }
82
- }
83
- if (!supportsAdoptedStyleSheets) {
84
- FAST.getById(/* KernelServiceId.styleSheetStrategy */ 5, () => StyleElementStrategy);
85
- }
31
+ export {};
@@ -1,6 +1,3 @@
1
- import { FAST } from "../platform.js";
2
- import "../interfaces.js";
3
- const styleSheetCache = new Map();
4
1
  let DefaultStyleStrategy;
5
2
  function reduceStyles(styles) {
6
3
  return styles
@@ -91,33 +88,3 @@ export class ElementStyles {
91
88
  */
92
89
  ElementStyles.supportsAdoptedStyleSheets = Array.isArray(document.adoptedStyleSheets) &&
93
90
  "replace" in CSSStyleSheet.prototype;
94
- /**
95
- * https://wicg.github.io/construct-stylesheets/
96
- * https://developers.google.com/web/updates/2019/02/constructable-stylesheets
97
- *
98
- * @internal
99
- */
100
- export class AdoptedStyleSheetsStrategy {
101
- constructor(styles) {
102
- this.sheets = styles.map((x) => {
103
- if (x instanceof CSSStyleSheet) {
104
- return x;
105
- }
106
- let sheet = styleSheetCache.get(x);
107
- if (sheet === void 0) {
108
- sheet = new CSSStyleSheet();
109
- sheet.replaceSync(x);
110
- styleSheetCache.set(x, sheet);
111
- }
112
- return sheet;
113
- });
114
- }
115
- addStylesTo(target) {
116
- target.adoptedStyleSheets = [...target.adoptedStyleSheets, ...this.sheets];
117
- }
118
- removeStylesFrom(target) {
119
- const sheets = this.sheets;
120
- target.adoptedStyleSheets = target.adoptedStyleSheets.filter((x) => sheets.indexOf(x) === -1);
121
- }
122
- }
123
- ElementStyles.setDefaultStrategy(FAST.getById(5 /* KernelServiceId.styleSheetStrategy */, () => AdoptedStyleSheetsStrategy));
@@ -15154,7 +15154,25 @@
15154
15154
  "excerptTokens": [
15155
15155
  {
15156
15156
  "kind": "Content",
15157
- "text": "export interface StyleTarget "
15157
+ "text": "export interface StyleTarget extends "
15158
+ },
15159
+ {
15160
+ "kind": "Reference",
15161
+ "text": "Pick",
15162
+ "canonicalReference": "!Pick:type"
15163
+ },
15164
+ {
15165
+ "kind": "Content",
15166
+ "text": "<"
15167
+ },
15168
+ {
15169
+ "kind": "Reference",
15170
+ "text": "Node",
15171
+ "canonicalReference": "!Node:interface"
15172
+ },
15173
+ {
15174
+ "kind": "Content",
15175
+ "text": ", \"getRootNode\"> "
15158
15176
  }
15159
15177
  ],
15160
15178
  "releaseTag": "Public",
@@ -15369,7 +15387,12 @@
15369
15387
  "name": "removeChild"
15370
15388
  }
15371
15389
  ],
15372
- "extendsTokenRanges": []
15390
+ "extendsTokenRanges": [
15391
+ {
15392
+ "startIndex": 1,
15393
+ "endIndex": 4
15394
+ }
15395
+ ]
15373
15396
  },
15374
15397
  {
15375
15398
  "kind": "Interface",
@@ -32,8 +32,6 @@ export declare type AddBehavior = (behavior: HostBehavior<HTMLElement>) => void;
32
32
  */
33
33
  export declare type AddViewBehaviorFactory = (factory: ViewBehaviorFactory) => string;
34
34
 
35
- /* Excluded from this release type: AdoptedStyleSheetsStrategy */
36
-
37
35
  /**
38
36
  * An observer for arrays.
39
37
  * @public
@@ -2300,7 +2298,7 @@ export declare interface StyleStrategy {
2300
2298
  * A node that can be targeted by styles.
2301
2299
  * @public
2302
2300
  */
2303
- export declare interface StyleTarget {
2301
+ export declare interface StyleTarget extends Pick<Node, "getRootNode"> {
2304
2302
  /**
2305
2303
  * Stylesheets to be adopted by the node.
2306
2304
  */