Skip to content

Commit

Permalink
Execute JavaScript elements in dynamic HTML
Browse files Browse the repository at this point in the history
  • Loading branch information
georgestagg committed Aug 28, 2024
1 parent a25849a commit e33a900
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 35 deletions.
9 changes: 9 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Quarto Live (development version)

## Bug fixes

* Ensure that JavaScript scripts that have been dynamically added via HTML output are executed.

# Quarto Live 0.1.1

Initial release.
54 changes: 27 additions & 27 deletions _extensions/live/resources/live-runtime.js

Large diffs are not rendered by default.

14 changes: 7 additions & 7 deletions _extensions/live/resources/pyodide-worker.js

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions live-runtime/src/evaluate-pyodide.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
EvaluateValue,
} from "./evaluate";
import { PyodideInterfaceWorker } from './pyodide-worker';
import { replaceScriptChildren } from './utils';

declare global {
interface Window {
Expand Down Expand Up @@ -304,6 +305,7 @@ export class PyodideEvaluator implements ExerciseEvaluator {
const outputDiv = document.createElement("div");
outputDiv.className = "cell-output cell-output-pyodide";
outputDiv.innerHTML = html;
replaceScriptChildren(outputDiv);
container.appendChild(outputDiv);
}
};
Expand Down
3 changes: 2 additions & 1 deletion live-runtime/src/evaluate-webr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import {
ExerciseEvaluator,
OJSEvaluateElement,
} from "./evaluate";
import { arrayBufferToBase64 } from './utils';
import { arrayBufferToBase64, replaceScriptChildren } from './utils';

declare global {
interface Window {
Expand Down Expand Up @@ -340,6 +340,7 @@ export class WebREvaluator implements ExerciseEvaluator {
const outputDiv = document.createElement("div");
outputDiv.className = "cell-output cell-output-webr";
outputDiv.innerHTML = html;
replaceScriptChildren(outputDiv);

// Add HTML output to the DOM
appendSource();
Expand Down
16 changes: 16 additions & 0 deletions live-runtime/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,19 @@ export function replaceInObject<T>(
}
return obj;
}

export function replaceScriptChildren(container: HTMLElement) {
for (let script of container.getElementsByTagName('script')) {
if (!script.type || script.type == "text/javascript" || script.type == "module") {
const newScript = document.createElement('script');
if (script.async) newScript.async = script.async;
if (script.crossOrigin) newScript.crossOrigin = script.crossOrigin;
if (script.defer) newScript.defer = script.async;
if (script.integrity) newScript.integrity = script.integrity;
if (script.src) newScript.src = script.src;
if (script.text) newScript.text = script.text;
if (script.type) newScript.type = script.type;
script.parentNode.replaceChild(newScript, script);
}
}
}

0 comments on commit e33a900

Please sign in to comment.