@hotwired/turbo 7.0.0-rc.3 → 7.0.1

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.
@@ -1,5 +1,5 @@
1
1
  /*
2
- Turbo 7.0.0-rc.2
2
+ Turbo 7.0.0
3
3
  Copyright © 2021 Basecamp, LLC
4
4
  */
5
5
  (function (global, factory) {
@@ -39,10 +39,20 @@ Copyright © 2021 Basecamp, LLC
39
39
  }
40
40
  }
41
41
  (function () {
42
- if ("SubmitEvent" in window)
42
+ if ("submitter" in Event.prototype)
43
43
  return;
44
+ let prototype;
45
+ if ("SubmitEvent" in window && /Apple Computer/.test(navigator.vendor)) {
46
+ prototype = window.SubmitEvent.prototype;
47
+ }
48
+ else if ("SubmitEvent" in window) {
49
+ return;
50
+ }
51
+ else {
52
+ prototype = window.Event.prototype;
53
+ }
44
54
  addEventListener("click", clickCaptured, true);
45
- Object.defineProperty(Event.prototype, "submitter", {
55
+ Object.defineProperty(prototype, "submitter", {
46
56
  get() {
47
57
  if (this.type == "submit" && this.target instanceof HTMLFormElement) {
48
58
  return submittersByForm.get(this.target);
@@ -228,11 +238,11 @@ Copyright © 2021 Basecamp, LLC
228
238
  return this.header("Content-Type");
229
239
  }
230
240
  get responseText() {
231
- return this.response.text();
241
+ return this.response.clone().text();
232
242
  }
233
243
  get responseHTML() {
234
244
  if (this.isHTML) {
235
- return this.response.text();
245
+ return this.response.clone().text();
236
246
  }
237
247
  else {
238
248
  return Promise.resolve(undefined);
@@ -245,7 +255,12 @@ Copyright © 2021 Basecamp, LLC
245
255
 
246
256
  function dispatch(eventName, { target, cancelable, detail } = {}) {
247
257
  const event = new CustomEvent(eventName, { cancelable, bubbles: true, detail });
248
- void (target || document.documentElement).dispatchEvent(event);
258
+ if (target && target.isConnected) {
259
+ target.dispatchEvent(event);
260
+ }
261
+ else {
262
+ document.documentElement.dispatchEvent(event);
263
+ }
249
264
  return event;
250
265
  }
251
266
  function nextAnimationFrame() {
@@ -307,7 +322,7 @@ Copyright © 2021 Basecamp, LLC
307
322
  }
308
323
  }
309
324
  class FetchRequest {
310
- constructor(delegate, method, location, body = new URLSearchParams) {
325
+ constructor(delegate, method, location, body = new URLSearchParams, target = null) {
311
326
  this.abortController = new AbortController;
312
327
  this.resolveRequestPromise = (value) => { };
313
328
  this.delegate = delegate;
@@ -320,6 +335,7 @@ Copyright © 2021 Basecamp, LLC
320
335
  this.body = body;
321
336
  this.url = location;
322
337
  }
338
+ this.target = target;
323
339
  }
324
340
  get location() {
325
341
  return this.url;
@@ -355,7 +371,7 @@ Copyright © 2021 Basecamp, LLC
355
371
  }
356
372
  async receive(response) {
357
373
  const fetchResponse = new FetchResponse(response);
358
- const event = dispatch("turbo:before-fetch-response", { cancelable: true, detail: { fetchResponse } });
374
+ const event = dispatch("turbo:before-fetch-response", { cancelable: true, detail: { fetchResponse }, target: this.target });
359
375
  if (event.defaultPrevented) {
360
376
  this.delegate.requestPreventedHandlingResponse(this, fetchResponse);
361
377
  }
@@ -392,7 +408,15 @@ Copyright © 2021 Basecamp, LLC
392
408
  }
393
409
  async allowRequestToBeIntercepted(fetchOptions) {
394
410
  const requestInterception = new Promise(resolve => this.resolveRequestPromise = resolve);
395
- const event = dispatch("turbo:before-fetch-request", { cancelable: true, detail: { fetchOptions, url: this.url.href, resume: this.resolveRequestPromise } });
411
+ const event = dispatch("turbo:before-fetch-request", {
412
+ cancelable: true,
413
+ detail: {
414
+ fetchOptions,
415
+ url: this.url.href,
416
+ resume: this.resolveRequestPromise
417
+ },
418
+ target: this.target
419
+ });
396
420
  if (event.defaultPrevented)
397
421
  await requestInterception;
398
422
  }
@@ -505,7 +529,7 @@ Copyright © 2021 Basecamp, LLC
505
529
  this.formElement = formElement;
506
530
  this.submitter = submitter;
507
531
  this.formData = buildFormData(formElement, submitter);
508
- this.fetchRequest = new FetchRequest(this, this.method, this.location, this.body);
532
+ this.fetchRequest = new FetchRequest(this, this.method, this.location, this.body, this.formElement);
509
533
  this.mustRedirect = mustRedirect;
510
534
  }
511
535
  get method() {
@@ -672,8 +696,8 @@ Copyright © 2021 Basecamp, LLC
672
696
  class FormInterceptor {
673
697
  constructor(delegate, element) {
674
698
  this.submitBubbled = ((event) => {
675
- if (event.target instanceof HTMLFormElement) {
676
- const form = event.target;
699
+ const form = event.target;
700
+ if (form instanceof HTMLFormElement && form.closest("turbo-frame, html") == this.element) {
677
701
  const submitter = event.submitter || undefined;
678
702
  if (this.delegate.shouldInterceptFormSubmission(form, submitter)) {
679
703
  event.preventDefault();
@@ -1118,6 +1142,7 @@ Copyright © 2021 Basecamp, LLC
1118
1142
  super(...arguments);
1119
1143
  this.detailsByOuterHTML = this.children
1120
1144
  .filter((element) => !elementIsNoscript(element))
1145
+ .map((element) => elementWithoutNonce(element))
1121
1146
  .reduce((result, element) => {
1122
1147
  const { outerHTML } = element;
1123
1148
  const details = outerHTML in result
@@ -1202,6 +1227,12 @@ Copyright © 2021 Basecamp, LLC
1202
1227
  const tagName = element.tagName.toLowerCase();
1203
1228
  return tagName == "meta" && element.getAttribute("name") == name;
1204
1229
  }
1230
+ function elementWithoutNonce(element) {
1231
+ if (element.hasAttribute("nonce")) {
1232
+ element.setAttribute("nonce", "");
1233
+ }
1234
+ return element;
1235
+ }
1205
1236
 
1206
1237
  class PageSnapshot extends Snapshot {
1207
1238
  constructor(element, headSnapshot) {
@@ -1745,18 +1776,18 @@ Copyright © 2021 Basecamp, LLC
1745
1776
  return this.shouldRedirect(element, submitter);
1746
1777
  }
1747
1778
  formSubmissionIntercepted(element, submitter) {
1748
- const frame = this.findFrameElement(element);
1779
+ const frame = this.findFrameElement(element, submitter);
1749
1780
  if (frame) {
1750
1781
  frame.removeAttribute("reloadable");
1751
1782
  frame.delegate.formSubmissionIntercepted(element, submitter);
1752
1783
  }
1753
1784
  }
1754
1785
  shouldRedirect(element, submitter) {
1755
- const frame = this.findFrameElement(element);
1786
+ const frame = this.findFrameElement(element, submitter);
1756
1787
  return frame ? frame != element.closest("turbo-frame") : false;
1757
1788
  }
1758
- findFrameElement(element) {
1759
- const id = element.getAttribute("data-turbo-frame");
1789
+ findFrameElement(element, submitter) {
1790
+ const id = (submitter === null || submitter === void 0 ? void 0 : submitter.getAttribute("data-turbo-frame")) || element.getAttribute("data-turbo-frame");
1760
1791
  if (id && id != "_top") {
1761
1792
  const frame = this.element.querySelector(`#${id}:not([disabled])`);
1762
1793
  if (frame instanceof FrameElement) {
@@ -2468,12 +2499,14 @@ Copyright © 2021 Basecamp, LLC
2468
2499
  this.convertLinkWithMethodClickToFormSubmission(link) || this.visit(location.href, { action });
2469
2500
  }
2470
2501
  convertLinkWithMethodClickToFormSubmission(link) {
2502
+ var _a;
2471
2503
  const linkMethod = link.getAttribute("data-turbo-method");
2472
2504
  if (linkMethod) {
2473
2505
  const form = document.createElement("form");
2474
2506
  form.method = linkMethod;
2475
2507
  form.action = link.getAttribute("href") || "undefined";
2476
- document.body.appendChild(form);
2508
+ form.hidden = true;
2509
+ (_a = link.parentNode) === null || _a === void 0 ? void 0 : _a.insertBefore(form, link);
2477
2510
  return dispatch("submit", { cancelable: true, target: form });
2478
2511
  }
2479
2512
  else {
@@ -2503,7 +2536,7 @@ Copyright © 2021 Basecamp, LLC
2503
2536
  this.notifyApplicationAfterVisitingSamePageLocation(oldURL, newURL);
2504
2537
  }
2505
2538
  willSubmitForm(form, submitter) {
2506
- return this.elementDriveEnabled(form) && this.elementDriveEnabled(submitter);
2539
+ return this.elementDriveEnabled(form) && (!submitter || this.elementDriveEnabled(submitter));
2507
2540
  }
2508
2541
  formSubmitted(form, submitter) {
2509
2542
  this.navigator.submitForm(form, submitter);
@@ -2624,7 +2657,7 @@ Copyright © 2021 Basecamp, LLC
2624
2657
  };
2625
2658
 
2626
2659
  const session = new Session;
2627
- const { navigator } = session;
2660
+ const { navigator: navigator$1 } = session;
2628
2661
  function start() {
2629
2662
  session.start();
2630
2663
  }
@@ -2652,7 +2685,7 @@ Copyright © 2021 Basecamp, LLC
2652
2685
 
2653
2686
  var Turbo = /*#__PURE__*/Object.freeze({
2654
2687
  __proto__: null,
2655
- navigator: navigator,
2688
+ navigator: navigator$1,
2656
2689
  session: session,
2657
2690
  PageRenderer: PageRenderer,
2658
2691
  PageSnapshot: PageSnapshot,
@@ -2782,7 +2815,7 @@ Copyright © 2021 Basecamp, LLC
2782
2815
  this.reloadable = false;
2783
2816
  this.formSubmission = new FormSubmission(this, element, submitter);
2784
2817
  if (this.formSubmission.fetchRequest.isIdempotent) {
2785
- this.navigateFrame(element, this.formSubmission.fetchRequest.url.href);
2818
+ this.navigateFrame(element, this.formSubmission.fetchRequest.url.href, submitter);
2786
2819
  }
2787
2820
  else {
2788
2821
  const { fetchRequest } = this.formSubmission;
@@ -2819,7 +2852,7 @@ Copyright © 2021 Basecamp, LLC
2819
2852
  frame.setAttribute("busy", "");
2820
2853
  }
2821
2854
  formSubmissionSucceededWithResponse(formSubmission, response) {
2822
- const frame = this.findFrameElement(formSubmission.formElement);
2855
+ const frame = this.findFrameElement(formSubmission.formElement, formSubmission.submitter);
2823
2856
  frame.delegate.loadResponse(response);
2824
2857
  }
2825
2858
  formSubmissionFailedWithResponse(formSubmission, fetchResponse) {
@@ -2840,7 +2873,7 @@ Copyright © 2021 Basecamp, LLC
2840
2873
  viewInvalidated() {
2841
2874
  }
2842
2875
  async visit(url) {
2843
- const request = new FetchRequest(this, FetchMethod.get, expandURL(url));
2876
+ const request = new FetchRequest(this, FetchMethod.get, expandURL(url), undefined, this.element);
2844
2877
  return new Promise(resolve => {
2845
2878
  this.resolveVisitPromise = () => {
2846
2879
  this.resolveVisitPromise = () => { };
@@ -2849,13 +2882,14 @@ Copyright © 2021 Basecamp, LLC
2849
2882
  request.perform();
2850
2883
  });
2851
2884
  }
2852
- navigateFrame(element, url) {
2853
- const frame = this.findFrameElement(element);
2885
+ navigateFrame(element, url, submitter) {
2886
+ const frame = this.findFrameElement(element, submitter);
2887
+ frame.setAttribute("reloadable", "");
2854
2888
  frame.src = url;
2855
2889
  }
2856
- findFrameElement(element) {
2890
+ findFrameElement(element, submitter) {
2857
2891
  var _a;
2858
- const id = element.getAttribute("data-turbo-frame") || this.element.getAttribute("target");
2892
+ const id = (submitter === null || submitter === void 0 ? void 0 : submitter.getAttribute("data-turbo-frame")) || element.getAttribute("data-turbo-frame") || this.element.getAttribute("target");
2859
2893
  return (_a = getFrameElementById(id)) !== null && _a !== void 0 ? _a : this.element;
2860
2894
  }
2861
2895
  async extractForeignFrameElement(container) {
@@ -2877,7 +2911,7 @@ Copyright © 2021 Basecamp, LLC
2877
2911
  return new FrameElement();
2878
2912
  }
2879
2913
  shouldInterceptNavigation(element, submitter) {
2880
- const id = element.getAttribute("data-turbo-frame") || this.element.getAttribute("target");
2914
+ const id = (submitter === null || submitter === void 0 ? void 0 : submitter.getAttribute("data-turbo-frame")) || element.getAttribute("data-turbo-frame") || this.element.getAttribute("target");
2881
2915
  if (!this.enabled || id == "_top") {
2882
2916
  return false;
2883
2917
  }
@@ -3129,7 +3163,7 @@ Copyright © 2021 Basecamp, LLC
3129
3163
  exports.clearCache = clearCache;
3130
3164
  exports.connectStreamSource = connectStreamSource;
3131
3165
  exports.disconnectStreamSource = disconnectStreamSource;
3132
- exports.navigator = navigator;
3166
+ exports.navigator = navigator$1;
3133
3167
  exports.registerAdapter = registerAdapter;
3134
3168
  exports.renderStreamMessage = renderStreamMessage;
3135
3169
  exports.session = session;