Respect text-only emoji setting after svgification
Follow-up to #271. Skip svgifyEmoji when body.text-emojis is set so deEmojify can strip Unicode from replies; also unwrap existing .emoji spans from messages rendered before the setting was applied. Related to #270
This commit is contained in:
@@ -2553,14 +2553,23 @@ function initializeEventListeners() {
|
||||
});
|
||||
}
|
||||
|
||||
const _DEOJ_SKIP = '.sources-section, .thinking-toggle, .memory-used-pill';
|
||||
|
||||
/** Walk all text nodes inside an element and replace emojis with text descriptions */
|
||||
function deEmojify(root) {
|
||||
if (!root || !root.querySelectorAll) return;
|
||||
// Monochrome SVG spans from svgifyEmoji — Unicode lives in aria-label only
|
||||
root.querySelectorAll('.emoji[aria-label]').forEach((span) => {
|
||||
if (span.closest(_DEOJ_SKIP)) return;
|
||||
const label = span.getAttribute('aria-label') || '';
|
||||
span.replaceWith(document.createTextNode(emojiToText(label)));
|
||||
});
|
||||
const walker = document.createTreeWalker(root, NodeFilter.SHOW_TEXT);
|
||||
const nodes = [];
|
||||
while (walker.nextNode()) nodes.push(walker.currentNode);
|
||||
for (const node of nodes) {
|
||||
// Skip UI elements that use unicode symbols as functional icons
|
||||
if (node.parentElement && node.parentElement.closest('.sources-section, .thinking-toggle, .memory-used-pill')) continue;
|
||||
if (node.parentElement && node.parentElement.closest(_DEOJ_SKIP)) continue;
|
||||
if (EMOJI_RE.test(node.textContent)) {
|
||||
EMOJI_RE.lastIndex = 0; // reset regex state
|
||||
node.textContent = emojiToText(node.textContent);
|
||||
|
||||
@@ -233,8 +233,13 @@ function _svgifyText(text) {
|
||||
}
|
||||
return out;
|
||||
}
|
||||
/** When "Text-only Emojis" is on, keep Unicode in HTML so deEmojify() can strip them. */
|
||||
function _useSvgEmoji() {
|
||||
return typeof document === 'undefined' || !document.body?.classList.contains('text-emojis');
|
||||
}
|
||||
|
||||
export function svgifyEmoji(html) {
|
||||
if (!html || !_EMOJI_RE.test(html)) return html;
|
||||
if (!_useSvgEmoji() || !html || !_EMOJI_RE.test(html)) return html;
|
||||
const parts = html.split(/(<[^>]*>)/); // odd indices = tags
|
||||
let codeDepth = 0;
|
||||
for (let i = 0; i < parts.length; i++) {
|
||||
@@ -282,7 +287,7 @@ export function processWithThinking(text) {
|
||||
html += mdToHtml(content);
|
||||
}
|
||||
|
||||
return svgifyEmoji(html);
|
||||
return _useSvgEmoji() ? svgifyEmoji(html) : html;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -539,7 +544,7 @@ export function mdToHtml(src) {
|
||||
s = s.replace(`___CODE_BLOCK_${index}___`, block);
|
||||
});
|
||||
|
||||
return svgifyEmoji(s);
|
||||
return _useSvgEmoji() ? svgifyEmoji(s) : s;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user