/*! (c) PyScript Development Team */ import "@ungap/with-resolvers"; import { define, XWorker } from "polyscript"; // TODO: this is not strictly polyscript related but handy ... not sure // we should factor this utility out a part but this works anyway. import { queryTarget } from "../node_modules/polyscript/esm/script-handler.js"; import { dedent, dispatch } from "../node_modules/polyscript/esm/utils.js"; import { Hook } from "../node_modules/polyscript/esm/worker/hooks.js"; import sync from "./sync.js"; import stdlib from "./stdlib.js"; import { config, plugins } from "./config.js"; import { robustFetch as fetch, getText } from "./fetch.js"; const { assign, defineProperty, entries } = Object; const TYPE = "py"; // allows lazy element features on code evaluation let currentElement; // create a unique identifier when/if needed let id = 0; const getID = (prefix = TYPE) => `${prefix}-${id++}`; // generic helper to disambiguate between custom element and script const isScript = ({ tagName }) => tagName === "SCRIPT"; // helper for all script[type="py"] out there const before = (script) => { defineProperty(document, "currentScript", { configurable: true, get: () => script, }); }; const after = () => { delete document.currentScript; }; /** * Given a generic DOM Element, tries to fetch the 'src' attribute, if present. * It either throws an error if the 'src' can't be fetched or it returns a fallback * content as source. */ const fetchSource = async (tag, io, asText) => { if (tag.hasAttribute("src")) { try { return await fetch(tag.getAttribute("src")).then(getText); } catch (error) { io.stderr(error); } } if (asText) return dedent(tag.textContent); console.warn( `Deprecated: use