index.dom.js 1.0 KB

1234567891011121314151617181920212223242526272829303132
  1. /// <reference lib="dom" />
  2. /* global document */
  3. const element = document.createElement('i')
  4. /**
  5. * @param {string} value
  6. * @returns {string | false}
  7. */
  8. export function decodeNamedCharacterReference(value) {
  9. const characterReference = '&' + value + ';'
  10. element.innerHTML = characterReference
  11. const character = element.textContent
  12. // Some named character references do not require the closing semicolon
  13. // (`&not`, for instance), which leads to situations where parsing the assumed
  14. // named reference of `&notit;` will result in the string `¬it;`.
  15. // When we encounter a trailing semicolon after parsing, and the character
  16. // reference to decode was not a semicolon (`&semi;`), we can assume that the
  17. // matching was not complete.
  18. if (
  19. character.charCodeAt(character.length - 1) === 59 /* `;` */ &&
  20. value !== 'semi'
  21. ) {
  22. return false
  23. }
  24. // If the decoded string is equal to the input, the character reference was
  25. // not valid.
  26. return character === characterReference ? false : character
  27. }