feat: add check for table highlighting

This commit is contained in:
RohitR311
2024-12-26 19:16:44 +05:30
parent fcc71e0841
commit 97efd15631

View File

@@ -84,36 +84,44 @@ export const getElementInformation = async (
if (originalEl) { if (originalEl) {
let element = originalEl; let element = originalEl;
while (element.parentElement) { if (element.tagName === 'TD' || element.tagName === 'TH') {
if (element.tagName.toLowerCase() === 'body' || const tableParent = element.closest('table');
element.tagName.toLowerCase() === 'html') { if (tableParent) {
break; element = tableParent;
} }
}
const parentRect = element.parentElement.getBoundingClientRect(); if (element.tagName !== 'TABLE') {
const childRect = element.getBoundingClientRect(); while (element.parentElement) {
if (element.tagName.toLowerCase() === 'body' ||
element.tagName.toLowerCase() === 'html') {
break;
}
const fullyContained = const parentRect = element.parentElement.getBoundingClientRect();
parentRect.left <= childRect.left && const childRect = element.getBoundingClientRect();
parentRect.right >= childRect.right &&
parentRect.top <= childRect.top &&
parentRect.bottom >= childRect.bottom;
const significantOverlap = const fullyContained =
(childRect.width * childRect.height) / parentRect.left <= childRect.left &&
(parentRect.width * parentRect.height) > 0.5; parentRect.right >= childRect.right &&
parentRect.top <= childRect.top &&
parentRect.bottom >= childRect.bottom;
if (fullyContained && significantOverlap) { const significantOverlap =
// Only traverse up if next parent isn't body or html (childRect.width * childRect.height) /
const nextParent = element.parentElement; (parentRect.width * parentRect.height) > 0.5;
if (nextParent.tagName.toLowerCase() !== 'body' &&
nextParent.tagName.toLowerCase() !== 'html') { if (fullyContained && significantOverlap) {
element = nextParent; const nextParent = element.parentElement;
if (nextParent.tagName.toLowerCase() !== 'body' &&
nextParent.tagName.toLowerCase() !== 'html') {
element = nextParent;
} else {
break;
}
} else { } else {
break; break;
} }
} else {
break;
} }
} }
@@ -212,36 +220,44 @@ export const getRect = async (page: Page, coordinates: Coordinates, listSelector
if (originalEl) { if (originalEl) {
let element = originalEl; let element = originalEl;
while (element.parentElement) { if (element.tagName === 'TD' || element.tagName === 'TH') {
if (element.tagName.toLowerCase() === 'body' || const tableParent = element.closest('table');
element.tagName.toLowerCase() === 'html') { if (tableParent) {
break; element = tableParent;
} }
}
const parentRect = element.parentElement.getBoundingClientRect(); if (element.tagName !== 'TABLE') {
const childRect = element.getBoundingClientRect(); while (element.parentElement) {
if (element.tagName.toLowerCase() === 'body' ||
element.tagName.toLowerCase() === 'html') {
break;
}
const fullyContained = const parentRect = element.parentElement.getBoundingClientRect();
parentRect.left <= childRect.left && const childRect = element.getBoundingClientRect();
parentRect.right >= childRect.right &&
parentRect.top <= childRect.top &&
parentRect.bottom >= childRect.bottom;
const significantOverlap = const fullyContained =
(childRect.width * childRect.height) / parentRect.left <= childRect.left &&
(parentRect.width * parentRect.height) > 0.5; parentRect.right >= childRect.right &&
parentRect.top <= childRect.top &&
parentRect.bottom >= childRect.bottom;
if (fullyContained && significantOverlap) { const significantOverlap =
// Only traverse up if next parent isn't body or html (childRect.width * childRect.height) /
const nextParent = element.parentElement; (parentRect.width * parentRect.height) > 0.5;
if (nextParent.tagName.toLowerCase() !== 'body' &&
nextParent.tagName.toLowerCase() !== 'html') { if (fullyContained && significantOverlap) {
element = nextParent; const nextParent = element.parentElement;
if (nextParent.tagName.toLowerCase() !== 'body' &&
nextParent.tagName.toLowerCase() !== 'html') {
element = nextParent;
} else {
break;
}
} else { } else {
break; break;
} }
} else {
break;
} }
} }
@@ -933,37 +949,45 @@ export const getNonUniqueSelectors = async (page: Page, coordinates: Coordinates
let element = originalEl; let element = originalEl;
// if (listSelector === '') { if (element.tagName === 'TD' || element.tagName === 'TH') {
while (element.parentElement) { const tableParent = element.closest('table');
if (element.tagName.toLowerCase() === 'body' || if (tableParent) {
element.tagName.toLowerCase() === 'html') { element = tableParent;
break;
} }
}
const parentRect = element.parentElement.getBoundingClientRect(); // if (listSelector === '') {
const childRect = element.getBoundingClientRect(); if (element.tagName !== 'TABLE') {
while (element.parentElement) {
if (element.tagName.toLowerCase() === 'body' ||
element.tagName.toLowerCase() === 'html') {
break;
}
const fullyContained = const parentRect = element.parentElement.getBoundingClientRect();
parentRect.left <= childRect.left && const childRect = element.getBoundingClientRect();
parentRect.right >= childRect.right &&
parentRect.top <= childRect.top &&
parentRect.bottom >= childRect.bottom;
const significantOverlap = const fullyContained =
(childRect.width * childRect.height) / parentRect.left <= childRect.left &&
(parentRect.width * parentRect.height) > 0.5; parentRect.right >= childRect.right &&
parentRect.top <= childRect.top &&
parentRect.bottom >= childRect.bottom;
if (fullyContained && significantOverlap) { const significantOverlap =
// Only traverse up if next parent isn't body or html (childRect.width * childRect.height) /
const nextParent = element.parentElement; (parentRect.width * parentRect.height) > 0.5;
if (nextParent.tagName.toLowerCase() !== 'body' &&
nextParent.tagName.toLowerCase() !== 'html') { if (fullyContained && significantOverlap) {
element = nextParent; const nextParent = element.parentElement;
if (nextParent.tagName.toLowerCase() !== 'body' &&
nextParent.tagName.toLowerCase() !== 'html') {
element = nextParent;
} else {
break;
}
} else { } else {
break; break;
} }
} else {
break;
} }
} }
// } // }