improvement: Call DomPurify.addHook only if it exists (#5428)
On server-side execution `dompurify` exports factory function instead of a purifier instance. Because of this, server-side code that imports SwaggerUI (e.g. via `swagger-ui-react`) fails, since `DomPurify.addHook` does not exist. This affects universal rendering apps which share code between client-side and server-side.
This commit is contained in:
@@ -5,6 +5,7 @@ import { linkify } from "remarkable/linkify"
|
|||||||
import DomPurify from "dompurify"
|
import DomPurify from "dompurify"
|
||||||
import cx from "classnames"
|
import cx from "classnames"
|
||||||
|
|
||||||
|
if (DomPurify.addHook) {
|
||||||
DomPurify.addHook("beforeSanitizeElements", function (current, ) {
|
DomPurify.addHook("beforeSanitizeElements", function (current, ) {
|
||||||
// Attach safe `rel` values to all elements that contain an `href`,
|
// Attach safe `rel` values to all elements that contain an `href`,
|
||||||
// i.e. all anchors that are links.
|
// i.e. all anchors that are links.
|
||||||
@@ -15,6 +16,7 @@ DomPurify.addHook("beforeSanitizeElements", function (current, ) {
|
|||||||
}
|
}
|
||||||
return current
|
return current
|
||||||
})
|
})
|
||||||
|
}
|
||||||
|
|
||||||
function Markdown({ source, className = "", getConfigs }) {
|
function Markdown({ source, className = "", getConfigs }) {
|
||||||
if (typeof source !== "string") {
|
if (typeof source !== "string") {
|
||||||
|
|||||||
Reference in New Issue
Block a user