document.currentScript.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536
  1. describe("currentScript", () => {
  2. loadLocalPage("/res/html/misc/blank.html");
  3. beforeInitialPageLoad(page => {
  4. expect(page.document.currentScript).toBeNull();
  5. });
  6. afterInitialPageLoad(page => {
  7. test("reset to null even if currentScript is adopted into another document", () => {
  8. const script = page.document.createElement("script");
  9. script.id = "test";
  10. script.innerText = `
  11. const newDocument = globalThis.pageObject.document.implementation.createHTMLDocument();
  12. const thisScript = globalThis.pageObject.document.getElementById("test");
  13. // currentScript should stay the same even across adoption.
  14. expect(globalThis.pageObject.document.currentScript).toBe(thisScript);
  15. newDocument.adoptNode(thisScript);
  16. expect(globalThis.pageObject.document.currentScript).toBe(thisScript);
  17. `;
  18. // currentScript should be null before and after running the script on insertion.
  19. expect(page.document.currentScript).toBeNull();
  20. expect(script.ownerDocument).toBe(page.document);
  21. globalThis.pageObject = page;
  22. page.document.body.appendChild(script);
  23. globalThis.pageObject = undefined;
  24. expect(page.document.currentScript).toBeNull();
  25. expect(script.ownerDocument).not.toBe(page.document);
  26. });
  27. });
  28. waitForPageToLoad();
  29. });