diff --git a/core/src/config.js b/core/src/config.js index 6144e946..d0c2a7e9 100644 --- a/core/src/config.js +++ b/core/src/config.js @@ -70,6 +70,7 @@ for (const [TYPE] of TYPES) { let config, type, + parser, pyElement, pyConfigs = $$(`${TYPE}-config`), attrConfigs = $$( @@ -92,9 +93,11 @@ for (const [TYPE] of TYPES) { [pyElement] = pyConfigs; config = pyElement.getAttribute("src") || pyElement.textContent; type = pyElement.getAttribute("type"); + parser = pyElement.getAttribute("config-parser"); } else if (attrConfigs.length) { [pyElement, ...attrConfigs] = attrConfigs; config = pyElement.getAttribute("config"); + parser = pyElement.getAttribute("config-parser"); // throw an error if dirrent scripts use different configs if ( attrConfigs.some((el) => el.getAttribute("config") !== config) @@ -120,9 +123,12 @@ for (const [TYPE] of TYPES) { } } else if (toml || type === "toml") { try { - const { parse } = await import( - /* webpackIgnore: true */ "./3rd-party/toml.js" - ); + const module = parser + ? await import(parser) + : await import( + /* webpackIgnore: true */ "./3rd-party/toml.js" + ); + const parse = module.parse || module.default; parsed = parse(text); } catch (e) { error = syntaxError("TOML", url, e); diff --git a/core/tests/index.html b/core/tests/index.html index 03b64beb..92284850 100644 --- a/core/tests/index.html +++ b/core/tests/index.html @@ -14,5 +14,5 @@ a:hover { opacity: 1; } - + diff --git a/core/tests/javascript/config-parser/file.py b/core/tests/javascript/config-parser/file.py new file mode 100644 index 00000000..01d9c808 --- /dev/null +++ b/core/tests/javascript/config-parser/file.py @@ -0,0 +1 @@ +print("OK") diff --git a/core/tests/javascript/config-parser/index.html b/core/tests/javascript/config-parser/index.html new file mode 100644 index 00000000..683571bf --- /dev/null +++ b/core/tests/javascript/config-parser/index.html @@ -0,0 +1,21 @@ + + + + + + + + + + + [files] + 'file.py' = "" + + + + diff --git a/core/tests/js_tests.spec.js b/core/tests/js_tests.spec.js index bb386283..080069b9 100644 --- a/core/tests/js_tests.spec.js +++ b/core/tests/js_tests.spec.js @@ -2,6 +2,13 @@ import { test, expect } from '@playwright/test'; test.setTimeout(120 * 1000); +test('config-parser custom TOML', async ({ page }) => { + await page.goto('http://localhost:8080/tests/javascript/config-parser/index.html'); + await page.waitForSelector('html.done'); + const body = await page.evaluate(() => document.body.innerText); + await expect(body.trim()).toBe('OK'); +}); + test('MicroPython display', async ({ page }) => { await page.goto('http://localhost:8080/tests/javascript/mpy.html'); await page.waitForSelector('html.done.worker');