index.js 885 B

12345678910111213141516171819202122232425262728293031323334
  1. /**
  2. * @typedef {import('hast').Nodes} Nodes
  3. */
  4. // HTML whitespace expression.
  5. // See <https://infra.spec.whatwg.org/#ascii-whitespace>.
  6. const re = /[ \t\n\f\r]/g
  7. /**
  8. * Check if the given value is *inter-element whitespace*.
  9. *
  10. * @param {Nodes | string} thing
  11. * Thing to check (`Node` or `string`).
  12. * @returns {boolean}
  13. * Whether the `value` is inter-element whitespace (`boolean`): consisting of
  14. * zero or more of space, tab (`\t`), line feed (`\n`), carriage return
  15. * (`\r`), or form feed (`\f`); if a node is passed it must be a `Text` node,
  16. * whose `value` field is checked.
  17. */
  18. export function whitespace(thing) {
  19. return typeof thing === 'object'
  20. ? thing.type === 'text'
  21. ? empty(thing.value)
  22. : false
  23. : empty(thing)
  24. }
  25. /**
  26. * @param {string} value
  27. * @returns {boolean}
  28. */
  29. function empty(value) {
  30. return value.replace(re, '') === ''
  31. }