Skip to content

Commit

Permalink
Optimize removeScript
Browse files Browse the repository at this point in the history
  • Loading branch information
sidharthv96 committed Jan 25, 2024
1 parent 3778c41 commit a327068
Showing 1 changed file with 9 additions and 8 deletions.
17 changes: 9 additions & 8 deletions packages/mermaid/src/diagrams/common/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@ export const getRows = (s?: string): string[] => {
return str.split('#br#');
};

const checkNodeTypeAndGetAttribute = (node: Element, attribute: string) => {
if (node.tagName !== 'A') {
return null;
}
return node.getAttribute(attribute);
};

Check warning on line 26 in packages/mermaid/src/diagrams/common/common.ts

View check run for this annotation

Codecov / codecov/patch

packages/mermaid/src/diagrams/common/common.ts#L22-L26

Added lines #L22 - L26 were not covered by tests

/**
* Removes script tags from a text
*
Expand All @@ -30,19 +37,13 @@ export const removeScript = (txt: string): string => {
if (hasTarget) {
const TEMPORARY_ATTRIBUTE = 'data-temp-href-target';
DOMPurify.addHook('beforeSanitizeAttributes', (node: Element) => {
if (node.tagName !== 'A') {
return;
}
const target = node.getAttribute(TARGET);
const target = checkNodeTypeAndGetAttribute(node, TARGET);
if (target) {
node.setAttribute(TEMPORARY_ATTRIBUTE, target);
}
});
DOMPurify.addHook('afterSanitizeAttributes', (node: Element) => {
if (node.tagName !== 'A') {
return;
}
const target = node.getAttribute(TEMPORARY_ATTRIBUTE);
const target = checkNodeTypeAndGetAttribute(node, TEMPORARY_ATTRIBUTE);
if (target) {
node.setAttribute(TARGET, target);
node.removeAttribute(TEMPORARY_ATTRIBUTE);
Expand Down

0 comments on commit a327068

Please sign in to comment.