Skip to content

Commit

Permalink
fix: Fix checking if scope is a jquery element
Browse files Browse the repository at this point in the history
  • Loading branch information
gadenbuie committed Jan 14, 2025
1 parent 0952f3e commit db123a1
Showing 1 changed file with 19 additions and 2 deletions.
21 changes: 19 additions & 2 deletions srcts/src/shiny/bind.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,19 @@ import { shinyAppBindOutput, shinyAppUnbindOutput } from "./initedMethods";
import { sendImageSizeFns } from "./sendImageSize";

type BindScope = HTMLElement | JQuery<HTMLElement>;
type BindAllScope = HTMLElement | JQuery<HTMLElement> | Text;

/**
* Type guard to check if a value is a jQuery object containing HTMLElements
* @param value The value to check
* @returns A type predicate indicating if the value is a jQuery<HTMLElement>
*/
function isJQuery<T = HTMLElement>(value: unknown): value is JQuery<T> {
return (
Object.prototype.hasOwnProperty.call(value, "jquery") &&
typeof (value as any).jquery === "string"
);
}

// todo make sure allowDeferred can NOT be supplied and still work
function valueChangeCallback(
Expand Down Expand Up @@ -78,7 +91,11 @@ const bindingsRegistry = (() => {
* @returns ShinyClientMessageEvent if current ID bindings are invalid,
* otherwise returns an ok status.
*/
function checkValidity(scope: BindScope): void {
function checkValidity(scope: BindAllScope): void {
if (scope instanceof Text) {
scope = scope.parentElement || document.documentElement;
}

type BindingCounts = { [T in BindingTypes]: number };
const duplicateIds = new Map<string, BindingCounts>();
const problems: Set<string> = new Set();
Expand Down Expand Up @@ -146,7 +163,7 @@ const bindingsRegistry = (() => {
}:\n${duplicateIdMsg}`;

const event = new ShinyClientMessageEvent({ headline, message });
const scopeElement = scope instanceof HTMLElement ? scope : scope.get(0);
const scopeElement = isJQuery(scope) ? scope.get(0) : scope;
(scopeElement || window).dispatchEvent(event);
}

Expand Down

0 comments on commit db123a1

Please sign in to comment.