actionIconGlyphHelpers.mjs 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. const fallbackNamedHtmlEntities = {
  2. amp: '&',
  3. apos: "'",
  4. darr: '\u2193',
  5. gt: '>',
  6. laquo: '\u00ab',
  7. larr: '\u2190',
  8. nbsp: '\u00a0',
  9. quot: '"',
  10. raquo: '\u00bb',
  11. rarr: '\u2192',
  12. uarr: '\u2191',
  13. }
  14. export function decodeHtmlEntities(text) {
  15. if (typeof document !== 'undefined') {
  16. const textarea = document.createElement('textarea')
  17. textarea.innerHTML = text
  18. return textarea.value
  19. }
  20. return text.replace(/&#x([0-9a-fA-F]+);?/g, (_, hex) => {
  21. const codePoint = Number.parseInt(hex, 16)
  22. return Number.isFinite(codePoint) ? String.fromCodePoint(codePoint) : ''
  23. }).replace(/&#(\d+);?/g, (_, decimal) => {
  24. const codePoint = Number.parseInt(decimal, 10)
  25. return Number.isFinite(codePoint) ? String.fromCodePoint(codePoint) : ''
  26. }).replace(/&([a-zA-Z][a-zA-Z0-9]+);?/g, (entity, name) => {
  27. return fallbackNamedHtmlEntities[name] ?? entity
  28. })
  29. }
  30. export function glyphLooksLikeHtml(text) {
  31. const trimmedText = text.trim()
  32. return trimmedText.startsWith('<') || /<img\b/i.test(text) || /\/custom-webui\//i.test(text)
  33. }