buildDomTree.js 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320
  1. window.buildDomTree = (
  2. args = {
  3. doHighlightElements: true,
  4. focusHighlightIndex: -1,
  5. viewportExpansion: 0,
  6. debugMode: false,
  7. },
  8. ) => {
  9. const { doHighlightElements, focusHighlightIndex, viewportExpansion, debugMode } = args;
  10. let highlightIndex = 0; // Reset highlight index
  11. // Add timing stack to handle recursion
  12. const TIMING_STACK = {
  13. nodeProcessing: [],
  14. treeTraversal: [],
  15. highlighting: [],
  16. current: null,
  17. };
  18. function pushTiming(type) {
  19. TIMING_STACK[type] = TIMING_STACK[type] || [];
  20. TIMING_STACK[type].push(performance.now());
  21. }
  22. function popTiming(type) {
  23. const start = TIMING_STACK[type].pop();
  24. const duration = performance.now() - start;
  25. return duration;
  26. }
  27. // Only initialize performance tracking if in debug mode
  28. const PERF_METRICS = debugMode
  29. ? {
  30. buildDomTreeCalls: 0,
  31. timings: {
  32. buildDomTree: 0,
  33. highlightElement: 0,
  34. isInteractiveElement: 0,
  35. isElementVisible: 0,
  36. isTopElement: 0,
  37. isInExpandedViewport: 0,
  38. isTextNodeVisible: 0,
  39. getEffectiveScroll: 0,
  40. },
  41. cacheMetrics: {
  42. boundingRectCacheHits: 0,
  43. boundingRectCacheMisses: 0,
  44. computedStyleCacheHits: 0,
  45. computedStyleCacheMisses: 0,
  46. getBoundingClientRectTime: 0,
  47. getComputedStyleTime: 0,
  48. boundingRectHitRate: 0,
  49. computedStyleHitRate: 0,
  50. overallHitRate: 0,
  51. },
  52. nodeMetrics: {
  53. totalNodes: 0,
  54. processedNodes: 0,
  55. skippedNodes: 0,
  56. },
  57. buildDomTreeBreakdown: {
  58. totalTime: 0,
  59. totalSelfTime: 0,
  60. buildDomTreeCalls: 0,
  61. domOperations: {
  62. getBoundingClientRect: 0,
  63. getComputedStyle: 0,
  64. },
  65. domOperationCounts: {
  66. getBoundingClientRect: 0,
  67. getComputedStyle: 0,
  68. },
  69. },
  70. }
  71. : null;
  72. // Simple timing helper that only runs in debug mode
  73. function measureTime(fn) {
  74. if (!debugMode) return fn;
  75. return function (...args) {
  76. const start = performance.now();
  77. const result = fn.apply(this, args);
  78. const duration = performance.now() - start;
  79. return result;
  80. };
  81. }
  82. // Helper to measure DOM operations
  83. function measureDomOperation(operation, name) {
  84. if (!debugMode) return operation();
  85. const start = performance.now();
  86. const result = operation();
  87. const duration = performance.now() - start;
  88. if (PERF_METRICS && name in PERF_METRICS.buildDomTreeBreakdown.domOperations) {
  89. PERF_METRICS.buildDomTreeBreakdown.domOperations[name] += duration;
  90. PERF_METRICS.buildDomTreeBreakdown.domOperationCounts[name]++;
  91. }
  92. return result;
  93. }
  94. // Add caching mechanisms at the top level
  95. const DOM_CACHE = {
  96. boundingRects: new WeakMap(),
  97. computedStyles: new WeakMap(),
  98. clearCache: () => {
  99. DOM_CACHE.boundingRects = new WeakMap();
  100. DOM_CACHE.computedStyles = new WeakMap();
  101. },
  102. };
  103. // Cache helper functions
  104. function getCachedBoundingRect(element) {
  105. if (!element) return null;
  106. if (DOM_CACHE.boundingRects.has(element)) {
  107. if (debugMode && PERF_METRICS) {
  108. PERF_METRICS.cacheMetrics.boundingRectCacheHits++;
  109. }
  110. return DOM_CACHE.boundingRects.get(element);
  111. }
  112. if (debugMode && PERF_METRICS) {
  113. PERF_METRICS.cacheMetrics.boundingRectCacheMisses++;
  114. }
  115. let rect;
  116. if (debugMode) {
  117. const start = performance.now();
  118. rect = element.getBoundingClientRect();
  119. const duration = performance.now() - start;
  120. if (PERF_METRICS) {
  121. PERF_METRICS.buildDomTreeBreakdown.domOperations.getBoundingClientRect += duration;
  122. PERF_METRICS.buildDomTreeBreakdown.domOperationCounts.getBoundingClientRect++;
  123. }
  124. } else {
  125. rect = element.getBoundingClientRect();
  126. }
  127. if (rect) {
  128. DOM_CACHE.boundingRects.set(element, rect);
  129. }
  130. return rect;
  131. }
  132. function getCachedComputedStyle(element) {
  133. if (!element) return null;
  134. if (DOM_CACHE.computedStyles.has(element)) {
  135. if (debugMode && PERF_METRICS) {
  136. PERF_METRICS.cacheMetrics.computedStyleCacheHits++;
  137. }
  138. return DOM_CACHE.computedStyles.get(element);
  139. }
  140. if (debugMode && PERF_METRICS) {
  141. PERF_METRICS.cacheMetrics.computedStyleCacheMisses++;
  142. }
  143. let style;
  144. if (debugMode) {
  145. const start = performance.now();
  146. style = window.getComputedStyle(element);
  147. const duration = performance.now() - start;
  148. if (PERF_METRICS) {
  149. PERF_METRICS.buildDomTreeBreakdown.domOperations.getComputedStyle += duration;
  150. PERF_METRICS.buildDomTreeBreakdown.domOperationCounts.getComputedStyle++;
  151. }
  152. } else {
  153. style = window.getComputedStyle(element);
  154. }
  155. if (style) {
  156. DOM_CACHE.computedStyles.set(element, style);
  157. }
  158. return style;
  159. }
  160. /**
  161. * Hash map of DOM nodes indexed by their highlight index.
  162. *
  163. * @type {Object<string, any>}
  164. */
  165. const DOM_HASH_MAP = {};
  166. const ID = { current: 0 };
  167. const HIGHLIGHT_CONTAINER_ID = 'playwright-highlight-container';
  168. /**
  169. * Highlights an element in the DOM and returns the index of the next element.
  170. */
  171. function highlightElement(element, index, parentIframe = null) {
  172. if (!element) return index;
  173. // Store overlays and the single label for updating
  174. const overlays = [];
  175. let label = null;
  176. let labelWidth = 20; // Approximate label width
  177. let labelHeight = 16; // Approximate label height
  178. try {
  179. // Create or get highlight container
  180. let container = document.getElementById(HIGHLIGHT_CONTAINER_ID);
  181. if (!container) {
  182. container = document.createElement('div');
  183. container.id = HIGHLIGHT_CONTAINER_ID;
  184. container.style.position = 'fixed';
  185. container.style.pointerEvents = 'none';
  186. container.style.top = '0';
  187. container.style.left = '0';
  188. container.style.width = '100%';
  189. container.style.height = '100%';
  190. container.style.zIndex = '2147483647';
  191. document.body.appendChild(container);
  192. }
  193. // Get element client rects
  194. const rects = element.getClientRects(); // Use getClientRects()
  195. if (!rects || rects.length === 0) return index; // Exit if no rects
  196. // Generate a color based on the index
  197. const colors = [
  198. '#FF0000',
  199. '#00FF00',
  200. '#0000FF',
  201. '#FFA500',
  202. '#800080',
  203. '#008080',
  204. '#FF69B4',
  205. '#4B0082',
  206. '#FF4500',
  207. '#2E8B57',
  208. '#DC143C',
  209. '#4682B4',
  210. ];
  211. const colorIndex = index % colors.length;
  212. const baseColor = colors[colorIndex];
  213. const backgroundColor = baseColor + '1A'; // 10% opacity version of the color
  214. // Get iframe offset if necessary
  215. let iframeOffset = { x: 0, y: 0 };
  216. if (parentIframe) {
  217. const iframeRect = parentIframe.getBoundingClientRect(); // Keep getBoundingClientRect for iframe offset
  218. iframeOffset.x = iframeRect.left;
  219. iframeOffset.y = iframeRect.top;
  220. }
  221. // Create highlight overlays for each client rect
  222. for (const rect of rects) {
  223. if (rect.width === 0 || rect.height === 0) continue; // Skip empty rects
  224. const overlay = document.createElement('div');
  225. overlay.style.position = 'fixed';
  226. overlay.style.border = `2px solid ${baseColor}`;
  227. overlay.style.backgroundColor = backgroundColor;
  228. overlay.style.pointerEvents = 'none';
  229. overlay.style.boxSizing = 'border-box';
  230. const top = rect.top + iframeOffset.y;
  231. const left = rect.left + iframeOffset.x;
  232. overlay.style.top = `${top}px`;
  233. overlay.style.left = `${left}px`;
  234. overlay.style.width = `${rect.width}px`;
  235. overlay.style.height = `${rect.height}px`;
  236. container.appendChild(overlay);
  237. overlays.push({ element: overlay, initialRect: rect }); // Store overlay and its rect
  238. }
  239. // Create and position a single label relative to the first rect
  240. const firstRect = rects[0];
  241. label = document.createElement('div');
  242. label.className = 'playwright-highlight-label';
  243. label.style.position = 'fixed';
  244. label.style.background = baseColor;
  245. label.style.color = 'white';
  246. label.style.padding = '1px 4px';
  247. label.style.borderRadius = '4px';
  248. label.style.fontSize = `${Math.min(12, Math.max(8, firstRect.height / 2))}px`;
  249. label.textContent = index;
  250. labelWidth = label.offsetWidth > 0 ? label.offsetWidth : labelWidth; // Update actual width if possible
  251. labelHeight = label.offsetHeight > 0 ? label.offsetHeight : labelHeight; // Update actual height if possible
  252. const firstRectTop = firstRect.top + iframeOffset.y;
  253. const firstRectLeft = firstRect.left + iframeOffset.x;
  254. let labelTop = firstRectTop + 2;
  255. let labelLeft = firstRectLeft + firstRect.width - labelWidth - 2;
  256. // Adjust label position if first rect is too small
  257. if (firstRect.width < labelWidth + 4 || firstRect.height < labelHeight + 4) {
  258. labelTop = firstRectTop - labelHeight - 2;
  259. labelLeft = firstRectLeft + firstRect.width - labelWidth; // Align with right edge
  260. if (labelLeft < iframeOffset.x) labelLeft = firstRectLeft; // Prevent going off-left
  261. }
  262. // Ensure label stays within viewport bounds slightly better
  263. labelTop = Math.max(0, Math.min(labelTop, window.innerHeight - labelHeight));
  264. labelLeft = Math.max(0, Math.min(labelLeft, window.innerWidth - labelWidth));
  265. label.style.top = `${labelTop}px`;
  266. label.style.left = `${labelLeft}px`;
  267. container.appendChild(label);
  268. // Update positions on scroll/resize
  269. const updatePositions = () => {
  270. const newRects = element.getClientRects(); // Get fresh rects
  271. let newIframeOffset = { x: 0, y: 0 };
  272. if (parentIframe) {
  273. const iframeRect = parentIframe.getBoundingClientRect(); // Keep getBoundingClientRect for iframe
  274. newIframeOffset.x = iframeRect.left;
  275. newIframeOffset.y = iframeRect.top;
  276. }
  277. // Update each overlay
  278. overlays.forEach((overlayData, i) => {
  279. if (i < newRects.length) {
  280. // Check if rect still exists
  281. const newRect = newRects[i];
  282. const newTop = newRect.top + newIframeOffset.y;
  283. const newLeft = newRect.left + newIframeOffset.x;
  284. overlayData.element.style.top = `${newTop}px`;
  285. overlayData.element.style.left = `${newLeft}px`;
  286. overlayData.element.style.width = `${newRect.width}px`;
  287. overlayData.element.style.height = `${newRect.height}px`;
  288. overlayData.element.style.display = newRect.width === 0 || newRect.height === 0 ? 'none' : 'block';
  289. } else {
  290. // If fewer rects now, hide extra overlays
  291. overlayData.element.style.display = 'none';
  292. }
  293. });
  294. // If there are fewer new rects than overlays, hide the extras
  295. if (newRects.length < overlays.length) {
  296. for (let i = newRects.length; i < overlays.length; i++) {
  297. overlays[i].element.style.display = 'none';
  298. }
  299. }
  300. // Update label position based on the first new rect
  301. if (label && newRects.length > 0) {
  302. const firstNewRect = newRects[0];
  303. const firstNewRectTop = firstNewRect.top + newIframeOffset.y;
  304. const firstNewRectLeft = firstNewRect.left + newIframeOffset.x;
  305. let newLabelTop = firstNewRectTop + 2;
  306. let newLabelLeft = firstNewRectLeft + firstNewRect.width - labelWidth - 2;
  307. if (firstNewRect.width < labelWidth + 4 || firstNewRect.height < labelHeight + 4) {
  308. newLabelTop = firstNewRectTop - labelHeight - 2;
  309. newLabelLeft = firstNewRectLeft + firstNewRect.width - labelWidth;
  310. if (newLabelLeft < newIframeOffset.x) newLabelLeft = firstNewRectLeft;
  311. }
  312. // Ensure label stays within viewport bounds
  313. newLabelTop = Math.max(0, Math.min(newLabelTop, window.innerHeight - labelHeight));
  314. newLabelLeft = Math.max(0, Math.min(newLabelLeft, window.innerWidth - labelWidth));
  315. label.style.top = `${newLabelTop}px`;
  316. label.style.left = `${newLabelLeft}px`;
  317. label.style.display = 'block';
  318. } else if (label) {
  319. // Hide label if element has no rects anymore
  320. label.style.display = 'none';
  321. }
  322. };
  323. window.addEventListener('scroll', updatePositions, true); // Use capture phase
  324. window.addEventListener('resize', updatePositions);
  325. // TODO: Add cleanup logic to remove listeners and elements when done.
  326. return index + 1;
  327. } finally {
  328. // popTiming('highlighting'); // Assuming this was a typo and should be removed or corrected
  329. }
  330. }
  331. /**
  332. * Returns an XPath tree string for an element.
  333. */
  334. function getXPathTree(element, stopAtBoundary = true) {
  335. const segments = [];
  336. let currentElement = element;
  337. while (currentElement && currentElement.nodeType === Node.ELEMENT_NODE) {
  338. // Stop if we hit a shadow root or iframe
  339. if (
  340. stopAtBoundary &&
  341. (currentElement.parentNode instanceof ShadowRoot || currentElement.parentNode instanceof HTMLIFrameElement)
  342. ) {
  343. break;
  344. }
  345. let index = 0;
  346. let sibling = currentElement.previousSibling;
  347. while (sibling) {
  348. if (sibling.nodeType === Node.ELEMENT_NODE && sibling.nodeName === currentElement.nodeName) {
  349. index++;
  350. }
  351. sibling = sibling.previousSibling;
  352. }
  353. const tagName = currentElement.nodeName.toLowerCase();
  354. const xpathIndex = index > 0 ? `[${index + 1}]` : '';
  355. segments.unshift(`${tagName}${xpathIndex}`);
  356. currentElement = currentElement.parentNode;
  357. }
  358. return segments.join('/');
  359. }
  360. /**
  361. * Checks if a text node is visible.
  362. */
  363. function isTextNodeVisible(textNode) {
  364. try {
  365. const range = document.createRange();
  366. range.selectNodeContents(textNode);
  367. const rects = range.getClientRects(); // Use getClientRects for Range
  368. if (!rects || rects.length === 0) {
  369. return false;
  370. }
  371. let isAnyRectVisible = false;
  372. let isAnyRectInViewport = false;
  373. for (const rect of rects) {
  374. // Check size
  375. if (rect.width > 0 && rect.height > 0) {
  376. isAnyRectVisible = true;
  377. // Viewport check for this rect
  378. if (
  379. !(
  380. rect.bottom < -viewportExpansion ||
  381. rect.top > window.innerHeight + viewportExpansion ||
  382. rect.right < -viewportExpansion ||
  383. rect.left > window.innerWidth + viewportExpansion
  384. ) ||
  385. viewportExpansion === -1
  386. ) {
  387. isAnyRectInViewport = true;
  388. break; // Found a visible rect in viewport, no need to check others
  389. }
  390. }
  391. }
  392. if (!isAnyRectVisible || !isAnyRectInViewport) {
  393. return false;
  394. }
  395. // Check parent visibility
  396. const parentElement = textNode.parentElement;
  397. if (!parentElement) return false;
  398. try {
  399. return (
  400. isInViewport &&
  401. parentElement.checkVisibility({
  402. checkOpacity: true,
  403. checkVisibilityCSS: true,
  404. })
  405. );
  406. } catch (e) {
  407. // Fallback if checkVisibility is not supported
  408. const style = window.getComputedStyle(parentElement);
  409. return isInViewport && style.display !== 'none' && style.visibility !== 'hidden' && style.opacity !== '0';
  410. }
  411. } catch (e) {
  412. console.warn('Error checking text node visibility:', e);
  413. return false;
  414. }
  415. }
  416. // Helper function to check if element is accepted
  417. function isElementAccepted(element) {
  418. if (!element || !element.tagName) return false;
  419. // Always accept body and common container elements
  420. const alwaysAccept = new Set(['body', 'div', 'main', 'article', 'section', 'nav', 'header', 'footer']);
  421. const tagName = element.tagName.toLowerCase();
  422. if (alwaysAccept.has(tagName)) return true;
  423. const leafElementDenyList = new Set(['svg', 'script', 'style', 'link', 'meta', 'noscript', 'template']);
  424. return !leafElementDenyList.has(tagName);
  425. }
  426. /**
  427. * Checks if an element is visible.
  428. */
  429. function isElementVisible(element) {
  430. const style = getCachedComputedStyle(element);
  431. return (
  432. element.offsetWidth > 0 && element.offsetHeight > 0 && style.visibility !== 'hidden' && style.display !== 'none'
  433. );
  434. }
  435. /**
  436. * Checks if an element is interactive.
  437. *
  438. * lots of comments, and uncommented code - to show the logic of what we already tried
  439. *
  440. * One of the things we tried at the beginning was also to use event listeners, and other fancy class, style stuff -> what actually worked best was just combining most things with computed cursor style :)
  441. */
  442. function isInteractiveElement(element) {
  443. if (!element || element.nodeType !== Node.ELEMENT_NODE) {
  444. return false;
  445. }
  446. // Define interactive cursors
  447. const interactiveCursors = new Set([
  448. 'pointer', // Link/clickable elements
  449. 'move', // Movable elements
  450. 'text', // Text selection
  451. 'grab', // Grabbable elements
  452. 'grabbing', // Currently grabbing
  453. 'cell', // Table cell selection
  454. 'copy', // Copy operation
  455. 'alias', // Alias creation
  456. 'all-scroll', // Scrollable content
  457. 'col-resize', // Column resize
  458. 'context-menu', // Context menu available
  459. 'crosshair', // Precise selection
  460. 'e-resize', // East resize
  461. 'ew-resize', // East-west resize
  462. 'help', // Help available
  463. 'n-resize', // North resize
  464. 'ne-resize', // Northeast resize
  465. 'nesw-resize', // Northeast-southwest resize
  466. 'ns-resize', // North-south resize
  467. 'nw-resize', // Northwest resize
  468. 'nwse-resize', // Northwest-southeast resize
  469. 'row-resize', // Row resize
  470. 's-resize', // South resize
  471. 'se-resize', // Southeast resize
  472. 'sw-resize', // Southwest resize
  473. 'vertical-text', // Vertical text selection
  474. 'w-resize', // West resize
  475. 'zoom-in', // Zoom in
  476. 'zoom-out', // Zoom out
  477. ]);
  478. // Define non-interactive cursors
  479. const nonInteractiveCursors = new Set([
  480. 'not-allowed', // Action not allowed
  481. 'no-drop', // Drop not allowed
  482. 'wait', // Processing
  483. 'progress', // In progress
  484. 'initial', // Initial value
  485. 'inherit', // Inherited value
  486. //? Let's just include all potentially clickable elements that are not specifically blocked
  487. // 'none', // No cursor
  488. // 'default', // Default cursor
  489. // 'auto', // Browser default
  490. ]);
  491. function doesElementHaveInteractivePointer(element) {
  492. if (element.tagName.toLowerCase() === 'html') return false;
  493. const style = getCachedComputedStyle(element);
  494. if (interactiveCursors.has(style.cursor)) return true;
  495. return false;
  496. }
  497. let isInteractiveCursor = doesElementHaveInteractivePointer(element);
  498. // Genius fix for almost all interactive elements
  499. if (isInteractiveCursor) {
  500. return true;
  501. }
  502. const interactiveElements = new Set([
  503. 'a', // Links
  504. 'button', // Buttons
  505. 'input', // All input types (text, checkbox, radio, etc.)
  506. 'select', // Dropdown menus
  507. 'textarea', // Text areas
  508. 'details', // Expandable details
  509. 'summary', // Summary element (clickable part of details)
  510. 'label', // Form labels (often clickable)
  511. 'option', // Select options
  512. 'optgroup', // Option groups
  513. 'fieldset', // Form fieldsets (can be interactive with legend)
  514. 'legend', // Fieldset legends
  515. ]);
  516. // Define explicit disable attributes and properties
  517. const explicitDisableTags = new Set([
  518. 'disabled', // Standard disabled attribute
  519. // 'aria-disabled', // ARIA disabled state
  520. 'readonly', // Read-only state
  521. // 'aria-readonly', // ARIA read-only state
  522. // 'aria-hidden', // Hidden from accessibility
  523. // 'hidden', // Hidden attribute
  524. // 'inert', // Inert attribute
  525. // 'aria-inert', // ARIA inert state
  526. // 'tabindex="-1"', // Removed from tab order
  527. // 'aria-hidden="true"' // Hidden from screen readers
  528. ]);
  529. // handle inputs, select, checkbox, radio, textarea, button and make sure they are not cursor style disabled/not-allowed
  530. if (interactiveElements.has(element.tagName.toLowerCase())) {
  531. const style = getCachedComputedStyle(element);
  532. // Check for non-interactive cursor
  533. if (nonInteractiveCursors.has(style.cursor)) {
  534. return false;
  535. }
  536. // Check for explicit disable attributes
  537. for (const disableTag of explicitDisableTags) {
  538. if (
  539. element.hasAttribute(disableTag) ||
  540. element.getAttribute(disableTag) === 'true' ||
  541. element.getAttribute(disableTag) === ''
  542. ) {
  543. return false;
  544. }
  545. }
  546. // Check for disabled property on form elements
  547. if (element.disabled) {
  548. return false;
  549. }
  550. // Check for readonly property on form elements
  551. if (element.readOnly) {
  552. return false;
  553. }
  554. // Check for inert property
  555. if (element.inert) {
  556. return false;
  557. }
  558. return true;
  559. }
  560. const tagName = element.tagName.toLowerCase();
  561. const role = element.getAttribute('role');
  562. const ariaRole = element.getAttribute('aria-role');
  563. // Added enhancement to capture dropdown interactive elements
  564. if (
  565. element.classList &&
  566. (element.classList.contains('button') ||
  567. element.classList.contains('dropdown-toggle') ||
  568. element.getAttribute('data-index') ||
  569. element.getAttribute('data-toggle') === 'dropdown' ||
  570. element.getAttribute('aria-haspopup') === 'true')
  571. ) {
  572. return true;
  573. }
  574. const interactiveRoles = new Set([
  575. 'button', // Directly clickable element
  576. // 'link', // Clickable link
  577. // 'menuitem', // Clickable menu item
  578. 'menuitemradio', // Radio-style menu item (selectable)
  579. 'menuitemcheckbox', // Checkbox-style menu item (toggleable)
  580. 'radio', // Radio button (selectable)
  581. 'checkbox', // Checkbox (toggleable)
  582. 'tab', // Tab (clickable to switch content)
  583. 'switch', // Toggle switch (clickable to change state)
  584. 'slider', // Slider control (draggable)
  585. 'spinbutton', // Number input with up/down controls
  586. 'combobox', // Dropdown with text input
  587. 'searchbox', // Search input field
  588. 'textbox', // Text input field
  589. // 'listbox', // Selectable list
  590. 'option', // Selectable option in a list
  591. 'scrollbar', // Scrollable control
  592. ]);
  593. // Basic role/attribute checks
  594. const hasInteractiveRole =
  595. interactiveElements.has(tagName) || interactiveRoles.has(role) || interactiveRoles.has(ariaRole);
  596. if (hasInteractiveRole) return true;
  597. // check whether element has event listeners
  598. try {
  599. if (typeof getEventListeners === 'function') {
  600. const listeners = getEventListeners(element);
  601. const mouseEvents = ['click', 'mousedown', 'mouseup', 'dblclick'];
  602. for (const eventType of mouseEvents) {
  603. if (listeners[eventType] && listeners[eventType].length > 0) {
  604. return true; // Found a mouse interaction listener
  605. }
  606. }
  607. } else {
  608. // Fallback: Check common event attributes if getEventListeners is not available
  609. const commonMouseAttrs = ['onclick', 'onmousedown', 'onmouseup', 'ondblclick'];
  610. if (commonMouseAttrs.some(attr => element.hasAttribute(attr))) {
  611. return true;
  612. }
  613. }
  614. } catch (e) {
  615. // console.warn(`Could not check event listeners for ${element.tagName}:`, e);
  616. // If checking listeners fails, rely on other checks
  617. }
  618. return false;
  619. }
  620. /**
  621. * Checks if an element is the topmost element at its position.
  622. */
  623. function isTopElement(element) {
  624. const rects = element.getClientRects(); // Use getClientRects
  625. if (!rects || rects.length === 0) {
  626. return false; // No geometry, cannot be top
  627. }
  628. let isAnyRectInViewport = false;
  629. for (const rect of rects) {
  630. // Use the same logic as isInExpandedViewport check
  631. if (
  632. (rect.width > 0 &&
  633. rect.height > 0 &&
  634. !(
  635. // Only check non-empty rects
  636. (
  637. rect.bottom < -viewportExpansion ||
  638. rect.top > window.innerHeight + viewportExpansion ||
  639. rect.right < -viewportExpansion ||
  640. rect.left > window.innerWidth + viewportExpansion
  641. )
  642. )) ||
  643. viewportExpansion === -1
  644. ) {
  645. isAnyRectInViewport = true;
  646. break;
  647. }
  648. }
  649. if (!isAnyRectInViewport) {
  650. return false; // All rects are outside the viewport area
  651. }
  652. // Find the correct document context and root element
  653. let doc = element.ownerDocument;
  654. // If we're in an iframe, elements are considered top by default
  655. if (doc !== window.document) {
  656. return true;
  657. }
  658. // For shadow DOM, we need to check within its own root context
  659. const shadowRoot = element.getRootNode();
  660. if (shadowRoot instanceof ShadowRoot) {
  661. const centerX = rects[Math.floor(rects.length / 2)].left + rects[Math.floor(rects.length / 2)].width / 2;
  662. const centerY = rects[Math.floor(rects.length / 2)].top + rects[Math.floor(rects.length / 2)].height / 2;
  663. try {
  664. const topEl = measureDomOperation(() => shadowRoot.elementFromPoint(centerX, centerY), 'elementFromPoint');
  665. if (!topEl) return false;
  666. let current = topEl;
  667. while (current && current !== shadowRoot) {
  668. if (current === element) return true;
  669. current = current.parentElement;
  670. }
  671. return false;
  672. } catch (e) {
  673. return true;
  674. }
  675. }
  676. // For elements in viewport, check if they're topmost
  677. const centerX = rects[Math.floor(rects.length / 2)].left + rects[Math.floor(rects.length / 2)].width / 2;
  678. const centerY = rects[Math.floor(rects.length / 2)].top + rects[Math.floor(rects.length / 2)].height / 2;
  679. try {
  680. const topEl = document.elementFromPoint(centerX, centerY);
  681. if (!topEl) return false;
  682. let current = topEl;
  683. while (current && current !== document.documentElement) {
  684. if (current === element) return true;
  685. current = current.parentElement;
  686. }
  687. return false;
  688. } catch (e) {
  689. return true;
  690. }
  691. }
  692. /**
  693. * Checks if an element is within the expanded viewport.
  694. */
  695. function isInExpandedViewport(element, viewportExpansion) {
  696. return true;
  697. if (viewportExpansion === -1) {
  698. return true;
  699. }
  700. const rects = element.getClientRects(); // Use getClientRects
  701. if (!rects || rects.length === 0) {
  702. // Fallback to getBoundingClientRect if getClientRects is empty,
  703. // useful for elements like <svg> that might not have client rects but have a bounding box.
  704. const boundingRect = getCachedBoundingRect(element);
  705. if (!boundingRect || boundingRect.width === 0 || boundingRect.height === 0) {
  706. return false;
  707. }
  708. return !(
  709. boundingRect.bottom < -viewportExpansion ||
  710. boundingRect.top > window.innerHeight + viewportExpansion ||
  711. boundingRect.right < -viewportExpansion ||
  712. boundingRect.left > window.innerWidth + viewportExpansion
  713. );
  714. }
  715. // Check if *any* client rect is within the viewport
  716. for (const rect of rects) {
  717. if (rect.width === 0 || rect.height === 0) continue; // Skip empty rects
  718. if (
  719. !(
  720. rect.bottom < -viewportExpansion ||
  721. rect.top > window.innerHeight + viewportExpansion ||
  722. rect.right < -viewportExpansion ||
  723. rect.left > window.innerWidth + viewportExpansion
  724. )
  725. ) {
  726. return true; // Found at least one rect in the viewport
  727. }
  728. }
  729. return false; // No rects were found in the viewport
  730. }
  731. // Add this new helper function
  732. function getEffectiveScroll(element) {
  733. let currentEl = element;
  734. let scrollX = 0;
  735. let scrollY = 0;
  736. return measureDomOperation(() => {
  737. while (currentEl && currentEl !== document.documentElement) {
  738. if (currentEl.scrollLeft || currentEl.scrollTop) {
  739. scrollX += currentEl.scrollLeft;
  740. scrollY += currentEl.scrollTop;
  741. }
  742. currentEl = currentEl.parentElement;
  743. }
  744. scrollX += window.scrollX;
  745. scrollY += window.scrollY;
  746. return { scrollX, scrollY };
  747. }, 'scrollOperations');
  748. }
  749. // Add these helper functions at the top level
  750. function isInteractiveCandidate(element) {
  751. if (!element || element.nodeType !== Node.ELEMENT_NODE) return false;
  752. const tagName = element.tagName.toLowerCase();
  753. // Fast-path for common interactive elements
  754. const interactiveElements = new Set(['a', 'button', 'input', 'select', 'textarea', 'details', 'summary']);
  755. if (interactiveElements.has(tagName)) return true;
  756. // Quick attribute checks without getting full lists
  757. const hasQuickInteractiveAttr =
  758. element.hasAttribute('onclick') ||
  759. element.hasAttribute('role') ||
  760. element.hasAttribute('tabindex') ||
  761. element.hasAttribute('aria-') ||
  762. element.hasAttribute('data-action') ||
  763. element.getAttribute('contenteditable') == 'true';
  764. return hasQuickInteractiveAttr;
  765. }
  766. // --- Define constants for distinct interaction check ---
  767. const DISTINCT_INTERACTIVE_TAGS = new Set([
  768. 'a',
  769. 'button',
  770. 'input',
  771. 'select',
  772. 'textarea',
  773. 'summary',
  774. 'details',
  775. 'label',
  776. 'option',
  777. ]);
  778. const INTERACTIVE_ROLES = new Set([
  779. 'button',
  780. 'link',
  781. 'menuitem',
  782. 'menuitemradio',
  783. 'menuitemcheckbox',
  784. 'radio',
  785. 'checkbox',
  786. 'tab',
  787. 'switch',
  788. 'slider',
  789. 'spinbutton',
  790. 'combobox',
  791. 'searchbox',
  792. 'textbox',
  793. 'listbox',
  794. 'option',
  795. 'scrollbar',
  796. ]);
  797. /**
  798. * Checks if an element likely represents a distinct interaction
  799. * separate from its parent (if the parent is also interactive).
  800. */
  801. function isElementDistinctInteraction(element) {
  802. if (!element || element.nodeType !== Node.ELEMENT_NODE) {
  803. return false;
  804. }
  805. const tagName = element.tagName.toLowerCase();
  806. const role = element.getAttribute('role');
  807. // Check if it's an iframe - always distinct boundary
  808. if (tagName === 'iframe') {
  809. return true;
  810. }
  811. // Check tag name
  812. if (DISTINCT_INTERACTIVE_TAGS.has(tagName)) {
  813. return true;
  814. }
  815. // Check interactive roles
  816. if (role && INTERACTIVE_ROLES.has(role)) {
  817. return true;
  818. }
  819. // Check contenteditable
  820. if (element.isContentEditable || element.getAttribute('contenteditable') === 'true') {
  821. return true;
  822. }
  823. // Check for common testing/automation attributes
  824. if (element.hasAttribute('data-testid') || element.hasAttribute('data-cy') || element.hasAttribute('data-test')) {
  825. return true;
  826. }
  827. // Check for explicit onclick handler (attribute or property)
  828. if (element.hasAttribute('onclick') || typeof element.onclick === 'function') {
  829. return true;
  830. }
  831. // Check for other common interaction event listeners
  832. try {
  833. if (typeof getEventListeners === 'function') {
  834. const listeners = getEventListeners(element);
  835. const interactionEvents = [
  836. 'mousedown',
  837. 'mouseup',
  838. 'keydown',
  839. 'keyup',
  840. 'submit',
  841. 'change',
  842. 'input',
  843. 'focus',
  844. 'blur',
  845. ];
  846. for (const eventType of interactionEvents) {
  847. if (listeners[eventType] && listeners[eventType].length > 0) {
  848. return true; // Found a common interaction listener
  849. }
  850. }
  851. } else {
  852. // Fallback: Check common event attributes if getEventListeners is not available
  853. const commonEventAttrs = [
  854. 'onmousedown',
  855. 'onmouseup',
  856. 'onkeydown',
  857. 'onkeyup',
  858. 'onsubmit',
  859. 'onchange',
  860. 'oninput',
  861. 'onfocus',
  862. 'onblur',
  863. ];
  864. if (commonEventAttrs.some(attr => element.hasAttribute(attr))) {
  865. return true;
  866. }
  867. }
  868. } catch (e) {
  869. // console.warn(`Could not check event listeners for ${element.tagName}:`, e);
  870. // If checking listeners fails, rely on other checks
  871. }
  872. // Default to false: if it's interactive but doesn't match above,
  873. // assume it triggers the same action as the parent.
  874. return false;
  875. }
  876. // --- End distinct interaction check ---
  877. /**
  878. * Handles the logic for deciding whether to highlight an element and performing the highlight.
  879. */
  880. function handleHighlighting(nodeData, node, parentIframe, isParentHighlighted) {
  881. if (!nodeData.isInteractive) return false; // Not interactive, definitely don't highlight
  882. let shouldHighlight = false;
  883. if (!isParentHighlighted) {
  884. // Parent wasn't highlighted, this interactive node can be highlighted.
  885. shouldHighlight = true;
  886. } else {
  887. // Parent *was* highlighted. Only highlight this node if it represents a distinct interaction.
  888. if (isElementDistinctInteraction(node)) {
  889. shouldHighlight = true;
  890. } else {
  891. // console.log(`Skipping highlight for ${nodeData.tagName} (parent highlighted)`);
  892. shouldHighlight = false;
  893. }
  894. }
  895. if (shouldHighlight) {
  896. // Check viewport status before assigning index and highlighting
  897. nodeData.isInViewport = isInExpandedViewport(node, viewportExpansion);
  898. if (nodeData.isInViewport) {
  899. nodeData.highlightIndex = highlightIndex++;
  900. if (doHighlightElements) {
  901. if (focusHighlightIndex >= 0) {
  902. if (focusHighlightIndex === nodeData.highlightIndex) {
  903. highlightElement(node, nodeData.highlightIndex, parentIframe);
  904. }
  905. } else {
  906. highlightElement(node, nodeData.highlightIndex, parentIframe);
  907. }
  908. return true; // Successfully highlighted
  909. }
  910. } else {
  911. // console.log(`Skipping highlight for ${nodeData.tagName} (outside viewport)`);
  912. }
  913. }
  914. return false; // Did not highlight
  915. }
  916. /**
  917. * Creates a node data object for a given node and its descendants.
  918. */
  919. function buildDomTree(node, parentIframe = null, isParentHighlighted = false) {
  920. if (debugMode) PERF_METRICS.nodeMetrics.totalNodes++;
  921. if (!node || node.id === HIGHLIGHT_CONTAINER_ID) {
  922. if (debugMode) PERF_METRICS.nodeMetrics.skippedNodes++;
  923. return null;
  924. }
  925. // Special handling for root node (body)
  926. if (node === document.body) {
  927. const nodeData = {
  928. tagName: 'body',
  929. attributes: {},
  930. xpath: '/body',
  931. children: [],
  932. };
  933. // Process children of body
  934. for (const child of node.childNodes) {
  935. const domElement = buildDomTree(child, parentIframe, false); // Body's children have no highlighted parent initially
  936. if (domElement) nodeData.children.push(domElement);
  937. }
  938. const id = `${ID.current++}`;
  939. DOM_HASH_MAP[id] = nodeData;
  940. if (debugMode) PERF_METRICS.nodeMetrics.processedNodes++;
  941. return id;
  942. }
  943. // Early bailout for non-element nodes except text
  944. if (node.nodeType !== Node.ELEMENT_NODE && node.nodeType !== Node.TEXT_NODE) {
  945. if (debugMode) PERF_METRICS.nodeMetrics.skippedNodes++;
  946. return null;
  947. }
  948. // Process text nodes
  949. if (node.nodeType === Node.TEXT_NODE) {
  950. const textContent = node.textContent.trim();
  951. if (!textContent) {
  952. if (debugMode) PERF_METRICS.nodeMetrics.skippedNodes++;
  953. return null;
  954. }
  955. // Only check visibility for text nodes that might be visible
  956. const parentElement = node.parentElement;
  957. if (!parentElement || parentElement.tagName.toLowerCase() === 'script') {
  958. if (debugMode) PERF_METRICS.nodeMetrics.skippedNodes++;
  959. return null;
  960. }
  961. const id = `${ID.current++}`;
  962. DOM_HASH_MAP[id] = {
  963. type: 'TEXT_NODE',
  964. text: textContent,
  965. isVisible: isTextNodeVisible(node),
  966. };
  967. if (debugMode) PERF_METRICS.nodeMetrics.processedNodes++;
  968. return id;
  969. }
  970. // Quick checks for element nodes
  971. if (node.nodeType === Node.ELEMENT_NODE && !isElementAccepted(node)) {
  972. if (debugMode) PERF_METRICS.nodeMetrics.skippedNodes++;
  973. return null;
  974. }
  975. // Early viewport check - only filter out elements clearly outside viewport
  976. if (viewportExpansion !== -1) {
  977. const rect = getCachedBoundingRect(node); // Keep for initial quick check
  978. const style = getCachedComputedStyle(node);
  979. // Skip viewport check for fixed/sticky elements as they may appear anywhere
  980. const isFixedOrSticky = style && (style.position === 'fixed' || style.position === 'sticky');
  981. // Check if element has actual dimensions using offsetWidth/Height (quick check)
  982. const hasSize = node.offsetWidth > 0 || node.offsetHeight > 0;
  983. // Use getBoundingClientRect for the quick OUTSIDE check.
  984. // isInExpandedViewport will do the more accurate check later if needed.
  985. if (
  986. !rect ||
  987. (!isFixedOrSticky &&
  988. !hasSize &&
  989. (rect.bottom < -viewportExpansion ||
  990. rect.top > window.innerHeight + viewportExpansion ||
  991. rect.right < -viewportExpansion ||
  992. rect.left > window.innerWidth + viewportExpansion))
  993. ) {
  994. // console.log("Skipping node outside viewport (quick check):", node.tagName, rect);
  995. if (debugMode) PERF_METRICS.nodeMetrics.skippedNodes++;
  996. return null;
  997. }
  998. }
  999. // Process element node
  1000. const nodeData = {
  1001. tagName: node.tagName.toLowerCase(),
  1002. attributes: {},
  1003. xpath: getXPathTree(node, true),
  1004. children: [],
  1005. };
  1006. // Get attributes for interactive elements or potential text containers
  1007. if (
  1008. isInteractiveCandidate(node) ||
  1009. node.tagName.toLowerCase() === 'iframe' ||
  1010. node.tagName.toLowerCase() === 'body'
  1011. ) {
  1012. const attributeNames = node.getAttributeNames?.() || [];
  1013. for (const name of attributeNames) {
  1014. nodeData.attributes[name] = node.getAttribute(name);
  1015. }
  1016. }
  1017. let nodeWasHighlighted = false;
  1018. // Perform visibility, interactivity, and highlighting checks
  1019. if (node.nodeType === Node.ELEMENT_NODE) {
  1020. nodeData.isVisible = isElementVisible(node); // isElementVisible uses offsetWidth/Height, which is fine
  1021. if (nodeData.isVisible) {
  1022. nodeData.isTopElement = isTopElement(node);
  1023. if (nodeData.isTopElement) {
  1024. nodeData.isInteractive = isInteractiveElement(node);
  1025. // Call the dedicated highlighting function
  1026. nodeWasHighlighted = handleHighlighting(nodeData, node, parentIframe, isParentHighlighted);
  1027. }
  1028. }
  1029. }
  1030. // Process children, with special handling for iframes and rich text editors
  1031. if (node.tagName) {
  1032. const tagName = node.tagName.toLowerCase();
  1033. // Handle iframes
  1034. if (tagName === 'iframe') {
  1035. try {
  1036. const iframeDoc = node.contentDocument || node.contentWindow?.document;
  1037. if (iframeDoc) {
  1038. for (const child of iframeDoc.childNodes) {
  1039. const domElement = buildDomTree(child, node, false);
  1040. if (domElement) nodeData.children.push(domElement);
  1041. }
  1042. }
  1043. } catch (e) {
  1044. console.warn('Unable to access iframe:', e);
  1045. }
  1046. }
  1047. // Handle rich text editors and contenteditable elements
  1048. else if (
  1049. node.isContentEditable ||
  1050. node.getAttribute('contenteditable') === 'true' ||
  1051. node.id === 'tinymce' ||
  1052. node.classList.contains('mce-content-body') ||
  1053. (tagName === 'body' && node.getAttribute('data-id')?.startsWith('mce_'))
  1054. ) {
  1055. // Process all child nodes to capture formatted text
  1056. for (const child of node.childNodes) {
  1057. const domElement = buildDomTree(child, parentIframe, nodeWasHighlighted);
  1058. if (domElement) nodeData.children.push(domElement);
  1059. }
  1060. } else {
  1061. // Handle shadow DOM
  1062. if (node.shadowRoot) {
  1063. nodeData.shadowRoot = true;
  1064. for (const child of node.shadowRoot.childNodes) {
  1065. const domElement = buildDomTree(child, parentIframe, nodeWasHighlighted);
  1066. if (domElement) nodeData.children.push(domElement);
  1067. }
  1068. }
  1069. // Handle regular elements
  1070. for (const child of node.childNodes) {
  1071. // Pass the highlighted status of the *current* node to its children
  1072. const passHighlightStatusToChild = nodeWasHighlighted || isParentHighlighted;
  1073. const domElement = buildDomTree(child, parentIframe, passHighlightStatusToChild);
  1074. if (domElement) nodeData.children.push(domElement);
  1075. }
  1076. }
  1077. }
  1078. // Skip empty anchor tags
  1079. if (nodeData.tagName === 'a' && nodeData.children.length === 0 && !nodeData.attributes.href) {
  1080. if (debugMode) PERF_METRICS.nodeMetrics.skippedNodes++;
  1081. return null;
  1082. }
  1083. const id = `${ID.current++}`;
  1084. DOM_HASH_MAP[id] = nodeData;
  1085. if (debugMode) PERF_METRICS.nodeMetrics.processedNodes++;
  1086. return id;
  1087. }
  1088. // After all functions are defined, wrap them with performance measurement
  1089. // Remove buildDomTree from here as we measure it separately
  1090. highlightElement = measureTime(highlightElement);
  1091. isInteractiveElement = measureTime(isInteractiveElement);
  1092. isElementVisible = measureTime(isElementVisible);
  1093. isTopElement = measureTime(isTopElement);
  1094. isInExpandedViewport = measureTime(isInExpandedViewport);
  1095. isTextNodeVisible = measureTime(isTextNodeVisible);
  1096. getEffectiveScroll = measureTime(getEffectiveScroll);
  1097. const rootId = buildDomTree(document.body);
  1098. // Clear the cache before starting
  1099. DOM_CACHE.clearCache();
  1100. // Only process metrics in debug mode
  1101. if (debugMode && PERF_METRICS) {
  1102. // Convert timings to seconds and add useful derived metrics
  1103. Object.keys(PERF_METRICS.timings).forEach(key => {
  1104. PERF_METRICS.timings[key] = PERF_METRICS.timings[key] / 1000;
  1105. });
  1106. Object.keys(PERF_METRICS.buildDomTreeBreakdown).forEach(key => {
  1107. if (typeof PERF_METRICS.buildDomTreeBreakdown[key] === 'number') {
  1108. PERF_METRICS.buildDomTreeBreakdown[key] = PERF_METRICS.buildDomTreeBreakdown[key] / 1000;
  1109. }
  1110. });
  1111. // Add some useful derived metrics
  1112. if (PERF_METRICS.buildDomTreeBreakdown.buildDomTreeCalls > 0) {
  1113. PERF_METRICS.buildDomTreeBreakdown.averageTimePerNode =
  1114. PERF_METRICS.buildDomTreeBreakdown.totalTime / PERF_METRICS.buildDomTreeBreakdown.buildDomTreeCalls;
  1115. }
  1116. PERF_METRICS.buildDomTreeBreakdown.timeInChildCalls =
  1117. PERF_METRICS.buildDomTreeBreakdown.totalTime - PERF_METRICS.buildDomTreeBreakdown.totalSelfTime;
  1118. // Add average time per operation to the metrics
  1119. Object.keys(PERF_METRICS.buildDomTreeBreakdown.domOperations).forEach(op => {
  1120. const time = PERF_METRICS.buildDomTreeBreakdown.domOperations[op];
  1121. const count = PERF_METRICS.buildDomTreeBreakdown.domOperationCounts[op];
  1122. if (count > 0) {
  1123. PERF_METRICS.buildDomTreeBreakdown.domOperations[`${op}Average`] = time / count;
  1124. }
  1125. });
  1126. // Calculate cache hit rates
  1127. const boundingRectTotal =
  1128. PERF_METRICS.cacheMetrics.boundingRectCacheHits + PERF_METRICS.cacheMetrics.boundingRectCacheMisses;
  1129. const computedStyleTotal =
  1130. PERF_METRICS.cacheMetrics.computedStyleCacheHits + PERF_METRICS.cacheMetrics.computedStyleCacheMisses;
  1131. if (boundingRectTotal > 0) {
  1132. PERF_METRICS.cacheMetrics.boundingRectHitRate =
  1133. PERF_METRICS.cacheMetrics.boundingRectCacheHits / boundingRectTotal;
  1134. }
  1135. if (computedStyleTotal > 0) {
  1136. PERF_METRICS.cacheMetrics.computedStyleHitRate =
  1137. PERF_METRICS.cacheMetrics.computedStyleCacheHits / computedStyleTotal;
  1138. }
  1139. if (boundingRectTotal + computedStyleTotal > 0) {
  1140. PERF_METRICS.cacheMetrics.overallHitRate =
  1141. (PERF_METRICS.cacheMetrics.boundingRectCacheHits + PERF_METRICS.cacheMetrics.computedStyleCacheHits) /
  1142. (boundingRectTotal + computedStyleTotal);
  1143. }
  1144. }
  1145. return debugMode ? { rootId, map: DOM_HASH_MAP, perfMetrics: PERF_METRICS } : { rootId, map: DOM_HASH_MAP };
  1146. };