Skip to content

Commit 14fca94

Browse files
committed
Disable form controls on xx-events until ready
1 parent f208e25 commit 14fca94

File tree

9 files changed

+58
-6
lines changed

9 files changed

+58
-6
lines changed

core.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/core.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/core.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

esm/listeners.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,20 @@ export const addAllListeners = (root) => {
5151
.join(' or ')}]`,
5252
root,
5353
)) {
54-
name = name.slice(name.lastIndexOf('-') + 1);
55-
if (name !== 'env') el.addEventListener(name, listener);
54+
const i = name.lastIndexOf('-');
55+
const type = name.slice(i + 1);
56+
if (type !== 'env') {
57+
el.addEventListener(type, listener);
58+
// automatically disable form controls that are not disabled already
59+
if ('disabled' in el && !el.disabled) {
60+
el.disabled = true;
61+
// set these to enable once the interpreter is known (registered + loaded)
62+
queueMicrotask(async () => {
63+
await awaitInterpreter(name.slice(0, i));
64+
el.disabled = false;
65+
});
66+
}
67+
}
5668
}
5769
};
5870
/* c8 ignore stop */

test/custom.html

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@
2323
'not the same config'
2424
);
2525
wrap.run(element.textContent);
26+
const button = document.createElement('button');
27+
button.setAttribute('mpy-click', 'all_good');
28+
button.textContent = 'runtime';
29+
document.body.append(button);
2630
}
2731
});
2832
</script>
@@ -33,6 +37,9 @@
3337
import a
3438
from b import x
3539
print(x)
40+
41+
def all_good(event):
42+
print('OK', event.type)
3643
</script>
3744
<script type="mpy" worker>
3845
print("worker")
@@ -46,5 +53,6 @@
4653
except:
4754
print("all good")
4855
</script>
56+
<button mpy-click="all_good">click</button>
4957
</body>
5058
</html>

test/integration.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@
55
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
66
<title>polyscript integration tests</title>
77
</head>
8-
<body><ul><li><strong>micropython</strong><ul><li><a href="/test/integration/interpreter/micropython/bootstrap.html">bootstrap</a></li><li><a href="/test/integration/interpreter/micropython/config-object.html">config-object</a></li><li><a href="/test/integration/interpreter/micropython/fetch.html">fetch</a></li><li><a href="/test/integration/interpreter/micropython/no-type.html">no-type</a></li><li><a href="/test/integration/interpreter/micropython/worker-attribute.html">worker-attribute</a></li><li><a href="/test/integration/interpreter/micropython/worker-bad.html">worker-bad</a></li><li><a href="/test/integration/interpreter/micropython/worker-empty-attribute.html">worker-empty-attribute</a></li><li><a href="/test/integration/interpreter/micropython/worker-error.html">worker-error</a></li><li><a href="/test/integration/interpreter/micropython/worker-lua.html">worker-lua</a></li><li><a href="/test/integration/interpreter/micropython/worker-tag.html">worker-tag</a></li><li><a href="/test/integration/interpreter/micropython/worker-window.html">worker-window</a></li><li><a href="/test/integration/interpreter/micropython/worker.html">worker</a></li></ul><li><strong>pyodide</strong><ul><li><a href="/test/integration/interpreter/pyodide/bootstrap.html">bootstrap</a></li><li><a href="/test/integration/interpreter/pyodide/fetch.html">fetch</a></li><li><a href="/test/integration/interpreter/pyodide/sync.html">sync</a></li><li><a href="/test/integration/interpreter/pyodide/worker-error.html">worker-error</a></li><li><a href="/test/integration/interpreter/pyodide/worker-transform.html">worker-transform</a></li><li><a href="/test/integration/interpreter/pyodide/worker.html">worker</a></li></ul><li><strong>ruby-wasm-wasi</strong><ul><li><a href="/test/integration/interpreter/ruby-wasm-wasi/bootstrap.html">bootstrap</a></li></ul><li><strong>wasmoon</strong><ul><li><a href="/test/integration/interpreter/wasmoon/bootstrap.html">bootstrap</a></li><li><a href="/test/integration/interpreter/wasmoon/worker.html">worker</a></li></ul></ul></body>
8+
<body><ul><li><strong>micropython</strong><ul><li><a href="/test/integration/interpreter/micropython/bootstrap.html">bootstrap</a></li><li><a href="/test/integration/interpreter/micropython/config-object.html">config-object</a></li><li><a href="/test/integration/interpreter/micropython/fetch.html">fetch</a></li><li><a href="/test/integration/interpreter/micropython/no-type.html">no-type</a></li><li><a href="/test/integration/interpreter/micropython/worker-attribute.html">worker-attribute</a></li><li><a href="/test/integration/interpreter/micropython/worker-bad.html">worker-bad</a></li><li><a href="/test/integration/interpreter/micropython/worker-empty-attribute.html">worker-empty-attribute</a></li><li><a href="/test/integration/interpreter/micropython/worker-error.html">worker-error</a></li><li><a href="/test/integration/interpreter/micropython/worker-lua.html">worker-lua</a></li><li><a href="/test/integration/interpreter/micropython/worker-tag.html">worker-tag</a></li><li><a href="/test/integration/interpreter/micropython/worker-window.html">worker-window</a></li><li><a href="/test/integration/interpreter/micropython/worker.html">worker</a></li></ul><li><strong>pyodide</strong><ul><li><a href="/test/integration/interpreter/pyodide/bootstrap.html">bootstrap</a></li><li><a href="/test/integration/interpreter/pyodide/button.html">button</a></li><li><a href="/test/integration/interpreter/pyodide/fetch.html">fetch</a></li><li><a href="/test/integration/interpreter/pyodide/sync.html">sync</a></li><li><a href="/test/integration/interpreter/pyodide/worker-error.html">worker-error</a></li><li><a href="/test/integration/interpreter/pyodide/worker-transform.html">worker-transform</a></li><li><a href="/test/integration/interpreter/pyodide/worker.html">worker</a></li></ul><li><strong>ruby-wasm-wasi</strong><ul><li><a href="/test/integration/interpreter/ruby-wasm-wasi/bootstrap.html">bootstrap</a></li></ul><li><strong>wasmoon</strong><ul><li><a href="/test/integration/interpreter/wasmoon/bootstrap.html">bootstrap</a></li><li><a href="/test/integration/interpreter/wasmoon/worker.html">worker</a></li></ul></ul></body>
99
</html>

test/integration/_shared.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,16 @@ exports.python = {
4343
await expect(logs[0]).toBe('hello from A');
4444
},
4545

46+
disabledUntilReady: ({ expect }, baseURL) => async ({ page }) => {
47+
await page.goto(`${baseURL}/button.html`);
48+
await page.waitForSelector('button[disabled]');
49+
await page.waitForSelector('button:not([disabled])');
50+
const button = await page.getByRole('button');
51+
await button.click();
52+
const result = await page.evaluate(() => document.querySelector('button').textContent);
53+
await expect(result).toContain('clicked');
54+
},
55+
4656
error: ({ expect }, baseURL) => async ({ page }) => {
4757
// Test that when the worker throws an error, the page does not crash and the
4858
// error is reported to the console.
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
6+
<script type="module">
7+
import { init } from '../utils.js';
8+
init('pyodide');
9+
</script>
10+
</head>
11+
<body>
12+
<script type="pyodide">
13+
import js
14+
15+
def clicked(event):
16+
event.target.textContent = "clicked"
17+
</script>
18+
<button pyodide-click="clicked">click me</button>
19+
</body>
20+
</html>

test/integration/pyodide.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,6 @@ module.exports = (playwright, baseURL) => {
2929
test('Pyodide Worker error', python.error(playwright, baseURL));
3030

3131
test('Pyodide transform', python.error(playwright, baseURL));
32+
33+
test('Pyodide events ready', python.disabledUntilReady(playwright, baseURL));
3234
};

0 commit comments

Comments
 (0)