@stencil/mock-doc 5.0.0-beta.34 → 5.0.0-beta.36

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/index.d.mts CHANGED
@@ -448,6 +448,18 @@ declare function cloneDocument(srcDoc: Document): Document;
448
448
  * @param win the mock window instance to update
449
449
  */
450
450
  declare function constrainTimeouts(win: MockWindow): void;
451
+ /**
452
+ * Reset a window so the same object can be used for another render, then parse `html` into its
453
+ * document.
454
+ *
455
+ * Code that captured the window's sub-objects (e.g. an SSR platform holding `win.location`,
456
+ * `win.console` or `win.document`) keeps working: those objects are kept and reset in place,
457
+ * rather than replaced. Everything else on the window - including anything a previous render
458
+ * added - is removed.
459
+ * @param win the window to reset
460
+ * @param html the HTML for the next render
461
+ */
462
+ declare function resetWindowForReuse(win: MockWindow, html: string): void;
451
463
  //#endregion
452
464
  //#region src/event.d.ts
453
465
  declare class MockEvent {
@@ -1041,4 +1053,4 @@ declare class MockResponse {
1041
1053
  clone(): MockResponse;
1042
1054
  }
1043
1055
  //#endregion
1044
- export { MockAttr, MockAttributeMap, MockComment, MockCustomEvent, MockDocument, MockElement, MockHTMLElement, MockHeaders, MockKeyboardEvent, MockMouseEvent, MockNode, MockRequest, type MockRequestInfo, type MockRequestInit, MockResponse, type MockResponseInit, MockTextNode, MockWindow, NODE_TYPES, type SerializeNodeToHtmlOptions, cloneAttributes, cloneDocument, cloneWindow, constrainTimeouts, createDocument, createFragment, parseHtmlToDocument, parseHtmlToFragment, patchWindow, resetDocument, serializeNodeToHtml, setupGlobal, teardownGlobal };
1056
+ export { MockAttr, MockAttributeMap, MockComment, MockCustomEvent, MockDocument, MockElement, MockHTMLElement, MockHeaders, MockKeyboardEvent, MockMouseEvent, MockNode, MockRequest, type MockRequestInfo, type MockRequestInit, MockResponse, type MockResponseInit, MockTextNode, MockWindow, NODE_TYPES, type SerializeNodeToHtmlOptions, cloneAttributes, cloneDocument, cloneWindow, constrainTimeouts, createDocument, createFragment, parseHtmlToDocument, parseHtmlToFragment, patchWindow, resetDocument, resetWindowForReuse, serializeNodeToHtml, setupGlobal, teardownGlobal };
package/dist/index.mjs CHANGED
@@ -21,9 +21,6 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
21
21
  value: mod,
22
22
  enumerable: true
23
23
  }) : target, mod));
24
- //#endregion
25
- //#region src/constants.ts
26
- const HYDRATE_ID = "s-id";
27
24
  const XML_NS = "http://www.w3.org/XML/1998/namespace";
28
25
  const XMLNS_NS = "http://www.w3.org/2000/xmlns/";
29
26
  /**
@@ -1041,19 +1038,7 @@ function* streamToHtml(node, opts, output) {
1041
1038
  const isWithinWhitespaceSensitiveNode = opts.newLines || (opts.indentSpaces ?? 0) > 0 ? isWithinWhitespaceSensitive(node) : false;
1042
1039
  if (!isWithinWhitespaceSensitiveNode && (opts.indentSpaces ?? 0) > 0 && ignoreTag === false) output.indent = output.indent + (opts.indentSpaces ?? 0);
1043
1040
  for (let i = 0; i < childNodeLength; i++) {
1044
- /**
1045
- * In cases where a user would pass in a declarative shadow dom of a
1046
- * Stencil component, we want to skip over the template tag as we
1047
- * will be parsing the shadow root of the component again.
1048
- *
1049
- * We know it is a hydrated Stencil component by checking if the `HYDRATE_ID`
1050
- * is set on the node.
1051
- */
1052
- const sId = node.attributes.getNamedItem(HYDRATE_ID);
1053
- if (childNodes[i].nodeName.toLowerCase() === "template" && sId) {
1054
- yield `\n${" ".repeat(output.indent)}<!--r.${sId.value}-->`;
1055
- continue;
1056
- }
1041
+ if (childNodes[i].nodeName.toLowerCase() === "template" && node.attributes.getNamedItem("s-id")) continue;
1057
1042
  yield* streamToHtml(childNodes[i], opts, output);
