[next] Fix #1730 - Make worker an empty attribute only (#1732)

This commit is contained in:
Andrea Giammarchi
2023-09-20 16:44:00 +02:00
committed by GitHub
parent 23e1ab81b3
commit 8f3c36deea
6 changed files with 31 additions and 43 deletions

View File

@@ -1,6 +1,7 @@
node_modules/
rollup/
test/
tests/
src/stdlib/_pyscript
src/stdlib/pyscript.py
package-lock.json

View File

@@ -1,17 +1,17 @@
{
"name": "@pyscript/core",
"version": "0.1.22",
"version": "0.2.0",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "@pyscript/core",
"version": "0.1.22",
"version": "0.2.0",
"license": "APACHE-2.0",
"dependencies": {
"@ungap/with-resolvers": "^0.1.0",
"basic-devtools": "^0.1.6",
"polyscript": "^0.3.10"
"polyscript": "^0.4.2"
},
"devDependencies": {
"@rollup/plugin-node-resolve": "^15.2.1",
@@ -948,9 +948,9 @@
"integrity": "sha512-yyVAOFKTAElc7KdLt2+UKGExNYwYb/Y/WE9i+1ezCQsJE8gbKSjewfpRqK2nQgZ4d4hhAAGgDCOcIZVilqE5UA=="
},
"node_modules/polyscript": {
"version": "0.3.10",
"resolved": "https://registry.npmjs.org/polyscript/-/polyscript-0.3.10.tgz",
"integrity": "sha512-uO9vg0oIbyjo2n7B3/PtOFSWGjpW9WfIVf02aWSkQ6eUrWDmfrB13R03oLofsQoDDfaMZ+odKh3HqoDWqXd99Q==",
"version": "0.4.2",
"resolved": "https://registry.npmjs.org/polyscript/-/polyscript-0.4.2.tgz",
"integrity": "sha512-3mM5Y/DdpYND8/INAUmgF5VL4InVc04xADB+of129t8RjXi3eZK4xoGRPZdTYzW+wM56WNptnC8fC9Zt7jKLoA==",
"dependencies": {
"@ungap/structured-clone": "^1.2.0",
"@ungap/with-resolvers": "^0.1.0",

View File

@@ -1,6 +1,6 @@
{
"name": "@pyscript/core",
"version": "0.1.22",
"version": "0.2.0",
"type": "module",
"description": "PyScript",
"module": "./index.js",
@@ -33,7 +33,7 @@
"dependencies": {
"@ungap/with-resolvers": "^0.1.0",
"basic-devtools": "^0.1.6",
"polyscript": "^0.3.10"
"polyscript": "^0.4.2"
},
"devDependencies": {
"@rollup/plugin-node-resolve": "^15.2.1",

View File

@@ -1,7 +1,7 @@
/*! (c) PyScript Development Team */
import "@ungap/with-resolvers";
import { define, XWorker } from "polyscript";
import { INVALID_CONTENT, 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.
@@ -41,36 +41,6 @@ const after = () => {
delete document.currentScript;
};
/**
* Some content that might contain Python/JS comments only.
* @param {string} text some content to evaluate
* @returns {boolean}
*/
const hasCommentsOnly = (text) =>
!text
.replace(/\/\*[\s\S]*?\*\//g, "")
.replace(/^\s*(?:\/\/|#).*/gm, "")
.trim();
/**
*
* @param {Element} scriptOrPyScript the element with possible `src` or `worker` content
* @returns {boolean}
*/
const hasAmbiguousContent = (
io,
{ localName, textContent, attributes: { src, worker } },
) => {
// any `src` or a non-empty `worker` attribute + not just comments
if ((src || worker?.value) && !hasCommentsOnly(textContent)) {
io.stderr(
`(${ErrorCode.CONFLICTING_CODE}) a ${localName} tag has content shadowed by attributes`,
);
return true;
}
return false;
};
/**
* 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
@@ -158,6 +128,9 @@ const workerHooks = {
[...hooks.codeAfterRunWorkerAsync].map(dedent).join("\n"),
};
// possible early errors sent by polyscript
const errors = new Map();
// define the module as both `<script type="py">` and `<py-script>`
// but only if the config didn't throw an error
error ||
@@ -165,6 +138,9 @@ error ||
config,
env: `${TYPE}-script`,
interpreter: "pyodide",
onerror(error, element) {
errors.set(element, error);
},
...workerHooks,
onWorkerReady(_, xworker) {
assign(xworker.sync, sync);
@@ -202,8 +178,19 @@ error ||
for (const callback of hooks.onInterpreterReady)
callback(pyodide, element);
// now that all possible plugins are configured,
// bail out if polyscript encountered an error
if (errors.has(element)) {
let { message } = errors.get(element);
errors.delete(element);
const clone = message === INVALID_CONTENT;
message = `(${ErrorCode.CONFLICTING_CODE}) ${message} for `;
message += element.cloneNode(clone).outerHTML;
pyodide.io.stderr(message);
return;
}
if (isScript(element)) {
if (hasAmbiguousContent(pyodide.io, element)) return;
const {
attributes: { async: isAsync, target },
} = element;
@@ -254,7 +241,6 @@ class PyScriptElement extends HTMLElement {
if (!this.executed) {
this.executed = true;
const { io, run, runAsync } = await this._pyodide.promise;
if (hasAmbiguousContent(io, this)) return;
const runner = this.hasAttribute("async") ? runAsync : run;
this.srcCode = await fetchSource(this, io, !this.childElementCount);
this.replaceChildren();

View File

@@ -1,9 +1,9 @@
// PyScript Error Plugin
import { hooks } from "../core.js";
hooks.onBeforeRun.add(function override(pyScript) {
hooks.onInterpreterReady.add(function override(pyScript) {
// be sure this override happens only once
hooks.onBeforeRun.delete(override);
hooks.onInterpreterReady.delete(override);
// trap generic `stderr` to propagate to it regardless
const { stderr } = pyScript.io;

View File

@@ -18,5 +18,6 @@
print(4, 5, 6)
second()
</py-script>
<py-script src="main.py" worker="worker.py"></py-script>
</head>
</html>