utils.tsx 761 B

1234567891011121314151617181920212223
  1. // Taken from https://stackoverflow.com/a/56859650/1015027
  2. export function findTextWithMarkup(
  3. contentNode: null | Element,
  4. textMatch: string | RegExp
  5. ) {
  6. const hasText = (node: Element) => node.textContent === textMatch;
  7. const nodeHasText = hasText(contentNode as Element);
  8. const childrenDontHaveText = Array.from(contentNode?.children || []).every(
  9. child => !hasText(child)
  10. );
  11. return nodeHasText && childrenDontHaveText;
  12. }
  13. /**
  14. * May be used with a *ByText RTL matcher to match text within multiple nodes
  15. *
  16. * e.g.: <div>Hello <span>world</span></div>
  17. */
  18. export function textWithMarkupMatcher(textMatch: string | RegExp) {
  19. return function (_: string, element: Element | null) {
  20. return findTextWithMarkup(element, textMatch);
  21. };
  22. }