constructSelector.tsx 1.4 KB

123456789101112131415161718192021222324252627282930313233343536
  1. import type {ReplayClickElement} from 'sentry/views/replays/types';
  2. function trimAttribute(elementAttribute, fullAlltribute) {
  3. return elementAttribute === '' ? '' : fullAlltribute;
  4. }
  5. export default function constructSelector(element: ReplayClickElement) {
  6. const fullAlt = '[alt="' + element.alt + '"]';
  7. const alt = trimAttribute(element.alt, fullAlt);
  8. const fullAriaLabel = '[aria="' + element.aria_label + '"]';
  9. const ariaLabel = trimAttribute(element.aria_label, fullAriaLabel);
  10. const trimClass = element.class.filter(e => e !== '');
  11. const classWithPeriod = trimClass.join('.');
  12. const classNoPeriod = classWithPeriod.replace('.', '');
  13. const classes = trimAttribute(classNoPeriod, '.' + classWithPeriod);
  14. const id = trimAttribute(element.id, '#' + element.id);
  15. const fullRole = '[role="' + element.role + '"]';
  16. const role = trimAttribute(element.role, fullRole);
  17. const tag = element.tag;
  18. const fullTestId = '[data-test-id="' + element.testid + '"]';
  19. const testId = trimAttribute(element.testid, fullTestId);
  20. const fullTitle = '[title="' + element.title + '"]';
  21. const title = trimAttribute(element.title, fullTitle);
  22. const fullSelector =
  23. tag + id + classes + fullRole + fullAriaLabel + fullTestId + fullAlt + fullTitle;
  24. const selector = tag + id + classes + role + ariaLabel + testId + alt + title;
  25. return {fullSelector, selector};
  26. }