1058
1043
  }
1059
1044
  if (ignoreTag === false) {
@@ -5269,6 +5254,77 @@ function constrainTimeouts(win) {
5269
5254
  win.__allowInterval = false;
5270
5255
  win.__maxTimeout = 0;
5271
5256
  }
5257
+ /**
5258
+ * Reset a window so the same object can be used for another render, then parse `html` into its
5259
+ * document.
5260
+ *
5261
+ * Code that captured the window's sub-objects (e.g. an SSR platform holding `win.location`,
5262
+ * `win.console` or `win.document`) keeps working: those objects are kept and reset in place,
5263
+ * rather than replaced. Everything else on the window - including anything a previous render
5264
+ * added - is removed.
5265
+ * @param win the window to reset
5266
+ * @param html the HTML for the next render
5267
+ */
5268
+ function resetWindowForReuse(win, html) {
5269
+ const retained = {
5270
+ console: win.console,
5271
+ document: win.document,
5272
+ eventCounts: win.performance.eventCounts,
5273
+ history: win.history,
5274
+ localStorage: win.localStorage,
5275
+ location: win.location,
5276
+ navigator: win.navigator,
5277
+ orientation: win.screen.orientation,
5278
+ performance: win.performance,
5279
+ screen: win.screen,
5280
+ sessionStorage: win.sessionStorage
5281
+ };
5282
+ if (win.__timeouts) win.__timeouts.forEach((timeoutId) => {
5283
+ nativeClearInterval(timeoutId);
5284
+ nativeClearTimeout(timeoutId);
5285
+ });
5286
+ resetObject(retained.console, createConsole());
5287
+ resetObject(retained.history, new MockHistory());
5288
+ resetObject(retained.location, new MockLocation());
5289
+ resetObject(retained.navigator, new MockNavigator());
5290
+ retained.localStorage.clear();
5291
+ retained.sessionStorage.clear();
5292
+ retained.eventCounts.clear();
5293
+ resetObject(retained.performance, {
5294
+ eventCounts: retained.eventCounts,
5295
+ interactionCount: 0,
5296
+ timeOrigin: Date.now()
5297
+ });
5298
+ for (const key of Reflect.ownKeys(win)) delete win[key];
5299
+ resetWindowDefaults(win);
5300
+ resetWindowDimensions(win);
5301
+ const defaultScreen = win.screen;
5302
+ resetObject(retained.orientation, defaultScreen.orientation);
5303
+ resetObject(retained.screen, {
5304
+ ...defaultScreen,
5305
+ orientation: retained.orientation
5306
+ });
5307
+ win.console = retained.console;
5308
+ win.customElements = null;
5309
+ win.document = retained.document;
5310
+ win.history = retained.history;
5311
+ win.localStorage = retained.localStorage;
5312
+ win.location = retained.location;
5313
+ win.navigator = retained.navigator;
5314
+ win.performance = retained.performance;
5315
+ win.screen = retained.screen;
5316
+ win.sessionStorage = retained.sessionStorage;
5317
+ resetDocumentForReuse(retained.document, win, html);
5318
+ }
5319
+ /**
5320
+ * Replace every own property of `target` with those of `defaults`, keeping the object itself.
5321
+ * @param target the object to reset in place
5322
+ * @param defaults the properties it should have afterwards
5323
+ */
5324
+ function resetObject(target, defaults) {
5325
+ for (const key of Reflect.ownKeys(target)) delete target[key];
5326
+ Object.assign(target, defaults);
5327
+ }
5272
5328
  function resetWindow(win) {
5273
5329
  if (win != null) {
5274
5330
  if (win.__timeouts) {
@@ -5577,6 +5633,37 @@ function resetDocument(doc) {
5577
5633
  } catch {}
5578
5634
  }
5579
5635
  }
5636
+ /**
5637
+ * Reset a document so the same object can be used for another render, then parse `html` into
5638
+ * it. The document object itself is kept (code holding a reference to it stays valid), but its
5639
+ * whole element tree is replaced with freshly parsed elements - so caches keyed on elements, like
5640
+ * the runtime's applied-styles cache keyed on `<head>`, start empty.
5641
+ * @param doc the document to reset
5642
+ * @param win the window that owns the document
5643
+ * @param html the HTML to parse into the document
5644
+ */
5645
+ function resetDocumentForReuse(doc, win, html) {
5646
+ resetDocument(doc);
5647
+ for (const key of Reflect.ownKeys(doc)) if (typeof key !== "string" || !REUSABLE_DOC_KEY_KEEPERS.has(key)) delete doc[key];
5648
+ doc.defaultView = win;
5649
+ doc.documentElement = parseDocumentUtil(doc, html).documentElement;
5650
+ }
5651
+ /** Structural document properties kept by {@link resetDocumentForReuse}; everything else is per-render state. */
5652
+ const REUSABLE_DOC_KEY_KEEPERS = /* @__PURE__ */ new Set([
5653
+ "_nodeValue",
5654
+ "nodeName",
5655
+ "nodeType",
5656
+ "ownerDocument",
5657
+ "parentNode",
5658
+ "_childNodes",
5659
+ "__namespaceURI",
5660
+ "__localName",
5661
+ "defaultView",
5662
+ "cookie",
5663
+ "referrer",
5664
+ "compatMode",
5665
+ "contentType"
5666
+ ]);
5580
5667
  const DOC_KEY_KEEPERS = /* @__PURE__ */ new Set([
5581
5668
  "nodeName",
5582
5669
  "nodeType",
@@ -5615,4 +5702,4 @@ function setOwnerDocument(elm, ownerDocument) {
5615
5702
  }
5616
5703
  }
5617
5704
  //#endregion
5618
- export { MockAttr, MockAttributeMap, MockComment, MockCustomEvent, MockDocument, MockElement, MockHTMLElement, MockHeaders, MockKeyboardEvent, MockMouseEvent, MockNode, MockRequest, MockResponse, MockTextNode, MockWindow, NODE_TYPES, cloneAttributes, cloneDocument, cloneWindow, constrainTimeouts, createDocument, createFragment, parseHtmlToDocument, parseHtmlToFragment, patchWindow, resetDocument, serializeNodeToHtml, setupGlobal, teardownGlobal };
5705
+ export { MockAttr, MockAttributeMap, MockComment, MockCustomEvent, MockDocument, MockElement, MockHTMLElement, MockHeaders, MockKeyboardEvent, MockMouseEvent, MockNode, MockRequest, MockResponse, MockTextNode, MockWindow, NODE_TYPES, cloneAttributes, cloneDocument, cloneWindow, constrainTimeouts, createDocument, createFragment, parseHtmlToDocument, parseHtmlToFragment, patchWindow, resetDocument, resetWindowForReuse, serializeNodeToHtml, setupGlobal, teardownGlobal };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@stencil/mock-doc",
3
- "version": "5.0.0-beta.34",
3
+ "version": "5.0.0-beta.36",
4
4
  "description": "A minimal mock DOM implementation for SSR and testing",
5
5
  "keywords": [
6
6
  "dom",