From adc244e1bd13e4dc8806b49796a2f02fbf873586 Mon Sep 17 00:00:00 2001 From: Raghunandan Date: Wed, 15 Feb 2023 18:38:42 +0100 Subject: [PATCH] Use outerHTML during sanitize to preserve html tag attribs Signed-off-by: Raghunandan --- .../src/reader/transformers/html/transformer.ts | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/plugins/techdocs/src/reader/transformers/html/transformer.ts b/plugins/techdocs/src/reader/transformers/html/transformer.ts index a197a2d4b7..61498a37b0 100644 --- a/plugins/techdocs/src/reader/transformers/html/transformer.ts +++ b/plugins/techdocs/src/reader/transformers/html/transformer.ts @@ -15,12 +15,12 @@ */ import DOMPurify from 'dompurify'; -import { useMemo, useCallback } from 'react'; +import { useCallback, useMemo } from 'react'; -import { useApi, configApiRef } from '@backstage/core-plugin-api'; +import { configApiRef, useApi } from '@backstage/core-plugin-api'; import { Transformer } from '../transformer'; -import { removeUnsafeLinks, removeUnsafeIframes } from './hooks'; +import { removeUnsafeIframes, removeUnsafeLinks } from './hooks'; /** * Returns html sanitizer configuration @@ -34,7 +34,7 @@ const useSanitizerConfig = () => { }; /** - * Returns a transformer that sanitizes the dom's internal html. + * Returns a transformer that sanitizes the dom */ export const useSanitizerTransformer = (): Transformer => { const config = useSanitizerConfig(); @@ -51,7 +51,8 @@ export const useSanitizerTransformer = (): Transformer => { DOMPurify.addHook('beforeSanitizeElements', removeUnsafeIframes(hosts)); } - return DOMPurify.sanitize(dom.innerHTML, { + // using outerHTML as we want to preserve the html tag attributes (lang) + return DOMPurify.sanitize(dom.outerHTML, { ADD_TAGS: tags, FORBID_TAGS: ['style'], WHOLE_DOCUMENT: true,