Merge pull request #1346 from akhilmhdh/fix/multi-line-fixed

fix: resolved encoding issue in multi line input
This commit is contained in:
Akhil Mohan
2024-01-29 18:41:55 +05:30
committed by GitHub
@@ -1,43 +1,46 @@
/* eslint-disable react/no-danger */ /* eslint-disable react/no-danger */
import { forwardRef, TextareaHTMLAttributes } from "react"; import { forwardRef, TextareaHTMLAttributes } from "react";
import sanitizeHtml, { DisallowedTagsModes } from "sanitize-html";
import { twMerge } from "tailwind-merge"; import { twMerge } from "tailwind-merge";
import { useToggle } from "@app/hooks"; import { useToggle } from "@app/hooks";
const REGEX = /\${([^}]+)}/g; const REGEX = /(\${([^}]+)})/g;
const replaceContentWithDot = (str: string) => { const replaceContentWithDot = (str: string) => {
let finalStr = ""; let finalStr = "";
for (let i = 0; i < str.length; i += 1) { for (let i = 0; i < str.length; i += 1) {
const char = str.at(i); const char = str.at(i);
finalStr += char === "\n" ? "\n" : "&#8226;"; finalStr += char === "\n" ? "\n" : "*";
} }
return finalStr; return finalStr;
}; };
const sanitizeConf = {
allowedTags: ["span"],
disallowedTagsMode: "escape" as DisallowedTagsModes
};
const syntaxHighlight = (content?: string | null, isVisible?: boolean) => { const syntaxHighlight = (content?: string | null, isVisible?: boolean) => {
if (content === "") return "EMPTY"; if (content === "") return "EMPTY";
if (!content) return "EMPTY"; if (!content) return "EMPTY";
if (!isVisible) return replaceContentWithDot(content); if (!isVisible) return replaceContentWithDot(content);
const sanitizedContent = sanitizeHtml( let skipNext = false;
content.replaceAll("<", "&lt;").replaceAll(">", "&gt;"), const formatedContent = content.split(REGEX).flatMap((el, i) => {
sanitizeConf const isInterpolationSyntax = el.startsWith("${") && el.endsWith("}");
); if (isInterpolationSyntax) {
const newContent = sanitizedContent.replace( skipNext = true;
REGEX, return (
(_a, b) => <span className="ph-no-capture text-yellow" key={`secret-value-${i + 1}`}>
`<span class="ph-no-capture text-yellow">&#36;&#123;<span class="ph-no-capture text-yello-200/80">${b}</span>&#125;</span>` &#36;&#123;<span className="ph-no-capture text-yello-200/80">{el.slice(2, -1)}</span>
); &#125;
</span>
);
}
if (skipNext) {
skipNext = false;
return [];
}
return el;
});
// akhilmhdh: Dont remove this br. I am still clueless how this works but weirdly enough // akhilmhdh: Dont remove this br. I am still clueless how this works but weirdly enough
// when break is added a line break works properly // when break is added a line break works properly
return `${newContent}<br/>`; return formatedContent.concat(<br />);
}; };
type Props = TextareaHTMLAttributes<HTMLTextAreaElement> & { type Props = TextareaHTMLAttributes<HTMLTextAreaElement> & {
@@ -59,18 +62,15 @@ export const SecretInput = forwardRef<HTMLTextAreaElement, Props>(
return ( return (
<div <div
className={twMerge("overflow-auto w-full no-scrollbar rounded-md", containerClassName)} className={twMerge("w-full overflow-auto rounded-md no-scrollbar", containerClassName)}
style={{ maxHeight: `${21 * 7}px` }} style={{ maxHeight: `${21 * 7}px` }}
> >
<div className="relative overflow-hidden"> <div className="relative overflow-hidden">
<pre aria-hidden className="m-0 "> <pre aria-hidden className="m-0 ">
<code className={`inline-block w-full ${commonClassName}`}> <code className={`inline-block w-full ${commonClassName}`}>
<span <span style={{ whiteSpace: "break-spaces" }}>
style={{ whiteSpace: "break-spaces" }} {syntaxHighlight(value, isVisible || isSecretFocused)}
dangerouslySetInnerHTML={{ </span>
__html: syntaxHighlight(value, isVisible || isSecretFocused) ?? ""
}}
/>
</code> </code>
</pre> </pre>
<textarea <textarea