All files / lib/util decode-entities.js

100% Statements 24/24
71.42% Branches 5/7
100% Functions 1/1
100% Lines 24/24

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 252x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 129x 129x 121x 121x 129x 129x 129x 129x  
import globalThis from './global';
import { EMPTY } from './types';
 
// Safely lookup if we have access to a DIV element for decoding entities.
const element = /** @type {any} */ (globalThis).document
  ? /** @type {HTMLDivElement} */ (document.createElement('div'))
  : null;
 
/**
 * Decodes HTML strings.
 *
 * @see http://stackoverflow.com/a/5796718
 * @param {string} string - Incoming string HTML
 * @return {string} Unescaped HTML
 */
export default function decodeEntities(string) {
  // If there are no HTML entities, we can safely pass the string through.
  if (!element || !string || !string.indexOf || !string.includes('&')) {
    return string;
  }
 
  element.innerHTML = string;
  return element.textContent || EMPTY.STR;
}