All files targeting.ts

78.26% Statements 54/69
73.52% Branches 25/34
90.9% Functions 10/11
83.87% Lines 52/62

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187                                                                            1x 10x         10x 2x     2x 2x         8x 8x 7x 4x 4x           4x             1x       2x   2x   2x 2x 1x 1x     1x         1x 1x 1x 1x   1x 1x 1x 1x 1x       1x 1x 1x 1x         1x 1x 1x 1x             1x                 1x                           1x 1x   1x 1x                 1x 1x   1x 1x                 1x       2x   2x             1x 1x    
/**
 * WalkMe Targeting Module
 *
 * DOM element targeting utilities for finding, waiting for,
 * and interacting with target elements for tour steps.
 */
 
// ---------------------------------------------------------------------------
// Types
// ---------------------------------------------------------------------------
 
export interface TargetResult {
  element: HTMLElement | null
  found: boolean
  selector: string
  method: 'css' | 'data-walkme' | 'data-cy'
}
 
export interface WaitForTargetOptions {
  /** Maximum time to wait in ms (default: 5000) */
  timeout?: number
  /** Polling interval in ms (default: 200) */
  interval?: number
}
 
// ---------------------------------------------------------------------------
// Element Finding
// ---------------------------------------------------------------------------
 
/**
 * Find a target element using various selector strategies.
 *
 * Supports:
 * 1. CSS selectors: `#id`, `.class`, `[attribute="value"]`
 * 2. Data-walkme attribute shorthand: if selector has no CSS special chars,
 *    tries `[data-walkme-target="selector"]` first
 * 3. Data-cy attribute: `[data-cy="value"]`
 */
export function findTarget(selector: string): TargetResult {
  Iif (typeof window === 'undefined') {
    return { element: null, found: false, selector, method: 'css' }
  }
 
  // Try data-walkme-target first if selector looks like a plain name
  if (/^[a-zA-Z0-9_-]+$/.test(selector)) {
    const walkmeEl = document.querySelector<HTMLElement>(
      `[data-walkme-target="${selector}"]`,
    )
    if (walkmeEl) {
      return { element: walkmeEl, found: true, selector, method: 'data-walkme' }
    }
  }
 
  // Try as CSS selector
  try {
    const el = document.querySelector<HTMLElement>(selector)
    if (el) {
      const method = selector.includes('data-cy') ? 'data-cy' : 'css'
      return { element: el, found: true, selector, method }
    }
  } catch {
    // Invalid selector - return not found
  }
 
  return { element: null, found: false, selector, method: 'css' }
}
 
/**
 * Wait for a target element to appear in the DOM.
 * Uses MutationObserver for efficient watching.
 */
export function waitForTarget(
  selector: string,
  options: WaitForTargetOptions = {},
): Promise<TargetResult> {
  const { timeout = 5000, interval = 200 } = options
 
  return new Promise((resolve) => {
    // Try immediately first
    const immediate = findTarget(selector)
    if (immediate.found) {
      resolve(immediate)
      return
    }
 
    Iif (typeof window === 'undefined') {
      resolve({ element: null, found: false, selector, method: 'css' })
      return
    }
 
    let resolved = false
    let observer: MutationObserver | null = null
    let timeoutId: ReturnType<typeof setTimeout> | null = null
    let intervalId: ReturnType<typeof setInterval> | null = null
 
    const cleanup = () => {
      resolved = true
      observer?.disconnect()
      if (timeoutId) clearTimeout(timeoutId)
      if (intervalId) clearInterval(intervalId)
    }
 
    // Set up timeout
    timeoutId = setTimeout(() => {
      if (!resolved) {
        cleanup()
        resolve({ element: null, found: false, selector, method: 'css' })
      }
    }, timeout)
 
    // Poll as a fallback (MutationObserver doesn't catch everything)
    intervalId = setInterval(() => {
      Iif (resolved) return
      const result = findTarget(selector)
      Iif (result.found) {
        cleanup()
        resolve(result)
      }
    }, interval)
 
    // MutationObserver for immediate detection
    observer = new MutationObserver(() => {
      Iif (resolved) return
      const result = findTarget(selector)
      Iif (result.found) {
        cleanup()
        resolve(result)
      }
    })
 
    observer.observe(document.body, {
      childList: true,
      subtree: true,
      attributes: true,
      attributeFilter: ['data-walkme-target', 'data-cy', 'id', 'class'],
    })
  })
}
 
// ---------------------------------------------------------------------------
// Element Utilities
// ---------------------------------------------------------------------------
 
/** Check if an element is visible (not hidden by CSS) */
export function isElementVisible(element: HTMLElement): boolean {
  Iif (typeof window === 'undefined') return false
 
  const style = window.getComputedStyle(element)
  return (
    style.display !== 'none' &&
    style.visibility !== 'hidden' &&
    style.opacity !== '0' &&
    element.offsetParent !== null
  )
}
 
/** Check if an element is within the current viewport */
export function isElementInViewport(element: HTMLElement): boolean {
  Iif (typeof window === 'undefined') return false
 
  const rect = element.getBoundingClientRect()
  return (
    rect.top >= 0 &&
    rect.left >= 0 &&
    rect.bottom <= window.innerHeight &&
    rect.right <= window.innerWidth
  )
}
 
/** Scroll the viewport to make an element visible */
export function scrollToElement(
  element: HTMLElement,
  options: { behavior?: ScrollBehavior; block?: ScrollLogicalPosition } = {},
): void {
  Iif (typeof window === 'undefined') return
 
  element.scrollIntoView({
    behavior: options.behavior ?? 'smooth',
    block: options.block ?? 'center',
  })
}
 
/** Get the bounding rect of an element */
export function getElementRect(element: HTMLElement): DOMRect {
  return element.getBoundingClientRect()
}