Sandbox: Fix prismjs syntax for plugins defining its own language (#73393)

* Sandbox: Fix prismjs syntax for plugins defining its own language

* Add clarifying comment
This commit is contained in:
Esteban Beltran 2023-08-22 13:04:57 +02:00 committed by GitHub
parent 040b7d2571
commit 050b9d9a85
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,4 +1,5 @@
import { isNearMembraneProxy, ProxyTarget } from '@locker/near-membrane-shared';
import Prism from 'prismjs';
import { DataSourceApi } from '@grafana/data';
import { config } from '@grafana/runtime';
@ -94,10 +95,16 @@ export function markDomElementStyleAsALiveTarget(el: Element) {
* but not all objects, only the ones that are allowed to be modified
*/
export function patchObjectAsLiveTarget(obj: unknown) {
if (!obj) {
return;
}
// do not patch it twice
if (Object.hasOwn(obj, SANDBOX_LIVE_VALUE)) {
return;
}
if (
obj &&
// do not define it twice
!Object.hasOwn(obj, SANDBOX_LIVE_VALUE) &&
// only for proxies
isNearMembraneProxy(obj) &&
// do not patch functions
@ -107,6 +114,15 @@ export function patchObjectAsLiveTarget(obj: unknown) {
(isReactClassComponent(obj) || obj instanceof DataSourceApi)
) {
Reflect.defineProperty(obj, SANDBOX_LIVE_VALUE, {});
} else {
// prismjs languages are defined by directly modifying the prism.languages objects.
// Plugins inside the sandbox can't modify objects from the blue realm and prismjs.languages
// is one of them.
// Marking it as a live target allows plugins inside the sandbox to modify the object directly
// and make syntax work again.
if (obj === Prism.languages) {
Object.defineProperty(obj, SANDBOX_LIVE_VALUE, {});
}
}
}