From 039d4661fb009ae68f70c138476a1781fbae8533 Mon Sep 17 00:00:00 2001 From: Lim Chee Aun Date: Sun, 11 Dec 2022 09:28:02 +0800 Subject: [PATCH] Remove target=_blank from username links Mobile Safari ignores the click preventDefault and not show the Account popover. Removing target_blank fixes this ignorance Kinda hacky fix for now --- src/components/name-text.jsx | 2 +- src/components/status.jsx | 10 +++++++++- src/utils/enhance-content.js | 9 +++++++-- 3 files changed, 17 insertions(+), 4 deletions(-) diff --git a/src/components/name-text.jsx b/src/components/name-text.jsx index 285bf886..1b8ab775 100644 --- a/src/components/name-text.jsx +++ b/src/components/name-text.jsx @@ -15,7 +15,7 @@ export default ({ account, showAvatar, showAcct, short, external }) => { { if (external) return; diff --git a/src/components/status.jsx b/src/components/status.jsx index f047324f..9eeb7041 100644 --- a/src/components/status.jsx +++ b/src/components/status.jsx @@ -506,7 +506,7 @@ function Status({ statusID, status, withinContext, size = 'm', skeleton }) { {size !== 's' && ( { e.preventDefault(); @@ -604,6 +604,14 @@ function Status({ statusID, status, withinContext, size = 'm', skeleton }) { dangerouslySetInnerHTML={{ __html: enhanceContent(content, { emojis, + postEnhanceDOM: (dom) => { + dom + .querySelectorAll('a.u-url[target="_blank"]') + .forEach((a) => { + // Remove target="_blank" from links + a.removeAttribute('target'); + }); + }, }), }} /> diff --git a/src/utils/enhance-content.js b/src/utils/enhance-content.js index af3e8f6d..c048bbb3 100644 --- a/src/utils/enhance-content.js +++ b/src/utils/enhance-content.js @@ -1,7 +1,7 @@ import emojifyText from './emojify-text'; export default (content, opts = {}) => { - const { emojis } = opts; + const { emojis, postEnhanceDOM = () => {} } = opts; let enhancedContent = content; const dom = document.createElement('div'); dom.innerHTML = enhancedContent; @@ -27,7 +27,7 @@ export default (content, opts = {}) => { const pre = document.createElement('pre'); const code = document.createElement('code'); const breaks = block.querySelectorAll('br'); - Array.from(breaks).forEach((br) => br.replaceWith('\n')); + breaks.forEach((br) => br.replaceWith('\n')); code.innerHTML = block.innerText .trim() // .replace(/^```/g, '') @@ -37,6 +37,11 @@ export default (content, opts = {}) => { block.replaceWith(pre); }); + if (postEnhanceDOM) { + postEnhanceDOM(dom); // mutate dom + } + enhancedContent = dom.innerHTML; + return enhancedContent; };