Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

insertUI() now supports execution of <script> #3630

Merged
merged 4 commits into from
May 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ shiny development

* Closed tidyverse/dplyr#5552: Compatibility of dplyr 1.0 (and rlang chained errors in general) with `req()`, `validate()`, and friends.

* Closed #1545: `insertUI()` now executes `<script>` tags. (#3630)

* Closed #2955: Input and output bindings previously attempted to use `el['data-input-id']`, but that never worked. They now use `el.getAttribute('data-input-id')` instead. (#3538)

* Closed tidyverse/dplyr#6154: Values from an `actionButton()` had S3 classes in the incorrect order.
Expand Down
33 changes: 18 additions & 15 deletions inst/www/shared/shiny.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions inst/www/shared/shiny.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion inst/www/shared/shiny.min.js

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions inst/www/shared/shiny.min.js.map

Large diffs are not rendered by default.

36 changes: 22 additions & 14 deletions srcts/src/shiny/singletons.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import $ from "jquery";
import { toLowerCase } from "../utils";
import type { BindScope } from "./bind";

const reSingleton = /<!--(SHINY.SINGLETON\[([\w]+)\])-->([\s\S]*?)<!--\/\1-->/;
Expand All @@ -23,21 +22,30 @@ function renderHtml(

addToHead(processed.head);
register(processed.singletons);
if (where === "replace") {
$(el).html(processed.html);
} else {
let elElements: HTMLElement[];

if (el instanceof HTMLElement) {
elElements = [el];
} else {
elElements = el.toArray();
}
$.each(elElements, (i, el) => {
// type InsertPosition = "beforebegin" | "afterbegin" | "beforeend" | "afterend"
el.insertAdjacentHTML(toLowerCase(where), processed.html);
});
// N.B. even though the DOM insertion below _could_ be done with vanilla JS,
// we intentionally use jQuery so that <script> tags execute.
// https://github.com/rstudio/shiny/pull/3630
switch (where.toLowerCase()) {
case "replace":
$(el).html(processed.html);
break;
case "beforebegin":
$(el).before(processed.html);
break;
case "afterbegin":
$(el).prepend(processed.html);
break;
case "beforeend":
$(el).append(processed.html);
break;
case "afterend":
$(el).after(processed.html);
break;
default:
throw new Error("Unknown where position: " + where);
}

return processed;
}
// Take an object where keys are names of singletons, and merges it into
Expand Down