iframe-reload-on-src-or-srcdoc-change.html 3.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <iframe id="test-iframe"></iframe>
  2. <script src="../include.js"></script>
  3. <script>
  4. asyncTest((done) => {
  5. window.addEventListener("message", (e) => {
  6. println(e.data);
  7. if (e.data === "DONE")
  8. done();
  9. });
  10. let testCount = 1;
  11. function createTest(asBlob, final = false) {
  12. let html = `
  13. &lt;script&gt;
  14. parent.postMessage(${testCount++}, "*");
  15. ${final ? "parent.postMessage('DONE', '*')" : ""}
  16. &lt;/script&gt;
  17. `;
  18. html = html.replaceAll("&lt;", "<").replaceAll("&gt;", ">");
  19. return asBlob
  20. ? URL.createObjectURL(new Blob([html], { type: "text/html" }))
  21. : html;
  22. }
  23. // https://html.spec.whatwg.org/multipage/iframe-embed-object.html#the-iframe-element:process-the-iframe-attributes-2
  24. // https://html.spec.whatwg.org/multipage/iframe-embed-object.html#the-iframe-element:process-the-iframe-attributes-3
  25. // Whenever an iframe element with a non-null content navigable has its srcdoc attribute set, changed, or removed,
  26. // the user agent must process the iframe attributes.
  27. // Similarly, whenever an iframe element with a non-null content navigable but with no srcdoc attribute specified
  28. // has its src attribute set, changed, or removed, the user agent must process the iframe attributes.
  29. const firstSrcdoc = createTest(false);
  30. const testIframe = document.getElementById("test-iframe");
  31. testIframe.addEventListener("load", () => {
  32. switch (testCount)
  33. {
  34. case 2:
  35. // Change srcdoc.
  36. testIframe.srcdoc = createTest(false);
  37. break;
  38. case 3:
  39. // Remove srcdoc.
  40. testIframe.removeAttribute("srcdoc");
  41. testCount++;
  42. // FIXME: Do this in test case 4. However, it does not work currently as the navigation to
  43. // about:blank following srcdoc does not fire a load event.
  44. // Set src.
  45. testIframe.src = createTest(true);
  46. break;
  47. case 5:
  48. // Change src.
  49. testIframe.src = createTest(true);
  50. break;
  51. case 6:
  52. // srcdoc takes priority.
  53. testIframe.srcdoc = createTest(false);
  54. break;
  55. case 7:
  56. // Changing src has no effect when srcdoc is specified.
  57. testIframe.src = createTest(true);
  58. setTimeout(() => {
  59. testIframe.srcdoc = createTest(false);
  60. }, 0)
  61. break;
  62. case 9:
  63. // Removing src has no effect when srcdoc is specified.
  64. testIframe.removeAttribute("src");
  65. setTimeout(() => {
  66. testIframe.srcdoc = createTest(false);
  67. }, 0);
  68. break;
  69. case 10:
  70. // Setting src has no effect when srcdoc is specified.
  71. testIframe.src = createTest(true);
  72. setTimeout(() => {
  73. testIframe.srcdoc = createTest(false);
  74. }, 0);
  75. break;
  76. case 12:
  77. // Changing any other attributes doesn't cause a reload.
  78. testIframe.setAttribute("data-hello", "world");
  79. setTimeout(() => {
  80. testIframe.srcdoc = createTest(false, true);
  81. }, 0);
  82. break;
  83. default:
  84. break;
  85. }
  86. });
  87. // Set srcdoc.
  88. testIframe.srcdoc = firstSrcdoc;
  89. });
  90. </script>