constructSelector.tsx 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. import type {ReplayClickElement} from 'sentry/views/replays/types';
  2. function trimAttribute(elementAttribute, fullAlltribute) {
  3. return elementAttribute === '' ? '' : fullAlltribute;
  4. }
  5. // fullSelector is used for searches since searching without all attributes returns too many replays
  6. // selector is the selector shown in the selector widget/table and when you hover
  7. export default function constructSelector(element: ReplayClickElement) {
  8. const tag = element.tag;
  9. const id = trimAttribute(element.id, '#' + element.id);
  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 fullAlt = '[alt="' + element.alt + '"]';
  15. const alt = trimAttribute(element.alt, fullAlt);
  16. const fullAriaLabel = '[aria="' + element.aria_label + '"]';
  17. const ariaLabel = trimAttribute(element.aria_label, fullAriaLabel);
  18. const fullRole = '[role="' + element.role + '"]';
  19. const role = trimAttribute(element.role, fullRole);
  20. const fullTestId = '[data-test-id="' + element.testid + '"]';
  21. const testId = trimAttribute(element.testid, fullTestId);
  22. const fullTitle = '[title="' + element.title + '"]';
  23. const title = trimAttribute(element.title, fullTitle);
  24. const fullComponentName = '[data-sentry-component="' + element.component_name + '"]';
  25. const componentName = trimAttribute(element.component_name, fullComponentName);
  26. const fullSelector =
  27. tag +
  28. id +
  29. classes +
  30. fullRole +
  31. fullAriaLabel +
  32. fullTestId +
  33. fullAlt +
  34. fullTitle +
  35. fullComponentName;
  36. const selector =
  37. (componentName ? element.component_name + id : tag + id + classes) +
  38. role +
  39. ariaLabel +
  40. testId +
  41. alt +
  42. title;
  43. return {fullSelector, selector};
  44. }