Update the Lite custom element parser (#7975)

* Update the Lite custom element parser to deal with the text inside `<gradio-lite>` when there is no `<gradio-code>` elements, which works with other custom elements like `<gradio-requirements>`

* add changeset

* Remove `.trim()`

---------

Co-authored-by: gradio-pr-bot <gradio-pr-bot@users.noreply.github.com>
This commit is contained in:
Yuichiro Tachibana (Tsuchiya) 2024-04-16 07:19:02 +09:00 committed by GitHub
parent 2a5cb97824
commit c9ddd847d6
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 23 additions and 11 deletions

View File

@ -0,0 +1,6 @@
---
"@gradio/app": minor
"gradio": minor
---
feat:Update the Lite custom element parser

View File

@ -115,11 +115,6 @@ export function bootstrap_custom_element(
}
parseGradioLiteAppOptions(): GradioLiteAppOptions {
// When gradioLiteAppElement only contains text content, it is treated as the Python code.
if (this.childElementCount === 0) {
return { code: this.textContent ?? "" };
}
// When it contains child elements, parse them as options. Available child elements are:
// * <gradio-file />
// Represents a file to be mounted in the virtual file system of the Wasm worker.
@ -157,13 +152,24 @@ export function bootstrap_custom_element(
}
const codeElements = this.getElementsByTagName("gradio-code");
if (codeElements.length > 1) {
console.warn(
"Multiple <gradio-code> elements are found. Only the first one will be used."
);
if (codeElements.length === 0) {
// If there is no <gradio-code> element, try to parse the content of the custom element as code.
let code = "";
this.childNodes.forEach((node) => {
if (node.nodeType === Node.TEXT_NODE) {
code += node.textContent;
}
});
options.code = code || undefined;
} else {
if (codeElements.length > 1) {
console.warn(
"Multiple <gradio-code> elements are found. Only the first one will be used."
);
}
const firstCodeElement = codeElements[0];
options.code = firstCodeElement?.textContent ?? undefined;
}
const firstCodeElement = codeElements[0];
options.code = firstCodeElement?.textContent ?? undefined;
const requirementsElements = this.getElementsByTagName(
"gradio-requirements"